Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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/timeutil"
  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 timeutil.TimeStamp `xorm:"created"`
  16. }
  17. // Seconds returns the amount of time passed since creation, based on local server time
  18. func (s Stopwatch) Seconds() int64 {
  19. return int64(timeutil.TimeStampNow() - s.CreatedUnix)
  20. }
  21. // Duration returns a human-readable duration string based on local server time
  22. func (s Stopwatch) Duration() string {
  23. return SecToTime(s.Seconds())
  24. }
  25. func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
  26. sw = new(Stopwatch)
  27. exists, err = e.
  28. Where("user_id = ?", userID).
  29. And("issue_id = ?", issueID).
  30. Get(sw)
  31. return
  32. }
  33. // GetUserStopwatches return list of all stopwatches of a user
  34. func GetUserStopwatches(userID int64, listOptions ListOptions) ([]*Stopwatch, error) {
  35. sws := make([]*Stopwatch, 0, 8)
  36. sess := x.Where("stopwatch.user_id = ?", userID)
  37. if listOptions.Page != 0 {
  38. sess = listOptions.setSessionPagination(sess)
  39. }
  40. err := sess.Find(&sws)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return sws, nil
  45. }
  46. // StopwatchExists returns true if the stopwatch exists
  47. func StopwatchExists(userID, issueID int64) bool {
  48. _, exists, _ := getStopwatch(x, userID, issueID)
  49. return exists
  50. }
  51. // HasUserStopwatch returns true if the user has a stopwatch
  52. func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
  53. sw = new(Stopwatch)
  54. exists, err = x.
  55. Where("user_id = ?", userID).
  56. Get(sw)
  57. return
  58. }
  59. // CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
  60. func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
  61. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  62. if err != nil {
  63. return err
  64. }
  65. if err := issue.loadRepo(x); err != nil {
  66. return err
  67. }
  68. if exists {
  69. // Create tracked time out of the time difference between start date and actual date
  70. timediff := time.Now().Unix() - int64(sw.CreatedUnix)
  71. // Create TrackedTime
  72. tt := &TrackedTime{
  73. Created: time.Now(),
  74. IssueID: issue.ID,
  75. UserID: user.ID,
  76. Time: timediff,
  77. }
  78. if _, err := x.Insert(tt); err != nil {
  79. return err
  80. }
  81. if _, err := CreateComment(&CreateCommentOptions{
  82. Doer: user,
  83. Issue: issue,
  84. Repo: issue.Repo,
  85. Content: SecToTime(timediff),
  86. Type: CommentTypeStopTracking,
  87. TimeID: tt.ID,
  88. }); err != nil {
  89. return err
  90. }
  91. if _, err := x.Delete(sw); err != nil {
  92. return err
  93. }
  94. } else {
  95. // if another stopwatch is running: stop it
  96. exists, sw, err := HasUserStopwatch(user.ID)
  97. if err != nil {
  98. return err
  99. }
  100. if exists {
  101. issue, err := getIssueByID(x, sw.IssueID)
  102. if err != nil {
  103. return err
  104. }
  105. if err := CreateOrStopIssueStopwatch(user, issue); err != nil {
  106. return err
  107. }
  108. }
  109. // Create stopwatch
  110. sw = &Stopwatch{
  111. UserID: user.ID,
  112. IssueID: issue.ID,
  113. }
  114. if _, err := x.Insert(sw); err != nil {
  115. return err
  116. }
  117. if _, err := CreateComment(&CreateCommentOptions{
  118. Doer: user,
  119. Issue: issue,
  120. Repo: issue.Repo,
  121. Type: CommentTypeStartTracking,
  122. }); err != nil {
  123. return err
  124. }
  125. }
  126. return nil
  127. }
  128. // CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
  129. func CancelStopwatch(user *User, issue *Issue) error {
  130. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  131. if err != nil {
  132. return err
  133. }
  134. if exists {
  135. if _, err := x.Delete(sw); err != nil {
  136. return err
  137. }
  138. if err := issue.loadRepo(x); err != nil {
  139. return err
  140. }
  141. if _, err := CreateComment(&CreateCommentOptions{
  142. Doer: user,
  143. Issue: issue,
  144. Repo: issue.Repo,
  145. Type: CommentTypeCancelTracking,
  146. }); err != nil {
  147. return err
  148. }
  149. }
  150. return nil
  151. }
  152. // SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
  153. func SecToTime(duration int64) string {
  154. seconds := duration % 60
  155. minutes := (duration / (60)) % 60
  156. hours := duration / (60 * 60)
  157. var hrs string
  158. if hours > 0 {
  159. hrs = fmt.Sprintf("%dh", hours)
  160. }
  161. if minutes > 0 {
  162. if hours == 0 {
  163. hrs = fmt.Sprintf("%dmin", minutes)
  164. } else {
  165. hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
  166. }
  167. }
  168. if seconds > 0 {
  169. if hours == 0 && minutes == 0 {
  170. hrs = fmt.Sprintf("%ds", seconds)
  171. } else {
  172. hrs = fmt.Sprintf("%s %ds", hrs, seconds)
  173. }
  174. }
  175. return hrs
  176. }