From 7616dd7686fec5afae9744fdd4453f1430d1bb48 Mon Sep 17 00:00:00 2001 From: Romain Date: Fri, 6 Aug 2021 08:23:24 +0200 Subject: [PATCH 1/2] Separate open and closed issue in metrics --- models/models.go | 5 +++-- modules/metrics/collector.go | 26 +++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) mode change 100644 => 100755 models/models.go mode change 100644 => 100755 modules/metrics/collector.go diff --git a/models/models.go b/models/models.go old mode 100644 new mode 100755 index 610933d3270bd..c6ee772180e85 --- a/models/models.go +++ b/models/models.go @@ -272,7 +272,7 @@ type Statistic struct { Counter struct { User, Org, PublicKey, Repo, Watch, Star, Action, Access, - Issue, Comment, Oauth, Follow, + IssueClosed, IssueOpen, Comment, Oauth, Follow, Mirror, Release, LoginSource, Webhook, Milestone, Label, HookTask, Team, UpdateTask, Attachment int64 @@ -289,7 +289,8 @@ func GetStatistic() (stats Statistic) { stats.Counter.Star, _ = x.Count(new(Star)) stats.Counter.Action, _ = x.Count(new(Action)) stats.Counter.Access, _ = x.Count(new(Access)) - stats.Counter.Issue, _ = x.Count(new(Issue)) + stats.Counter.IssueClosed, _ = x.Where("is_closed=?", true).Count(new(Issue)) + stats.Counter.IssueOpen, _ = x.Where("is_closed=?", false).Count(new(Issue)) stats.Counter.Comment, _ = x.Count(new(Comment)) stats.Counter.Oauth = 0 stats.Counter.Follow, _ = x.Count(new(Follow)) diff --git a/modules/metrics/collector.go b/modules/metrics/collector.go old mode 100644 new mode 100755 index 6f6cf7cb64659..5cd70d70ef1d3 --- a/modules/metrics/collector.go +++ b/modules/metrics/collector.go @@ -21,7 +21,8 @@ type Collector struct { Comments *prometheus.Desc Follows *prometheus.Desc HookTasks *prometheus.Desc - Issues *prometheus.Desc + IssuesOpen *prometheus.Desc + IssuesClosed *prometheus.Desc Labels *prometheus.Desc LoginSources *prometheus.Desc Milestones *prometheus.Desc @@ -72,9 +73,14 @@ func NewCollector() Collector { "Number of HookTasks", nil, nil, ), - Issues: prometheus.NewDesc( - namespace+"issues", - "Number of Issues", + IssuesOpen: prometheus.NewDesc( + namespace+"issues_open", + "Number of open Issues", + nil, nil, + ), + IssuesClosed: prometheus.NewDesc( + namespace+"issues_closed", + "Number of closed Issues", nil, nil, ), Labels: prometheus.NewDesc( @@ -164,7 +170,8 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) { ch <- c.Comments ch <- c.Follows ch <- c.HookTasks - ch <- c.Issues + ch <- c.IssuesOpen + ch <- c.IssuesClosed ch <- c.Labels ch <- c.LoginSources ch <- c.Milestones @@ -217,9 +224,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) { float64(stats.Counter.HookTask), ) ch <- prometheus.MustNewConstMetric( - c.Issues, + c.IssuesClosed, + prometheus.GaugeValue, + float64(stats.Counter.IssueClosed), + ) + ch <- prometheus.MustNewConstMetric( + c.IssuesOpen, prometheus.GaugeValue, - float64(stats.Counter.Issue), + float64(stats.Counter.IssueOpen), ) ch <- prometheus.MustNewConstMetric( c.Labels, From ed1f31941d51eee2a2a9847db7add86f0f1112fe Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 7 Aug 2021 10:12:06 +0100 Subject: [PATCH 2/2] Get the issue counts in one query Signed-off-by: Andrew Thornton --- models/models.go | 23 ++++++++++++++++++++--- modules/metrics/collector.go | 12 ++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/models/models.go b/models/models.go index c6ee772180e85..378747dbce51f 100755 --- a/models/models.go +++ b/models/models.go @@ -272,7 +272,8 @@ type Statistic struct { Counter struct { User, Org, PublicKey, Repo, Watch, Star, Action, Access, - IssueClosed, IssueOpen, Comment, Oauth, Follow, + Issue, IssueClosed, IssueOpen, + Comment, Oauth, Follow, Mirror, Release, LoginSource, Webhook, Milestone, Label, HookTask, Team, UpdateTask, Attachment int64 @@ -289,8 +290,24 @@ func GetStatistic() (stats Statistic) { stats.Counter.Star, _ = x.Count(new(Star)) stats.Counter.Action, _ = x.Count(new(Action)) stats.Counter.Access, _ = x.Count(new(Access)) - stats.Counter.IssueClosed, _ = x.Where("is_closed=?", true).Count(new(Issue)) - stats.Counter.IssueOpen, _ = x.Where("is_closed=?", false).Count(new(Issue)) + + type IssueCount struct { + Count int64 + IsClosed bool + } + issueCounts := []IssueCount{} + + _ = x.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts) + for _, c := range issueCounts { + if c.IsClosed { + stats.Counter.IssueClosed = c.Count + } else { + stats.Counter.IssueOpen = c.Count + } + } + + stats.Counter.Issue = stats.Counter.IssueClosed + stats.Counter.IssueOpen + stats.Counter.Comment, _ = x.Count(new(Comment)) stats.Counter.Oauth = 0 stats.Counter.Follow, _ = x.Count(new(Follow)) diff --git a/modules/metrics/collector.go b/modules/metrics/collector.go index 5cd70d70ef1d3..f906b58f7da82 100755 --- a/modules/metrics/collector.go +++ b/modules/metrics/collector.go @@ -21,6 +21,7 @@ type Collector struct { Comments *prometheus.Desc Follows *prometheus.Desc HookTasks *prometheus.Desc + Issues *prometheus.Desc IssuesOpen *prometheus.Desc IssuesClosed *prometheus.Desc Labels *prometheus.Desc @@ -73,6 +74,11 @@ func NewCollector() Collector { "Number of HookTasks", nil, nil, ), + Issues: prometheus.NewDesc( + namespace+"issues", + "Number of Issues", + nil, nil, + ), IssuesOpen: prometheus.NewDesc( namespace+"issues_open", "Number of open Issues", @@ -170,6 +176,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) { ch <- c.Comments ch <- c.Follows ch <- c.HookTasks + ch <- c.Issues ch <- c.IssuesOpen ch <- c.IssuesClosed ch <- c.Labels @@ -223,6 +230,11 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) { prometheus.GaugeValue, float64(stats.Counter.HookTask), ) + ch <- prometheus.MustNewConstMetric( + c.Issues, + prometheus.GaugeValue, + float64(stats.Counter.Issue), + ) ch <- prometheus.MustNewConstMetric( c.IssuesClosed, prometheus.GaugeValue,