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 4.6KB

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