2021-09-19 17:19:59 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-09-19 17:19:59 +05:30
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
package activities
|
2021-09-19 17:19:59 +05:30
|
|
|
|
2021-09-24 17:02:56 +05:30
|
|
|
import (
|
2021-12-10 13:44:24 +05:30
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2022-01-02 18:42:35 +05:30
|
|
|
"code.gitea.io/gitea/models/auth"
|
2021-09-24 17:02:56 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-04-08 14:41:15 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2022-03-29 19:46:31 +05:30
|
|
|
project_model "code.gitea.io/gitea/models/project"
|
2021-11-19 19:09:57 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-17 15:28:31 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 10:43:16 +05:30
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2021-10-04 04:16:44 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-09-24 17:02:56 +05:30
|
|
|
)
|
2021-09-19 17:19:59 +05:30
|
|
|
|
|
|
|
// Statistic contains the database statistics
|
|
|
|
type Statistic struct {
|
|
|
|
Counter struct {
|
|
|
|
User, Org, PublicKey,
|
2023-06-03 19:33:41 +05:30
|
|
|
Repo, Watch, Star, Access,
|
2021-09-19 17:19:59 +05:30
|
|
|
Issue, IssueClosed, IssueOpen,
|
|
|
|
Comment, Oauth, Follow,
|
2022-01-02 18:42:35 +05:30
|
|
|
Mirror, Release, AuthSource, Webhook,
|
2021-09-19 17:19:59 +05:30
|
|
|
Milestone, Label, HookTask,
|
2021-10-02 07:02:15 +05:30
|
|
|
Team, UpdateTask, Project,
|
|
|
|
ProjectBoard, Attachment int64
|
2021-10-06 00:09:37 +05:30
|
|
|
IssueByLabel []IssueByLabelCount
|
|
|
|
IssueByRepository []IssueByRepositoryCount
|
2021-09-19 17:19:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 04:16:44 +05:30
|
|
|
// IssueByLabelCount contains the number of issue group by label
|
|
|
|
type IssueByLabelCount struct {
|
|
|
|
Count int64
|
|
|
|
Label string
|
|
|
|
}
|
|
|
|
|
2021-10-06 00:09:37 +05:30
|
|
|
// IssueByRepositoryCount contains the number of issue group by repository
|
|
|
|
type IssueByRepositoryCount struct {
|
|
|
|
Count int64
|
|
|
|
OwnerName string
|
|
|
|
Repository string
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
// GetStatistic returns the database statistics
|
|
|
|
func GetStatistic() (stats Statistic) {
|
2021-10-02 07:02:15 +05:30
|
|
|
e := db.GetEngine(db.DefaultContext)
|
2022-05-02 19:05:45 +05:30
|
|
|
stats.Counter.User = user_model.CountUsers(nil)
|
2022-05-20 19:38:52 +05:30
|
|
|
stats.Counter.Org, _ = organization.CountOrgs(organization.FindOrgOptions{IncludePrivate: true})
|
2021-12-10 13:44:24 +05:30
|
|
|
stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
|
2022-05-20 19:38:52 +05:30
|
|
|
stats.Counter.Repo, _ = repo_model.CountRepositories(db.DefaultContext, repo_model.CountRepositoryOptions{})
|
2021-12-12 21:18:20 +05:30
|
|
|
stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
|
|
|
|
stats.Counter.Star, _ = e.Count(new(repo_model.Star))
|
2022-05-11 15:39:36 +05:30
|
|
|
stats.Counter.Access, _ = e.Count(new(access_model.Access))
|
2021-09-19 17:19:59 +05:30
|
|
|
|
|
|
|
type IssueCount struct {
|
|
|
|
Count int64
|
|
|
|
IsClosed bool
|
|
|
|
}
|
2021-10-04 04:16:44 +05:30
|
|
|
|
|
|
|
if setting.Metrics.EnabledIssueByLabel {
|
|
|
|
stats.Counter.IssueByLabel = []IssueByLabelCount{}
|
|
|
|
|
|
|
|
_ = e.Select("COUNT(*) AS count, l.name AS label").
|
|
|
|
Join("LEFT", "label l", "l.id=il.label_id").
|
|
|
|
Table("issue_label il").
|
|
|
|
GroupBy("l.name").
|
|
|
|
Find(&stats.Counter.IssueByLabel)
|
|
|
|
}
|
|
|
|
|
2021-10-06 00:09:37 +05:30
|
|
|
if setting.Metrics.EnabledIssueByRepository {
|
|
|
|
stats.Counter.IssueByRepository = []IssueByRepositoryCount{}
|
|
|
|
|
|
|
|
_ = e.Select("COUNT(*) AS count, r.owner_name, r.name AS repository").
|
|
|
|
Join("LEFT", "repository r", "r.id=i.repo_id").
|
|
|
|
Table("issue i").
|
|
|
|
GroupBy("r.owner_name, r.name").
|
|
|
|
Find(&stats.Counter.IssueByRepository)
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:33:41 +05:30
|
|
|
var issueCounts []IssueCount
|
2021-09-19 17:19:59 +05:30
|
|
|
|
2021-10-02 07:02:15 +05:30
|
|
|
_ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
|
2021-09-19 17:19:59 +05:30
|
|
|
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
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
stats.Counter.Comment, _ = e.Count(new(issues_model.Comment))
|
2021-09-19 17:19:59 +05:30
|
|
|
stats.Counter.Oauth = 0
|
2021-11-17 15:28:31 +05:30
|
|
|
stats.Counter.Follow, _ = e.Count(new(user_model.Follow))
|
2021-12-10 06:57:50 +05:30
|
|
|
stats.Counter.Mirror, _ = e.Count(new(repo_model.Mirror))
|
2022-08-25 08:01:57 +05:30
|
|
|
stats.Counter.Release, _ = e.Count(new(repo_model.Release))
|
2022-01-02 18:42:35 +05:30
|
|
|
stats.Counter.AuthSource = auth.CountSources()
|
2021-11-10 10:43:16 +05:30
|
|
|
stats.Counter.Webhook, _ = e.Count(new(webhook.Webhook))
|
2022-04-08 14:41:15 +05:30
|
|
|
stats.Counter.Milestone, _ = e.Count(new(issues_model.Milestone))
|
2022-06-13 15:07:59 +05:30
|
|
|
stats.Counter.Label, _ = e.Count(new(issues_model.Label))
|
2021-11-10 10:43:16 +05:30
|
|
|
stats.Counter.HookTask, _ = e.Count(new(webhook.HookTask))
|
2022-03-29 11:59:02 +05:30
|
|
|
stats.Counter.Team, _ = e.Count(new(organization.Team))
|
2021-11-19 19:09:57 +05:30
|
|
|
stats.Counter.Attachment, _ = e.Count(new(repo_model.Attachment))
|
2022-03-29 19:46:31 +05:30
|
|
|
stats.Counter.Project, _ = e.Count(new(project_model.Project))
|
|
|
|
stats.Counter.ProjectBoard, _ = e.Count(new(project_model.Board))
|
2022-06-20 15:32:49 +05:30
|
|
|
return stats
|
2021-09-19 17:19:59 +05:30
|
|
|
}
|