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

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