gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activities
  4. import (
  5. "context"
  6. asymkey_model "code.gitea.io/gitea/models/asymkey"
  7. "code.gitea.io/gitea/models/auth"
  8. "code.gitea.io/gitea/models/db"
  9. git_model "code.gitea.io/gitea/models/git"
  10. issues_model "code.gitea.io/gitea/models/issues"
  11. "code.gitea.io/gitea/models/organization"
  12. access_model "code.gitea.io/gitea/models/perm/access"
  13. project_model "code.gitea.io/gitea/models/project"
  14. repo_model "code.gitea.io/gitea/models/repo"
  15. user_model "code.gitea.io/gitea/models/user"
  16. "code.gitea.io/gitea/models/webhook"
  17. "code.gitea.io/gitea/modules/optional"
  18. "code.gitea.io/gitea/modules/setting"
  19. "code.gitea.io/gitea/modules/structs"
  20. )
  21. // Statistic contains the database statistics
  22. type Statistic struct {
  23. Counter struct {
  24. UsersActive, UsersNotActive,
  25. Org, PublicKey,
  26. Repo, Watch, Star, Access,
  27. Issue, IssueClosed, IssueOpen,
  28. Comment, Oauth, Follow,
  29. Mirror, Release, AuthSource, Webhook,
  30. Milestone, Label, HookTask,
  31. Team, UpdateTask, Project,
  32. ProjectColumn, Attachment,
  33. Branches, Tags, CommitStatus int64
  34. IssueByLabel []IssueByLabelCount
  35. IssueByRepository []IssueByRepositoryCount
  36. }
  37. }
  38. // IssueByLabelCount contains the number of issue group by label
  39. type IssueByLabelCount struct {
  40. Count int64
  41. Label string
  42. }
  43. // IssueByRepositoryCount contains the number of issue group by repository
  44. type IssueByRepositoryCount struct {
  45. Count int64
  46. OwnerName string
  47. Repository string
  48. }
  49. // GetStatistic returns the database statistics
  50. func GetStatistic(ctx context.Context) (stats Statistic) {
  51. e := db.GetEngine(ctx)
  52. // Number of active users
  53. usersActiveOpts := user_model.CountUserFilter{
  54. IsActive: optional.Some(true),
  55. }
  56. stats.Counter.UsersActive = user_model.CountUsers(ctx, &usersActiveOpts)
  57. // Number of inactive users
  58. usersNotActiveOpts := user_model.CountUserFilter{
  59. IsActive: optional.Some(false),
  60. }
  61. stats.Counter.UsersNotActive = user_model.CountUsers(ctx, &usersNotActiveOpts)
  62. stats.Counter.Org, _ = db.Count[organization.Organization](ctx, organization.FindOrgOptions{IncludeVisibility: structs.VisibleTypePrivate})
  63. stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
  64. stats.Counter.Repo, _ = repo_model.CountRepositories(ctx, repo_model.CountRepositoryOptions{})
  65. stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
  66. stats.Counter.Star, _ = e.Count(new(repo_model.Star))
  67. stats.Counter.Access, _ = e.Count(new(access_model.Access))
  68. stats.Counter.Branches, _ = e.Count(new(git_model.Branch))
  69. stats.Counter.Tags, _ = e.Where("is_draft=?", false).Count(new(repo_model.Release))
  70. stats.Counter.CommitStatus, _ = e.Count(new(git_model.CommitStatus))
  71. type IssueCount struct {
  72. Count int64
  73. IsClosed bool
  74. }
  75. if setting.Metrics.EnabledIssueByLabel {
  76. stats.Counter.IssueByLabel = []IssueByLabelCount{}
  77. _ = e.Select("COUNT(*) AS count, l.name AS label").
  78. Join("LEFT", "label l", "l.id=il.label_id").
  79. Table("issue_label il").
  80. GroupBy("l.name").
  81. Find(&stats.Counter.IssueByLabel)
  82. }
  83. if setting.Metrics.EnabledIssueByRepository {
  84. stats.Counter.IssueByRepository = []IssueByRepositoryCount{}
  85. _ = e.Select("COUNT(*) AS count, r.owner_name, r.name AS repository").
  86. Join("LEFT", "repository r", "r.id=i.repo_id").
  87. Table("issue i").
  88. GroupBy("r.owner_name, r.name").
  89. Find(&stats.Counter.IssueByRepository)
  90. }
  91. var issueCounts []IssueCount
  92. _ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
  93. for _, c := range issueCounts {
  94. if c.IsClosed {
  95. stats.Counter.IssueClosed = c.Count
  96. } else {
  97. stats.Counter.IssueOpen = c.Count
  98. }
  99. }
  100. stats.Counter.Issue = stats.Counter.IssueClosed + stats.Counter.IssueOpen
  101. stats.Counter.Comment, _ = e.Count(new(issues_model.Comment))
  102. stats.Counter.Oauth = 0
  103. stats.Counter.Follow, _ = e.Count(new(user_model.Follow))
  104. stats.Counter.Mirror, _ = e.Count(new(repo_model.Mirror))
  105. stats.Counter.Release, _ = e.Count(new(repo_model.Release))
  106. stats.Counter.AuthSource, _ = db.Count[auth.Source](ctx, auth.FindSourcesOptions{})
  107. stats.Counter.Webhook, _ = e.Count(new(webhook.Webhook))
  108. stats.Counter.Milestone, _ = e.Count(new(issues_model.Milestone))
  109. stats.Counter.Label, _ = e.Count(new(issues_model.Label))
  110. stats.Counter.HookTask, _ = e.Count(new(webhook.HookTask))
  111. stats.Counter.Team, _ = e.Count(new(organization.Team))
  112. stats.Counter.Attachment, _ = e.Count(new(repo_model.Attachment))
  113. stats.Counter.Project, _ = e.Count(new(project_model.Project))
  114. stats.Counter.ProjectColumn, _ = e.Count(new(project_model.Column))
  115. return stats
  116. }