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.

347 lines
10 KiB

  1. // Copyright 2017 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. "fmt"
  7. "sort"
  8. "time"
  9. "code.gitea.io/gitea/modules/git"
  10. "github.com/go-xorm/xorm"
  11. )
  12. // ActivityAuthorData represents statistical git commit count data
  13. type ActivityAuthorData struct {
  14. Name string `json:"name"`
  15. Login string `json:"login"`
  16. AvatarLink string `json:"avatar_link"`
  17. Commits int64 `json:"commits"`
  18. }
  19. // ActivityStats represets issue and pull request information.
  20. type ActivityStats struct {
  21. OpenedPRs PullRequestList
  22. OpenedPRAuthorCount int64
  23. MergedPRs PullRequestList
  24. MergedPRAuthorCount int64
  25. OpenedIssues IssueList
  26. OpenedIssueAuthorCount int64
  27. ClosedIssues IssueList
  28. ClosedIssueAuthorCount int64
  29. UnresolvedIssues IssueList
  30. PublishedReleases []*Release
  31. PublishedReleaseAuthorCount int64
  32. Code *git.CodeActivityStats
  33. }
  34. // GetActivityStats return stats for repository at given time range
  35. func GetActivityStats(repo *Repository, timeFrom time.Time, releases, issues, prs, code bool) (*ActivityStats, error) {
  36. stats := &ActivityStats{Code: &git.CodeActivityStats{}}
  37. if releases {
  38. if err := stats.FillReleases(repo.ID, timeFrom); err != nil {
  39. return nil, fmt.Errorf("FillReleases: %v", err)
  40. }
  41. }
  42. if prs {
  43. if err := stats.FillPullRequests(repo.ID, timeFrom); err != nil {
  44. return nil, fmt.Errorf("FillPullRequests: %v", err)
  45. }
  46. }
  47. if issues {
  48. if err := stats.FillIssues(repo.ID, timeFrom); err != nil {
  49. return nil, fmt.Errorf("FillIssues: %v", err)
  50. }
  51. }
  52. if err := stats.FillUnresolvedIssues(repo.ID, timeFrom, issues, prs); err != nil {
  53. return nil, fmt.Errorf("FillUnresolvedIssues: %v", err)
  54. }
  55. if code {
  56. gitRepo, err := git.OpenRepository(repo.RepoPath())
  57. if err != nil {
  58. return nil, fmt.Errorf("OpenRepository: %v", err)
  59. }
  60. code, err := gitRepo.GetCodeActivityStats(timeFrom, repo.DefaultBranch)
  61. if err != nil {
  62. return nil, fmt.Errorf("FillFromGit: %v", err)
  63. }
  64. stats.Code = code
  65. }
  66. return stats, nil
  67. }
  68. // GetActivityStatsTopAuthors returns top author stats for git commits for all branches
  69. func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) {
  70. gitRepo, err := git.OpenRepository(repo.RepoPath())
  71. if err != nil {
  72. return nil, fmt.Errorf("OpenRepository: %v", err)
  73. }
  74. code, err := gitRepo.GetCodeActivityStats(timeFrom, "")
  75. if err != nil {
  76. return nil, fmt.Errorf("FillFromGit: %v", err)
  77. }
  78. if code.Authors == nil {
  79. return nil, nil
  80. }
  81. users := make(map[int64]*ActivityAuthorData)
  82. for k, v := range code.Authors {
  83. if len(k) == 0 {
  84. continue
  85. }
  86. u, err := GetUserByEmail(k)
  87. if u == nil || IsErrUserNotExist(err) {
  88. continue
  89. }
  90. if err != nil {
  91. return nil, err
  92. }
  93. if user, ok := users[u.ID]; !ok {
  94. users[u.ID] = &ActivityAuthorData{
  95. Name: u.DisplayName(),
  96. Login: u.LowerName,
  97. AvatarLink: u.AvatarLink(),
  98. Commits: v,
  99. }
  100. } else {
  101. user.Commits += v
  102. }
  103. }
  104. v := make([]*ActivityAuthorData, 0)
  105. for _, u := range users {
  106. v = append(v, u)
  107. }
  108. sort.Slice(v[:], func(i, j int) bool {
  109. return v[i].Commits < v[j].Commits
  110. })
  111. cnt := count
  112. if cnt > len(v) {
  113. cnt = len(v)
  114. }
  115. return v[:cnt], nil
  116. }
  117. // ActivePRCount returns total active pull request count
  118. func (stats *ActivityStats) ActivePRCount() int {
  119. return stats.OpenedPRCount() + stats.MergedPRCount()
  120. }
  121. // OpenedPRCount returns opened pull request count
  122. func (stats *ActivityStats) OpenedPRCount() int {
  123. return len(stats.OpenedPRs)
  124. }
  125. // OpenedPRPerc returns opened pull request percents from total active
  126. func (stats *ActivityStats) OpenedPRPerc() int {
  127. return int(float32(stats.OpenedPRCount()) / float32(stats.ActivePRCount()) * 100.0)
  128. }
  129. // MergedPRCount returns merged pull request count
  130. func (stats *ActivityStats) MergedPRCount() int {
  131. return len(stats.MergedPRs)
  132. }
  133. // MergedPRPerc returns merged pull request percent from total active
  134. func (stats *ActivityStats) MergedPRPerc() int {
  135. return int(float32(stats.MergedPRCount()) / float32(stats.ActivePRCount()) * 100.0)
  136. }
  137. // ActiveIssueCount returns total active issue count
  138. func (stats *ActivityStats) ActiveIssueCount() int {
  139. return stats.OpenedIssueCount() + stats.ClosedIssueCount()
  140. }
  141. // OpenedIssueCount returns open issue count
  142. func (stats *ActivityStats) OpenedIssueCount() int {
  143. return len(stats.OpenedIssues)
  144. }
  145. // OpenedIssuePerc returns open issue count percent from total active
  146. func (stats *ActivityStats) OpenedIssuePerc() int {
  147. return int(float32(stats.OpenedIssueCount()) / float32(stats.ActiveIssueCount()) * 100.0)
  148. }
  149. // ClosedIssueCount returns closed issue count
  150. func (stats *ActivityStats) ClosedIssueCount() int {
  151. return len(stats.ClosedIssues)
  152. }
  153. // ClosedIssuePerc returns closed issue count percent from total active
  154. func (stats *ActivityStats) ClosedIssuePerc() int {
  155. return int(float32(stats.ClosedIssueCount()) / float32(stats.ActiveIssueCount()) * 100.0)
  156. }
  157. // UnresolvedIssueCount returns unresolved issue and pull request count
  158. func (stats *ActivityStats) UnresolvedIssueCount() int {
  159. return len(stats.UnresolvedIssues)
  160. }
  161. // PublishedReleaseCount returns published release count
  162. func (stats *ActivityStats) PublishedReleaseCount() int {
  163. return len(stats.PublishedReleases)
  164. }
  165. // FillPullRequests returns pull request information for activity page
  166. func (stats *ActivityStats) FillPullRequests(repoID int64, fromTime time.Time) error {
  167. var err error
  168. var count int64
  169. // Merged pull requests
  170. sess := pullRequestsForActivityStatement(repoID, fromTime, true)
  171. sess.OrderBy("pull_request.merged_unix DESC")
  172. stats.MergedPRs = make(PullRequestList, 0)
  173. if err = sess.Find(&stats.MergedPRs); err != nil {
  174. return err
  175. }
  176. if err = stats.MergedPRs.LoadAttributes(); err != nil {
  177. return err
  178. }
  179. // Merged pull request authors
  180. sess = pullRequestsForActivityStatement(repoID, fromTime, true)
  181. if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil {
  182. return err
  183. }
  184. stats.MergedPRAuthorCount = count
  185. // Opened pull requests
  186. sess = pullRequestsForActivityStatement(repoID, fromTime, false)
  187. sess.OrderBy("issue.created_unix ASC")
  188. stats.OpenedPRs = make(PullRequestList, 0)
  189. if err = sess.Find(&stats.OpenedPRs); err != nil {
  190. return err
  191. }
  192. if err = stats.OpenedPRs.LoadAttributes(); err != nil {
  193. return err
  194. }
  195. // Opened pull request authors
  196. sess = pullRequestsForActivityStatement(repoID, fromTime, false)
  197. if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("pull_request").Get(&count); err != nil {
  198. return err
  199. }
  200. stats.OpenedPRAuthorCount = count
  201. return nil
  202. }
  203. func pullRequestsForActivityStatement(repoID int64, fromTime time.Time, merged bool) *xorm.Session {
  204. sess := x.Where("pull_request.base_repo_id=?", repoID).
  205. Join("INNER", "issue", "pull_request.issue_id = issue.id")
  206. if merged {
  207. sess.And("pull_request.has_merged = ?", true)
  208. sess.And("pull_request.merged_unix >= ?", fromTime.Unix())
  209. } else {
  210. sess.And("issue.is_closed = ?", false)
  211. sess.And("issue.created_unix >= ?", fromTime.Unix())
  212. }
  213. return sess
  214. }
  215. // FillIssues returns issue information for activity page
  216. func (stats *ActivityStats) FillIssues(repoID int64, fromTime time.Time) error {
  217. var err error
  218. var count int64
  219. // Closed issues
  220. sess := issuesForActivityStatement(repoID, fromTime, true, false)
  221. sess.OrderBy("issue.closed_unix DESC")
  222. stats.ClosedIssues = make(IssueList, 0)
  223. if err = sess.Find(&stats.ClosedIssues); err != nil {
  224. return err
  225. }
  226. // Closed issue authors
  227. sess = issuesForActivityStatement(repoID, fromTime, true, false)
  228. if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil {
  229. return err
  230. }
  231. stats.ClosedIssueAuthorCount = count
  232. // New issues
  233. sess = issuesForActivityStatement(repoID, fromTime, false, false)
  234. sess.OrderBy("issue.created_unix ASC")
  235. stats.OpenedIssues = make(IssueList, 0)
  236. if err = sess.Find(&stats.OpenedIssues); err != nil {
  237. return err
  238. }
  239. // Opened issue authors
  240. sess = issuesForActivityStatement(repoID, fromTime, false, false)
  241. if _, err = sess.Select("count(distinct issue.poster_id) as `count`").Table("issue").Get(&count); err != nil {
  242. return err
  243. }
  244. stats.OpenedIssueAuthorCount = count
  245. return nil
  246. }
  247. // FillUnresolvedIssues returns unresolved issue and pull request information for activity page
  248. func (stats *ActivityStats) FillUnresolvedIssues(repoID int64, fromTime time.Time, issues, prs bool) error {
  249. // Check if we need to select anything
  250. if !issues && !prs {
  251. return nil
  252. }
  253. sess := issuesForActivityStatement(repoID, fromTime, false, true)
  254. if !issues || !prs {
  255. sess.And("issue.is_pull = ?", prs)
  256. }
  257. sess.OrderBy("issue.updated_unix DESC")
  258. stats.UnresolvedIssues = make(IssueList, 0)
  259. return sess.Find(&stats.UnresolvedIssues)
  260. }
  261. func issuesForActivityStatement(repoID int64, fromTime time.Time, closed, unresolved bool) *xorm.Session {
  262. sess := x.Where("issue.repo_id = ?", repoID).
  263. And("issue.is_closed = ?", closed)
  264. if !unresolved {
  265. sess.And("issue.is_pull = ?", false)
  266. if closed {
  267. sess.And("issue.closed_unix >= ?", fromTime.Unix())
  268. } else {
  269. sess.And("issue.created_unix >= ?", fromTime.Unix())
  270. }
  271. } else {
  272. sess.And("issue.created_unix < ?", fromTime.Unix())
  273. sess.And("issue.updated_unix >= ?", fromTime.Unix())
  274. }
  275. return sess
  276. }
  277. // FillReleases returns release information for activity page
  278. func (stats *ActivityStats) FillReleases(repoID int64, fromTime time.Time) error {
  279. var err error
  280. var count int64
  281. // Published releases list
  282. sess := releasesForActivityStatement(repoID, fromTime)
  283. sess.OrderBy("release.created_unix DESC")
  284. stats.PublishedReleases = make([]*Release, 0)
  285. if err = sess.Find(&stats.PublishedReleases); err != nil {
  286. return err
  287. }
  288. // Published releases authors
  289. sess = releasesForActivityStatement(repoID, fromTime)
  290. if _, err = sess.Select("count(distinct release.publisher_id) as `count`").Table("release").Get(&count); err != nil {
  291. return err
  292. }
  293. stats.PublishedReleaseAuthorCount = count
  294. return nil
  295. }
  296. func releasesForActivityStatement(repoID int64, fromTime time.Time) *xorm.Session {
  297. return x.Where("release.repo_id = ?", repoID).
  298. And("release.is_draft = ?", false).
  299. And("release.created_unix >= ?", fromTime.Unix())
  300. }