Skip to content

Commit 45bb69c

Browse files
committed
fix parsing request
1 parent 871dd31 commit 45bb69c

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

coderd/insights.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
// Duplicated in codersdk.
2424
const insightsTimeLayout = time.RFC3339
2525

26+
// Day duration in nanoseconds
27+
var dayNanoseconds = 24 * time.Hour.Nanoseconds()
28+
2629
// @Summary Get deployment DAUs
2730
// @ID get-deployment-daus
2831
// @Security CoderSessionToken
@@ -184,7 +187,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
184187
if !ok {
185188
return
186189
}
187-
interval, ok := verifyInsightsInterval(ctx, rw, intervalString)
190+
interval, ok := parseInsightsInterval(ctx, rw, intervalString, startTime, endTime)
188191
if !ok {
189192
return
190193
}
@@ -198,7 +201,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
198201
eg.SetLimit(4)
199202

200203
// The following insights data queries have a theoretical chance to be
201-
// inconsistent between eachother when looking at "today", however, the
204+
// inconsistent between each other when looking at "today", however, the
202205
// overhead from a transaction is not worth it.
203206
eg.Go(func() error {
204207
var err error
@@ -207,7 +210,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
207210
StartTime: startTime,
208211
EndTime: endTime,
209212
TemplateIDs: templateIDs,
210-
IntervalDays: 1,
213+
IntervalDays: interval.Days(),
211214
})
212215
if err != nil {
213216
return xerrors.Errorf("get template daily insights: %w", err)
@@ -531,9 +534,18 @@ func parseInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, s
531534
return startTime, endTime, true
532535
}
533536

534-
func verifyInsightsInterval(ctx context.Context, rw http.ResponseWriter, intervalString string) (codersdk.InsightsReportInterval, bool) {
537+
func parseInsightsInterval(ctx context.Context, rw http.ResponseWriter, intervalString string, startTime, endTime time.Time) (codersdk.InsightsReportInterval, bool) {
535538
switch v := codersdk.InsightsReportInterval(intervalString); v {
536-
case codersdk.InsightsReportIntervalDay, codersdk.InsightsReportIntervalWeek, "":
539+
case codersdk.InsightsReportIntervalDay, "":
540+
return v, true
541+
case codersdk.InsightsReportIntervalWeek:
542+
if !isMultipleOfDay(startTime, endTime) {
543+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
544+
Message: "Query parameter has invalid value.",
545+
Detail: "Duration between start_time and end_time must multiple of 1 day.",
546+
})
547+
return "", false
548+
}
537549
return v, true
538550
default:
539551
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
@@ -548,3 +560,7 @@ func verifyInsightsInterval(ctx context.Context, rw http.ResponseWriter, interva
548560
return "", false
549561
}
550562
}
563+
564+
func isMultipleOfDay(startTime, endTime time.Time) bool {
565+
return endTime.Sub(startTime).Nanoseconds()%dayNanoseconds == 0
566+
}

codersdk/insights.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ const insightsTimeLayout = time.RFC3339
2020
// smaller insights report within a time range.
2121
type InsightsReportInterval string
2222

23+
// Days returns the duration of the interval in days.
24+
func (interval InsightsReportInterval) Days() int32 {
25+
switch interval {
26+
case InsightsReportIntervalDay:
27+
return 1
28+
case InsightsReportIntervalWeek:
29+
return 7
30+
default:
31+
panic("developer error: unsupported report interval")
32+
}
33+
}
34+
2335
// InsightsReportInterval enums.
2436
const (
2537
InsightsReportIntervalDay InsightsReportInterval = "day"

0 commit comments

Comments
 (0)