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.

milestone_list.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "context"
  6. "strings"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/optional"
  9. "xorm.io/builder"
  10. )
  11. // MilestoneList is a list of milestones offering additional functionality
  12. type MilestoneList []*Milestone
  13. func (milestones MilestoneList) getMilestoneIDs() []int64 {
  14. ids := make([]int64, 0, len(milestones))
  15. for _, ms := range milestones {
  16. ids = append(ids, ms.ID)
  17. }
  18. return ids
  19. }
  20. // FindMilestoneOptions contain options to get milestones
  21. type FindMilestoneOptions struct {
  22. db.ListOptions
  23. RepoID int64
  24. IsClosed optional.Option[bool]
  25. Name string
  26. SortType string
  27. RepoCond builder.Cond
  28. RepoIDs []int64
  29. }
  30. func (opts FindMilestoneOptions) ToConds() builder.Cond {
  31. cond := builder.NewCond()
  32. if opts.RepoID != 0 {
  33. cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
  34. }
  35. if opts.IsClosed.Has() {
  36. cond = cond.And(builder.Eq{"is_closed": opts.IsClosed.Value()})
  37. }
  38. if opts.RepoCond != nil && opts.RepoCond.IsValid() {
  39. cond = cond.And(builder.In("repo_id", builder.Select("id").From("repository").Where(opts.RepoCond)))
  40. }
  41. if len(opts.RepoIDs) > 0 {
  42. cond = cond.And(builder.In("repo_id", opts.RepoIDs))
  43. }
  44. if len(opts.Name) != 0 {
  45. cond = cond.And(db.BuildCaseInsensitiveLike("name", opts.Name))
  46. }
  47. return cond
  48. }
  49. func (opts FindMilestoneOptions) ToOrders() string {
  50. switch opts.SortType {
  51. case "furthestduedate":
  52. return "deadline_unix DESC"
  53. case "leastcomplete":
  54. return "completeness ASC"
  55. case "mostcomplete":
  56. return "completeness DESC"
  57. case "leastissues":
  58. return "num_issues ASC"
  59. case "mostissues":
  60. return "num_issues DESC"
  61. case "id":
  62. return "id ASC"
  63. default:
  64. return "deadline_unix ASC, id ASC"
  65. }
  66. }
  67. // GetMilestoneIDsByNames returns a list of milestone ids by given names.
  68. // It doesn't filter them by repo, so it could return milestones belonging to different repos.
  69. // It's used for filtering issues via indexer, otherwise it would be useless.
  70. // Since it could return milestones with the same name, so the length of returned ids could be more than the length of names.
  71. func GetMilestoneIDsByNames(ctx context.Context, names []string) ([]int64, error) {
  72. var ids []int64
  73. return ids, db.GetEngine(ctx).Table("milestone").
  74. Where(db.BuildCaseInsensitiveIn("name", names)).
  75. Cols("id").
  76. Find(&ids)
  77. }
  78. // LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
  79. func (milestones MilestoneList) LoadTotalTrackedTimes(ctx context.Context) error {
  80. type totalTimesByMilestone struct {
  81. MilestoneID int64
  82. Time int64
  83. }
  84. if len(milestones) == 0 {
  85. return nil
  86. }
  87. trackedTimes := make(map[int64]int64, len(milestones))
  88. // Get total tracked time by milestone_id
  89. rows, err := db.GetEngine(ctx).Table("issue").
  90. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  91. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  92. Where("tracked_time.deleted = ?", false).
  93. Select("milestone_id, sum(time) as time").
  94. In("milestone_id", milestones.getMilestoneIDs()).
  95. GroupBy("milestone_id").
  96. Rows(new(totalTimesByMilestone))
  97. if err != nil {
  98. return err
  99. }
  100. defer rows.Close()
  101. for rows.Next() {
  102. var totalTime totalTimesByMilestone
  103. err = rows.Scan(&totalTime)
  104. if err != nil {
  105. return err
  106. }
  107. trackedTimes[totalTime.MilestoneID] = totalTime.Time
  108. }
  109. for _, milestone := range milestones {
  110. milestone.TotalTrackedTime = trackedTimes[milestone.ID]
  111. }
  112. return nil
  113. }
  114. // CountMilestonesByRepoCondAndKw map from repo conditions and the keyword of milestones' name to number of milestones matching the options`
  115. func CountMilestonesMap(ctx context.Context, opts FindMilestoneOptions) (map[int64]int64, error) {
  116. sess := db.GetEngine(ctx).Where(opts.ToConds())
  117. countsSlice := make([]*struct {
  118. RepoID int64
  119. Count int64
  120. }, 0, 10)
  121. if err := sess.GroupBy("repo_id").
  122. Select("repo_id AS repo_id, COUNT(*) AS count").
  123. Table("milestone").
  124. Find(&countsSlice); err != nil {
  125. return nil, err
  126. }
  127. countMap := make(map[int64]int64, len(countsSlice))
  128. for _, c := range countsSlice {
  129. countMap[c.RepoID] = c.Count
  130. }
  131. return countMap, nil
  132. }
  133. // MilestonesStats represents milestone statistic information.
  134. type MilestonesStats struct {
  135. OpenCount, ClosedCount int64
  136. }
  137. // Total returns the total counts of milestones
  138. func (m MilestonesStats) Total() int64 {
  139. return m.OpenCount + m.ClosedCount
  140. }
  141. // GetMilestonesStatsByRepoCondAndKw returns milestone statistic information for dashboard by given repo conditions and name keyword.
  142. func GetMilestonesStatsByRepoCondAndKw(ctx context.Context, repoCond builder.Cond, keyword string) (*MilestonesStats, error) {
  143. var err error
  144. stats := &MilestonesStats{}
  145. sess := db.GetEngine(ctx).Where("is_closed = ?", false)
  146. if len(keyword) > 0 {
  147. sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)})
  148. }
  149. if repoCond.IsValid() {
  150. sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
  151. }
  152. stats.OpenCount, err = sess.Count(new(Milestone))
  153. if err != nil {
  154. return nil, err
  155. }
  156. sess = db.GetEngine(ctx).Where("is_closed = ?", true)
  157. if len(keyword) > 0 {
  158. sess = sess.And(builder.Like{"UPPER(name)", strings.ToUpper(keyword)})
  159. }
  160. if repoCond.IsValid() {
  161. sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
  162. }
  163. stats.ClosedCount, err = sess.Count(new(Milestone))
  164. if err != nil {
  165. return nil, err
  166. }
  167. return stats, nil
  168. }