From e2a3f3d259d230b7d1f62138e789ccdfd8f43b73 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 2 May 2022 15:35:45 +0200 Subject: Federation: return useful statistic information for nodeinfo (#19561) Add statistic information for total user count, active user count, issue count and comment count for `/nodeinfo` --- models/issue_test.go | 7 +++++++ models/statistic.go | 2 +- models/user/user.go | 23 ++++++++++++++++------- 3 files changed, 24 insertions(+), 8 deletions(-) (limited to 'models') diff --git a/models/issue_test.go b/models/issue_test.go index 19e1295264..5382c04f43 100644 --- a/models/issue_test.go +++ b/models/issue_test.go @@ -590,3 +590,10 @@ func TestLoadTotalTrackedTime(t *testing.T) { assert.Equal(t, int64(3682), milestone.TotalTrackedTime) } + +func TestCountIssues(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + count, err := CountIssues(&IssuesOptions{}) + assert.NoError(t, err) + assert.EqualValues(t, 15, count) +} diff --git a/models/statistic.go b/models/statistic.go index 87c1bd6d75..d858102be8 100644 --- a/models/statistic.go +++ b/models/statistic.go @@ -49,7 +49,7 @@ type IssueByRepositoryCount struct { // GetStatistic returns the database statistics func GetStatistic() (stats Statistic) { e := db.GetEngine(db.DefaultContext) - stats.Counter.User = user_model.CountUsers() + stats.Counter.User = user_model.CountUsers(nil) stats.Counter.Org = organization.CountOrganizations() stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey)) stats.Counter.Repo = repo_model.CountRepositories(true) diff --git a/models/user/user.go b/models/user/user.go index 5b556c9884..6aa63a0a56 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -744,16 +744,25 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e return committer.Commit() } -func countUsers(e db.Engine) int64 { - count, _ := e. - Where("type=0"). - Count(new(User)) - return count +// CountUserFilter represent optional filters for CountUsers +type CountUserFilter struct { + LastLoginSince *int64 } // CountUsers returns number of users. -func CountUsers() int64 { - return countUsers(db.GetEngine(db.DefaultContext)) +func CountUsers(opts *CountUserFilter) int64 { + return countUsers(db.DefaultContext, opts) +} + +func countUsers(ctx context.Context, opts *CountUserFilter) int64 { + sess := db.GetEngine(ctx).Where(builder.Eq{"type": "0"}) + + if opts != nil && opts.LastLoginSince != nil { + sess = sess.Where(builder.Gte{"last_login_unix": *opts.LastLoginSince}) + } + + count, _ := sess.Count(new(User)) + return count } // GetVerifyUser get user by verify code -- cgit v1.2.3