Skip to content

Commit d4ffac5

Browse files
committed
feat(coderd): add parameter insights to template insights
Part of #8514 Refs #8109
1 parent b03cc29 commit d4ffac5

File tree

14 files changed

+505
-32
lines changed

14 files changed

+505
-32
lines changed

coderd/apidoc/docs.go

+47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbauthz/dbauthz.go

+8
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,14 @@ func (q *querier) GetTemplateInsights(ctx context.Context, arg database.GetTempl
11891189
return q.db.GetTemplateInsights(ctx, arg)
11901190
}
11911191

1192+
func (q *querier) GetTemplateParameterInsights(ctx context.Context, arg database.GetTemplateParameterInsightsParams) ([]database.GetTemplateParameterInsightsRow, error) {
1193+
// FIXME: this should maybe be READ rbac.ResourceTemplate or it's own resource.
1194+
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
1195+
return nil, err
1196+
}
1197+
return q.db.GetTemplateParameterInsights(ctx, arg)
1198+
}
1199+
11921200
func (q *querier) GetTemplateVersionByID(ctx context.Context, tvid uuid.UUID) (database.TemplateVersion, error) {
11931201
tv, err := q.db.GetTemplateVersionByID(ctx, tvid)
11941202
if err != nil {

coderd/database/dbfake/dbfake.go

+9
Original file line numberDiff line numberDiff line change
@@ -2067,6 +2067,15 @@ func (q *FakeQuerier) GetTemplateInsights(_ context.Context, arg database.GetTem
20672067
return result, nil
20682068
}
20692069

2070+
func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg database.GetTemplateParameterInsightsParams) ([]database.GetTemplateParameterInsightsRow, error) {
2071+
err := validateDatabaseType(arg)
2072+
if err != nil {
2073+
return nil, err
2074+
}
2075+
2076+
panic("not implemented")
2077+
}
2078+
20702079
func (q *FakeQuerier) GetTemplateVersionByID(ctx context.Context, templateVersionID uuid.UUID) (database.TemplateVersion, error) {
20712080
q.mutex.RLock()
20722081
defer q.mutex.RUnlock()

coderd/database/dbmetrics/dbmetrics.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+102
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/insights.sql

+53
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,56 @@ SELECT
103103
COUNT(DISTINCT user_id) AS active_users
104104
FROM usage_by_day, unnest(template_ids) as template_id
105105
GROUP BY from_, to_;
106+
107+
108+
-- name: GetTemplateParameterInsights :many
109+
-- GetTemplateParameterInsights does for each template in a given timeframe,
110+
-- look for the latest workspace build (for every workspace) that has been
111+
-- created in the timeframe and return the aggregate usage counts of parameter
112+
-- values.
113+
WITH latest_workspace_builds AS (
114+
SELECT
115+
wb.id,
116+
wbmax.template_id,
117+
wb.template_version_id
118+
FROM (
119+
SELECT
120+
tv.template_id, wbmax.workspace_id, MAX(wbmax.build_number) as max_build_number
121+
FROM workspace_builds wbmax
122+
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
123+
WHERE
124+
wbmax.created_at >= @start_time::timestamptz
125+
AND wbmax.created_at < @end_time::timestamptz
126+
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN tv.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
127+
GROUP BY tv.template_id, wbmax.workspace_id
128+
) wbmax
129+
JOIN workspace_builds wb ON (
130+
wb.workspace_id = wbmax.workspace_id
131+
AND wb.build_number = wbmax.max_build_number
132+
)
133+
), unique_template_params AS (
134+
SELECT
135+
ROW_NUMBER() OVER () AS num,
136+
array_agg(DISTINCT wb.template_id)::uuid[] AS template_ids,
137+
array_agg(wb.id)::uuid[] AS workspace_build_ids,
138+
tvp.name,
139+
tvp.display_name,
140+
tvp.description,
141+
tvp.options
142+
FROM latest_workspace_builds wb
143+
JOIN template_version_parameters tvp ON (tvp.template_version_id = wb.template_version_id)
144+
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options
145+
)
146+
147+
SELECT
148+
utp.num,
149+
utp.template_ids,
150+
utp.name,
151+
utp.display_name,
152+
utp.description,
153+
utp.options,
154+
wbp.value,
155+
COUNT(wbp.value) AS count
156+
FROM unique_template_params utp
157+
JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.workspace_build_id] AND utp.name = wbp.name)
158+
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, wbp.value;

0 commit comments

Comments
 (0)