Skip to content

Commit 020e774

Browse files
authored
feat: add label 'state' to metric 'gitea_users' (#34326)
This PR adds the label _state_ to the metric _gitea_users_. With the change, _gitea_users_ would be reported like this: ``` ... # HELP gitea_users Number of Users # TYPE gitea_users gauge gitea_users{state="active"} 20 gitea_users{state="inactive"} 10 ... ``` The metrics above would be from a Gitea instance with 30 user accounts. 20 of the accounts are active and 10 of the accounts are not active. Resolve #34325
1 parent dd886d7 commit 020e774

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

models/activities/statistic.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ import (
1717
repo_model "code.gitea.io/gitea/models/repo"
1818
user_model "code.gitea.io/gitea/models/user"
1919
"code.gitea.io/gitea/models/webhook"
20+
"code.gitea.io/gitea/modules/optional"
2021
"code.gitea.io/gitea/modules/setting"
2122
)
2223

2324
// Statistic contains the database statistics
2425
type Statistic struct {
2526
Counter struct {
26-
User, Org, PublicKey,
27+
UsersActive, UsersNotActive,
28+
Org, PublicKey,
2729
Repo, Watch, Star, Access,
2830
Issue, IssueClosed, IssueOpen,
2931
Comment, Oauth, Follow,
@@ -53,7 +55,19 @@ type IssueByRepositoryCount struct {
5355
// GetStatistic returns the database statistics
5456
func GetStatistic(ctx context.Context) (stats Statistic) {
5557
e := db.GetEngine(ctx)
56-
stats.Counter.User = user_model.CountUsers(ctx, nil)
58+
59+
// Number of active users
60+
usersActiveOpts := user_model.CountUserFilter{
61+
IsActive: optional.Some(true),
62+
}
63+
stats.Counter.UsersActive = user_model.CountUsers(ctx, &usersActiveOpts)
64+
65+
// Number of inactive users
66+
usersNotActiveOpts := user_model.CountUserFilter{
67+
IsActive: optional.Some(false),
68+
}
69+
stats.Counter.UsersNotActive = user_model.CountUsers(ctx, &usersNotActiveOpts)
70+
5771
stats.Counter.Org, _ = db.Count[organization.Organization](ctx, organization.FindOrgOptions{IncludePrivate: true})
5872
stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
5973
stats.Counter.Repo, _ = repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{})

models/user/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ func IsLastAdminUser(ctx context.Context, user *User) bool {
828828
type CountUserFilter struct {
829829
LastLoginSince *int64
830830
IsAdmin optional.Option[bool]
831+
IsActive optional.Option[bool]
831832
}
832833

833834
// CountUsers returns number of users.
@@ -848,6 +849,10 @@ func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
848849
if opts.IsAdmin.Has() {
849850
cond = cond.And(builder.Eq{"is_admin": opts.IsAdmin.Value()})
850851
}
852+
853+
if opts.IsActive.Has() {
854+
cond = cond.And(builder.Eq{"is_active": opts.IsActive.Value()})
855+
}
851856
}
852857

853858
count, err := sess.Where(cond).Count(new(User))

modules/metrics/collector.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func NewCollector() Collector {
184184
Users: prometheus.NewDesc(
185185
namespace+"users",
186186
"Number of Users",
187-
nil, nil,
187+
[]string{"state"}, nil,
188188
),
189189
Watches: prometheus.NewDesc(
190190
namespace+"watches",
@@ -373,7 +373,14 @@ func (c Collector) Collect(ch chan<- prometheus.Metric) {
373373
ch <- prometheus.MustNewConstMetric(
374374
c.Users,
375375
prometheus.GaugeValue,
376-
float64(stats.Counter.User),
376+
float64(stats.Counter.UsersActive),
377+
"active", // state label
378+
)
379+
ch <- prometheus.MustNewConstMetric(
380+
c.Users,
381+
prometheus.GaugeValue,
382+
float64(stats.Counter.UsersNotActive),
383+
"inactive", // state label
377384
)
378385
ch <- prometheus.MustNewConstMetric(
379386
c.Watches,

0 commit comments

Comments
 (0)