Skip to content

Commit e862719

Browse files
authored
feat(coderd): expose parameter description and type (#8944)
1 parent 00be8ab commit e862719

File tree

12 files changed

+108
-53
lines changed

12 files changed

+108
-53
lines changed

coderd/apidoc/docs.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/db2sdk/db2sdk.go

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package db2sdk
33

44
import (
55
"encoding/json"
6+
"sort"
67

78
"github.com/google/uuid"
89

@@ -29,20 +30,10 @@ func WorkspaceBuildParameter(p database.WorkspaceBuildParameter) codersdk.Worksp
2930
}
3031

3132
func TemplateVersionParameter(param database.TemplateVersionParameter) (codersdk.TemplateVersionParameter, error) {
32-
var protoOptions []*proto.RichParameterOption
33-
err := json.Unmarshal(param.Options, &protoOptions)
33+
options, err := templateVersionParameterOptions(param.Options)
3434
if err != nil {
3535
return codersdk.TemplateVersionParameter{}, err
3636
}
37-
options := make([]codersdk.TemplateVersionParameterOption, 0)
38-
for _, option := range protoOptions {
39-
options = append(options, codersdk.TemplateVersionParameterOption{
40-
Name: option.Name,
41-
Description: option.Description,
42-
Value: option.Value,
43-
Icon: option.Icon,
44-
})
45-
}
4637

4738
descriptionPlaintext, err := parameter.Plaintext(param.Description)
4839
if err != nil {
@@ -132,3 +123,61 @@ func Role(role rbac.Role) codersdk.Role {
132123
Name: role.Name,
133124
}
134125
}
126+
127+
func TemplateInsightsParameters(parameterRows []database.GetTemplateParameterInsightsRow) ([]codersdk.TemplateParameterUsage, error) {
128+
parametersByNum := make(map[int64]*codersdk.TemplateParameterUsage)
129+
for _, param := range parameterRows {
130+
if _, ok := parametersByNum[param.Num]; !ok {
131+
var opts []codersdk.TemplateVersionParameterOption
132+
err := json.Unmarshal(param.Options, &opts)
133+
if err != nil {
134+
return nil, err
135+
}
136+
137+
plaintextDescription, err := parameter.Plaintext(param.Description)
138+
if err != nil {
139+
return nil, err
140+
}
141+
142+
parametersByNum[param.Num] = &codersdk.TemplateParameterUsage{
143+
TemplateIDs: param.TemplateIDs,
144+
Name: param.Name,
145+
Type: param.Type,
146+
DisplayName: param.DisplayName,
147+
Description: plaintextDescription,
148+
Options: opts,
149+
}
150+
}
151+
parametersByNum[param.Num].Values = append(parametersByNum[param.Num].Values, codersdk.TemplateParameterValue{
152+
Value: param.Value,
153+
Count: param.Count,
154+
})
155+
}
156+
parametersUsage := []codersdk.TemplateParameterUsage{}
157+
for _, param := range parametersByNum {
158+
parametersUsage = append(parametersUsage, *param)
159+
}
160+
161+
sort.Slice(parametersUsage, func(i, j int) bool {
162+
return parametersUsage[i].Name < parametersUsage[j].Name
163+
})
164+
return parametersUsage, nil
165+
}
166+
167+
func templateVersionParameterOptions(rawOptions json.RawMessage) ([]codersdk.TemplateVersionParameterOption, error) {
168+
var protoOptions []*proto.RichParameterOption
169+
err := json.Unmarshal(rawOptions, &protoOptions)
170+
if err != nil {
171+
return nil, err
172+
}
173+
var options []codersdk.TemplateVersionParameterOption
174+
for _, option := range protoOptions {
175+
options = append(options, codersdk.TemplateVersionParameterOption{
176+
Name: option.Name,
177+
Description: option.Description,
178+
Value: option.Value,
179+
Icon: option.Icon,
180+
})
181+
}
182+
return options, nil
183+
}

coderd/database/dbfake/dbfake.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2186,6 +2186,7 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
21862186
uniqueTemplateParams[key] = &database.GetTemplateParameterInsightsRow{
21872187
Num: num,
21882188
Name: tvp.Name,
2189+
Type: tvp.Type,
21892190
DisplayName: tvp.DisplayName,
21902191
Description: tvp.Description,
21912192
Options: tvp.Options,
@@ -2220,6 +2221,7 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
22202221
TemplateIDs: uniqueSortedUUIDs(utp.TemplateIDs),
22212222
Name: utp.Name,
22222223
DisplayName: utp.DisplayName,
2224+
Type: utp.Type,
22232225
Description: utp.Description,
22242226
Options: utp.Options,
22252227
Value: value,

coderd/database/queries.sql.go

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/insights.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,23 @@ WITH latest_workspace_builds AS (
149149
tvp.name,
150150
tvp.display_name,
151151
tvp.description,
152-
tvp.options
152+
tvp.options,
153+
tvp.type
153154
FROM latest_workspace_builds wb
154155
JOIN template_version_parameters tvp ON (tvp.template_version_id = wb.template_version_id)
155-
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options
156+
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options, tvp.type
156157
)
157158

158159
SELECT
159160
utp.num,
160161
utp.template_ids,
161162
utp.name,
163+
utp.type,
162164
utp.display_name,
163165
utp.description,
164166
utp.options,
165167
wbp.value,
166168
COUNT(wbp.value) AS count
167169
FROM unique_template_params utp
168170
JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.workspace_build_id] AND utp.name = wbp.name)
169-
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, wbp.value;
171+
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, utp.type, wbp.value;

coderd/insights.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package coderd
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"net/http"
8-
"sort"
97
"time"
108

119
"github.com/google/uuid"
1210
"golang.org/x/exp/slices"
1311
"golang.org/x/xerrors"
1412

1513
"github.com/coder/coder/coderd/database"
14+
"github.com/coder/coder/coderd/database/db2sdk"
1615
"github.com/coder/coder/coderd/httpapi"
1716
"github.com/coder/coder/coderd/rbac"
1817
"github.com/coder/coder/codersdk"
@@ -244,7 +243,7 @@ func (api *API) insightsTemplates(rw http.ResponseWriter, r *http.Request) {
244243
return
245244
}
246245

247-
parametersUsage, err := convertTemplateInsightsParameters(parameterRows)
246+
parametersUsage, err := db2sdk.TemplateInsightsParameters(parameterRows)
248247
if err != nil {
249248
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
250249
Message: "Internal error converting template parameter insights.",
@@ -315,39 +314,6 @@ func convertTemplateInsightsBuiltinApps(usage database.GetTemplateInsightsRow) [
315314
}
316315
}
317316

318-
func convertTemplateInsightsParameters(parameterRows []database.GetTemplateParameterInsightsRow) ([]codersdk.TemplateParameterUsage, error) {
319-
parametersByNum := make(map[int64]*codersdk.TemplateParameterUsage)
320-
for _, param := range parameterRows {
321-
if _, ok := parametersByNum[param.Num]; !ok {
322-
var opts []codersdk.TemplateVersionParameterOption
323-
err := json.Unmarshal(param.Options, &opts)
324-
if err != nil {
325-
return nil, xerrors.Errorf("unmarshal template parameter options: %w", err)
326-
}
327-
parametersByNum[param.Num] = &codersdk.TemplateParameterUsage{
328-
TemplateIDs: param.TemplateIDs,
329-
Name: param.Name,
330-
DisplayName: param.DisplayName,
331-
Options: opts,
332-
}
333-
}
334-
parametersByNum[param.Num].Values = append(parametersByNum[param.Num].Values, codersdk.TemplateParameterValue{
335-
Value: param.Value,
336-
Count: param.Count,
337-
})
338-
}
339-
parametersUsage := []codersdk.TemplateParameterUsage{}
340-
for _, param := range parametersByNum {
341-
parametersUsage = append(parametersUsage, *param)
342-
}
343-
344-
sort.Slice(parametersUsage, func(i, j int) bool {
345-
return parametersUsage[i].Name < parametersUsage[j].Name
346-
})
347-
348-
return parametersUsage, nil
349-
}
350-
351317
// parseInsightsStartAndEndTime parses the start and end time query parameters
352318
// and returns the parsed values. The client provided timezone must be preserved
353319
// when parsing the time. Verification is performed so that the start and end

coderd/insights_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ func TestTemplateInsights(t *testing.T) {
405405
// The workspace uses 3 parameters
406406
require.Len(t, resp.Report.ParametersUsage, 3)
407407
assert.Equal(t, firstParameterName, resp.Report.ParametersUsage[0].Name)
408+
assert.Equal(t, firstParameterType, resp.Report.ParametersUsage[0].Type)
409+
assert.Equal(t, firstParameterDescription, resp.Report.ParametersUsage[0].Description)
408410
assert.Equal(t, firstParameterDisplayName, resp.Report.ParametersUsage[0].DisplayName)
409411
assert.Contains(t, resp.Report.ParametersUsage[0].Values, codersdk.TemplateParameterValue{
410412
Value: firstParameterValue,
@@ -414,6 +416,8 @@ func TestTemplateInsights(t *testing.T) {
414416
assert.Empty(t, resp.Report.ParametersUsage[0].Options)
415417

416418
assert.Equal(t, secondParameterName, resp.Report.ParametersUsage[1].Name)
419+
assert.Equal(t, secondParameterType, resp.Report.ParametersUsage[1].Type)
420+
assert.Equal(t, secondParameterDescription, resp.Report.ParametersUsage[1].Description)
417421
assert.Equal(t, secondParameterDisplayName, resp.Report.ParametersUsage[1].DisplayName)
418422
assert.Contains(t, resp.Report.ParametersUsage[1].Values, codersdk.TemplateParameterValue{
419423
Value: secondParameterValue,
@@ -423,6 +427,8 @@ func TestTemplateInsights(t *testing.T) {
423427
assert.Empty(t, resp.Report.ParametersUsage[1].Options)
424428

425429
assert.Equal(t, thirdParameterName, resp.Report.ParametersUsage[2].Name)
430+
assert.Equal(t, thirdParameterType, resp.Report.ParametersUsage[2].Type)
431+
assert.Equal(t, thirdParameterDescription, resp.Report.ParametersUsage[2].Description)
426432
assert.Equal(t, thirdParameterDisplayName, resp.Report.ParametersUsage[2].DisplayName)
427433
assert.Contains(t, resp.Report.ParametersUsage[2].Values, codersdk.TemplateParameterValue{
428434
Value: thirdParameterValue,

codersdk/insights.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ type TemplateParameterUsage struct {
138138
TemplateIDs []uuid.UUID `json:"template_ids" format:"uuid"`
139139
DisplayName string `json:"display_name"`
140140
Name string `json:"name"`
141+
Type string `json:"type"`
142+
Description string `json:"description"`
141143
Options []TemplateVersionParameterOption `json:"options,omitempty"`
142144
Values []TemplateParameterValue `json:"values"`
143145
}

docs/api/insights.md

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)