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_tracked_time.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "time"
  7. "code.gitea.io/gitea/modules/setting"
  8. api "code.gitea.io/sdk/gitea"
  9. "github.com/go-xorm/builder"
  10. "github.com/go-xorm/xorm"
  11. )
  12. // TrackedTime represents a time that was spent for a specific issue.
  13. type TrackedTime struct {
  14. ID int64 `xorm:"pk autoincr" json:"id"`
  15. IssueID int64 `xorm:"INDEX" json:"issue_id"`
  16. UserID int64 `xorm:"INDEX" json:"user_id"`
  17. Created time.Time `xorm:"-" json:"created"`
  18. CreatedUnix int64 `xorm:"created" json:"-"`
  19. Time int64 `json:"time"`
  20. }
  21. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  22. func (t *TrackedTime) AfterLoad() {
  23. t.Created = time.Unix(t.CreatedUnix, 0).In(setting.UILocation)
  24. }
  25. // APIFormat converts TrackedTime to API format
  26. func (t *TrackedTime) APIFormat() *api.TrackedTime {
  27. return &api.TrackedTime{
  28. ID: t.ID,
  29. IssueID: t.IssueID,
  30. UserID: t.UserID,
  31. Time: t.Time,
  32. Created: t.Created,
  33. }
  34. }
  35. // FindTrackedTimesOptions represent the filters for tracked times. If an ID is 0 it will be ignored.
  36. type FindTrackedTimesOptions struct {
  37. IssueID int64
  38. UserID int64
  39. RepositoryID int64
  40. MilestoneID int64
  41. }
  42. // ToCond will convert each condition into a xorm-Cond
  43. func (opts *FindTrackedTimesOptions) ToCond() builder.Cond {
  44. cond := builder.NewCond()
  45. if opts.IssueID != 0 {
  46. cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
  47. }
  48. if opts.UserID != 0 {
  49. cond = cond.And(builder.Eq{"user_id": opts.UserID})
  50. }
  51. if opts.RepositoryID != 0 {
  52. cond = cond.And(builder.Eq{"issue.repo_id": opts.RepositoryID})
  53. }
  54. if opts.MilestoneID != 0 {
  55. cond = cond.And(builder.Eq{"issue.milestone_id": opts.MilestoneID})
  56. }
  57. return cond
  58. }
  59. // ToSession will convert the given options to a xorm Session by using the conditions from ToCond and joining with issue table if required
  60. func (opts *FindTrackedTimesOptions) ToSession(e Engine) *xorm.Session {
  61. if opts.RepositoryID > 0 || opts.MilestoneID > 0 {
  62. return e.Join("INNER", "issue", "issue.id = tracked_time.issue_id").Where(opts.ToCond())
  63. }
  64. return x.Where(opts.ToCond())
  65. }
  66. // GetTrackedTimes returns all tracked times that fit to the given options.
  67. func GetTrackedTimes(options FindTrackedTimesOptions) (trackedTimes []*TrackedTime, err error) {
  68. err = options.ToSession(x).Find(&trackedTimes)
  69. return
  70. }
  71. // AddTime will add the given time (in seconds) to the issue
  72. func AddTime(user *User, issue *Issue, time int64) (*TrackedTime, error) {
  73. tt := &TrackedTime{
  74. IssueID: issue.ID,
  75. UserID: user.ID,
  76. Time: time,
  77. }
  78. if _, err := x.Insert(tt); err != nil {
  79. return nil, err
  80. }
  81. if _, err := CreateComment(&CreateCommentOptions{
  82. Issue: issue,
  83. Repo: issue.Repo,
  84. Doer: user,
  85. Content: SecToTime(time),
  86. Type: CommentTypeAddTimeManual,
  87. }); err != nil {
  88. return nil, err
  89. }
  90. return tt, nil
  91. }
  92. // TotalTimes returns the spent time for each user by an issue
  93. func TotalTimes(options FindTrackedTimesOptions) (map[*User]string, error) {
  94. trackedTimes, err := GetTrackedTimes(options)
  95. if err != nil {
  96. return nil, err
  97. }
  98. //Adding total time per user ID
  99. totalTimesByUser := make(map[int64]int64)
  100. for _, t := range trackedTimes {
  101. totalTimesByUser[t.UserID] += t.Time
  102. }
  103. totalTimes := make(map[*User]string)
  104. //Fetching User and making time human readable
  105. for userID, total := range totalTimesByUser {
  106. user, err := GetUserByID(userID)
  107. if err != nil {
  108. if IsErrUserNotExist(err) {
  109. continue
  110. }
  111. return nil, err
  112. }
  113. totalTimes[user] = SecToTime(total)
  114. }
  115. return totalTimes, nil
  116. }