本站源代码
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.

163 lines
3.5KB

  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. 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 err := issue.loadRepo(x); err != nil {
  45. return err
  46. }
  47. if exists {
  48. // Create tracked time out of the time difference between start date and actual date
  49. timediff := time.Now().Unix() - int64(sw.CreatedUnix)
  50. // Create TrackedTime
  51. tt := &TrackedTime{
  52. Created: time.Now(),
  53. IssueID: issue.ID,
  54. UserID: user.ID,
  55. Time: timediff,
  56. }
  57. if _, err := x.Insert(tt); err != nil {
  58. return err
  59. }
  60. if _, err := CreateComment(&CreateCommentOptions{
  61. Doer: user,
  62. Issue: issue,
  63. Repo: issue.Repo,
  64. Content: SecToTime(timediff),
  65. Type: CommentTypeStopTracking,
  66. }); err != nil {
  67. return err
  68. }
  69. if _, err := x.Delete(sw); err != nil {
  70. return err
  71. }
  72. } else {
  73. // Create stopwatch
  74. sw = &Stopwatch{
  75. UserID: user.ID,
  76. IssueID: issue.ID,
  77. }
  78. if _, err := x.Insert(sw); err != nil {
  79. return err
  80. }
  81. if _, err := CreateComment(&CreateCommentOptions{
  82. Doer: user,
  83. Issue: issue,
  84. Repo: issue.Repo,
  85. Type: CommentTypeStartTracking,
  86. }); err != nil {
  87. return err
  88. }
  89. }
  90. return nil
  91. }
  92. // CancelStopwatch removes the given stopwatch and logs it into issue's timeline.
  93. func CancelStopwatch(user *User, issue *Issue) error {
  94. sw, exists, err := getStopwatch(x, user.ID, issue.ID)
  95. if err != nil {
  96. return err
  97. }
  98. if exists {
  99. if _, err := x.Delete(sw); err != nil {
  100. return err
  101. }
  102. if err := issue.loadRepo(x); err != nil {
  103. return err
  104. }
  105. if _, err := CreateComment(&CreateCommentOptions{
  106. Doer: user,
  107. Issue: issue,
  108. Repo: issue.Repo,
  109. Type: CommentTypeCancelTracking,
  110. }); err != nil {
  111. return err
  112. }
  113. }
  114. return nil
  115. }
  116. // SecToTime converts an amount of seconds to a human-readable string (example: 66s -> 1min 6s)
  117. func SecToTime(duration int64) string {
  118. seconds := duration % 60
  119. minutes := (duration / (60)) % 60
  120. hours := duration / (60 * 60)
  121. var hrs string
  122. if hours > 0 {
  123. hrs = fmt.Sprintf("%dh", hours)
  124. }
  125. if minutes > 0 {
  126. if hours == 0 {
  127. hrs = fmt.Sprintf("%dmin", minutes)
  128. } else {
  129. hrs = fmt.Sprintf("%s %dmin", hrs, minutes)
  130. }
  131. }
  132. if seconds > 0 {
  133. if hours == 0 && minutes == 0 {
  134. hrs = fmt.Sprintf("%ds", seconds)
  135. } else {
  136. hrs = fmt.Sprintf("%s %ds", hrs, seconds)
  137. }
  138. }
  139. return hrs
  140. }
上海开阖软件有限公司 沪ICP备12045867号-1