Skip to content

Commit ed1f319

Browse files
committed
Get the issue counts in one query
Signed-off-by: Andrew Thornton <[email protected]>
1 parent f4bfaa2 commit ed1f319

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

models/models.go

+20-3
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-
IssueClosed, IssueOpen, 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,8 +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.IssueClosed, _ = x.Where("is_closed=?", true).Count(new(Issue))
293-
stats.Counter.IssueOpen, _ = x.Where("is_closed=?", false).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+
294311
stats.Counter.Comment, _ = x.Count(new(Comment))
295312
stats.Counter.Oauth = 0
296313
stats.Counter.Follow, _ = x.Count(new(Follow))

modules/metrics/collector.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Collector struct {
2121
Comments *prometheus.Desc
2222
Follows *prometheus.Desc
2323
HookTasks *prometheus.Desc
24+
Issues *prometheus.Desc
2425
IssuesOpen *prometheus.Desc
2526
IssuesClosed *prometheus.Desc
2627
Labels *prometheus.Desc
@@ -73,6 +74,11 @@ func NewCollector() Collector {
7374
"Number of HookTasks",
7475
nil, nil,
7576
),
77+
Issues: prometheus.NewDesc(
78+
namespace+"issues",
79+
"Number of Issues",
80+
nil, nil,
81+
),
7682
IssuesOpen: prometheus.NewDesc(
7783
namespace+"issues_open",
7884
"Number of open Issues",
@@ -170,6 +176,7 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
170176
ch <- c.Comments
171177
ch <- c.Follows
172178
ch <- c.HookTasks
179+
ch <- c.Issues
173180
ch <- c.IssuesOpen
174181
ch <- c.IssuesClosed
175182
ch <- c.Labels
@@ -223,6 +230,11 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
223230
prometheus.GaugeValue,
224231
float64(stats.Counter.HookTask),
225232
)
233+
ch <- prometheus.MustNewConstMetric(
234+
c.Issues,
235+
prometheus.GaugeValue,
236+
float64(stats.Counter.Issue),
237+
)
226238
ch <- prometheus.MustNewConstMetric(
227239
c.IssuesClosed,
228240
prometheus.GaugeValue,

0 commit comments

Comments
 (0)