A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

662 lines
17 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>
4 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 (
  6. "fmt"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/modules/setting"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "xorm.io/builder"
  13. "xorm.io/xorm"
  14. )
  15. // Milestone represents a milestone of repository.
  16. type Milestone struct {
  17. ID int64 `xorm:"pk autoincr"`
  18. RepoID int64 `xorm:"INDEX"`
  19. Repo *Repository `xorm:"-"`
  20. Name string
  21. Content string `xorm:"TEXT"`
  22. RenderedContent string `xorm:"-"`
  23. IsClosed bool
  24. NumIssues int
  25. NumClosedIssues int
  26. NumOpenIssues int `xorm:"-"`
  27. Completeness int // Percentage(1-100).
  28. IsOverdue bool `xorm:"-"`
  29. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  30. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  31. DeadlineUnix timeutil.TimeStamp
  32. ClosedDateUnix timeutil.TimeStamp
  33. DeadlineString string `xorm:"-"`
  34. TotalTrackedTime int64 `xorm:"-"`
  35. TimeSinceUpdate int64 `xorm:"-"`
  36. }
  37. // BeforeUpdate is invoked from XORM before updating this object.
  38. func (m *Milestone) BeforeUpdate() {
  39. if m.NumIssues > 0 {
  40. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  41. } else {
  42. m.Completeness = 0
  43. }
  44. }
  45. // AfterLoad is invoked from XORM after setting the value of a field of
  46. // this object.
  47. func (m *Milestone) AfterLoad() {
  48. if !m.UpdatedUnix.IsZero() {
  49. m.TimeSinceUpdate = time.Now().Unix() - m.UpdatedUnix.AsTime().Unix()
  50. }
  51. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  52. if m.DeadlineUnix.Year() == 9999 {
  53. return
  54. }
  55. m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
  56. if m.IsClosed {
  57. m.IsOverdue = m.ClosedDateUnix >= m.DeadlineUnix
  58. } else {
  59. m.IsOverdue = timeutil.TimeStampNow() >= m.DeadlineUnix
  60. }
  61. }
  62. // State returns string representation of milestone status.
  63. func (m *Milestone) State() api.StateType {
  64. if m.IsClosed {
  65. return api.StateClosed
  66. }
  67. return api.StateOpen
  68. }
  69. // NewMilestone creates new milestone of repository.
  70. func NewMilestone(m *Milestone) (err error) {
  71. sess := x.NewSession()
  72. defer sess.Close()
  73. if err = sess.Begin(); err != nil {
  74. return err
  75. }
  76. m.Name = strings.TrimSpace(m.Name)
  77. if _, err = sess.Insert(m); err != nil {
  78. return err
  79. }
  80. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  81. return err
  82. }
  83. return sess.Commit()
  84. }
  85. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  86. m := new(Milestone)
  87. has, err := e.ID(id).Where("repo_id=?", repoID).Get(m)
  88. if err != nil {
  89. return nil, err
  90. } else if !has {
  91. return nil, ErrMilestoneNotExist{ID: id, RepoID: repoID}
  92. }
  93. return m, nil
  94. }
  95. // GetMilestoneByRepoID returns the milestone in a repository.
  96. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  97. return getMilestoneByRepoID(x, repoID, id)
  98. }
  99. // GetMilestoneByRepoIDANDName return a milestone if one exist by name and repo
  100. func GetMilestoneByRepoIDANDName(repoID int64, name string) (*Milestone, error) {
  101. var mile Milestone
  102. has, err := x.Where("repo_id=? AND name=?", repoID, name).Get(&mile)
  103. if err != nil {
  104. return nil, err
  105. }
  106. if !has {
  107. return nil, ErrMilestoneNotExist{Name: name, RepoID: repoID}
  108. }
  109. return &mile, nil
  110. }
  111. // GetMilestoneByID returns the milestone via id .
  112. func GetMilestoneByID(id int64) (*Milestone, error) {
  113. var m Milestone
  114. has, err := x.ID(id).Get(&m)
  115. if err != nil {
  116. return nil, err
  117. } else if !has {
  118. return nil, ErrMilestoneNotExist{ID: id, RepoID: 0}
  119. }
  120. return &m, nil
  121. }
  122. // UpdateMilestone updates information of given milestone.
  123. func UpdateMilestone(m *Milestone, oldIsClosed bool) error {
  124. sess := x.NewSession()
  125. defer sess.Close()
  126. if err := sess.Begin(); err != nil {
  127. return err
  128. }
  129. if m.IsClosed && !oldIsClosed {
  130. m.ClosedDateUnix = timeutil.TimeStampNow()
  131. }
  132. if err := updateMilestone(sess, m); err != nil {
  133. return err
  134. }
  135. if err := updateMilestoneCompleteness(sess, m.ID); err != nil {
  136. return err
  137. }
  138. // if IsClosed changed, update milestone numbers of repository
  139. if oldIsClosed != m.IsClosed {
  140. if err := updateRepoMilestoneNum(sess, m.RepoID); err != nil {
  141. return err
  142. }
  143. }
  144. return sess.Commit()
  145. }
  146. func updateMilestone(e Engine, m *Milestone) error {
  147. m.Name = strings.TrimSpace(m.Name)
  148. _, err := e.ID(m.ID).AllCols().
  149. SetExpr("num_issues", builder.Select("count(*)").From("issue").Where(
  150. builder.Eq{"milestone_id": m.ID},
  151. )).
  152. SetExpr("num_closed_issues", builder.Select("count(*)").From("issue").Where(
  153. builder.Eq{
  154. "milestone_id": m.ID,
  155. "is_closed": true,
  156. },
  157. )).
  158. Update(m)
  159. return err
  160. }
  161. func updateMilestoneCompleteness(e Engine, milestoneID int64) error {
  162. _, err := e.Exec("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?",
  163. milestoneID,
  164. )
  165. return err
  166. }
  167. // ChangeMilestoneStatusByRepoIDAndID changes a milestone open/closed status if the milestone ID is in the repo.
  168. func ChangeMilestoneStatusByRepoIDAndID(repoID, milestoneID int64, isClosed bool) error {
  169. sess := x.NewSession()
  170. defer sess.Close()
  171. if err := sess.Begin(); err != nil {
  172. return err
  173. }
  174. m := &Milestone{
  175. ID: milestoneID,
  176. RepoID: repoID,
  177. }
  178. has, err := sess.ID(milestoneID).Where("repo_id = ?", repoID).Get(m)
  179. if err != nil {
  180. return err
  181. } else if !has {
  182. return ErrMilestoneNotExist{ID: milestoneID, RepoID: repoID}
  183. }
  184. if err := changeMilestoneStatus(sess, m, isClosed); err != nil {
  185. return err
  186. }
  187. return sess.Commit()
  188. }
  189. // ChangeMilestoneStatus changes the milestone open/closed status.
  190. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  191. sess := x.NewSession()
  192. defer sess.Close()
  193. if err = sess.Begin(); err != nil {
  194. return err
  195. }
  196. if err := changeMilestoneStatus(sess, m, isClosed); err != nil {
  197. return err
  198. }
  199. return sess.Commit()
  200. }
  201. func changeMilestoneStatus(e Engine, m *Milestone, isClosed bool) error {
  202. m.IsClosed = isClosed
  203. if isClosed {
  204. m.ClosedDateUnix = timeutil.TimeStampNow()
  205. }
  206. count, err := e.ID(m.ID).Where("repo_id = ? AND is_closed = ?", m.RepoID, !isClosed).Cols("is_closed", "closed_date_unix").Update(m)
  207. if err != nil {
  208. return err
  209. }
  210. if count < 1 {
  211. return nil
  212. }
  213. return updateRepoMilestoneNum(e, m.RepoID)
  214. }
  215. func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
  216. if err := updateIssueCols(e, issue, "milestone_id"); err != nil {
  217. return err
  218. }
  219. if oldMilestoneID > 0 {
  220. if err := updateMilestoneTotalNum(e, oldMilestoneID); err != nil {
  221. return err
  222. }
  223. if issue.IsClosed {
  224. if err := updateMilestoneClosedNum(e, oldMilestoneID); err != nil {
  225. return err
  226. }
  227. }
  228. }
  229. if issue.MilestoneID > 0 {
  230. if err := updateMilestoneTotalNum(e, issue.MilestoneID); err != nil {
  231. return err
  232. }
  233. if issue.IsClosed {
  234. if err := updateMilestoneClosedNum(e, issue.MilestoneID); err != nil {
  235. return err
  236. }
  237. }
  238. }
  239. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  240. if err := issue.loadRepo(e); err != nil {
  241. return err
  242. }
  243. opts := &CreateCommentOptions{
  244. Type: CommentTypeMilestone,
  245. Doer: doer,
  246. Repo: issue.Repo,
  247. Issue: issue,
  248. OldMilestoneID: oldMilestoneID,
  249. MilestoneID: issue.MilestoneID,
  250. }
  251. if _, err := createComment(e, opts); err != nil {
  252. return err
  253. }
  254. }
  255. return nil
  256. }
  257. // ChangeMilestoneAssign changes assignment of milestone for issue.
  258. func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
  259. sess := x.NewSession()
  260. defer sess.Close()
  261. if err = sess.Begin(); err != nil {
  262. return err
  263. }
  264. if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
  265. return err
  266. }
  267. if err = sess.Commit(); err != nil {
  268. return fmt.Errorf("Commit: %v", err)
  269. }
  270. return nil
  271. }
  272. // DeleteMilestoneByRepoID deletes a milestone from a repository.
  273. func DeleteMilestoneByRepoID(repoID, id int64) error {
  274. m, err := GetMilestoneByRepoID(repoID, id)
  275. if err != nil {
  276. if IsErrMilestoneNotExist(err) {
  277. return nil
  278. }
  279. return err
  280. }
  281. repo, err := GetRepositoryByID(m.RepoID)
  282. if err != nil {
  283. return err
  284. }
  285. sess := x.NewSession()
  286. defer sess.Close()
  287. if err = sess.Begin(); err != nil {
  288. return err
  289. }
  290. if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
  291. return err
  292. }
  293. numMilestones, err := countRepoMilestones(sess, repo.ID)
  294. if err != nil {
  295. return err
  296. }
  297. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  298. if err != nil {
  299. return err
  300. }
  301. repo.NumMilestones = int(numMilestones)
  302. repo.NumClosedMilestones = int(numClosedMilestones)
  303. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  304. return err
  305. }
  306. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  307. return err
  308. }
  309. return sess.Commit()
  310. }
  311. // MilestoneList is a list of milestones offering additional functionality
  312. type MilestoneList []*Milestone
  313. func (milestones MilestoneList) getMilestoneIDs() []int64 {
  314. ids := make([]int64, 0, len(milestones))
  315. for _, ms := range milestones {
  316. ids = append(ids, ms.ID)
  317. }
  318. return ids
  319. }
  320. // GetMilestonesOption contain options to get milestones
  321. type GetMilestonesOption struct {
  322. ListOptions
  323. RepoID int64
  324. State api.StateType
  325. Name string
  326. SortType string
  327. }
  328. // GetMilestones returns milestones filtered by GetMilestonesOption's
  329. func GetMilestones(opts GetMilestonesOption) (MilestoneList, error) {
  330. sess := x.Where("repo_id = ?", opts.RepoID)
  331. switch opts.State {
  332. case api.StateClosed:
  333. sess = sess.And("is_closed = ?", true)
  334. case api.StateAll:
  335. break
  336. // api.StateOpen:
  337. default:
  338. sess = sess.And("is_closed = ?", false)
  339. }
  340. if len(opts.Name) != 0 {
  341. sess = sess.And(builder.Like{"name", opts.Name})
  342. }
  343. if opts.Page != 0 {
  344. sess = opts.setSessionPagination(sess)
  345. }
  346. switch opts.SortType {
  347. case "furthestduedate":
  348. sess.Desc("deadline_unix")
  349. case "leastcomplete":
  350. sess.Asc("completeness")
  351. case "mostcomplete":
  352. sess.Desc("completeness")
  353. case "leastissues":
  354. sess.Asc("num_issues")
  355. case "mostissues":
  356. sess.Desc("num_issues")
  357. case "id":
  358. sess.Asc("id")
  359. default:
  360. sess.Asc("deadline_unix").Asc("id")
  361. }
  362. miles := make([]*Milestone, 0, opts.PageSize)
  363. return miles, sess.Find(&miles)
  364. }
  365. // SearchMilestones search milestones
  366. func SearchMilestones(repoCond builder.Cond, page int, isClosed bool, sortType string) (MilestoneList, error) {
  367. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  368. sess := x.Where("is_closed = ?", isClosed)
  369. if repoCond.IsValid() {
  370. sess.In("repo_id", builder.Select("id").From("repository").Where(repoCond))
  371. }
  372. if page > 0 {
  373. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  374. }
  375. switch sortType {
  376. case "furthestduedate":
  377. sess.Desc("deadline_unix")
  378. case "leastcomplete":
  379. sess.Asc("completeness")
  380. case "mostcomplete":
  381. sess.Desc("completeness")
  382. case "leastissues":
  383. sess.Asc("num_issues")
  384. case "mostissues":
  385. sess.Desc("num_issues")
  386. default:
  387. sess.Asc("deadline_unix")
  388. }
  389. return miles, sess.Find(&miles)
  390. }
  391. // GetMilestonesByRepoIDs returns a list of milestones of given repositories and status.
  392. func GetMilestonesByRepoIDs(repoIDs []int64, page int, isClosed bool, sortType string) (MilestoneList, error) {
  393. return SearchMilestones(
  394. builder.In("repo_id", repoIDs),
  395. page,
  396. isClosed,
  397. sortType,
  398. )
  399. }
  400. // ____ _ _
  401. // / ___|| |_ __ _| |_ ___
  402. // \___ \| __/ _` | __/ __|
  403. // ___) | || (_| | |_\__ \
  404. // |____/ \__\__,_|\__|___/
  405. //
  406. // MilestonesStats represents milestone statistic information.
  407. type MilestonesStats struct {
  408. OpenCount, ClosedCount int64
  409. }
  410. // Total returns the total counts of milestones
  411. func (m MilestonesStats) Total() int64 {
  412. return m.OpenCount + m.ClosedCount
  413. }
  414. // GetMilestonesStatsByRepoCond returns milestone statistic information for dashboard by given conditions.
  415. func GetMilestonesStatsByRepoCond(repoCond builder.Cond) (*MilestonesStats, error) {
  416. var err error
  417. stats := &MilestonesStats{}
  418. sess := x.Where("is_closed = ?", false)
  419. if repoCond.IsValid() {
  420. sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
  421. }
  422. stats.OpenCount, err = sess.Count(new(Milestone))
  423. if err != nil {
  424. return nil, err
  425. }
  426. sess = x.Where("is_closed = ?", true)
  427. if repoCond.IsValid() {
  428. sess.And(builder.In("repo_id", builder.Select("id").From("repository").Where(repoCond)))
  429. }
  430. stats.ClosedCount, err = sess.Count(new(Milestone))
  431. if err != nil {
  432. return nil, err
  433. }
  434. return stats, nil
  435. }
  436. func countRepoMilestones(e Engine, repoID int64) (int64, error) {
  437. return e.
  438. Where("repo_id=?", repoID).
  439. Count(new(Milestone))
  440. }
  441. func countRepoClosedMilestones(e Engine, repoID int64) (int64, error) {
  442. return e.
  443. Where("repo_id=? AND is_closed=?", repoID, true).
  444. Count(new(Milestone))
  445. }
  446. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  447. func CountRepoClosedMilestones(repoID int64) (int64, error) {
  448. return countRepoClosedMilestones(x, repoID)
  449. }
  450. // CountMilestonesByRepoCond map from repo conditions to number of milestones matching the options`
  451. func CountMilestonesByRepoCond(repoCond builder.Cond, isClosed bool) (map[int64]int64, error) {
  452. sess := x.Where("is_closed = ?", isClosed)
  453. if repoCond.IsValid() {
  454. sess.In("repo_id", builder.Select("id").From("repository").Where(repoCond))
  455. }
  456. countsSlice := make([]*struct {
  457. RepoID int64
  458. Count int64
  459. }, 0, 10)
  460. if err := sess.GroupBy("repo_id").
  461. Select("repo_id AS repo_id, COUNT(*) AS count").
  462. Table("milestone").
  463. Find(&countsSlice); err != nil {
  464. return nil, err
  465. }
  466. countMap := make(map[int64]int64, len(countsSlice))
  467. for _, c := range countsSlice {
  468. countMap[c.RepoID] = c.Count
  469. }
  470. return countMap, nil
  471. }
  472. func updateRepoMilestoneNum(e Engine, repoID int64) error {
  473. _, err := e.Exec("UPDATE `repository` SET num_milestones=(SELECT count(*) FROM milestone WHERE repo_id=?),num_closed_milestones=(SELECT count(*) FROM milestone WHERE repo_id=? AND is_closed=?) WHERE id=?",
  474. repoID,
  475. repoID,
  476. true,
  477. repoID,
  478. )
  479. return err
  480. }
  481. func updateMilestoneTotalNum(e Engine, milestoneID int64) (err error) {
  482. if _, err = e.Exec("UPDATE `milestone` SET num_issues=(SELECT count(*) FROM issue WHERE milestone_id=?) WHERE id=?",
  483. milestoneID,
  484. milestoneID,
  485. ); err != nil {
  486. return
  487. }
  488. return updateMilestoneCompleteness(e, milestoneID)
  489. }
  490. func updateMilestoneClosedNum(e Engine, milestoneID int64) (err error) {
  491. if _, err = e.Exec("UPDATE `milestone` SET num_closed_issues=(SELECT count(*) FROM issue WHERE milestone_id=? AND is_closed=?) WHERE id=?",
  492. milestoneID,
  493. true,
  494. milestoneID,
  495. ); err != nil {
  496. return
  497. }
  498. return updateMilestoneCompleteness(e, milestoneID)
  499. }
  500. // _____ _ _ _____ _
  501. // |_ _| __ __ _ ___| | _____ __| |_ _(_)_ __ ___ ___ ___
  502. // | || '__/ _` |/ __| |/ / _ \/ _` | | | | | '_ ` _ \ / _ \/ __|
  503. // | || | | (_| | (__| < __/ (_| | | | | | | | | | | __/\__ \
  504. // |_||_| \__,_|\___|_|\_\___|\__,_| |_| |_|_| |_| |_|\___||___/
  505. //
  506. func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error {
  507. type totalTimesByMilestone struct {
  508. MilestoneID int64
  509. Time int64
  510. }
  511. if len(milestones) == 0 {
  512. return nil
  513. }
  514. trackedTimes := make(map[int64]int64, len(milestones))
  515. // Get total tracked time by milestone_id
  516. rows, err := e.Table("issue").
  517. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  518. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  519. Where("tracked_time.deleted = ?", false).
  520. Select("milestone_id, sum(time) as time").
  521. In("milestone_id", milestones.getMilestoneIDs()).
  522. GroupBy("milestone_id").
  523. Rows(new(totalTimesByMilestone))
  524. if err != nil {
  525. return err
  526. }
  527. defer rows.Close()
  528. for rows.Next() {
  529. var totalTime totalTimesByMilestone
  530. err = rows.Scan(&totalTime)
  531. if err != nil {
  532. return err
  533. }
  534. trackedTimes[totalTime.MilestoneID] = totalTime.Time
  535. }
  536. for _, milestone := range milestones {
  537. milestone.TotalTrackedTime = trackedTimes[milestone.ID]
  538. }
  539. return nil
  540. }
  541. func (m *Milestone) loadTotalTrackedTime(e Engine) error {
  542. type totalTimesByMilestone struct {
  543. MilestoneID int64
  544. Time int64
  545. }
  546. totalTime := &totalTimesByMilestone{MilestoneID: m.ID}
  547. has, err := e.Table("issue").
  548. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  549. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  550. Where("tracked_time.deleted = ?", false).
  551. Select("milestone_id, sum(time) as time").
  552. Where("milestone_id = ?", m.ID).
  553. GroupBy("milestone_id").
  554. Get(totalTime)
  555. if err != nil {
  556. return err
  557. } else if !has {
  558. return nil
  559. }
  560. m.TotalTrackedTime = totalTime.Time
  561. return nil
  562. }
  563. // LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
  564. func (milestones MilestoneList) LoadTotalTrackedTimes() error {
  565. return milestones.loadTotalTrackedTimes(x)
  566. }
  567. // LoadTotalTrackedTime loads the tracked time for the milestone
  568. func (m *Milestone) LoadTotalTrackedTime() error {
  569. return m.loadTotalTrackedTime(x)
  570. }