Skip to content

Commit 90def67

Browse files
committed
Use interval
1 parent 45e6e95 commit 90def67

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,17 +2418,18 @@ func (q *FakeQuerier) GetTemplateInsightsByInterval(ctx context.Context, arg dat
24182418
q.mutex.RLock()
24192419
defer q.mutex.RUnlock()
24202420

2421-
type dailyStat struct {
2421+
type statByInterval struct {
24222422
startTime, endTime time.Time
24232423
userSet map[uuid.UUID]struct{}
24242424
templateIDSet map[uuid.UUID]struct{}
24252425
}
2426-
dailyStats := []dailyStat{{arg.StartTime, arg.StartTime.AddDate(0, 0, 1), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})}}
2427-
for dailyStats[len(dailyStats)-1].endTime.Before(arg.EndTime) {
2428-
dailyStats = append(dailyStats, dailyStat{dailyStats[len(dailyStats)-1].endTime, dailyStats[len(dailyStats)-1].endTime.AddDate(0, 0, 1), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})})
2426+
2427+
statsByInterval := []statByInterval{{arg.StartTime, arg.StartTime.Add(arg.Interval), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})}}
2428+
for statsByInterval[len(statsByInterval)-1].endTime.Before(arg.EndTime) {
2429+
statsByInterval = append(statsByInterval, statByInterval{statsByInterval[len(statsByInterval)-1].endTime, statsByInterval[len(statsByInterval)-1].endTime.Add(arg.Interval), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})})
24292430
}
2430-
if dailyStats[len(dailyStats)-1].endTime.After(arg.EndTime) {
2431-
dailyStats[len(dailyStats)-1].endTime = arg.EndTime
2431+
if statsByInterval[len(statsByInterval)-1].endTime.After(arg.EndTime) {
2432+
statsByInterval[len(statsByInterval)-1].endTime = arg.EndTime
24322433
}
24332434

24342435
for _, s := range q.workspaceAgentStats {
@@ -2442,7 +2443,7 @@ func (q *FakeQuerier) GetTemplateInsightsByInterval(ctx context.Context, arg dat
24422443
continue
24432444
}
24442445

2445-
for _, ds := range dailyStats {
2446+
for _, ds := range statsByInterval {
24462447
if s.CreatedAt.Before(ds.startTime) || s.CreatedAt.Equal(ds.endTime) || s.CreatedAt.After(ds.endTime) {
24472448
continue
24482449
}
@@ -2461,7 +2462,7 @@ func (q *FakeQuerier) GetTemplateInsightsByInterval(ctx context.Context, arg dat
24612462
continue
24622463
}
24632464

2464-
for _, ds := range dailyStats {
2465+
for _, ds := range statsByInterval {
24652466
// (was.session_started_at >= ts.from_ AND was.session_started_at < ts.to_)
24662467
// OR (was.session_ended_at > ts.from_ AND was.session_ended_at < ts.to_)
24672468
// OR (was.session_started_at < ts.from_ AND was.session_ended_at >= ts.to_)
@@ -2477,7 +2478,7 @@ func (q *FakeQuerier) GetTemplateInsightsByInterval(ctx context.Context, arg dat
24772478
}
24782479

24792480
var result []database.GetTemplateInsightsByIntervalRow
2480-
for _, ds := range dailyStats {
2481+
for _, ds := range statsByInterval {
24812482
templateIDs := make([]uuid.UUID, 0, len(ds.templateIDSet))
24822483
for templateID := range ds.templateIDSet {
24832484
templateIDs = append(templateIDs, templateID)

coderd/insights.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
207207
StartTime: startTime,
208208
EndTime: endTime,
209209
TemplateIDs: templateIDs,
210+
Interval: 24 * time.Hour,
210211
})
211212
if err != nil {
212213
return xerrors.Errorf("get template daily insights: %w", err)

0 commit comments

Comments
 (0)