Skip to content

feat!: add sections parameter to template insights #10010

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 6 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add parseTemplateInsightsSections
  • Loading branch information
mtojek committed Oct 3, 2023
commit c330f0bb8237b40a9acb4f9dd10f66a3014be021
29 changes: 22 additions & 7 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
endTimeString = p.String(vals, "", "end_time")
intervalString = p.String(vals, "", "interval")
templateIDs = p.UUIDs(vals, []uuid.UUID{}, "template_ids")
sections = stringsAsTemplateInsightsSections(
p.Strings(vals,
templateInsightsSectionAsStrings(codersdk.TemplateInsightsSectionIntervalReports, codersdk.TemplateInsightsSectionReport),
"sections")...)
sectionStrings = p.Strings(vals, templateInsightsSectionAsStrings(codersdk.TemplateInsightsSectionIntervalReports, codersdk.TemplateInsightsSectionReport), "sections")
)
p.ErrorExcessParams(vals)
if len(p.Errors) > 0 {
Expand All @@ -279,6 +276,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
if !ok {
return
}
sections, ok := parseTemplateInsightsSections(ctx, rw, sectionStrings)
if !ok {
return
}

var usage database.GetTemplateInsightsRow
var appUsage []database.GetTemplateAppInsightsRow
Expand Down Expand Up @@ -683,10 +684,24 @@ func templateInsightsSectionAsStrings(sections ...codersdk.TemplateInsightsSecti
return t
}

func stringsAsTemplateInsightsSections(sections ...string) []codersdk.TemplateInsightsSection {
func parseTemplateInsightsSections(ctx context.Context, rw http.ResponseWriter, sections []string) ([]codersdk.TemplateInsightsSection, bool) {
t := make([]codersdk.TemplateInsightsSection, len(sections))
for i, s := range sections {
t[i] = codersdk.TemplateInsightsSection(s)
switch v := codersdk.TemplateInsightsSection(s); v {
case codersdk.TemplateInsightsSectionIntervalReports, codersdk.TemplateInsightsSectionReport:
t[i] = v
default:
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
Message: "Query parameter has invalid value.",
Validations: []codersdk.ValidationError{
{
Field: "sections",
Detail: fmt.Sprintf("must be one of %v", []codersdk.TemplateInsightsSection{codersdk.TemplateInsightsSectionIntervalReports, codersdk.TemplateInsightsSectionReport}),
},
},
})
return nil, false
}
}
return t
return t, true
}
8 changes: 8 additions & 0 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,14 @@ func TestTemplateInsights_BadRequest(t *testing.T) {
Interval: codersdk.InsightsReportIntervalWeek,
})
assert.Error(t, err, "last report interval must have at least 6 days")

_, err = client.TemplateInsights(ctx, codersdk.TemplateInsightsRequest{
StartTime: today.AddDate(0, 0, -1),
EndTime: today,
Interval: codersdk.InsightsReportIntervalWeek,
Sections: []codersdk.TemplateInsightsSection{"invalid"},
})
assert.Error(t, err, "want error for bad section")
}

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