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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "github.com/go-xorm/xorm"
  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. Created time.Time `xorm:"-"`
  16. CreatedUnix int64
  17. }
  18. // BeforeInsert will be invoked by XORM before inserting a record
  19. // representing this object.
  20. func (s *Stopwatch) BeforeInsert() {
  21. s.CreatedUnix = time.Now().Unix()
  22. }
  23. // AfterSet is invoked from XORM after setting the value of a field of this object.
  24. func (s *Stopwatch) AfterSet(colName string, _ xorm.Cell) {
  25. switch colName {
  26. case "created_unix":
  27. s.Created = time.Unix(s.CreatedUnix, 0).Local()
  28. }
  29. }
  30. func getStopwatch(e Engine, userID, issueID int64) (sw *Stopwatch, exists bool, err error) {
  31. sw = new(Stopwatch)
  32. exists, err = e.
  33. Where("user_id = ?", userID).
  34. And("issue_id = ?", issueID).
  35. Get(sw)
  36. return
  37. }
  38. // StopwatchExists returns true if the stopwatch exists
  39. func StopwatchExists(userID int64, issueID int64) bool {
  40. _, exists, _ := getStopwatch(x, userID, issueID)
  41. return exists
  42. }
  43. // HasUserStopwatch returns true if the user has a stopwatch
  44. func HasUserStopwatch(userID int64) (exists bool, sw *Stopwatch, err error) {
  45. sw = new(Stopwatch)
  46. exists, err = x.
  47. Where("user_id = ?", userID).
  48. Get(sw)
  49. return
  50. }
  51. // CreateOrStopIssueStopwatch will create or remove a stopwatch and will log it into issue's timeline.
  52. func CreateOrStopIssueStopwatch(user *User, issue *Issue) error {
  53. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  54. if err != nil {
  55. return err
  56. }
  57. if exists {
  58. // Create tracked time out of the time difference between start date and actual date
  59. timediff := time.Now().Unix() - sw.CreatedUnix
  60. // Create TrackedTime
  61. tt := &TrackedTime{
  62. Created: time.Now(),
  63. IssueID: issue.ID,
  64. UserID: user.ID,
  65. Time: timediff,
  66. }
  67. if _, err := x.Insert(tt); err != nil {
  68. return err
  69. }
  70. if _, err := CreateComment(&CreateCommentOptions{
  71. Doer: user,
  72. Issue: issue,
  73. Repo: issue.Repo,
  74. Content: secToTime(timediff),
  75. Type: CommentTypeStopTracking,
  76. }); err != nil {
  77. return err
  78. }
  79. if _, err := x.Delete(sw); err != nil {
  80. return err
  81. }
  82. } else {
  83. // Create stopwatch
  84. sw = &Stopwatch{
  85. UserID: user.ID,
  86. IssueID: issue.ID,
  87. Created: time.Now(),
  88. }
  89. if _, err := x.Insert(sw); err != nil {
  90. return err
  91. }
  92. if _, err := CreateComment(&CreateCommentOptions{
  93. Doer: user,
  94. Issue: issue,
  95. Repo: issue.Repo,
  96. Type: CommentTypeStartTracking,
  97. }); err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }
  103. // CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
  104. func CancelStopwatch(user *User, issue *Issue) error {
  105. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  106. if err != nil {
  107. return err
  108. }
  109. if exists {
  110. if _, err := x.Delete(sw); err != nil {
  111. return err
  112. }
  113. if _, err := CreateComment(&CreateCommentOptions{
  114. Doer: user,
  115. Issue: issue,
  116. Repo: issue.Repo,
  117. Type: CommentTypeCancelTracking,
  118. }); err != nil {
  119. return err
  120. }
  121. }
  122. return nil
  123. }
  124. func secToTime(duration int64) string {
  125. seconds := duration % 60
  126. minutes := (duration / (60)) % 60
  127. hours := duration / (60 * 60)
  128. var hrs string
  129. if hours > 0 {
  130. hrs = fmt.Sprintf("%dh", hours)
  131. }
  132. if minutes > 0 {
  133. if hours == 0 {
  134. hrs = fmt.Sprintf("%dmin", minutes)
  135. } else {
  136. hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
  137. }
  138. }
  139. if seconds > 0 {
  140. if hours == 0 && minutes == 0 {
  141. hrs = fmt.Sprintf("%ds", seconds)
  142. } else {
  143. hrs = fmt.Sprintf("%s %ds", hrs, seconds)
  144. }
  145. }
  146. return hrs
  147. }