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.

statistic.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activities
  4. import (
  5. asymkey_model "code.gitea.io/gitea/models/asymkey"
  6. "code.gitea.io/gitea/models/auth"
  7. "code.gitea.io/gitea/models/db"
  8. issues_model "code.gitea.io/gitea/models/issues"
  9. "code.gitea.io/gitea/models/organization"
  10. access_model "code.gitea.io/gitea/models/perm/access"
  11. project_model "code.gitea.io/gitea/models/project"
  12. repo_model "code.gitea.io/gitea/models/repo"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/models/webhook"
  15. "code.gitea.io/gitea/modules/setting"
  16. )
  17. // Statistic contains the database statistics
  18. type Statistic struct {
  19. Counter struct {
  20. User, Org, PublicKey,
  21. Repo, Watch, Star, Action, Access,
  22. Issue, IssueClosed, IssueOpen,
  23. Comment, Oauth, Follow,
  24. Mirror, Release, AuthSource, Webhook,
  25. Milestone, Label, HookTask,
  26. Team, UpdateTask, Project,
  27. ProjectBoard, Attachment int64
  28. IssueByLabel []IssueByLabelCount
  29. IssueByRepository []IssueByRepositoryCount
  30. }
  31. }
  32. // IssueByLabelCount contains the number of issue group by label
  33. type IssueByLabelCount struct {
  34. Count int64
  35. Label string
  36. }
  37. // IssueByRepositoryCount contains the number of issue group by repository
  38. type IssueByRepositoryCount struct {
  39. Count int64
  40. OwnerName string
  41. Repository string
  42. }
  43. // GetStatistic returns the database statistics
  44. func GetStatistic() (stats Statistic) {
  45. e := db.GetEngine(db.DefaultContext)
  46. stats.Counter.User = user_model.CountUsers(nil)
  47. stats.Counter.Org, _ = organization.CountOrgs(organization.FindOrgOptions{IncludePrivate: true})
  48. stats.Counter.PublicKey, _ = e.Count(new(asymkey_model.PublicKey))
  49. stats.Counter.Repo, _ = repo_model.CountRepositories(db.DefaultContext, repo_model.CountRepositoryOptions{})
  50. stats.Counter.Watch, _ = e.Count(new(repo_model.Watch))
  51. stats.Counter.Star, _ = e.Count(new(repo_model.Star))
  52. stats.Counter.Action, _ = db.EstimateCount(db.DefaultContext, new(Action))
  53. stats.Counter.Access, _ = e.Count(new(access_model.Access))
  54. type IssueCount struct {
  55. Count int64
  56. IsClosed bool
  57. }
  58. if setting.Metrics.EnabledIssueByLabel {
  59. stats.Counter.IssueByLabel = []IssueByLabelCount{}
  60. _ = e.Select("COUNT(*) AS count, l.name AS label").
  61. Join("LEFT", "label l", "l.id=il.label_id").
  62. Table("issue_label il").
  63. GroupBy("l.name").
  64. Find(&stats.Counter.IssueByLabel)
  65. }
  66. if setting.Metrics.EnabledIssueByRepository {
  67. stats.Counter.IssueByRepository = []IssueByRepositoryCount{}
  68. _ = e.Select("COUNT(*) AS count, r.owner_name, r.name AS repository").
  69. Join("LEFT", "repository r", "r.id=i.repo_id").
  70. Table("issue i").
  71. GroupBy("r.owner_name, r.name").
  72. Find(&stats.Counter.IssueByRepository)
  73. }
  74. issueCounts := []IssueCount{}
  75. _ = e.Select("COUNT(*) AS count, is_closed").Table("issue").GroupBy("is_closed").Find(&issueCounts)
  76. for _, c := range issueCounts {
  77. if c.IsClosed {
  78. stats.Counter.IssueClosed = c.Count
  79. } else {
  80. stats.Counter.IssueOpen = c.Count
  81. }
  82. }
  83. stats.Counter.Issue = stats.Counter.IssueClosed + stats.Counter.IssueOpen
  84. stats.Counter.Comment, _ = e.Count(new(issues_model.Comment))
  85. stats.Counter.Oauth = 0
  86. stats.Counter.Follow, _ = e.Count(new(user_model.Follow))
  87. stats.Counter.Mirror, _ = e.Count(new(repo_model.Mirror))
  88. stats.Counter.Release, _ = e.Count(new(repo_model.Release))
  89. stats.Counter.AuthSource = auth.CountSources()
  90. stats.Counter.Webhook, _ = e.Count(new(webhook.Webhook))
  91. stats.Counter.Milestone, _ = e.Count(new(issues_model.Milestone))
  92. stats.Counter.Label, _ = e.Count(new(issues_model.Label))
  93. stats.Counter.HookTask, _ = e.Count(new(webhook.HookTask))
  94. stats.Counter.Team, _ = e.Count(new(organization.Team))
  95. stats.Counter.Attachment, _ = e.Count(new(repo_model.Attachment))
  96. stats.Counter.Project, _ = e.Count(new(project_model.Project))
  97. stats.Counter.ProjectBoard, _ = e.Count(new(project_model.Board))
  98. return stats
  99. }