Skip to content

Commit

Permalink
Get the issue counts in one query
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Thornton <[email protected]>
  • Loading branch information
zeripath committed Aug 7, 2021
1 parent f4bfaa2 commit ed1f319
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
23 changes: 20 additions & 3 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
12 changes: 12 additions & 0 deletions modules/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ed1f319

Please sign in to comment.