2023-07-03 00:16:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hansmi/paperhooks/pkg/client"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tagClient interface {
|
2023-07-27 00:14:04 +02:00
|
|
|
ListAllTags(context.Context, client.ListTagsOptions, func(context.Context, client.Tag) error) error
|
2023-07-03 00:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type tagCollector struct {
|
|
|
|
cl tagClient
|
|
|
|
|
|
|
|
infoDesc *prometheus.Desc
|
|
|
|
docCountDesc *prometheus.Desc
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTagCollector(cl tagClient) *tagCollector {
|
|
|
|
return &tagCollector{
|
|
|
|
cl: cl,
|
|
|
|
|
|
|
|
infoDesc: prometheus.NewDesc("paperless_tag_info",
|
|
|
|
"Static information about a tag.",
|
|
|
|
[]string{"id", "name", "slug"}, nil),
|
|
|
|
docCountDesc: prometheus.NewDesc("paperless_tag_document_count",
|
|
|
|
"Number of documents associated with a tag.",
|
|
|
|
[]string{"id"}, nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tagCollector) describe(ch chan<- *prometheus.Desc) {
|
|
|
|
ch <- c.infoDesc
|
|
|
|
ch <- c.docCountDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tagCollector) collect(ctx context.Context, ch chan<- prometheus.Metric) error {
|
2023-07-27 00:14:04 +02:00
|
|
|
var opts client.ListTagsOptions
|
|
|
|
|
2023-07-03 00:16:08 +02:00
|
|
|
opts.Ordering.Field = "name"
|
|
|
|
|
|
|
|
return c.cl.ListAllTags(ctx, opts, func(_ context.Context, tag client.Tag) error {
|
|
|
|
id := strconv.FormatInt(tag.ID, 10)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.infoDesc, prometheus.GaugeValue, 1,
|
|
|
|
id,
|
|
|
|
tag.Name,
|
|
|
|
tag.Slug,
|
|
|
|
)
|
|
|
|
|
|
|
|
ch <- prometheus.MustNewConstMetric(c.docCountDesc, prometheus.GaugeValue,
|
|
|
|
float64(tag.DocumentCount), id)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|