Files
Atay-Makhzan/models/issues/tracked_time.go
T

317 lines
8.2 KiB
Go
Raw Normal View History

2017-09-12 08:48:13 +02: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 issues
2017-09-12 08:48:13 +02:00
import (
"context"
2017-09-12 08:48:13 +02:00
"time"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
2022-02-15 17:50:10 +01:00
"code.gitea.io/gitea/modules/util"
2017-10-31 19:25:14 -07:00
"xorm.io/builder"
2017-09-12 08:48:13 +02:00
)
// TrackedTime represents a time that was spent for a specific issue.
type TrackedTime struct {
ID int64 `xorm:"pk autoincr"`
IssueID int64 `xorm:"INDEX"`
Issue *Issue `xorm:"-"`
UserID int64 `xorm:"INDEX"`
User *user_model.User `xorm:"-"`
Created time.Time `xorm:"-"`
CreatedUnix int64 `xorm:"created"`
Time int64 `xorm:"NOT NULL"`
Deleted bool `xorm:"NOT NULL DEFAULT false"`
2017-09-12 08:48:13 +02:00
}
func init() {
db.RegisterModel(new(TrackedTime))
}
2019-12-27 21:30:58 +01:00
// TrackedTimeList is a List of TrackedTime's
type TrackedTimeList []*TrackedTime
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (t *TrackedTime) AfterLoad() {
t.Created = time.Unix(t.CreatedUnix, 0).In(setting.DefaultUILocation)
2017-09-12 08:48:13 +02:00
}
2019-12-27 21:30:58 +01:00
// LoadAttributes load Issue, User
func (t *TrackedTime) LoadAttributes() (err error) {
return t.loadAttributes(db.DefaultContext)
2019-12-27 21:30:58 +01:00
}
func (t *TrackedTime) loadAttributes(ctx context.Context) (err error) {
2019-12-27 21:30:58 +01:00
if t.Issue == nil {
t.Issue, err = GetIssueByID(ctx, t.IssueID)
2019-12-27 21:30:58 +01:00
if err != nil {
return
}
2022-04-08 17:11:15 +08:00
err = t.Issue.LoadRepo(ctx)
2019-12-27 21:30:58 +01:00
if err != nil {
return
}
}
if t.User == nil {
t.User, err = user_model.GetUserByIDCtx(ctx, t.UserID)
2019-12-27 21:30:58 +01:00
if err != nil {
return
}
}
return err
2019-12-27 21:30:58 +01:00
}
// LoadAttributes load Issue, User
func (tl TrackedTimeList) LoadAttributes() (err error) {
for _, t := range tl {
if err = t.LoadAttributes(); err != nil {
return err
}
}
return err
2019-12-27 21:30:58 +01:00
}
2017-09-12 08:48:13 +02:00
// FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
type FindTrackedTimesOptions struct {
db.ListOptions
2020-01-08 22:14:00 +01:00
IssueID int64
UserID int64
RepositoryID int64
MilestoneID int64
CreatedAfterUnix int64
CreatedBeforeUnix int64
2017-09-12 08:48:13 +02:00
}
2021-08-12 14:43:08 +02:00
// toCond will convert each condition into a xorm-Cond
func (opts *FindTrackedTimesOptions) toCond() builder.Cond {
2019-12-27 21:30:58 +01:00
cond := builder.NewCond().And(builder.Eq{"tracked_time.deleted": false})
2017-09-12 08:48:13 +02:00
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})
}
if opts.MilestoneID != 0 {
cond = cond.And(builder.Eq{"issue.milestone_id": opts.MilestoneID})
}
2020-01-08 22:14:00 +01:00
if opts.CreatedAfterUnix != 0 {
cond = cond.And(builder.Gte{"tracked_time.created_unix": opts.CreatedAfterUnix})
}
if opts.CreatedBeforeUnix != 0 {
cond = cond.And(builder.Lte{"tracked_time.created_unix": opts.CreatedBeforeUnix})
}
2017-09-12 08:48:13 +02:00
return cond
}
2021-08-12 14:43:08 +02: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 db.Engine) db.Engine {
2020-01-24 19:00:29 +00:00
sess := e
if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
2020-01-24 19:00:29 +00:00
sess = e.Join("INNER", "issue", "issue.id = tracked_time.issue_id")
}
2020-01-24 19:00:29 +00:00
2021-08-12 14:43:08 +02:00
sess = sess.Where(opts.toCond())
2020-01-24 19:00:29 +00:00
if opts.Page != 0 {
sess = db.SetEnginePagination(sess, opts)
2020-01-24 19:00:29 +00:00
}
return sess
}
2019-12-27 21:30:58 +01:00
// GetTrackedTimes returns all tracked times that fit to the given options.
func GetTrackedTimes(ctx context.Context, options *FindTrackedTimesOptions) (trackedTimes TrackedTimeList, err error) {
err = options.toSession(db.GetEngine(ctx)).Find(&trackedTimes)
return trackedTimes, err
2019-12-27 21:30:58 +01:00
}
2021-08-12 14:43:08 +02:00
// CountTrackedTimes returns count of tracked times that fit to the given options.
func CountTrackedTimes(opts *FindTrackedTimesOptions) (int64, error) {
2021-09-23 16:45:36 +01:00
sess := db.GetEngine(db.DefaultContext).Where(opts.toCond())
2021-08-12 14:43:08 +02:00
if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
sess = sess.Join("INNER", "issue", "issue.id = tracked_time.issue_id")
}
return sess.Count(&TrackedTime{})
}
2019-12-27 21:30:58 +01:00
// GetTrackedSeconds return sum of seconds
func GetTrackedSeconds(ctx context.Context, opts FindTrackedTimesOptions) (trackedSeconds int64, err error) {
return opts.toSession(db.GetEngine(ctx)).SumInt(&TrackedTime{}, "time")
2019-12-27 21:30:58 +01:00
}
2017-09-12 08:48:13 +02:00
// AddTime will add the given time (in seconds) to the issue
func AddTime(user *user_model.User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) {
2021-11-19 21:39:57 +08:00
ctx, committer, err := db.TxContext()
if err != nil {
2019-12-27 21:30:58 +01:00
return nil, err
2017-09-12 08:48:13 +02:00
}
2021-11-19 21:39:57 +08:00
defer committer.Close()
2019-12-27 21:30:58 +01:00
t, err := addTime(ctx, user, issue, amount, created)
2019-12-27 21:30:58 +01:00
if err != nil {
2017-09-12 08:48:13 +02:00
return nil, err
}
2019-12-27 21:30:58 +01:00
2022-04-08 17:11:15 +08:00
if err := issue.LoadRepo(ctx); err != nil {
2018-12-13 23:55:43 +08:00
return nil, err
}
2019-12-27 21:30:58 +01:00
2022-04-08 17:11:15 +08:00
if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
2017-09-12 08:48:13 +02:00
Issue: issue,
Repo: issue.Repo,
Doer: user,
2022-02-15 17:50:10 +01:00
Content: util.SecToTime(amount),
2017-09-12 08:48:13 +02:00
Type: CommentTypeAddTimeManual,
2021-02-19 10:52:11 +00:00
TimeID: t.ID,
2017-09-12 08:48:13 +02:00
}); err != nil {
return nil, err
}
2019-12-27 21:30:58 +01:00
2021-11-19 21:39:57 +08:00
return t, committer.Commit()
2019-12-27 21:30:58 +01:00
}
func addTime(ctx context.Context, user *user_model.User, issue *Issue, amount int64, created time.Time) (*TrackedTime, error) {
2019-12-27 21:30:58 +01:00
if created.IsZero() {
created = time.Now()
}
tt := &TrackedTime{
IssueID: issue.ID,
UserID: user.ID,
Time: amount,
Created: created,
}
return tt, db.Insert(ctx, tt)
2017-09-12 08:48:13 +02:00
}
// TotalTimes returns the spent time for each user by an issue
func TotalTimes(options *FindTrackedTimesOptions) (map[*user_model.User]string, error) {
trackedTimes, err := GetTrackedTimes(db.DefaultContext, options)
2017-09-12 08:48:13 +02:00
if err != nil {
return nil, err
}
// Adding total time per user ID
2017-09-12 08:48:13 +02:00
totalTimesByUser := make(map[int64]int64)
for _, t := range trackedTimes {
totalTimesByUser[t.UserID] += t.Time
}
totalTimes := make(map[*user_model.User]string)
// Fetching User and making time human readable
2017-09-12 08:48:13 +02:00
for userID, total := range totalTimesByUser {
user, err := user_model.GetUserByID(userID)
2017-09-12 08:48:13 +02:00
if err != nil {
if user_model.IsErrUserNotExist(err) {
2017-09-12 08:48:13 +02:00
continue
}
return nil, err
}
2022-02-15 17:50:10 +01:00
totalTimes[user] = util.SecToTime(total)
2017-09-12 08:48:13 +02:00
}
return totalTimes, nil
}
2019-12-27 21:30:58 +01:00
// DeleteIssueUserTimes deletes times for issue
func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error {
2021-11-19 21:39:57 +08:00
ctx, committer, err := db.TxContext()
if err != nil {
2019-12-27 21:30:58 +01:00
return err
}
2021-11-19 21:39:57 +08:00
defer committer.Close()
2019-12-27 21:30:58 +01:00
opts := FindTrackedTimesOptions{
IssueID: issue.ID,
UserID: user.ID,
}
removedTime, err := deleteTimes(ctx, opts)
2019-12-27 21:30:58 +01:00
if err != nil {
return err
}
if removedTime == 0 {
return db.ErrNotExist{}
2019-12-27 21:30:58 +01:00
}
2022-04-08 17:11:15 +08:00
if err := issue.LoadRepo(ctx); err != nil {
2019-12-27 21:30:58 +01:00
return err
}
2022-04-08 17:11:15 +08:00
if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
2019-12-27 21:30:58 +01:00
Issue: issue,
Repo: issue.Repo,
Doer: user,
2022-02-15 17:50:10 +01:00
Content: "- " + util.SecToTime(removedTime),
2019-12-27 21:30:58 +01:00
Type: CommentTypeDeleteTimeManual,
}); err != nil {
return err
}
2021-11-19 21:39:57 +08:00
return committer.Commit()
2019-12-27 21:30:58 +01:00
}
// DeleteTime delete a specific Time
func DeleteTime(t *TrackedTime) error {
2021-11-19 21:39:57 +08:00
ctx, committer, err := db.TxContext()
if err != nil {
2019-12-27 21:30:58 +01:00
return err
}
2021-11-19 21:39:57 +08:00
defer committer.Close()
2019-12-27 21:30:58 +01:00
if err := t.loadAttributes(ctx); err != nil {
2020-05-09 16:18:44 +02:00
return err
}
if err := deleteTime(ctx, t); err != nil {
2019-12-27 21:30:58 +01:00
return err
}
2022-04-08 17:11:15 +08:00
if _, err := CreateCommentCtx(ctx, &CreateCommentOptions{
2019-12-27 21:30:58 +01:00
Issue: t.Issue,
Repo: t.Issue.Repo,
Doer: t.User,
2022-02-15 17:50:10 +01:00
Content: "- " + util.SecToTime(t.Time),
2019-12-27 21:30:58 +01:00
Type: CommentTypeDeleteTimeManual,
}); err != nil {
return err
}
2021-11-19 21:39:57 +08:00
return committer.Commit()
2019-12-27 21:30:58 +01:00
}
func deleteTimes(ctx context.Context, opts FindTrackedTimesOptions) (removedTime int64, err error) {
removedTime, err = GetTrackedSeconds(ctx, opts)
2019-12-27 21:30:58 +01:00
if err != nil || removedTime == 0 {
return
}
_, err = opts.toSession(db.GetEngine(ctx)).Table("tracked_time").Cols("deleted").Update(&TrackedTime{Deleted: true})
return removedTime, err
2019-12-27 21:30:58 +01:00
}
func deleteTime(ctx context.Context, t *TrackedTime) error {
2019-12-27 21:30:58 +01:00
if t.Deleted {
return db.ErrNotExist{ID: t.ID}
2019-12-27 21:30:58 +01:00
}
t.Deleted = true
_, err := db.GetEngine(ctx).ID(t.ID).Cols("deleted").Update(t)
2019-12-27 21:30:58 +01:00
return err
}
// GetTrackedTimeByID returns raw TrackedTime without loading attributes by id
func GetTrackedTimeByID(id int64) (*TrackedTime, error) {
2020-05-09 16:18:44 +02:00
time := new(TrackedTime)
2021-09-23 16:45:36 +01:00
has, err := db.GetEngine(db.DefaultContext).ID(id).Get(time)
2019-12-27 21:30:58 +01:00
if err != nil {
return nil, err
} else if !has {
return nil, db.ErrNotExist{ID: id}
2019-12-27 21:30:58 +01:00
}
return time, nil
}