2017-09-12 06:48:13 +00:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-12-13 23:45:31 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-11-01 02:25:14 +00:00
|
|
|
api "code.gitea.io/sdk/gitea"
|
|
|
|
|
2017-09-12 06:48:13 +00:00
|
|
|
"github.com/go-xorm/builder"
|
2018-04-29 05:58:47 +00:00
|
|
|
"github.com/go-xorm/xorm"
|
2017-09-12 06:48:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TrackedTime represents a time that was spent for a specific issue.
|
|
|
|
type TrackedTime struct {
|
|
|
|
ID int64 `xorm:"pk autoincr" json:"id"`
|
|
|
|
IssueID int64 `xorm:"INDEX" json:"issue_id"`
|
|
|
|
UserID int64 `xorm:"INDEX" json:"user_id"`
|
|
|
|
Created time.Time `xorm:"-" json:"created"`
|
2017-10-01 16:52:35 +00:00
|
|
|
CreatedUnix int64 `xorm:"created" json:"-"`
|
2017-09-12 06:48:13 +00:00
|
|
|
Time int64 `json:"time"`
|
|
|
|
}
|
|
|
|
|
2017-10-01 16:52:35 +00:00
|
|
|
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
|
|
|
|
func (t *TrackedTime) AfterLoad() {
|
2017-12-13 23:45:31 +00:00
|
|
|
t.Created = time.Unix(t.CreatedUnix, 0).In(setting.UILocation)
|
2017-09-12 06:48:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 02:25:14 +00:00
|
|
|
// APIFormat converts TrackedTime to API format
|
|
|
|
func (t *TrackedTime) APIFormat() *api.TrackedTime {
|
|
|
|
return &api.TrackedTime{
|
|
|
|
ID: t.ID,
|
|
|
|
IssueID: t.IssueID,
|
|
|
|
UserID: t.UserID,
|
|
|
|
Time: t.Time,
|
|
|
|
Created: t.Created,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:48:13 +00:00
|
|
|
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
|
|
|
|
type FindTrackedTimesOptions struct {
|
|
|
|
IssueID int64
|
|
|
|
UserID int64
|
|
|
|
RepositoryID int64
|
2018-04-29 05:58:47 +00:00
|
|
|
MilestoneID int64
|
2017-09-12 06:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToCond will convert each condition into a xorm-Cond
|
|
|
|
func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
|
|
|
|
cond := builder.NewCond()
|
|
|
|
if opts.IssueID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
|
|
|
|
}
|
|
|
|
if opts.UserID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"user_id": opts.UserID})
|
|
|
|
}
|
|
|
|
if opts.RepositoryID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"issue.repo_id": opts.RepositoryID})
|
|
|
|
}
|
2018-04-29 05:58:47 +00:00
|
|
|
if opts.MilestoneID != 0 {
|
|
|
|
cond = cond.And(builder.Eq{"issue.milestone_id": opts.MilestoneID})
|
|
|
|
}
|
2017-09-12 06:48:13 +00:00
|
|
|
return cond
|
|
|
|
}
|
|
|
|
|
2018-04-29 05:58:47 +00:00
|
|
|
// ToSession will convert the given options to a xorm Session by using the conditions from ToCond and joining with issue table if required
|
|
|
|
func (opts *FindTrackedTimesOptions) ToSession(e Engine) *xorm.Session {
|
|
|
|
if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
|
|
|
|
return e.Join("INNER", "issue", "issue.id = tracked_time.issue_id").Where(opts.ToCond())
|
|
|
|
}
|
|
|
|
return x.Where(opts.ToCond())
|
|
|
|
}
|
|
|
|
|
2017-09-12 06:48:13 +00:00
|
|
|
// GetTrackedTimes returns all tracked times that fit to the given options.
|
|
|
|
func GetTrackedTimes(options FindTrackedTimesOptions) (trackedTimes []*TrackedTime, err error) {
|
2018-04-29 05:58:47 +00:00
|
|
|
err = options.ToSession(x).Find(&trackedTimes)
|
2017-09-12 06:48:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTime will add the given time (in seconds) to the issue
|
|
|
|
func AddTime(user *User, issue *Issue, time int64) (*TrackedTime, error) {
|
|
|
|
tt := &TrackedTime{
|
|
|
|
IssueID: issue.ID,
|
|
|
|
UserID: user.ID,
|
|
|
|
Time: time,
|
|
|
|
}
|
|
|
|
if _, err := x.Insert(tt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-13 15:55:43 +00:00
|
|
|
if err := issue.loadRepo(x); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-12 06:48:13 +00:00
|
|
|
if _, err := CreateComment(&CreateCommentOptions{
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Doer: user,
|
2018-04-29 05:58:47 +00:00
|
|
|
Content: SecToTime(time),
|
2017-09-12 06:48:13 +00:00
|
|
|
Type: CommentTypeAddTimeManual,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TotalTimes returns the spent time for each user by an issue
|
|
|
|
func TotalTimes(options FindTrackedTimesOptions) (map[*User]string, error) {
|
|
|
|
trackedTimes, err := GetTrackedTimes(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
//Adding total time per user ID
|
|
|
|
totalTimesByUser := make(map[int64]int64)
|
|
|
|
for _, t := range trackedTimes {
|
|
|
|
totalTimesByUser[t.UserID] += t.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
totalTimes := make(map[*User]string)
|
|
|
|
//Fetching User and making time human readable
|
|
|
|
for userID, total := range totalTimesByUser {
|
|
|
|
user, err := GetUserByID(userID)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrUserNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-29 05:58:47 +00:00
|
|
|
totalTimes[user] = SecToTime(total)
|
2017-09-12 06:48:13 +00:00
|
|
|
}
|
|
|
|
return totalTimes, nil
|
|
|
|
}
|