Skip to content

feat(coderd): support weekly aggregated insights #9684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 19, 2023
Prev Previous commit
Next Next commit
test: BadRequest
  • Loading branch information
mtojek committed Sep 15, 2023
commit cee514d51e9f39a4f18ab08f1064ab360cf8dff0
12 changes: 6 additions & 6 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
// Duplicated in codersdk.
const insightsTimeLayout = time.RFC3339

// Day duration in nanoseconds
var dayNanoseconds = 24 * time.Hour.Nanoseconds()
// Week duration in nanoseconds
var weekNanoseconds = 7 * 24 * time.Hour.Nanoseconds()

// @Summary Get deployment DAUs
// @ID get-deployment-daus
Expand Down Expand Up @@ -539,10 +539,10 @@ func parseInsightsInterval(ctx context.Context, rw http.ResponseWriter, interval
case codersdk.InsightsReportIntervalDay, "":
return v, true
case codersdk.InsightsReportIntervalWeek:
if !isMultipleOfDay(startTime, endTime) {
if !isMultipleOfWeek(startTime, endTime) {
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Query parameter has invalid value.",
Detail: "Duration between start_time and end_time must multiple of 1 day.",
Detail: "Duration between start_time and end_time must multiple of 7 days.",
})
return "", false
}
Expand All @@ -561,6 +561,6 @@ func parseInsightsInterval(ctx context.Context, rw http.ResponseWriter, interval
}
}

func isMultipleOfDay(startTime, endTime time.Time) bool {
return endTime.Sub(startTime).Nanoseconds()%dayNanoseconds == 0
func isMultipleOfWeek(startTime, endTime time.Time) bool {
return endTime.Sub(startTime).Nanoseconds()%weekNanoseconds == 0
}
7 changes: 7 additions & 0 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,13 @@ func TestTemplateInsights_BadRequest(t *testing.T) {
Interval: "invalid",
})
assert.Error(t, err, "want error for bad interval")

_, err = client.TemplateInsights(ctx, codersdk.TemplateInsightsRequest{
StartTime: today.AddDate(0, 0, -4),
EndTime: today,
Interval: codersdk.InsightsReportIntervalWeek,
})
assert.Error(t, err, "start_time and end_time must be multiple of 7 days")
}

func TestTemplateInsights_RBAC(t *testing.T) {
Expand Down