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 all commits
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
59 changes: 55 additions & 4 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +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")
sectionStrings = p.Strings(vals, templateInsightsSectionAsStrings(codersdk.TemplateInsightsSectionIntervalReports, codersdk.TemplateInsightsSectionReport), "sections")
)
p.ErrorExcessParams(vals)
if len(p.Errors) > 0 {
Expand All @@ -275,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 All @@ -289,7 +294,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
// overhead from a transaction is not worth it.
eg.Go(func() error {
var err error
if interval != "" {
if interval != "" && slices.Contains(sections, codersdk.TemplateInsightsSectionIntervalReports) {
dailyUsage, err = api.Database.GetTemplateInsightsByInterval(egCtx, database.GetTemplateInsightsByIntervalParams{
StartTime: startTime,
EndTime: endTime,
Expand All @@ -303,6 +308,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
return nil
})
eg.Go(func() error {
if !slices.Contains(sections, codersdk.TemplateInsightsSectionReport) {
return nil
}

var err error
usage, err = api.Database.GetTemplateInsights(egCtx, database.GetTemplateInsightsParams{
StartTime: startTime,
Expand All @@ -315,6 +324,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
return nil
})
eg.Go(func() error {
if !slices.Contains(sections, codersdk.TemplateInsightsSectionReport) {
return nil
}

var err error
appUsage, err = api.Database.GetTemplateAppInsights(egCtx, database.GetTemplateAppInsightsParams{
StartTime: startTime,
Expand All @@ -330,6 +343,10 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
// Template parameter insights have no risk of inconsistency with the other
// insights.
eg.Go(func() error {
if !slices.Contains(sections, codersdk.TemplateInsightsSectionReport) {
return nil
}

var err error
parameterRows, err = api.Database.GetTemplateParameterInsights(ctx, database.GetTemplateParameterInsightsParams{
StartTime: startTime,
Expand Down Expand Up @@ -365,16 +382,20 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
}

resp := codersdk.TemplateInsightsResponse{
Report: codersdk.TemplateInsightsReport{
IntervalReports: []codersdk.TemplateInsightsIntervalReport{},
}

if slices.Contains(sections, codersdk.TemplateInsightsSectionReport) {
resp.Report = &codersdk.TemplateInsightsReport{
StartTime: startTime,
EndTime: endTime,
TemplateIDs: convertTemplateInsightsTemplateIDs(usage, appUsage),
ActiveUsers: convertTemplateInsightsActiveUsers(usage, appUsage),
AppsUsage: convertTemplateInsightsApps(usage, appUsage),
ParametersUsage: parametersUsage,
},
IntervalReports: []codersdk.TemplateInsightsIntervalReport{},
}
}

for _, row := range dailyUsage {
resp.IntervalReports = append(resp.IntervalReports, codersdk.TemplateInsightsIntervalReport{
// NOTE(mafredri): This might not be accurate over DST since the
Expand Down Expand Up @@ -654,3 +675,33 @@ func lastReportIntervalHasAtLeastSixDays(startTime, endTime time.Time) bool {
// when the duration can be shorter than 6 days: 5 days 23 hours.
return lastReportIntervalDays >= 6*24*time.Hour || startTime.AddDate(0, 0, 6).Equal(endTime)
}

func templateInsightsSectionAsStrings(sections ...codersdk.TemplateInsightsSection) []string {
t := make([]string, len(sections))
for i, s := range sections {
t[i] = string(s)
}
return t
}

func parseTemplateInsightsSections(ctx context.Context, rw http.ResponseWriter, sections []string) ([]codersdk.TemplateInsightsSection, bool) {
t := make([]codersdk.TemplateInsightsSection, len(sections))
for i, s := range sections {
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, true
}
32 changes: 32 additions & 0 deletions coderd/insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,30 @@ func TestTemplateInsights_Golden(t *testing.T) {
}
},
},
{
name: "three weeks second template only report",
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
return codersdk.TemplateInsightsRequest{
TemplateIDs: []uuid.UUID{templates[1].id},
StartTime: frozenWeekAgo.AddDate(0, 0, -14),
EndTime: frozenWeekAgo.AddDate(0, 0, 7),
Interval: codersdk.InsightsReportIntervalWeek,
Sections: []codersdk.TemplateInsightsSection{codersdk.TemplateInsightsSectionReport},
}
},
},
{
name: "three weeks second template only interval reports",
makeRequest: func(templates []*testTemplate) codersdk.TemplateInsightsRequest {
return codersdk.TemplateInsightsRequest{
TemplateIDs: []uuid.UUID{templates[1].id},
StartTime: frozenWeekAgo.AddDate(0, 0, -14),
EndTime: frozenWeekAgo.AddDate(0, 0, 7),
Interval: codersdk.InsightsReportIntervalWeek,
Sections: []codersdk.TemplateInsightsSection{codersdk.TemplateInsightsSectionIntervalReports},
}
},
},
},
},
{
Expand Down Expand Up @@ -2049,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"interval_reports": [
{
"start_time": "2023-08-01T00:00:00Z",
"end_time": "2023-08-08T00:00:00Z",
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"interval": "week",
"active_users": 1
},
{
"start_time": "2023-08-08T00:00:00Z",
"end_time": "2023-08-15T00:00:00Z",
"template_ids": [],
"interval": "week",
"active_users": 0
},
{
"start_time": "2023-08-15T00:00:00Z",
"end_time": "2023-08-22T00:00:00Z",
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"interval": "week",
"active_users": 1
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"report": {
"start_time": "2023-08-01T00:00:00Z",
"end_time": "2023-08-22T00:00:00Z",
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"active_users": 1,
"apps_usage": [
{
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"type": "builtin",
"display_name": "Visual Studio Code",
"slug": "vscode",
"icon": "/icon/code.svg",
"seconds": 3600
},
{
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"type": "builtin",
"display_name": "JetBrains",
"slug": "jetbrains",
"icon": "/icon/intellij.svg",
"seconds": 0
},
{
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"type": "builtin",
"display_name": "Web Terminal",
"slug": "reconnecting-pty",
"icon": "/icon/terminal.svg",
"seconds": 7200
},
{
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"type": "builtin",
"display_name": "SSH",
"slug": "ssh",
"icon": "/icon/terminal.svg",
"seconds": 10800
},
{
"template_ids": [
"00000000-0000-0000-0000-000000000002"
],
"type": "app",
"display_name": "app1",
"slug": "app1",
"icon": "/icon1.png",
"seconds": 21600
}
],
"parameters_usage": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@
}
],
"parameters_usage": []
},
"interval_reports": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,5 @@
]
}
]
},
"interval_reports": []
}
}
29 changes: 23 additions & 6 deletions codersdk/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ const (
InsightsReportIntervalWeek InsightsReportInterval = "week"
)

// TemplateInsightsSection defines the section to be included in the template insights response.
type TemplateInsightsSection string

// TemplateInsightsSection enums.
const (
TemplateInsightsSectionIntervalReports TemplateInsightsSection = "interval_reports"
TemplateInsightsSectionReport TemplateInsightsSection = "report"
)

// UserLatencyInsightsResponse is the response from the user latency insights
// endpoint.
type UserLatencyInsightsResponse struct {
Expand Down Expand Up @@ -158,8 +167,8 @@ func (c *Client) UserActivityInsights(ctx context.Context, req UserActivityInsig

// TemplateInsightsResponse is the response from the template insights endpoint.
type TemplateInsightsResponse struct {
Report TemplateInsightsReport `json:"report"`
IntervalReports []TemplateInsightsIntervalReport `json:"interval_reports"`
Report *TemplateInsightsReport `json:"report,omitempty"`
IntervalReports []TemplateInsightsIntervalReport `json:"interval_reports,omitempty"`
}

// TemplateInsightsReport is the report from the template insights endpoint.
Expand Down Expand Up @@ -221,10 +230,11 @@ type TemplateParameterValue struct {
}

type TemplateInsightsRequest struct {
StartTime time.Time `json:"start_time" format:"date-time"`
EndTime time.Time `json:"end_time" format:"date-time"`
TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"`
Interval InsightsReportInterval `json:"interval" example:"day"`
StartTime time.Time `json:"start_time" format:"date-time"`
EndTime time.Time `json:"end_time" format:"date-time"`
TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"`
Interval InsightsReportInterval `json:"interval" example:"day"`
Sections []TemplateInsightsSection `json:"sections" example:"report"`
}

func (c *Client) TemplateInsights(ctx context.Context, req TemplateInsightsRequest) (TemplateInsightsResponse, error) {
Expand All @@ -241,6 +251,13 @@ func (c *Client) TemplateInsights(ctx context.Context, req TemplateInsightsReque
if req.Interval != "" {
qp.Add("interval", string(req.Interval))
}
if len(req.Sections) > 0 {
var sections []string
for _, sec := range req.Sections {
sections = append(sections, string(sec))
}
qp.Add("sections", strings.Join(sections, ","))
}

reqURL := fmt.Sprintf("/api/v2/insights/templates?%s", qp.Encode())
resp, err := c.Request(ctx, http.MethodGet, reqURL, nil)
Expand Down
12 changes: 10 additions & 2 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading