diff options
author | Jonas Franz <info@jonasfranz.software> | 2018-04-29 07:58:47 +0200 |
---|---|---|
committer | Lunny Xiao <xiaolunwen@gmail.com> | 2018-04-29 13:58:47 +0800 |
commit | 8d5f58d8347196fca04fdf3a22d021d09d15e37f (patch) | |
tree | 4db9aabfec9a274b945dc3835d261c3267440ee3 /models/issue_milestone.go | |
parent | e3028d124f864b0ed2563cd0fea8b24612eb64bb (diff) | |
download | gitea-8d5f58d8347196fca04fdf3a22d021d09d15e37f.tar.gz gitea-8d5f58d8347196fca04fdf3a22d021d09d15e37f.zip |
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>
Diffstat (limited to 'models/issue_milestone.go')
-rw-r--r-- | models/issue_milestone.go | 62 |
1 files changed, 59 insertions, 3 deletions
diff --git a/models/issue_milestone.go b/models/issue_milestone.go index a5e0bd60df..949932fb2b 100644 --- a/models/issue_milestone.go +++ b/models/issue_milestone.go @@ -29,6 +29,8 @@ type Milestone struct { DeadlineString string `xorm:"-"` DeadlineUnix util.TimeStamp ClosedDateUnix util.TimeStamp + + TotalTrackedTime int64 `xorm:"-"` } // BeforeUpdate is invoked from XORM before updating this object. @@ -118,14 +120,69 @@ func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) { return getMilestoneByRepoID(x, repoID, id) } +// MilestoneList is a list of milestones offering additional functionality +type MilestoneList []*Milestone + +func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error { + type totalTimesByMilestone struct { + MilestoneID int64 + Time int64 + } + if len(milestones) == 0 { + return nil + } + var trackedTimes = make(map[int64]int64, len(milestones)) + + // Get total tracked time by milestone_id + rows, err := e.Table("issue"). + Join("INNER", "milestone", "issue.milestone_id = milestone.id"). + Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id"). + Select("milestone_id, sum(time) as time"). + In("milestone_id", milestones.getMilestoneIDs()). + GroupBy("milestone_id"). + Rows(new(totalTimesByMilestone)) + if err != nil { + return err + } + + defer rows.Close() + + for rows.Next() { + var totalTime totalTimesByMilestone + err = rows.Scan(&totalTime) + if err != nil { + return err + } + trackedTimes[totalTime.MilestoneID] = totalTime.Time + } + + for _, milestone := range milestones { + milestone.TotalTrackedTime = trackedTimes[milestone.ID] + } + return nil +} + +// LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request +func (milestones MilestoneList) LoadTotalTrackedTimes() error { + return milestones.loadTotalTrackedTimes(x) +} + +func (milestones MilestoneList) getMilestoneIDs() []int64 { + var ids = make([]int64, 0, len(milestones)) + for _, ms := range milestones { + ids = append(ids, ms.ID) + } + return ids +} + // GetMilestonesByRepoID returns all milestones of a repository. -func GetMilestonesByRepoID(repoID int64) ([]*Milestone, error) { +func GetMilestonesByRepoID(repoID int64) (MilestoneList, error) { miles := make([]*Milestone, 0, 10) return miles, x.Where("repo_id = ?", repoID).Find(&miles) } // GetMilestones returns a list of milestones of given repository and status. -func GetMilestones(repoID int64, page int, isClosed bool, sortType string) ([]*Milestone, error) { +func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (MilestoneList, error) { miles := make([]*Milestone, 0, setting.UI.IssuePagingNum) sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed) if page > 0 { @@ -146,7 +203,6 @@ func GetMilestones(repoID int64, page int, isClosed bool, sortType string) ([]*M default: sess.Asc("deadline_unix") } - return miles, sess.Find(&miles) } |