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.2 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) getAssigneeIDs() []int64 {
  135. var ids = make(map[int64]struct{}, len(issues))
  136. for _, issue := range issues {
  137. if _, ok := ids[issue.AssigneeID]; !ok {
  138. ids[issue.AssigneeID] = struct{}{}
  139. }
  140. }
  141. return keysInt64(ids)
  142. }
  143. func (issues IssueList) loadAssignees(e Engine) error {
  144. assigneeIDs := issues.getAssigneeIDs()
  145. if len(assigneeIDs) == 0 {
  146. return nil
  147. }
  148. assigneeMaps := make(map[int64]*User, len(assigneeIDs))
  149. err := e.
  150. In("id", assigneeIDs).
  151. Find(&assigneeMaps)
  152. if err != nil {
  153. return err
  154. }
  155. for _, issue := range issues {
  156. if issue.AssigneeID <= 0 {
  157. continue
  158. }
  159. var ok bool
  160. if issue.Assignee, ok = assigneeMaps[issue.AssigneeID]; !ok {
  161. issue.Assignee = NewGhostUser()
  162. }
  163. }
  164. return nil
  165. }
  166. func (issues IssueList) getPullIssueIDs() []int64 {
  167. var ids = make([]int64, 0, len(issues))
  168. for _, issue := range issues {
  169. if issue.IsPull && issue.PullRequest == nil {
  170. ids = append(ids, issue.ID)
  171. }
  172. }
  173. return ids
  174. }
  175. func (issues IssueList) loadPullRequests(e Engine) error {
  176. issuesIDs := issues.getPullIssueIDs()
  177. if len(issuesIDs) == 0 {
  178. return nil
  179. }
  180. pullRequestMaps := make(map[int64]*PullRequest, len(issuesIDs))
  181. rows, err := e.
  182. In("issue_id", issuesIDs).
  183. Rows(new(PullRequest))
  184. if err != nil {
  185. return err
  186. }
  187. defer rows.Close()
  188. for rows.Next() {
  189. var pr PullRequest
  190. err = rows.Scan(&pr)
  191. if err != nil {
  192. return err
  193. }
  194. pullRequestMaps[pr.IssueID] = &pr
  195. }
  196. for _, issue := range issues {
  197. issue.PullRequest = pullRequestMaps[issue.ID]
  198. }
  199. return nil
  200. }
  201. func (issues IssueList) loadAttachments(e Engine) (err error) {
  202. if len(issues) == 0 {
  203. return nil
  204. }
  205. var attachments = make(map[int64][]*Attachment, len(issues))
  206. rows, err := e.Table("attachment").
  207. Join("INNER", "issue", "issue.id = attachment.issue_id").
  208. In("issue.id", issues.getIssueIDs()).
  209. Rows(new(Attachment))
  210. if err != nil {
  211. return err
  212. }
  213. defer rows.Close()
  214. for rows.Next() {
  215. var attachment Attachment
  216. err = rows.Scan(&attachment)
  217. if err != nil {
  218. return err
  219. }
  220. attachments[attachment.IssueID] = append(attachments[attachment.IssueID], &attachment)
  221. }
  222. for _, issue := range issues {
  223. issue.Attachments = attachments[issue.ID]
  224. }
  225. return nil
  226. }
  227. func (issues IssueList) loadComments(e Engine) (err error) {
  228. if len(issues) == 0 {
  229. return nil
  230. }
  231. var comments = make(map[int64][]*Comment, len(issues))
  232. rows, err := e.Table("comment").
  233. Join("INNER", "issue", "issue.id = comment.issue_id").
  234. In("issue.id", issues.getIssueIDs()).
  235. Rows(new(Comment))
  236. if err != nil {
  237. return err
  238. }
  239. defer rows.Close()
  240. for rows.Next() {
  241. var comment Comment
  242. err = rows.Scan(&comment)
  243. if err != nil {
  244. return err
  245. }
  246. comments[comment.IssueID] = append(comments[comment.IssueID], &comment)
  247. }
  248. for _, issue := range issues {
  249. issue.Comments = comments[issue.ID]
  250. }
  251. return nil
  252. }
  253. func (issues IssueList) loadTotalTrackedTimes(e Engine) (err error) {
  254. type totalTimesByIssue struct {
  255. IssueID int64
  256. Time int64
  257. }
  258. if len(issues) == 0 {
  259. return nil
  260. }
  261. var trackedTimes = make(map[int64]int64, len(issues))
  262. var ids = make([]int64, 0, len(issues))
  263. for _, issue := range issues {
  264. if issue.Repo.IsTimetrackerEnabled() {
  265. ids = append(ids, issue.ID)
  266. }
  267. }
  268. // select issue_id, sum(time) from tracked_time where issue_id in (<issue ids in current page>) group by issue_id
  269. rows, err := e.Table("tracked_time").
  270. Select("issue_id, sum(time) as time").
  271. In("issue_id", ids).
  272. GroupBy("issue_id").
  273. Rows(new(totalTimesByIssue))
  274. if err != nil {
  275. return err
  276. }
  277. defer rows.Close()
  278. for rows.Next() {
  279. var totalTime totalTimesByIssue
  280. err = rows.Scan(&totalTime)
  281. if err != nil {
  282. return err
  283. }
  284. trackedTimes[totalTime.IssueID] = totalTime.Time
  285. }
  286. for _, issue := range issues {
  287. issue.TotalTrackedTime = trackedTimes[issue.ID]
  288. }
  289. return nil
  290. }
  291. // loadAttributes loads all attributes, expect for attachments and comments
  292. func (issues IssueList) loadAttributes(e Engine) (err error) {
  293. if _, err = issues.loadRepositories(e); err != nil {
  294. return
  295. }
  296. if err = issues.loadPosters(e); err != nil {
  297. return
  298. }
  299. if err = issues.loadLabels(e); err != nil {
  300. return
  301. }
  302. if err = issues.loadMilestones(e); err != nil {
  303. return
  304. }
  305. if err = issues.loadAssignees(e); err != nil {
  306. return
  307. }
  308. if err = issues.loadPullRequests(e); err != nil {
  309. return
  310. }
  311. if err = issues.loadTotalTrackedTimes(e); err != nil {
  312. return
  313. }
  314. return nil
  315. }
  316. // LoadAttributes loads attributes of the issues, except for attachments and
  317. // comments
  318. func (issues IssueList) LoadAttributes() error {
  319. return issues.loadAttributes(x)
  320. }
  321. // LoadAttachments loads attachments
  322. func (issues IssueList) LoadAttachments() error {
  323. return issues.loadAttachments(x)
  324. }
  325. // LoadComments loads comments
  326. func (issues IssueList) LoadComments() error {
  327. return issues.loadComments(x)
  328. }