@@ -23,6 +23,9 @@ import (
23
23
// Duplicated in codersdk.
24
24
const insightsTimeLayout = time .RFC3339
25
25
26
+ // Day duration in nanoseconds
27
+ var dayNanoseconds = 24 * time .Hour .Nanoseconds ()
28
+
26
29
// @Summary Get deployment DAUs
27
30
// @ID get-deployment-daus
28
31
// @Security CoderSessionToken
@@ -184,7 +187,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
184
187
if ! ok {
185
188
return
186
189
}
187
- interval , ok := verifyInsightsInterval (ctx , rw , intervalString )
190
+ interval , ok := parseInsightsInterval (ctx , rw , intervalString , startTime , endTime )
188
191
if ! ok {
189
192
return
190
193
}
@@ -198,7 +201,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
198
201
eg .SetLimit (4 )
199
202
200
203
// 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
202
205
// overhead from a transaction is not worth it.
203
206
eg .Go (func () error {
204
207
var err error
@@ -207,7 +210,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
207
210
StartTime : startTime ,
208
211
EndTime : endTime ,
209
212
TemplateIDs : templateIDs ,
210
- IntervalDays : 1 ,
213
+ IntervalDays : interval . Days () ,
211
214
})
212
215
if err != nil {
213
216
return xerrors .Errorf ("get template daily insights: %w" , err )
@@ -531,9 +534,18 @@ func parseInsightsStartAndEndTime(ctx context.Context, rw http.ResponseWriter, s
531
534
return startTime , endTime , true
532
535
}
533
536
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 ) {
535
538
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
+ }
537
549
return v , true
538
550
default :
539
551
httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
@@ -548,3 +560,7 @@ func verifyInsightsInterval(ctx context.Context, rw http.ResponseWriter, interva
548
560
return "" , false
549
561
}
550
562
}
563
+
564
+ func isMultipleOfDay (startTime , endTime time.Time ) bool {
565
+ return endTime .Sub (startTime ).Nanoseconds ()% dayNanoseconds == 0
566
+ }
0 commit comments