You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
3.9 KiB
116 lines
3.9 KiB
3 years ago
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package activities
|
||
3 years ago
|
|
||
3 years ago
|
import (
|
||
3 years ago
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
||
3 years ago
|
"code.gitea.io/gitea/models/auth"
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
issues_model "code.gitea.io/gitea/models/issues"
|
||
3 years ago
|
"code.gitea.io/gitea/models/organization"
|
||
3 years ago
|
access_model "code.gitea.io/gitea/models/perm/access"
|
||
3 years ago
|
project_model "code.gitea.io/gitea/models/project"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
3 years ago
|
"code.gitea.io/gitea/models/webhook"
|
||
3 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
3 years ago
|
)
|
||
3 years ago
|
|
||
|
// Statistic contains the database statistics
|
||
|
type Statistic struct {
|
||
|
Counter struct {
|
||
|
User, Org, PublicKey,
|
||
|
Repo, Watch, Star, Action, Access,
|
||
|
Issue, IssueClosed, IssueOpen,
|
||
|
Comment, Oauth, Follow,
|
||
3 years ago
|
Mirror, Release, AuthSource, Webhook,
|
||
3 years ago
|
Milestone, Label, HookTask,
|
||
3 years ago
|
Team, UpdateTask, Project,
|
||
|
ProjectBoard, Attachment int64
|
||
3 years ago
|
IssueByLabel []IssueByLabelCount
|
||
|
IssueByRepository []IssueByRepositoryCount
|
||
3 years ago
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
// IssueByLabelCount contains the number of issue group by label
|
||
|
type IssueByLabelCount struct {
|
||
|
Count int64
|
||
|
Label string
|
||
|
}
|
||
|
|
||
3 years ago
|
// IssueByRepositoryCount contains the number of issue group by repository
|
||
|
type IssueByRepositoryCount struct {
|
||
|
Count int64
|
||
|
OwnerName string
|
||
|
Repository string
|
||
|
}
|
||
|
|
||
3 years ago
|
// GetStatistic returns the database statistics
|
||
|
func GetStatistic() (stats Statistic) {
|
||
3 years ago
|
e := db.GetEngine(db.DefaultContext)
|
||
3 years ago
|
stats.Counter.User = user_model.CountUsers(nil)
|
||
3 years ago
|
stats.Counter.Org, _ = organization.CountOrgs(organization.FindOrgOptions{IncludePrivate: true})
|
||
3 years ago
|
stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
|
||
3 years ago
|
stats.Counter.Repo, _ = repo_model.CountRepositories(db.DefaultContext, repo_model.CountRepositoryOptions{})
|
||
3 years ago
|
stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
|
||
|
stats.Counter.Star, _ = e.Count(new(repo_model.Star))
|
||
3 years ago
|
stats.Counter.Action, _ = db.EstimateCount(db.DefaultContext, new(Action))
|
||
3 years ago
|
stats.Counter.Access, _ = e.Count(new(access_model.Access))
|
||
3 years ago
|
|
||
|
type IssueCount struct {
|
||
|
Count int64
|
||
|
IsClosed bool
|
||
|
}
|
||
3 years ago
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
3 years ago
|
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)
|
||
|
}
|
||
|
|
||
3 years ago
|
issueCounts := []IssueCount{}
|
||
|
|
||
3 years ago
|
_ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
|
||
3 years ago
|
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
|
||
|
|
||
3 years ago
|
stats.Counter.Comment, _ = e.Count(new(issues_model.Comment))
|
||
3 years ago
|
stats.Counter.Oauth = 0
|
||
3 years ago
|
stats.Counter.Follow, _ = e.Count(new(user_model.Follow))
|
||
3 years ago
|
stats.Counter.Mirror, _ = e.Count(new(repo_model.Mirror))
|
||
2 years ago
|
stats.Counter.Release, _ = e.Count(new(repo_model.Release))
|
||
3 years ago
|
stats.Counter.AuthSource = auth.CountSources()
|
||
3 years ago
|
stats.Counter.Webhook, _ = e.Count(new(webhook.Webhook))
|
||
3 years ago
|
stats.Counter.Milestone, _ = e.Count(new(issues_model.Milestone))
|
||
3 years ago
|
stats.Counter.Label, _ = e.Count(new(issues_model.Label))
|
||
3 years ago
|
stats.Counter.HookTask, _ = e.Count(new(webhook.HookTask))
|
||
3 years ago
|
stats.Counter.Team, _ = e.Count(new(organization.Team))
|
||
3 years ago
|
stats.Counter.Attachment, _ = e.Count(new(repo_model.Attachment))
|
||
3 years ago
|
stats.Counter.Project, _ = e.Count(new(project_model.Project))
|
||
|
stats.Counter.ProjectBoard, _ = e.Count(new(project_model.Board))
|
||
2 years ago
|
return stats
|
||
3 years ago
|
}
|