|
1 | 1 | package coderd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | +"context" |
4 | 5 | "fmt"
|
5 | 6 | "net/http"
|
6 | 7 | "time"
|
@@ -89,6 +90,10 @@ func (api *API) insightsUserLatency(rw http.ResponseWriter, r *http.Request) {
|
89 | 90 | return
|
90 | 91 | }
|
91 | 92 |
|
| 93 | +if!verifyInsightsStartAndEndTime(ctx,rw,startTime,endTime) { |
| 94 | +return |
| 95 | +} |
| 96 | + |
92 | 97 | // Should we verify all template IDs exist, or just return no rows?
|
93 | 98 | // _, err := api.Database.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{
|
94 | 99 | // IDs: templateIDs,
|
@@ -201,6 +206,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
|
201 | 206 | return
|
202 | 207 | }
|
203 | 208 |
|
| 209 | +if!verifyInsightsStartAndEndTime(ctx,rw,startTime,endTime) { |
| 210 | +return |
| 211 | +} |
| 212 | + |
204 | 213 | // Should we verify all template IDs exist, or just return no rows?
|
205 | 214 | // _, err := api.Database.GetTemplatesWithFilter(ctx, database.GetTemplatesWithFilterParams{
|
206 | 215 | // IDs: templateIDs,
|
@@ -313,3 +322,53 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
|
313 | 322 | }
|
314 | 323 | httpapi.Write(ctx,rw,http.StatusOK,resp)
|
315 | 324 | }
|
| 325 | + |
| 326 | +funcverifyInsightsStartAndEndTime(ctx context.Context,rw http.ResponseWriter,startTime,endTime time.Time)bool { |
| 327 | +for_,v:=range []struct { |
| 328 | +namestring |
| 329 | +t time.Time |
| 330 | +}{ |
| 331 | +{"start_time",startTime}, |
| 332 | +{"end_time",endTime}, |
| 333 | +} { |
| 334 | +ifv.t.IsZero() { |
| 335 | +httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{ |
| 336 | +Message:"Query parameter has invalid value.", |
| 337 | +Validations: []codersdk.ValidationError{ |
| 338 | +{ |
| 339 | +Field:v.name, |
| 340 | +Detail:"must be not be zero", |
| 341 | +}, |
| 342 | +}, |
| 343 | +}) |
| 344 | +returnfalse |
| 345 | +} |
| 346 | +h,m,s:=v.t.Clock() |
| 347 | +ifh!=0||m!=0||s!=0 { |
| 348 | +httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{ |
| 349 | +Message:"Query parameter has invalid value.", |
| 350 | +Validations: []codersdk.ValidationError{ |
| 351 | +{ |
| 352 | +Field:v.name, |
| 353 | +Detail:"clock must be 00:00:00", |
| 354 | +}, |
| 355 | +}, |
| 356 | +}) |
| 357 | +returnfalse |
| 358 | +} |
| 359 | +} |
| 360 | +ifendTime.Before(startTime) { |
| 361 | +httpapi.Write(ctx,rw,http.StatusBadRequest, codersdk.Response{ |
| 362 | +Message:"Query parameter has invalid value.", |
| 363 | +Validations: []codersdk.ValidationError{ |
| 364 | +{ |
| 365 | +Field:"end_time", |
| 366 | +Detail:"must be after start_time", |
| 367 | +}, |
| 368 | +}, |
| 369 | +}) |
| 370 | +returnfalse |
| 371 | +} |
| 372 | + |
| 373 | +returntrue |
| 374 | +} |