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 models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2017-12-11 05:37:04 +01:00
|
|
|
|
2019-08-15 16:46:21 +02:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-07-27 03:44:44 +02:00
|
|
|
|
|
|
|
"xorm.io/xorm"
|
2017-09-12 08:48:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Stopwatch represents a stopwatch for time tracking.
|
|
|
|
type Stopwatch struct {
|
2019-08-15 16:46:21 +02:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
IssueID int64 `xorm:"INDEX"`
|
|
|
|
UserID int64 `xorm:"INDEX"`
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
2017-09-12 08:48:13 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 15:51:52 +01:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
|
|
|
|
sw = new(Stopwatch)
|
|
|
|
exists, err = e.
|
|
|
|
Where("user_id = ?", userID).
|
|
|
|
And("issue_id = ?", issueID).
|
|
|
|
Get(sw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-12 05:23:05 +01:00
|
|
|
// GetUserStopwatches return list of all stopwatches of a user
|
2020-09-18 14:09:26 +02:00
|
|
|
func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, error) {
|
|
|
|
sws := make([]*Stopwatch, 0, 8)
|
2020-01-24 20:00:29 +01:00
|
|
|
sess := x.Where("stopwatch.user_id = ?", userID)
|
|
|
|
if listOptions.Page != 0 {
|
|
|
|
sess = listOptions.setSessionPagination(sess)
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:09:26 +02:00
|
|
|
err := sess.Find(&sws)
|
2019-12-12 05:23:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sws, nil
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
// StopwatchExists returns true if the stopwatch exists
|
2021-03-14 19:52:12 +01:00
|
|
|
func StopwatchExists(userID, issueID int64) bool {
|
2017-09-12 08:48:13 +02:00
|
|
|
_, exists, _ := getStopwatch(x, userID, issueID)
|
|
|
|
return exists
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasUserStopwatch returns true if the user has a stopwatch
|
|
|
|
func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
|
2021-07-27 03:44:44 +02:00
|
|
|
return hasUserStopwatch(x, userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasUserStopwatch(e Engine, userID int64) (exists bool, sw *Stopwatch, err error) {
|
2017-09-12 08:48:13 +02:00
|
|
|
sw = new(Stopwatch)
|
2021-07-27 03:44:44 +02:00
|
|
|
exists, err = e.
|
2017-09-12 08:48:13 +02:00
|
|
|
Where("user_id = ?", userID).
|
|
|
|
Get(sw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
|
|
|
|
func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
|
2021-07-27 03:44:44 +02:00
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := createOrStopIssueStopwatch(sess, user, issue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createOrStopIssueStopwatch(e *xorm.Session, user *User, issue *Issue) error {
|
|
|
|
sw, exists, err := getStopwatch(e, user.ID, issue.ID)
|
2017-09-12 08:48:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-27 03:44:44 +02:00
|
|
|
if err := issue.loadRepo(e); err != nil {
|
2018-12-13 16:55:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
if exists {
|
|
|
|
// Create tracked time out of the time difference between start date and actual date
|
2017-12-11 05:37:04 +01:00
|
|
|
timediff := time.Now().Unix() - int64(sw.CreatedUnix)
|
2017-09-12 08:48:13 +02:00
|
|
|
|
|
|
|
// Create TrackedTime
|
|
|
|
tt := &TrackedTime{
|
|
|
|
Created: time.Now(),
|
|
|
|
IssueID: issue.ID,
|
|
|
|
UserID: user.ID,
|
|
|
|
Time: timediff,
|
|
|
|
}
|
|
|
|
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := e.Insert(tt); err != nil {
|
2017-09-12 08:48:13 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := createComment(e, &CreateCommentOptions{
|
2017-09-12 08:48:13 +02:00
|
|
|
Doer: user,
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
2018-04-29 07:58:47 +02:00
|
|
|
Content: SecToTime(timediff),
|
2017-09-12 08:48:13 +02:00
|
|
|
Type: CommentTypeStopTracking,
|
2021-02-19 11:52:11 +01:00
|
|
|
TimeID: tt.ID,
|
2017-09-12 08:48:13 +02:00
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := e.Delete(sw); err != nil {
|
2017-09-12 08:48:13 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-14 19:52:12 +01:00
|
|
|
// if another stopwatch is running: stop it
|
2021-07-27 03:44:44 +02:00
|
|
|
exists, sw, err := hasUserStopwatch(e, user.ID)
|
2020-07-12 12:01:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exists {
|
2021-07-27 03:44:44 +02:00
|
|
|
issue, err := getIssueByID(e, sw.IssueID)
|
2020-07-12 12:01:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-27 03:44:44 +02:00
|
|
|
if err := createOrStopIssueStopwatch(e, user, issue); err != nil {
|
2020-07-12 12:01:20 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 08:48:13 +02:00
|
|
|
// Create stopwatch
|
|
|
|
sw = &Stopwatch{
|
|
|
|
UserID: user.ID,
|
|
|
|
IssueID: issue.ID,
|
|
|
|
}
|
|
|
|
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := e.Insert(sw); err != nil {
|
2017-09-12 08:48:13 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := createComment(e, &CreateCommentOptions{
|
2017-09-12 08:48:13 +02:00
|
|
|
Doer: user,
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Type: CommentTypeStartTracking,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
|
|
|
|
func CancelStopwatch(user *User, issue *Issue) error {
|
2021-07-27 03:44:44 +02:00
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := cancelStopwatch(sess, user, issue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancelStopwatch(e *xorm.Session, user *User, issue *Issue) error {
|
|
|
|
sw, exists, err := getStopwatch(e, user.ID, issue.ID)
|
2017-09-12 08:48:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := e.Delete(sw); err != nil {
|
2017-09-12 08:48:13 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-27 03:44:44 +02:00
|
|
|
if err := issue.loadRepo(e); err != nil {
|
2018-12-13 16:55:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-27 03:44:44 +02:00
|
|
|
if _, err := createComment(e, &CreateCommentOptions{
|
2017-09-12 08:48:13 +02:00
|
|
|
Doer: user,
|
|
|
|
Issue: issue,
|
|
|
|
Repo: issue.Repo,
|
|
|
|
Type: CommentTypeCancelTracking,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-29 07:58:47 +02:00
|
|
|
// SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
|
|
|
|
func SecToTime(duration int64) string {
|
2017-09-12 08:48:13 +02:00
|
|
|
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
|
|
|
|
}
|