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. // 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. 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, 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(ctx context.Context) (stats Statistic) {
  46. e := db.GetEngine(ctx)
  47. stats.Counter.User = user_model.CountUsers(ctx, 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(ctx, 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.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. var 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. }