Skip to content

Commit

Permalink
Add metric for "inbox" flag of tags
Browse files Browse the repository at this point in the history
Enable reproduction of the "Documents in inbox" count reported on
Paperless-ngx' main page when combined with the document count per tag.

Relates to issue #39.
  • Loading branch information
hansmi committed Nov 6, 2024
1 parent f5631fb commit df7648d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type tagCollector struct {

infoDesc *prometheus.Desc
docCountDesc *prometheus.Desc
inboxDesc *prometheus.Desc
}

func newTagCollector(cl tagClient) *tagCollector {
Expand All @@ -29,12 +30,16 @@ func newTagCollector(cl tagClient) *tagCollector {
docCountDesc: prometheus.NewDesc("paperless_tag_document_count",
"Number of documents associated with a tag.",
[]string{"id"}, nil),
inboxDesc: prometheus.NewDesc("paperless_tag_inbox",
"Whether the tag is marked as an inbox tag.",
[]string{"id"}, nil),
}
}

func (c *tagCollector) describe(ch chan<- *prometheus.Desc) {
ch <- c.infoDesc
ch <- c.docCountDesc
ch <- c.inboxDesc
}

func (c *tagCollector) collect(ctx context.Context, ch chan<- prometheus.Metric) error {
Expand All @@ -54,6 +59,15 @@ func (c *tagCollector) collect(ctx context.Context, ch chan<- prometheus.Metric)
ch <- prometheus.MustNewConstMetric(c.docCountDesc, prometheus.GaugeValue,
float64(tag.DocumentCount), id)

isInboxTag := 0

if tag.IsInboxTag {
isInboxTag = 1
}

ch <- prometheus.MustNewConstMetric(c.inboxDesc, prometheus.GaugeValue,
float64(isInboxTag), id)

return nil
})
}
9 changes: 9 additions & 0 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,27 @@ func TestTagCollect(t *testing.T) {
cl.items = append(cl.items, []client.Tag{
{ID: 8463, Name: "aaa", Slug: "aslug"},
{ID: 338, Name: "three-three-eight", DocumentCount: 13},
{ID: 2930, Name: "mybox", Slug: "mb", IsInboxTag: true},
{ID: 26429, Name: "last"},
}...)

testutil.CollectAndCompare(t, c, `
# HELP paperless_tag_document_count Number of documents associated with a tag.
# TYPE paperless_tag_document_count gauge
paperless_tag_document_count{id="26429"} 0
paperless_tag_document_count{id="2930"} 0
paperless_tag_document_count{id="338"} 13
paperless_tag_document_count{id="8463"} 0
# HELP paperless_tag_inbox Whether the tag is marked as an inbox tag.
# TYPE paperless_tag_inbox gauge
paperless_tag_inbox{id="26429"} 0
paperless_tag_inbox{id="2930"} 1
paperless_tag_inbox{id="338"} 0
paperless_tag_inbox{id="8463"} 0
# HELP paperless_tag_info Static information about a tag.
# TYPE paperless_tag_info gauge
paperless_tag_info{id="26429",name="last",slug=""} 1
paperless_tag_info{id="2930",name="mybox",slug="mb"} 1
paperless_tag_info{id="338",name="three-three-eight",slug=""} 1
paperless_tag_info{id="8463",name="aaa",slug="aslug"} 1
`)
Expand Down

0 comments on commit df7648d

Please sign in to comment.