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

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