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 4.1KB

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