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
At least 6 days
  • Loading branch information
mtojek committed Sep 15, 2023
commit 60e8db396ba543efbdf66a00c91340e467a60148
15 changes: 8 additions & 7 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ import (
// Duplicated in codersdk.
const insightsTimeLayout = time.RFC3339

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

// @Summary Get deployment DAUs
// @ID get-deployment-daus
// @Security CoderSessionToken
Expand Down Expand Up @@ -539,10 +536,10 @@ func parseInsightsInterval(ctx context.Context, rw http.ResponseWriter, interval
case codersdk.InsightsReportIntervalDay, "":
return v, true
case codersdk.InsightsReportIntervalWeek:
if !isMultipleOfWeek(startTime, endTime) {
if !lastReportIntervalHasAtLeastSixDays(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 7 days.",
Detail: "Last report interval should have at least 6 days.",
})
return "", false
}
Expand All @@ -561,6 +558,10 @@ func parseInsightsInterval(ctx context.Context, rw http.ResponseWriter, interval
}
}

func isMultipleOfWeek(startTime, endTime time.Time) bool {
return endTime.Sub(startTime).Nanoseconds()%weekNanoseconds == 0
func lastReportIntervalHasAtLeastSixDays(startTime, endTime time.Time) bool {
lastReportIntervalDays := endTime.Sub(startTime) % (7 * 24 * time.Hour)
if lastReportIntervalDays == 0 {
return true // this is a perfectly full week!
}
return lastReportIntervalDays >= 6*24*time.Hour
}
4 changes: 2 additions & 2 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,11 @@ func TestTemplateInsights_BadRequest(t *testing.T) {
assert.Error(t, err, "want error for bad interval")

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

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