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

134 lines
3.7KB

  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/gitea/modules/structs"
  9. "xorm.io/builder"
  10. "xorm.io/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.DefaultUILocation)
  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 := issue.loadRepo(x); err != nil {
  82. return nil, err
  83. }
  84. if _, err := CreateComment(&CreateCommentOptions{
  85. Issue: issue,
  86. Repo: issue.Repo,
  87. Doer: user,
  88. Content: SecToTime(time),
  89. Type: CommentTypeAddTimeManual,
  90. }); err != nil {
  91. return nil, err
  92. }
  93. return tt, nil
  94. }
  95. // TotalTimes returns the spent time for each user by an issue
  96. func TotalTimes(options FindTrackedTimesOptions) (map[*User]string, error) {
  97. trackedTimes, err := GetTrackedTimes(options)
  98. if err != nil {
  99. return nil, err
  100. }
  101. //Adding total time per user ID
  102. totalTimesByUser := make(map[int64]int64)
  103. for _, t := range trackedTimes {
  104. totalTimesByUser[t.UserID] += t.Time
  105. }
  106. totalTimes := make(map[*User]string)
  107. //Fetching User and making time human readable
  108. for userID, total := range totalTimesByUser {
  109. user, err := GetUserByID(userID)
  110. if err != nil {
  111. if IsErrUserNotExist(err) {
  112. continue
  113. }
  114. return nil, err
  115. }
  116. totalTimes[user] = SecToTime(total)
  117. }
  118. return totalTimes, nil
  119. }
上海开阖软件有限公司 沪ICP备12045867号-1