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.5KB

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