Skip to content

Commit 22a91a4

Browse files
committed
Use IntervalDays
1 parent e957d16 commit 22a91a4

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

coderd/database/dbfake/dbfake.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,9 +2424,11 @@ func (q *FakeQuerier) GetTemplateInsightsByInterval(ctx context.Context, arg dat
24242424
templateIDSet map[uuid.UUID]struct{}
24252425
}
24262426

2427-
statsByInterval := []statByInterval{{arg.StartTime, arg.StartTime.Add(time.Duration(arg.Interval)), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})}}
2427+
interval := time.Duration(arg.IntervalDays) * 24 * time.Hour
2428+
2429+
statsByInterval := []statByInterval{{arg.StartTime, arg.StartTime.Add(interval), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})}}
24282430
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(time.Duration(arg.Interval)), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})})
2431+
statsByInterval = append(statsByInterval, statByInterval{statsByInterval[len(statsByInterval)-1].endTime, statsByInterval[len(statsByInterval)-1].endTime.Add(interval), make(map[uuid.UUID]struct{}), make(map[uuid.UUID]struct{})})
24302432
}
24312433
if statsByInterval[len(statsByInterval)-1].endTime.After(arg.EndTime) {
24322434
statsByInterval[len(statsByInterval)-1].endTime = arg.EndTime

coderd/database/queries.sql.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/insights.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ WITH ts AS (
122122
SELECT
123123
d::timestamptz AS from_,
124124
CASE
125-
WHEN (d::timestamptz + @interval::interval) <= @end_time::timestamptz
126-
THEN (d::timestamptz + @interval::interval)
125+
WHEN (d::timestamptz + (@interval_days::int || ' day')::interval) <= @end_time::timestamptz
126+
THEN (d::timestamptz + (@interval_days::int || ' day')::interval)
127127
ELSE @end_time::timestamptz
128128
END AS to_
129129
FROM
130130
-- Subtract 1 second from end_time to avoid including the next interval in the results.
131-
generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 second'::interval, @interval::interval) AS d
131+
generate_series(@start_time::timestamptz, (@end_time::timestamptz) - '1 second'::interval, (@interval_days::int || ' day')::interval) AS d
132132
), unflattened_usage_by_interval AS (
133133
-- We select data from both workspace agent stats and workspace app stats to
134134
-- get a complete picture of usage. This matches how usage is calculated by

coderd/insights.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
204204
var err error
205205
if interval != "" {
206206
dailyUsage, err = api.Database.GetTemplateInsightsByInterval(egCtx, database.GetTemplateInsightsByIntervalParams{
207-
StartTime: startTime,
208-
EndTime: endTime,
209-
TemplateIDs: templateIDs,
210-
Interval: int64(24 * time.Hour),
207+
StartTime: startTime,
208+
EndTime: endTime,
209+
TemplateIDs: templateIDs,
210+
IntervalDays: 1,
211211
})
212212
if err != nil {
213213
return xerrors.Errorf("get template daily insights: %w", err)

0 commit comments

Comments
 (0)