Skip to content

Commit 5525452

Browse files
guillep2klunnyzeripathlafriks
authored
Divide GetIssueStats query in smaller chunks (#10176) (#10282)
* Divide GetIssueStats query in smaller chunks * Skip chunking if count is low enough * Fix lint * Define maxQueryParameters * Remove absMaxQueryParameters because of lint * Restart CI * Restart CI Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 987cd27 commit 5525452

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

models/issue.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,36 @@ type IssueStatsOptions struct {
13361336

13371337
// GetIssueStats returns issue statistic information by given conditions.
13381338
func GetIssueStats(opts *IssueStatsOptions) (*IssueStats, error) {
1339+
if len(opts.IssueIDs) <= maxQueryParameters {
1340+
return getIssueStatsChunk(opts, opts.IssueIDs)
1341+
}
1342+
1343+
// If too long a list of IDs is provided, we get the statistics in
1344+
// smaller chunks and get accumulates. Note: this could potentially
1345+
// get us invalid results. The alternative is to insert the list of
1346+
// ids in a temporary table and join from them.
1347+
accum := &IssueStats{}
1348+
for i := 0; i < len(opts.IssueIDs); {
1349+
chunk := i + maxQueryParameters
1350+
if chunk > len(opts.IssueIDs) {
1351+
chunk = len(opts.IssueIDs)
1352+
}
1353+
stats, err := getIssueStatsChunk(opts, opts.IssueIDs[i:chunk])
1354+
if err != nil {
1355+
return nil, err
1356+
}
1357+
accum.OpenCount += stats.OpenCount
1358+
accum.ClosedCount += stats.ClosedCount
1359+
accum.YourRepositoriesCount += stats.YourRepositoriesCount
1360+
accum.AssignCount += stats.AssignCount
1361+
accum.CreateCount += stats.CreateCount
1362+
accum.OpenCount += stats.MentionCount
1363+
i = chunk
1364+
}
1365+
return accum, nil
1366+
}
1367+
1368+
func getIssueStatsChunk(opts *IssueStatsOptions, issueIDs []int64) (*IssueStats, error) {
13391369
stats := &IssueStats{}
13401370

13411371
countSession := func(opts *IssueStatsOptions) *xorm.Session {

models/models.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ type Engine interface {
4646
Asc(colNames ...string) *xorm.Session
4747
}
4848

49+
const (
50+
// When queries are broken down in parts because of the number
51+
// of parameters, attempt to break by this amount
52+
maxQueryParameters = 300
53+
)
54+
4955
var (
5056
x *xorm.Engine
5157
tables []interface{}

0 commit comments

Comments
 (0)