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

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