Skip to content

Commit c64e2a3

Browse files
romdumdelvh
andauthored
Add projects and project boards in exposed metrics (#17202)
* Add projects and project boards in exposed metrics * Refactor db.GetEngine Co-authored-by: delvh <[email protected]>
1 parent 5b2b2cf commit c64e2a3

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

models/statistic.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,30 @@ type Statistic struct {
1818
Comment, Oauth, Follow,
1919
Mirror, Release, LoginSource, Webhook,
2020
Milestone, Label, HookTask,
21-
Team, UpdateTask, Attachment int64
21+
Team, UpdateTask, Project,
22+
ProjectBoard, Attachment int64
2223
}
2324
}
2425

2526
// GetStatistic returns the database statistics
2627
func GetStatistic() (stats Statistic) {
28+
e := db.GetEngine(db.DefaultContext)
2729
stats.Counter.User = CountUsers()
2830
stats.Counter.Org = CountOrganizations()
29-
stats.Counter.PublicKey, _ = db.GetEngine(db.DefaultContext).Count(new(PublicKey))
31+
stats.Counter.PublicKey, _ = e.Count(new(PublicKey))
3032
stats.Counter.Repo = CountRepositories(true)
31-
stats.Counter.Watch, _ = db.GetEngine(db.DefaultContext).Count(new(Watch))
32-
stats.Counter.Star, _ = db.GetEngine(db.DefaultContext).Count(new(Star))
33-
stats.Counter.Action, _ = db.GetEngine(db.DefaultContext).Count(new(Action))
34-
stats.Counter.Access, _ = db.GetEngine(db.DefaultContext).Count(new(Access))
33+
stats.Counter.Watch, _ = e.Count(new(Watch))
34+
stats.Counter.Star, _ = e.Count(new(Star))
35+
stats.Counter.Action, _ = e.Count(new(Action))
36+
stats.Counter.Access, _ = e.Count(new(Access))
3537

3638
type IssueCount struct {
3739
Count int64
3840
IsClosed bool
3941
}
4042
issueCounts := []IssueCount{}
4143

42-
_ = db.GetEngine(db.DefaultContext).Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
44+
_ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
4345
for _, c := range issueCounts {
4446
if c.IsClosed {
4547
stats.Counter.IssueClosed = c.Count
@@ -50,17 +52,19 @@ func GetStatistic() (stats Statistic) {
5052

5153
stats.Counter.Issue = stats.Counter.IssueClosed + stats.Counter.IssueOpen
5254

53-
stats.Counter.Comment, _ = db.GetEngine(db.DefaultContext).Count(new(Comment))
55+
stats.Counter.Comment, _ = e.Count(new(Comment))
5456
stats.Counter.Oauth = 0
55-
stats.Counter.Follow, _ = db.GetEngine(db.DefaultContext).Count(new(Follow))
56-
stats.Counter.Mirror, _ = db.GetEngine(db.DefaultContext).Count(new(Mirror))
57-
stats.Counter.Release, _ = db.GetEngine(db.DefaultContext).Count(new(Release))
57+
stats.Counter.Follow, _ = e.Count(new(Follow))
58+
stats.Counter.Mirror, _ = e.Count(new(Mirror))
59+
stats.Counter.Release, _ = e.Count(new(Release))
5860
stats.Counter.LoginSource = login.CountSources()
59-
stats.Counter.Webhook, _ = db.GetEngine(db.DefaultContext).Count(new(Webhook))
60-
stats.Counter.Milestone, _ = db.GetEngine(db.DefaultContext).Count(new(Milestone))
61-
stats.Counter.Label, _ = db.GetEngine(db.DefaultContext).Count(new(Label))
62-
stats.Counter.HookTask, _ = db.GetEngine(db.DefaultContext).Count(new(HookTask))
63-
stats.Counter.Team, _ = db.GetEngine(db.DefaultContext).Count(new(Team))
64-
stats.Counter.Attachment, _ = db.GetEngine(db.DefaultContext).Count(new(Attachment))
61+
stats.Counter.Webhook, _ = e.Count(new(Webhook))
62+
stats.Counter.Milestone, _ = e.Count(new(Milestone))
63+
stats.Counter.Label, _ = e.Count(new(Label))
64+
stats.Counter.HookTask, _ = e.Count(new(HookTask))
65+
stats.Counter.Team, _ = e.Count(new(Team))
66+
stats.Counter.Attachment, _ = e.Count(new(Attachment))
67+
stats.Counter.Project, _ = e.Count(new(Project))
68+
stats.Counter.ProjectBoard, _ = e.Count(new(ProjectBoard))
6569
return
6670
}

modules/metrics/collector.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Collector struct {
3030
Mirrors *prometheus.Desc
3131
Oauths *prometheus.Desc
3232
Organizations *prometheus.Desc
33+
Projects *prometheus.Desc
34+
ProjectBoards *prometheus.Desc
3335
PublicKeys *prometheus.Desc
3436
Releases *prometheus.Desc
3537
Repositories *prometheus.Desc
@@ -119,6 +121,16 @@ func NewCollector() Collector {
119121
"Number of Organizations",
120122
nil, nil,
121123
),
124+
Projects: prometheus.NewDesc(
125+
namespace+"projects",
126+
"Number of projects",
127+
nil, nil,
128+
),
129+
ProjectBoards: prometheus.NewDesc(
130+
namespace+"projects_boards",
131+
"Number of project boards",
132+
nil, nil,
133+
),
122134
PublicKeys: prometheus.NewDesc(
123135
namespace+"publickeys",
124136
"Number of PublicKeys",
@@ -185,6 +197,8 @@ func (c Collector) Describe(ch chan<- *prometheus.Desc) {
185197
ch <- c.Mirrors
186198
ch <- c.Oauths
187199
ch <- c.Organizations
200+
ch <- c.Projects
201+
ch <- c.ProjectBoards
188202
ch <- c.PublicKeys
189203
ch <- c.Releases
190204
ch <- c.Repositories
@@ -275,6 +289,16 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
275289
prometheus.GaugeValue,
276290
float64(stats.Counter.Org),
277291
)
292+
ch <- prometheus.MustNewConstMetric(
293+
c.Projects,
294+
prometheus.GaugeValue,
295+
float64(stats.Counter.Project),
296+
)
297+
ch <- prometheus.MustNewConstMetric(
298+
c.ProjectBoards,
299+
prometheus.GaugeValue,
300+
float64(stats.Counter.ProjectBoard),
301+
)
278302
ch <- prometheus.MustNewConstMetric(
279303
c.PublicKeys,
280304
prometheus.GaugeValue,

0 commit comments

Comments
 (0)