Skip to content

Commit ca86dba

Browse files
committed
Federation: ShareUserStatistics
1 parent 219c87e commit ca86dba

File tree

6 files changed

+38
-13
lines changed

6 files changed

+38
-13
lines changed

cmd/admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ func runCreateUser(c *cli.Context) error {
551551

552552
// If this is the first user being created.
553553
// Take it as the admin and don't force a password update.
554-
if n := user_model.CountUsers(); n == 0 {
554+
if n := user_model.CountUsers(nil); n == 0 {
555555
changePassword = false
556556
}
557557

models/statistic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type IssueByRepositoryCount struct {
4949
// GetStatistic returns the database statistics
5050
func GetStatistic() (stats Statistic) {
5151
e := db.GetEngine(db.DefaultContext)
52-
stats.Counter.User = user_model.CountUsers()
52+
stats.Counter.User = user_model.CountUsers(nil)
5353
stats.Counter.Org = organization.CountOrganizations()
5454
stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
5555
stats.Counter.Repo = repo_model.CountRepositories(true)

models/user/user.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,16 +711,27 @@ func CreateUser(u *User, overwriteDefault ...*CreateUserOverwriteOptions) (err e
711711
return committer.Commit()
712712
}
713713

714-
func countUsers(e db.Engine) int64 {
715-
count, _ := e.
716-
Where("type=0").
717-
Count(new(User))
718-
return count
714+
// CountUserFilter represent optional filters for CountUsers
715+
type CountUserFilter struct {
716+
LastLoginSince *int64
719717
}
720718

721719
// CountUsers returns number of users.
722-
func CountUsers() int64 {
723-
return countUsers(db.GetEngine(db.DefaultContext))
720+
func CountUsers(opts *CountUserFilter) int64 {
721+
return countUsers(db.DefaultContext, opts)
722+
}
723+
724+
func countUsers(ctx context.Context, opts *CountUserFilter) int64 {
725+
sess := db.GetEngine(ctx).Where(builder.Eq{"type": "0"})
726+
727+
if opts != nil {
728+
if opts.LastLoginSince != nil {
729+
sess = sess.Where(builder.Gte{"last_login_unix": *opts.LastLoginSince})
730+
}
731+
}
732+
733+
count, _ := sess.Count(new(User))
734+
return count
724735
}
725736

726737
// GetVerifyUser get user by verify code

modules/setting/federation.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import "code.gitea.io/gitea/modules/log"
99
// Federation settings
1010
var (
1111
Federation = struct {
12-
Enabled bool
12+
Enabled bool
13+
ShareUserStatistics bool
1314
}{
14-
Enabled: true,
15+
Enabled: true,
16+
ShareUserStatistics: true,
1517
}
1618
)
1719

routers/api/v1/misc/nodeinfo.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package misc
66

77
import (
88
"net/http"
9+
"time"
910

11+
user_model "code.gitea.io/gitea/models/user"
1012
"code.gitea.io/gitea/modules/context"
1113
"code.gitea.io/gitea/modules/setting"
1214
"code.gitea.io/gitea/modules/structs"
@@ -23,6 +25,16 @@ func NodeInfo(ctx *context.APIContext) {
2325
// "200":
2426
// "$ref": "#/responses/NodeInfo"
2527

28+
infoUsageUsers := structs.NodeInfoUsageUsers{}
29+
if setting.Federation.ShareUserStatistics {
30+
infoUsageUsers.Total = int(user_model.CountUsers(nil))
31+
now := time.Now()
32+
timeOneMonthAgo := now.AddDate(0, -1, 0).Unix()
33+
timeHaveYearAgo := now.AddDate(0, -6, 0).Unix()
34+
infoUsageUsers.ActiveMonth = int(user_model.CountUsers(&user_model.CountUserFilter{LastLoginSince: &timeOneMonthAgo}))
35+
infoUsageUsers.ActiveHalfyear = int(user_model.CountUsers(&user_model.CountUserFilter{LastLoginSince: &timeHaveYearAgo}))
36+
}
37+
2638
nodeInfo := &structs.NodeInfo{
2739
Version: "2.1",
2840
Software: structs.NodeInfoSoftware{
@@ -38,7 +50,7 @@ func NodeInfo(ctx *context.APIContext) {
3850
},
3951
OpenRegistrations: setting.Service.ShowRegistrationButton,
4052
Usage: structs.NodeInfoUsage{
41-
Users: structs.NodeInfoUsageUsers{},
53+
Users: infoUsageUsers,
4254
},
4355
}
4456
ctx.JSON(http.StatusOK, nodeInfo)

routers/web/auth/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ func createUserInContext(ctx *context.Context, tpl base.TplName, form interface{
602602
// sends a confirmation email if required.
603603
func handleUserCreated(ctx *context.Context, u *user_model.User, gothUser *goth.User) (ok bool) {
604604
// Auto-set admin for the only user.
605-
if user_model.CountUsers() == 1 {
605+
if user_model.CountUsers(nil) == 1 {
606606
u.IsAdmin = true
607607
u.IsActive = true
608608
u.SetLastLogin()

0 commit comments

Comments
 (0)