Skip to content

Commit 14762ab

Browse files
romdumzeripath
andauthored
Separate open and closed issue in metrics (#16637)
* Get the issue counts in one query Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Andrew Thornton <art27@cantab.net>
1 parent 620c569 commit 14762ab

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

models/models.go

100644100755
+20-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ type Statistic struct {
272272
Counter struct {
273273
User, Org, PublicKey,
274274
Repo, Watch, Star, Action, Access,
275-
Issue, Comment, Oauth, Follow,
275+
Issue, IssueClosed, IssueOpen,
276+
Comment, Oauth, Follow,
276277
Mirror, Release, LoginSource, Webhook,
277278
Milestone, Label, HookTask,
278279
Team, UpdateTask, Attachment int64
@@ -289,7 +290,24 @@ func GetStatistic() (stats Statistic) {
289290
stats.Counter.Star, _ = x.Count(new(Star))
290291
stats.Counter.Action, _ = x.Count(new(Action))
291292
stats.Counter.Access, _ = x.Count(new(Access))
292-
stats.Counter.Issue, _ = x.Count(new(Issue))
293+
294+
type IssueCount struct {
295+
Count int64
296+
IsClosed bool
297+
}
298+
issueCounts := []IssueCount{}
299+
300+
_ = x.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
301+
for _, c := range issueCounts {
302+
if c.IsClosed {
303+
stats.Counter.IssueClosed = c.Count
304+
} else {
305+
stats.Counter.IssueOpen = c.Count
306+
}
307+
}
308+
309+
stats.Counter.Issue = stats.Counter.IssueClosed + stats.Counter.IssueOpen
310+
293311
stats.Counter.Comment, _ = x.Count(new(Comment))
294312
stats.Counter.Oauth = 0
295313
stats.Counter.Follow, _ = x.Count(new(Follow))

modules/metrics/collector.go

100644100755
+24
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Collector struct {
2222
Follows *prometheus.Desc
2323
HookTasks *prometheus.Desc
2424
Issues *prometheus.Desc
25+
IssuesOpen *prometheus.Desc
26+
IssuesClosed *prometheus.Desc
2527
Labels *prometheus.Desc
2628
LoginSources *prometheus.Desc
2729
Milestones *prometheus.Desc
@@ -77,6 +79,16 @@ func NewCollector() Collector {
7779
"Number of Issues",
7880
nil, nil,
7981
),
82+
IssuesOpen: prometheus.NewDesc(
83+
namespace+"issues_open",
84+
"Number of open Issues",
85+
nil, nil,
86+
),
87+
IssuesClosed: prometheus.NewDesc(
88+
namespace+"issues_closed",
89+
"Number of closed Issues",
90+
nil, nil,
91+
),
8092
Labels: prometheus.NewDesc(
8193
namespace+"labels",
8294
"Number of Labels",
@@ -165,6 +177,8 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
165177
ch <- c.Follows
166178
ch <- c.HookTasks
167179
ch <- c.Issues
180+
ch <- c.IssuesOpen
181+
ch <- c.IssuesClosed
168182
ch <- c.Labels
169183
ch <- c.LoginSources
170184
ch <- c.Milestones
@@ -221,6 +235,16 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
221235
prometheus.GaugeValue,
222236
float64(stats.Counter.Issue),
223237
)
238+
ch <- prometheus.MustNewConstMetric(
239+
c.IssuesClosed,
240+
prometheus.GaugeValue,
241+
float64(stats.Counter.IssueClosed),
242+
)
243+
ch <- prometheus.MustNewConstMetric(
244+
c.IssuesOpen,
245+
prometheus.GaugeValue,
246+
float64(stats.Counter.IssueOpen),
247+
)
224248
ch <- prometheus.MustNewConstMetric(
225249
c.Labels,
226250
prometheus.GaugeValue,

0 commit comments

Comments
 (0)