aboutsummaryrefslogtreecommitdiffstats
path: root/models/issues/tracked_time.go
diff options
context:
space:
mode:
author6543 <m.huber@kithara.com>2023-10-19 16:08:31 +0200
committerGitHub <noreply@github.com>2023-10-19 14:08:31 +0000
commitadbc995c347e158a56264f2488997d7d59a4dd8b (patch)
treed571ede3f565910d8890c96932518e7793728647 /models/issues/tracked_time.go
parente83f2cbbacd2696f26dde7105d7f07833c0cc33e (diff)
downloadgitea-adbc995c347e158a56264f2488997d7d59a4dd8b.tar.gz
gitea-adbc995c347e158a56264f2488997d7d59a4dd8b.zip
Show total TrackedTime on issue/pull/milestone lists (#26672)
TODOs: - [x] write test for `GetIssueTotalTrackedTime` - [x] frontport kitharas template changes and make them mobile-friendly --- ![image](https://github.com/go-gitea/gitea/assets/24977596/6713da97-201f-4217-8588-4c4cec157171) ![image](https://github.com/go-gitea/gitea/assets/24977596/3a45aba8-26b5-4e6a-b97d-68bfc2bf9024) --- *Sponsored by Kithara Software GmbH*
Diffstat (limited to 'models/issues/tracked_time.go')
-rw-r--r--models/issues/tracked_time.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go
index 89c99c5cd1..795bddeb34 100644
--- a/models/issues/tracked_time.go
+++ b/models/issues/tracked_time.go
@@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
+ "xorm.io/xorm"
)
// TrackedTime represents a time that was spent for a specific issue.
@@ -325,3 +326,46 @@ func GetTrackedTimeByID(ctx context.Context, id int64) (*TrackedTime, error) {
}
return time, nil
}
+
+// GetIssueTotalTrackedTime returns the total tracked time for issues by given conditions.
+func GetIssueTotalTrackedTime(ctx context.Context, opts *IssuesOptions, isClosed bool) (int64, error) {
+ if len(opts.IssueIDs) <= MaxQueryParameters {
+ return getIssueTotalTrackedTimeChunk(ctx, opts, isClosed, opts.IssueIDs)
+ }
+
+ // If too long a list of IDs is provided,
+ // we get the statistics in smaller chunks and get accumulates
+ var accum int64
+ for i := 0; i < len(opts.IssueIDs); {
+ chunk := i + MaxQueryParameters
+ if chunk > len(opts.IssueIDs) {
+ chunk = len(opts.IssueIDs)
+ }
+ time, err := getIssueTotalTrackedTimeChunk(ctx, opts, isClosed, opts.IssueIDs[i:chunk])
+ if err != nil {
+ return 0, err
+ }
+ accum += time
+ i = chunk
+ }
+ return accum, nil
+}
+
+func getIssueTotalTrackedTimeChunk(ctx context.Context, opts *IssuesOptions, isClosed bool, issueIDs []int64) (int64, error) {
+ sumSession := func(opts *IssuesOptions, issueIDs []int64) *xorm.Session {
+ sess := db.GetEngine(ctx).
+ Table("tracked_time").
+ Where("tracked_time.deleted = ?", false).
+ Join("INNER", "issue", "tracked_time.issue_id = issue.id")
+
+ return applyIssuesOptions(sess, opts, issueIDs)
+ }
+
+ type trackedTime struct {
+ Time int64
+ }
+
+ return sumSession(opts, issueIDs).
+ And("issue.is_closed = ?", isClosed).
+ SumInt(new(trackedTime), "tracked_time.time")
+}