2017-09-12 12:18:13 +05:30
|
|
|
// 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 (
|
2021-11-19 19:09:57 +05:30
|
|
|
"context"
|
2017-09-12 12:18:13 +05:30
|
|
|
"fmt"
|
|
|
|
"time"
|
2017-12-11 10:07:04 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-09-12 12:18:13 +05:30
|
|
|
)
|
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// ErrIssueStopwatchNotExist represents an error that stopwatch is not exist
|
|
|
|
type ErrIssueStopwatchNotExist struct {
|
|
|
|
UserID int64
|
|
|
|
IssueID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrIssueStopwatchNotExist) Error() string {
|
|
|
|
return fmt.Sprintf("issue stopwatch doesn't exist[uid: %d, issue_id: %d", err.UserID, err.IssueID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist
|
|
|
|
type ErrIssueStopwatchAlreadyExist struct {
|
|
|
|
UserID int64
|
|
|
|
IssueID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err ErrIssueStopwatchAlreadyExist) Error() string {
|
|
|
|
return fmt.Sprintf("issue stopwatch already exists[uid: %d, issue_id: %d", err.UserID, err.IssueID)
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:18:13 +05:30
|
|
|
// Stopwatch represents a stopwatch for time tracking.
|
|
|
|
type Stopwatch struct {
|
2019-08-15 20:16:21 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
IssueID int64 `xorm:"INDEX"`
|
|
|
|
UserID int64 `xorm:"INDEX"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
2017-09-12 12:18:13 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(Stopwatch))
|
|
|
|
}
|
|
|
|
|
2021-01-21 20:21:52 +05:30
|
|
|
// Seconds returns the amount of time passed since creation, based on local server time
|
|
|
|
func (s Stopwatch) Seconds() int64 {
|
|
|
|
return int64(timeutil.TimeStampNow() - s.CreatedUnix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duration returns a human-readable duration string based on local server time
|
|
|
|
func (s Stopwatch) Duration() string {
|
|
|
|
return SecToTime(s.Seconds())
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
func getStopwatch(ctx context.Context, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
|
2017-09-12 12:18:13 +05:30
|
|
|
sw = new(Stopwatch)
|
2021-11-21 14:41:48 +05:30
|
|
|
exists, err = db.GetEngine(ctx).
|
2017-09-12 12:18:13 +05:30
|
|
|
Where("user_id = ?", userID).
|
|
|
|
And("issue_id = ?", issueID).
|
|
|
|
Get(sw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-12 09:53:05 +05:30
|
|
|
// GetUserStopwatches return list of all stopwatches of a user
|
2021-09-24 17:02:56 +05:30
|
|
|
func GetUserStopwatches(userID int64, listOptions db.ListOptions) ([]*Stopwatch, error) {
|
2020-09-18 17:39:26 +05:30
|
|
|
sws := make([]*Stopwatch, 0, 8)
|
2021-09-23 21:15:36 +05:30
|
|
|
sess := db.GetEngine(db.DefaultContext).Where("stopwatch.user_id = ?", userID)
|
2020-01-25 00:30:29 +05:30
|
|
|
if listOptions.Page != 0 {
|
2021-09-24 17:02:56 +05:30
|
|
|
sess = db.SetSessionPagination(sess, &listOptions)
|
2020-01-25 00:30:29 +05:30
|
|
|
}
|
|
|
|
|
2020-09-18 17:39:26 +05:30
|
|
|
err := sess.Find(&sws)
|
2019-12-12 09:53:05 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sws, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:13:08 +05:30
|
|
|
// CountUserStopwatches return count of all stopwatches of a user
|
|
|
|
func CountUserStopwatches(userID int64) (int64, error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
return db.GetEngine(db.DefaultContext).Where("user_id = ?", userID).Count(&Stopwatch{})
|
2021-08-12 18:13:08 +05:30
|
|
|
}
|
|
|
|
|
2017-09-12 12:18:13 +05:30
|
|
|
// StopwatchExists returns true if the stopwatch exists
|
2021-03-15 00:22:12 +05:30
|
|
|
func StopwatchExists(userID, issueID int64) bool {
|
2021-11-21 14:41:48 +05:30
|
|
|
_, exists, _ := getStopwatch(db.DefaultContext, userID, issueID)
|
2017-09-12 12:18:13 +05:30
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasUserStopwatch returns true if the user has a stopwatch
|
|
|
|
func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
|
2021-09-23 21:15:36 +05:30
|
|
|
return hasUserStopwatch(db.GetEngine(db.DefaultContext), userID)
|
2021-07-27 02:16:06 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func hasUserStopwatch(e db.Engine, userID int64) (exists bool, sw *Stopwatch, err error) {
|
2017-09-12 12:18:13 +05:30
|
|
|
sw = new(Stopwatch)
|
2021-07-27 02:16:06 +05:30
|
|
|
exists, err = e.
|
2017-09-12 12:18:13 +05:30
|
|
|
Where("user_id = ?", userID).
|
|
|
|
Get(sw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// FinishIssueStopwatchIfPossible if stopwatch exist then finish it otherwise ignore
|
2021-11-24 15:19:20 +05:30
|
|
|
func FinishIssueStopwatchIfPossible(ctx context.Context, user *user_model.User, issue *Issue) error {
|
2021-11-21 14:41:48 +05:30
|
|
|
_, exists, err := getStopwatch(ctx, user.ID, issue.ID)
|
2021-11-19 19:09:57 +05:30
|
|
|
if err != nil {
|
2021-07-27 02:16:06 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return FinishIssueStopwatch(ctx, user, issue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrStopIssueStopwatch create an issue stopwatch if it's not exist, otherwise finish it
|
2021-11-24 15:19:20 +05:30
|
|
|
func CreateOrStopIssueStopwatch(user *user_model.User, issue *Issue) error {
|
2021-11-21 14:41:48 +05:30
|
|
|
_, exists, err := getStopwatch(db.DefaultContext, user.ID, issue.ID)
|
|
|
|
if err != nil {
|
2021-07-27 02:16:06 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
if exists {
|
|
|
|
return FinishIssueStopwatch(db.DefaultContext, user, issue)
|
|
|
|
}
|
|
|
|
return CreateIssueStopwatch(db.DefaultContext, user, issue)
|
2021-07-27 02:16:06 +05:30
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// FinishIssueStopwatch if stopwatch exist then finish it otherwise return an error
|
2021-11-24 15:19:20 +05:30
|
|
|
func FinishIssueStopwatch(ctx context.Context, user *user_model.User, issue *Issue) error {
|
2021-11-21 14:41:48 +05:30
|
|
|
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID)
|
2017-09-12 12:18:13 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
if !exists {
|
|
|
|
return ErrIssueStopwatchNotExist{
|
|
|
|
UserID: user.ID,
|
|
|
|
IssueID: issue.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create tracked time out of the time difference between start date and actual date
|
|
|
|
timediff := time.Now().Unix() - int64(sw.CreatedUnix)
|
|
|
|
|
|
|
|
// Create TrackedTime
|
|
|
|
tt := &TrackedTime{
|
|
|
|
Created: time.Now(),
|
|
|
|
IssueID: issue.ID,
|
|
|
|
UserID: user.ID,
|
|
|
|
Time: timediff,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Insert(ctx, tt); err != nil {
|
2018-12-13 21:25:43 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := issue.loadRepo(ctx); err != nil {
|
2021-11-21 14:41:48 +05:30
|
|
|
return err
|
|
|
|
}
|
2017-09-12 12:18:13 +05:30
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{
|
|
|
|
Doer: user,
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Content: SecToTime(timediff),
|
|
|
|
Type: CommentTypeStopTracking,
|
|
|
|
TimeID: tt.ID,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = db.GetEngine(ctx).Delete(sw)
|
|
|
|
return err
|
|
|
|
}
|
2017-09-12 12:18:13 +05:30
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// CreateIssueStopwatch creates a stopwatch if not exist, otherwise return an error
|
2021-11-24 15:19:20 +05:30
|
|
|
func CreateIssueStopwatch(ctx context.Context, user *user_model.User, issue *Issue) error {
|
2021-11-21 14:41:48 +05:30
|
|
|
e := db.GetEngine(ctx)
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := issue.loadRepo(ctx); err != nil {
|
2021-11-21 14:41:48 +05:30
|
|
|
return err
|
|
|
|
}
|
2017-09-12 12:18:13 +05:30
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// if another stopwatch is running: stop it
|
|
|
|
exists, sw, err := hasUserStopwatch(e, user.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
issue, err := getIssueByID(e, sw.IssueID)
|
2020-07-12 15:31:20 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
if err := FinishIssueStopwatch(ctx, user, issue); err != nil {
|
2017-09-12 12:18:13 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
}
|
2017-09-12 12:18:13 +05:30
|
|
|
|
2021-11-21 14:41:48 +05:30
|
|
|
// Create stopwatch
|
|
|
|
sw = &Stopwatch{
|
|
|
|
UserID: user.ID,
|
|
|
|
IssueID: issue.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.Insert(ctx, sw); err != nil {
|
|
|
|
return err
|
2017-09-12 12:18:13 +05:30
|
|
|
}
|
2021-11-21 14:41:48 +05:30
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := issue.loadRepo(ctx); err != nil {
|
2021-11-21 14:41:48 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{
|
|
|
|
Doer: user,
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Type: CommentTypeStartTracking,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:18:13 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
|
2021-11-24 15:19:20 +05:30
|
|
|
func CancelStopwatch(user *user_model.User, issue *Issue) error {
|
2021-11-19 19:09:57 +05:30
|
|
|
ctx, committer, err := db.TxContext()
|
|
|
|
if err != nil {
|
2021-07-27 02:16:06 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-19 19:09:57 +05:30
|
|
|
defer committer.Close()
|
|
|
|
if err := cancelStopwatch(ctx, user, issue); err != nil {
|
2021-07-27 02:16:06 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-19 19:09:57 +05:30
|
|
|
return committer.Commit()
|
2021-07-27 02:16:06 +05:30
|
|
|
}
|
|
|
|
|
2021-11-24 15:19:20 +05:30
|
|
|
func cancelStopwatch(ctx context.Context, user *user_model.User, issue *Issue) error {
|
2021-11-19 19:09:57 +05:30
|
|
|
e := db.GetEngine(ctx)
|
2021-11-21 14:41:48 +05:30
|
|
|
sw, exists, err := getStopwatch(ctx, user.ID, issue.ID)
|
2017-09-12 12:18:13 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2021-07-27 02:16:06 +05:30
|
|
|
if _, err := e.Delete(sw); err != nil {
|
2017-09-12 12:18:13 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
if err := issue.loadRepo(ctx); err != nil {
|
2021-11-21 14:41:48 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-19 19:09:57 +05:30
|
|
|
if _, err := createComment(ctx, &CreateCommentOptions{
|
2017-09-12 12:18:13 +05:30
|
|
|
Doer: user,
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Type: CommentTypeCancelTracking,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-29 11:28:47 +05:30
|
|
|
// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
|
|
|
|
func SecToTime(duration int64) string {
|
2017-09-12 12:18:13 +05:30
|
|
|
seconds := duration % 60
|
|
|
|
minutes := (duration / (60)) % 60
|
|
|
|
hours := duration / (60 * 60)
|
|
|
|
|
|
|
|
var hrs string
|
|
|
|
|
|
|
|
if hours > 0 {
|
|
|
|
hrs = fmt.Sprintf("%dh", hours)
|
|
|
|
}
|
|
|
|
if minutes > 0 {
|
|
|
|
if hours == 0 {
|
|
|
|
hrs = fmt.Sprintf("%dmin", minutes)
|
|
|
|
} else {
|
|
|
|
hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if seconds > 0 {
|
|
|
|
if hours == 0 && minutes == 0 {
|
|
|
|
hrs = fmt.Sprintf("%ds", seconds)
|
|
|
|
} else {
|
|
|
|
hrs = fmt.Sprintf("%s %ds", hrs, seconds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hrs
|
|
|
|
}
|