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.

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