Skip to content
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
Next Next commit
Fix frontend
  • Loading branch information
mtojek committed Oct 3, 2023
commit 89733fdee791cf5c81413dbb259cba2741abb4d1
10 changes: 7 additions & 3 deletions coderd/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,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
24 changes: 24 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
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,64 @@
{
"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": []
},
"interval_reports": []
}
18 changes: 13 additions & 5 deletions codersdk/insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ 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"`
Report *TemplateInsightsReport `json:"report,omitempty"`
IntervalReports []TemplateInsightsIntervalReport `json:"interval_reports"`
}

Expand Down Expand Up @@ -248,10 +248,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 @@ -268,6 +269,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
3 changes: 2 additions & 1 deletion site/src/api/typesGenerated.ts

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

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getTemplatePageTitle } from "../utils";
import { Loader } from "components/Loader/Loader";
import {
DAUsResponse,
TemplateAppUsage,
TemplateInsightsResponse,
TemplateParameterUsage,
TemplateParameterValue,
Expand Down Expand Up @@ -105,11 +106,11 @@ export const TemplateInsightsPageView = ({
<UserLatencyPanel data={userLatency} />
<TemplateUsagePanel
sx={{ gridColumn: "span 3" }}
data={templateInsights?.report.apps_usage}
data={templateInsights?.report?.apps_usage}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we not need any change for interval_reports which is now possibly also undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave it for @BrunoQuaresma to polish the FE side.

/>
<TemplateParametersUsagePanel
sx={{ gridColumn: "span 3" }}
data={templateInsights?.report.parameters_usage}
data={templateInsights?.report?.parameters_usage}
/>
</Box>
</>
Expand Down Expand Up @@ -202,7 +203,7 @@ const TemplateUsagePanel = ({
data,
...panelProps
}: PanelProps & {
data: TemplateInsightsResponse["report"]["apps_usage"] | undefined;
data: TemplateAppUsage[] | undefined;
}) => {
const validUsage = data?.filter((u) => u.seconds > 0);
const totalInSeconds =
Expand Down Expand Up @@ -293,7 +294,7 @@ const TemplateParametersUsagePanel = ({
data,
...panelProps
}: PanelProps & {
data: TemplateInsightsResponse["report"]["parameters_usage"] | undefined;
data: TemplateParameterUsage[] | undefined;
}) => {
return (
<Panel {...panelProps}>
Expand Down