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.

384 lines
8.3 KiB

Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
  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 "fmt"
  6. // IssueList defines a list of issues
  7. type IssueList []*Issue
  8. func (issues IssueList) getRepoIDs() []int64 {
  9. repoIDs := make(map[int64]struct{}, len(issues))
  10. for _, issue := range issues {
  11. if _, ok := repoIDs[issue.RepoID]; !ok {
  12. repoIDs[issue.RepoID] = struct{}{}
  13. }
  14. }
  15. return keysInt64(repoIDs)
  16. }
  17. func (issues IssueList) loadRepositories(e Engine) ([]*Repository, error) {
  18. if len(issues) == 0 {
  19. return nil, nil
  20. }
  21. repoIDs := issues.getRepoIDs()
  22. repoMaps := make(map[int64]*Repository, len(repoIDs))
  23. err := e.
  24. In("id", repoIDs).
  25. Find(&repoMaps)
  26. if err != nil {
  27. return nil, fmt.Errorf("find repository: %v", err)
  28. }
  29. for _, issue := range issues {
  30. issue.Repo = repoMaps[issue.RepoID]
  31. }
  32. return valuesRepository(repoMaps), nil
  33. }
  34. // LoadRepositories loads issues' all repositories
  35. func (issues IssueList) LoadRepositories() ([]*Repository, error) {
  36. return issues.loadRepositories(x)
  37. }
  38. func (issues IssueList) getPosterIDs() []int64 {
  39. posterIDs := make(map[int64]struct{}, len(issues))
  40. for _, issue := range issues {
  41. if _, ok := posterIDs[issue.PosterID]; !ok {
  42. posterIDs[issue.PosterID] = struct{}{}
  43. }
  44. }
  45. return keysInt64(posterIDs)
  46. }
  47. func (issues IssueList) loadPosters(e Engine) error {
  48. if len(issues) == 0 {
  49. return nil
  50. }
  51. posterIDs := issues.getPosterIDs()
  52. posterMaps := make(map[int64]*User, len(posterIDs))
  53. err := e.
  54. In("id", posterIDs).
  55. Find(&posterMaps)
  56. if err != nil {
  57. return err
  58. }
  59. for _, issue := range issues {
  60. if issue.PosterID <= 0 {
  61. continue
  62. }
  63. var ok bool
  64. if issue.Poster, ok = posterMaps[issue.PosterID]; !ok {
  65. issue.Poster = NewGhostUser()
  66. }
  67. }
  68. return nil
  69. }
  70. func (issues IssueList) getIssueIDs() []int64 {
  71. var ids = make([]int64, 0, len(issues))
  72. for _, issue := range issues {
  73. ids = append(ids, issue.ID)
  74. }
  75. return ids
  76. }
  77. func (issues IssueList) loadLabels(e Engine) error {
  78. if len(issues) == 0 {
  79. return nil
  80. }
  81. type LabelIssue struct {
  82. Label *Label `xorm:"extends"`
  83. IssueLabel *IssueLabel `xorm:"extends"`
  84. }
  85. var issueLabels = make(map[int64][]*Label, len(issues)*3)
  86. rows, err := e.Table("label").
  87. Join("LEFT", "issue_label", "issue_label.label_id = label.id").
  88. In("issue_label.issue_id", issues.getIssueIDs()).
  89. Asc("label.name").
  90. Rows(new(LabelIssue))
  91. if err != nil {
  92. return err
  93. }
  94. defer rows.Close()
  95. for rows.Next() {
  96. var labelIssue LabelIssue
  97. err = rows.Scan(&labelIssue)
  98. if err != nil {
  99. return err
  100. }
  101. issueLabels[labelIssue.IssueLabel.IssueID] = append(issueLabels[labelIssue.IssueLabel.IssueID], labelIssue.Label)
  102. }
  103. for _, issue := range issues {
  104. issue.Labels = issueLabels[issue.ID]
  105. }
  106. return nil
  107. }
  108. func (issues IssueList) getMilestoneIDs() []int64 {
  109. var ids = make(map[int64]struct{}, len(issues))
  110. for _, issue := range issues {
  111. if _, ok := ids[issue.MilestoneID]; !ok {
  112. ids[issue.MilestoneID] = struct{}{}
  113. }
  114. }
  115. return keysInt64(ids)
  116. }
  117. func (issues IssueList) loadMilestones(e Engine) error {
  118. milestoneIDs := issues.getMilestoneIDs()
  119. if len(milestoneIDs) == 0 {
  120. return nil
  121. }
  122. milestoneMaps := make(map[int64]*Milestone, len(milestoneIDs))
  123. err := e.
  124. In("id", milestoneIDs).
  125. Find(&milestoneMaps)
  126. if err != nil {
  127. return err
  128. }
  129. for _, issue := range issues {
  130. issue.Milestone = milestoneMaps[issue.MilestoneID]
  131. }
  132. return nil
  133. }
  134. func (issues IssueList) loadAssignees(e Engine) error {
  135. if len(issues) == 0 {
  136. return nil
  137. }
  138. type AssigneeIssue struct {
  139. IssueAssignee *IssueAssignees `xorm:"extends"`
  140. Assignee *User `xorm:"extends"`
  141. }
  142. var assignees = make(map[int64][]*User, len(issues))
  143. rows, err := e.Table("issue_assignees").
  144. Join("INNER", "user", "`user`.id = `issue_assignees`.assignee_id").
  145. In("`issue_assignees`.issue_id", issues.getIssueIDs()).
  146. Rows(new(AssigneeIssue))
  147. if err != nil {
  148. return err
  149. }
  150. defer rows.Close()
  151. for rows.Next() {
  152. var assigneeIssue AssigneeIssue
  153. err = rows.Scan(&assigneeIssue)
  154. if err != nil {
  155. return err
  156. }
  157. assignees[assigneeIssue.IssueAssignee.IssueID] = append(assignees[assigneeIssue.IssueAssignee.IssueID], assigneeIssue.Assignee)
  158. }
  159. for _, issue := range issues {
  160. issue.Assignees = assignees[issue.ID]
  161. }
  162. return nil
  163. }
  164. func (issues IssueList) getPullIssueIDs() []int64 {
  165. var ids = make([]int64, 0, len(issues))
  166. for _, issue := range issues {
  167. if issue.IsPull && issue.PullRequest == nil {
  168. ids = append(ids, issue.ID)
  169. }
  170. }
  171. return ids
  172. }
  173. func (issues IssueList) loadPullRequests(e Engine) error {
  174. issuesIDs := issues.getPullIssueIDs()
  175. if len(issuesIDs) == 0 {
  176. return nil
  177. }
  178. pullRequestMaps := make(map[int64]*PullRequest, len(issuesIDs))
  179. rows, err := e.
  180. In("issue_id", issuesIDs).
  181. Rows(new(PullRequest))
  182. if err != nil {
  183. return err
  184. }
  185. defer rows.Close()
  186. for rows.Next() {
  187. var pr PullRequest
  188. err = rows.Scan(&pr)
  189. if err != nil {
  190. return err
  191. }
  192. pullRequestMaps[pr.IssueID] = &pr
  193. }
  194. for _, issue := range issues {
  195. issue.PullRequest = pullRequestMaps[issue.ID]
  196. }
  197. return nil
  198. }
  199. func (issues IssueList) loadAttachments(e Engine) (err error) {
  200. if len(issues) == 0 {
  201. return nil
  202. }
  203. var attachments = make(map[int64][]*Attachment, len(issues))
  204. rows, err := e.Table("attachment").
  205. Join("INNER", "issue", "issue.id = attachment.issue_id").
  206. In("issue.id", issues.getIssueIDs()).
  207. Rows(new(Attachment))
  208. if err != nil {
  209. return err
  210. }
  211. defer rows.Close()
  212. for rows.Next() {
  213. var attachment Attachment
  214. err = rows.Scan(&attachment)
  215. if err != nil {
  216. return err
  217. }
  218. attachments[attachment.IssueID] = append(attachments[attachment.IssueID], &attachment)
  219. }
  220. for _, issue := range issues {
  221. issue.Attachments = attachments[issue.ID]
  222. }
  223. return nil
  224. }
  225. func (issues IssueList) loadComments(e Engine) (err error) {
  226. if len(issues) == 0 {
  227. return nil
  228. }
  229. var comments = make(map[int64][]*Comment, len(issues))
  230. rows, err := e.Table("comment").
  231. Join("INNER", "issue", "issue.id = comment.issue_id").
  232. In("issue.id", issues.getIssueIDs()).
  233. Rows(new(Comment))
  234. if err != nil {
  235. return err
  236. }
  237. defer rows.Close()
  238. for rows.Next() {
  239. var comment Comment
  240. err = rows.Scan(&comment)
  241. if err != nil {
  242. return err
  243. }
  244. comments[comment.IssueID] = append(comments[comment.IssueID], &comment)
  245. }
  246. for _, issue := range issues {
  247. issue.Comments = comments[issue.ID]
  248. }
  249. return nil
  250. }
  251. func (issues IssueList) loadTotalTrackedTimes(e Engine) (err error) {
  252. type totalTimesByIssue struct {
  253. IssueID int64
  254. Time int64
  255. }
  256. if len(issues) == 0 {
  257. return nil
  258. }
  259. var trackedTimes = make(map[int64]int64, len(issues))
  260. var ids = make([]int64, 0, len(issues))
  261. for _, issue := range issues {
  262. if issue.Repo.IsTimetrackerEnabled() {
  263. ids = append(ids, issue.ID)
  264. }
  265. }
  266. // select issue_id, sum(time) from tracked_time where issue_id in (<issue ids in current page>) group by issue_id
  267. rows, err := e.Table("tracked_time").
  268. Select("issue_id, sum(time) as time").
  269. In("issue_id", ids).
  270. GroupBy("issue_id").
  271. Rows(new(totalTimesByIssue))
  272. if err != nil {
  273. return err
  274. }
  275. defer rows.Close()
  276. for rows.Next() {
  277. var totalTime totalTimesByIssue
  278. err = rows.Scan(&totalTime)
  279. if err != nil {
  280. return err
  281. }
  282. trackedTimes[totalTime.IssueID] = totalTime.Time
  283. }
  284. for _, issue := range issues {
  285. issue.TotalTrackedTime = trackedTimes[issue.ID]
  286. }
  287. return nil
  288. }
  289. // loadAttributes loads all attributes, expect for attachments and comments
  290. func (issues IssueList) loadAttributes(e Engine) (err error) {
  291. if _, err = issues.loadRepositories(e); err != nil {
  292. return
  293. }
  294. if err = issues.loadPosters(e); err != nil {
  295. return
  296. }
  297. if err = issues.loadLabels(e); err != nil {
  298. return
  299. }
  300. if err = issues.loadMilestones(e); err != nil {
  301. return
  302. }
  303. if err = issues.loadAssignees(e); err != nil {
  304. return
  305. }
  306. if err = issues.loadPullRequests(e); err != nil {
  307. return
  308. }
  309. if err = issues.loadTotalTrackedTimes(e); err != nil {
  310. return
  311. }
  312. return nil
  313. }
  314. // LoadAttributes loads attributes of the issues, except for attachments and
  315. // comments
  316. func (issues IssueList) LoadAttributes() error {
  317. return issues.loadAttributes(x)
  318. }
  319. // LoadAttachments loads attachments
  320. func (issues IssueList) LoadAttachments() error {
  321. return issues.loadAttachments(x)
  322. }
  323. // LoadComments loads comments
  324. func (issues IssueList) LoadComments() error {
  325. return issues.loadComments(x)
  326. }