You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

issue_stopwatch.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "time"
  8. "code.gitea.io/gitea/modules/util"
  9. )
  10. // Stopwatch represents a stopwatch for time tracking.
  11. type Stopwatch struct {
  12. ID int64 `xorm:"pk autoincr"`
  13. IssueID int64 `xorm:"INDEX"`
  14. UserID int64 `xorm:"INDEX"`
  15. CreatedUnix util.TimeStamp `xorm:"created"`
  16. }
  17. func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
  18. sw = new(Stopwatch)
  19. exists, err = e.
  20. Where("user_id = ?", userID).
  21. And("issue_id = ?", issueID).
  22. Get(sw)
  23. return
  24. }
  25. // StopwatchExists returns true if the stopwatch exists
  26. func StopwatchExists(userID int64, issueID int64) bool {
  27. _, exists, _ := getStopwatch(x, userID, issueID)
  28. return exists
  29. }
  30. // HasUserStopwatch returns true if the user has a stopwatch
  31. func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
  32. sw = new(Stopwatch)
  33. exists, err = x.
  34. Where("user_id = ?", userID).
  35. Get(sw)
  36. return
  37. }
  38. // CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
  39. func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
  40. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  41. if err != nil {
  42. return err
  43. }
  44. if exists {
  45. // Create tracked time out of the time difference between start date and actual date
  46. timediff := time.Now().Unix() - int64(sw.CreatedUnix)
  47. // Create TrackedTime
  48. tt := &TrackedTime{
  49. Created: time.Now(),
  50. IssueID: issue.ID,
  51. UserID: user.ID,
  52. Time: timediff,
  53. }
  54. if _, err := x.Insert(tt); err != nil {
  55. return err
  56. }
  57. if _, err := CreateComment(&CreateCommentOptions{
  58. Doer: user,
  59. Issue: issue,
  60. Repo: issue.Repo,
  61. Content: SecToTime(timediff),
  62. Type: CommentTypeStopTracking,
  63. }); err != nil {
  64. return err
  65. }
  66. if _, err := x.Delete(sw); err != nil {
  67. return err
  68. }
  69. } else {
  70. // Create stopwatch
  71. sw = &Stopwatch{
  72. UserID: user.ID,
  73. IssueID: issue.ID,
  74. }
  75. if _, err := x.Insert(sw); err != nil {
  76. return err
  77. }
  78. if _, err := CreateComment(&CreateCommentOptions{
  79. Doer: user,
  80. Issue: issue,
  81. Repo: issue.Repo,
  82. Type: CommentTypeStartTracking,
  83. }); err != nil {
  84. return err
  85. }
  86. }
  87. return nil
  88. }
  89. // CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
  90. func CancelStopwatch(user *User, issue *Issue) error {
  91. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  92. if err != nil {
  93. return err
  94. }
  95. if exists {
  96. if _, err := x.Delete(sw); err != nil {
  97. return err
  98. }
  99. if _, err := CreateComment(&CreateCommentOptions{
  100. Doer: user,
  101. Issue: issue,
  102. Repo: issue.Repo,
  103. Type: CommentTypeCancelTracking,
  104. }); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }
  110. // SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
  111. func SecToTime(duration int64) string {
  112. seconds := duration % 60
  113. minutes := (duration / (60)) % 60
  114. hours := duration / (60 * 60)
  115. var hrs string
  116. if hours > 0 {
  117. hrs = fmt.Sprintf("%dh", hours)
  118. }
  119. if minutes > 0 {
  120. if hours == 0 {
  121. hrs = fmt.Sprintf("%dmin", minutes)
  122. } else {
  123. hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
  124. }
  125. }
  126. if seconds > 0 {
  127. if hours == 0 && minutes == 0 {
  128. hrs = fmt.Sprintf("%ds", seconds)
  129. } else {
  130. hrs = fmt.Sprintf("%s %ds", hrs, seconds)
  131. }
  132. }
  133. return hrs
  134. }