mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
feat(coderd): add parameter insights to template insights (#8656)
This commit is contained in:
committed by
GitHub
parent
2ed453035e
commit
d3991fac26
@ -1227,6 +1227,25 @@ func (q *querier) GetTemplateInsights(ctx context.Context, arg database.GetTempl
|
||||
return q.db.GetTemplateInsights(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) GetTemplateParameterInsights(ctx context.Context, arg database.GetTemplateParameterInsightsParams) ([]database.GetTemplateParameterInsightsRow, error) {
|
||||
for _, templateID := range arg.TemplateIDs {
|
||||
template, err := q.db.GetTemplateByID(ctx, templateID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := q.authorizeContext(ctx, rbac.ActionUpdate, template); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if len(arg.TemplateIDs) == 0 {
|
||||
if err := q.authorizeContext(ctx, rbac.ActionUpdate, rbac.ResourceTemplate.All()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return q.db.GetTemplateParameterInsights(ctx, arg)
|
||||
}
|
||||
|
||||
func (q *querier) GetTemplateVersionByID(ctx context.Context, tvid uuid.UUID) (database.TemplateVersion, error) {
|
||||
tv, err := q.db.GetTemplateVersionByID(ctx, tvid)
|
||||
if err != nil {
|
||||
|
@ -596,6 +596,21 @@ func isNotNull(v interface{}) bool {
|
||||
// these methods remain unimplemented in the FakeQuerier.
|
||||
var ErrUnimplemented = xerrors.New("unimplemented")
|
||||
|
||||
func uniqueSortedUUIDs(uuids []uuid.UUID) []uuid.UUID {
|
||||
set := make(map[uuid.UUID]struct{})
|
||||
for _, id := range uuids {
|
||||
set[id] = struct{}{}
|
||||
}
|
||||
unique := make([]uuid.UUID, 0, len(set))
|
||||
for id := range set {
|
||||
unique = append(unique, id)
|
||||
}
|
||||
slices.SortFunc(unique, func(a, b uuid.UUID) bool {
|
||||
return a.String() < b.String()
|
||||
})
|
||||
return unique
|
||||
}
|
||||
|
||||
func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
|
||||
return xerrors.New("AcquireLock must only be called within a transaction")
|
||||
}
|
||||
@ -2122,6 +2137,100 @@ func (q *FakeQuerier) GetTemplateInsights(_ context.Context, arg database.GetTem
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg database.GetTemplateParameterInsightsParams) ([]database.GetTemplateParameterInsightsRow, error) {
|
||||
err := validateDatabaseType(arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q.mutex.RLock()
|
||||
defer q.mutex.RUnlock()
|
||||
|
||||
// WITH latest_workspace_builds ...
|
||||
latestWorkspaceBuilds := make(map[uuid.UUID]database.WorkspaceBuildTable)
|
||||
for _, wb := range q.workspaceBuilds {
|
||||
if wb.CreatedAt.Before(arg.StartTime) || wb.CreatedAt.Equal(arg.EndTime) || wb.CreatedAt.After(arg.EndTime) {
|
||||
continue
|
||||
}
|
||||
if latestWorkspaceBuilds[wb.WorkspaceID].BuildNumber < wb.BuildNumber {
|
||||
latestWorkspaceBuilds[wb.WorkspaceID] = wb
|
||||
}
|
||||
}
|
||||
if len(arg.TemplateIDs) > 0 {
|
||||
for wsID := range latestWorkspaceBuilds {
|
||||
ws, err := q.getWorkspaceByIDNoLock(ctx, wsID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if slices.Contains(arg.TemplateIDs, ws.TemplateID) {
|
||||
delete(latestWorkspaceBuilds, wsID)
|
||||
}
|
||||
}
|
||||
}
|
||||
// WITH unique_template_params ...
|
||||
num := int64(0)
|
||||
uniqueTemplateParams := make(map[string]*database.GetTemplateParameterInsightsRow)
|
||||
uniqueTemplateParamWorkspaceBuildIDs := make(map[string][]uuid.UUID)
|
||||
for _, wb := range latestWorkspaceBuilds {
|
||||
tv, err := q.getTemplateVersionByIDNoLock(ctx, wb.TemplateVersionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, tvp := range q.templateVersionParameters {
|
||||
if tvp.TemplateVersionID != tv.ID {
|
||||
continue
|
||||
}
|
||||
key := fmt.Sprintf("%s:%s:%s:%s", tvp.Name, tvp.DisplayName, tvp.Description, tvp.Options)
|
||||
if _, ok := uniqueTemplateParams[key]; !ok {
|
||||
num++
|
||||
uniqueTemplateParams[key] = &database.GetTemplateParameterInsightsRow{
|
||||
Num: num,
|
||||
Name: tvp.Name,
|
||||
DisplayName: tvp.DisplayName,
|
||||
Description: tvp.Description,
|
||||
Options: tvp.Options,
|
||||
}
|
||||
}
|
||||
uniqueTemplateParams[key].TemplateIDs = append(uniqueTemplateParams[key].TemplateIDs, tv.TemplateID.UUID)
|
||||
uniqueTemplateParamWorkspaceBuildIDs[key] = append(uniqueTemplateParamWorkspaceBuildIDs[key], wb.ID)
|
||||
}
|
||||
}
|
||||
// SELECT ...
|
||||
counts := make(map[string]map[string]int64)
|
||||
for key, utp := range uniqueTemplateParams {
|
||||
for _, wbp := range q.workspaceBuildParameters {
|
||||
if !slices.Contains(uniqueTemplateParamWorkspaceBuildIDs[key], wbp.WorkspaceBuildID) {
|
||||
continue
|
||||
}
|
||||
if wbp.Name != utp.Name {
|
||||
continue
|
||||
}
|
||||
if counts[key] == nil {
|
||||
counts[key] = make(map[string]int64)
|
||||
}
|
||||
counts[key][wbp.Value]++
|
||||
}
|
||||
}
|
||||
|
||||
var rows []database.GetTemplateParameterInsightsRow
|
||||
for key, utp := range uniqueTemplateParams {
|
||||
for value, count := range counts[key] {
|
||||
rows = append(rows, database.GetTemplateParameterInsightsRow{
|
||||
Num: utp.Num,
|
||||
TemplateIDs: uniqueSortedUUIDs(utp.TemplateIDs),
|
||||
Name: utp.Name,
|
||||
DisplayName: utp.DisplayName,
|
||||
Description: utp.Description,
|
||||
Options: utp.Options,
|
||||
Value: value,
|
||||
Count: count,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (q *FakeQuerier) GetTemplateVersionByID(ctx context.Context, templateVersionID uuid.UUID) (database.TemplateVersion, error) {
|
||||
q.mutex.RLock()
|
||||
defer q.mutex.RUnlock()
|
||||
|
@ -634,6 +634,13 @@ func (m metricsStore) GetTemplateInsights(ctx context.Context, arg database.GetT
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m metricsStore) GetTemplateParameterInsights(ctx context.Context, arg database.GetTemplateParameterInsightsParams) ([]database.GetTemplateParameterInsightsRow, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetTemplateParameterInsights(ctx, arg)
|
||||
m.queryLatencies.WithLabelValues("GetTemplateParameterInsights").Observe(time.Since(start).Seconds())
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m metricsStore) GetTemplateVersionByID(ctx context.Context, id uuid.UUID) (database.TemplateVersion, error) {
|
||||
start := time.Now()
|
||||
version, err := m.s.GetTemplateVersionByID(ctx, id)
|
||||
|
@ -1286,6 +1286,21 @@ func (mr *MockStoreMockRecorder) GetTemplateInsights(arg0, arg1 interface{}) *go
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTemplateInsights", reflect.TypeOf((*MockStore)(nil).GetTemplateInsights), arg0, arg1)
|
||||
}
|
||||
|
||||
// GetTemplateParameterInsights mocks base method.
|
||||
func (m *MockStore) GetTemplateParameterInsights(arg0 context.Context, arg1 database.GetTemplateParameterInsightsParams) ([]database.GetTemplateParameterInsightsRow, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetTemplateParameterInsights", arg0, arg1)
|
||||
ret0, _ := ret[0].([]database.GetTemplateParameterInsightsRow)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetTemplateParameterInsights indicates an expected call of GetTemplateParameterInsights.
|
||||
func (mr *MockStoreMockRecorder) GetTemplateParameterInsights(arg0, arg1 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTemplateParameterInsights", reflect.TypeOf((*MockStore)(nil).GetTemplateParameterInsights), arg0, arg1)
|
||||
}
|
||||
|
||||
// GetTemplateUserRoles mocks base method.
|
||||
func (m *MockStore) GetTemplateUserRoles(arg0 context.Context, arg1 uuid.UUID) ([]database.TemplateUser, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
@ -116,6 +116,11 @@ type sqlcQuerier interface {
|
||||
// GetTemplateInsights has a granularity of 5 minutes where if a session/app was
|
||||
// in use, we will add 5 minutes to the total usage for that session (per user).
|
||||
GetTemplateInsights(ctx context.Context, arg GetTemplateInsightsParams) (GetTemplateInsightsRow, error)
|
||||
// GetTemplateParameterInsights does for each template in a given timeframe,
|
||||
// look for the latest workspace build (for every workspace) that has been
|
||||
// created in the timeframe and return the aggregate usage counts of parameter
|
||||
// values.
|
||||
GetTemplateParameterInsights(ctx context.Context, arg GetTemplateParameterInsightsParams) ([]GetTemplateParameterInsightsRow, error)
|
||||
GetTemplateVersionByID(ctx context.Context, id uuid.UUID) (TemplateVersion, error)
|
||||
GetTemplateVersionByJobID(ctx context.Context, jobID uuid.UUID) (TemplateVersion, error)
|
||||
GetTemplateVersionByTemplateIDAndName(ctx context.Context, arg GetTemplateVersionByTemplateIDAndNameParams) (TemplateVersion, error)
|
||||
|
@ -1553,6 +1553,108 @@ func (q *sqlQuerier) GetTemplateInsights(ctx context.Context, arg GetTemplateIns
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getTemplateParameterInsights = `-- name: GetTemplateParameterInsights :many
|
||||
WITH latest_workspace_builds AS (
|
||||
SELECT
|
||||
wb.id,
|
||||
wbmax.template_id,
|
||||
wb.template_version_id
|
||||
FROM (
|
||||
SELECT
|
||||
tv.template_id, wbmax.workspace_id, MAX(wbmax.build_number) as max_build_number
|
||||
FROM workspace_builds wbmax
|
||||
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
|
||||
WHERE
|
||||
wbmax.created_at >= $1::timestamptz
|
||||
AND wbmax.created_at < $2::timestamptz
|
||||
AND CASE WHEN COALESCE(array_length($3::uuid[], 1), 0) > 0 THEN tv.template_id = ANY($3::uuid[]) ELSE TRUE END
|
||||
GROUP BY tv.template_id, wbmax.workspace_id
|
||||
) wbmax
|
||||
JOIN workspace_builds wb ON (
|
||||
wb.workspace_id = wbmax.workspace_id
|
||||
AND wb.build_number = wbmax.max_build_number
|
||||
)
|
||||
), unique_template_params AS (
|
||||
SELECT
|
||||
ROW_NUMBER() OVER () AS num,
|
||||
array_agg(DISTINCT wb.template_id)::uuid[] AS template_ids,
|
||||
array_agg(wb.id)::uuid[] AS workspace_build_ids,
|
||||
tvp.name,
|
||||
tvp.display_name,
|
||||
tvp.description,
|
||||
tvp.options
|
||||
FROM latest_workspace_builds wb
|
||||
JOIN template_version_parameters tvp ON (tvp.template_version_id = wb.template_version_id)
|
||||
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options
|
||||
)
|
||||
|
||||
SELECT
|
||||
utp.num,
|
||||
utp.template_ids,
|
||||
utp.name,
|
||||
utp.display_name,
|
||||
utp.description,
|
||||
utp.options,
|
||||
wbp.value,
|
||||
COUNT(wbp.value) AS count
|
||||
FROM unique_template_params utp
|
||||
JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.workspace_build_id] AND utp.name = wbp.name)
|
||||
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, wbp.value
|
||||
`
|
||||
|
||||
type GetTemplateParameterInsightsParams struct {
|
||||
StartTime time.Time `db:"start_time" json:"start_time"`
|
||||
EndTime time.Time `db:"end_time" json:"end_time"`
|
||||
TemplateIDs []uuid.UUID `db:"template_ids" json:"template_ids"`
|
||||
}
|
||||
|
||||
type GetTemplateParameterInsightsRow struct {
|
||||
Num int64 `db:"num" json:"num"`
|
||||
TemplateIDs []uuid.UUID `db:"template_ids" json:"template_ids"`
|
||||
Name string `db:"name" json:"name"`
|
||||
DisplayName string `db:"display_name" json:"display_name"`
|
||||
Description string `db:"description" json:"description"`
|
||||
Options json.RawMessage `db:"options" json:"options"`
|
||||
Value string `db:"value" json:"value"`
|
||||
Count int64 `db:"count" json:"count"`
|
||||
}
|
||||
|
||||
// GetTemplateParameterInsights does for each template in a given timeframe,
|
||||
// look for the latest workspace build (for every workspace) that has been
|
||||
// created in the timeframe and return the aggregate usage counts of parameter
|
||||
// values.
|
||||
func (q *sqlQuerier) GetTemplateParameterInsights(ctx context.Context, arg GetTemplateParameterInsightsParams) ([]GetTemplateParameterInsightsRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getTemplateParameterInsights, arg.StartTime, arg.EndTime, pq.Array(arg.TemplateIDs))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetTemplateParameterInsightsRow
|
||||
for rows.Next() {
|
||||
var i GetTemplateParameterInsightsRow
|
||||
if err := rows.Scan(
|
||||
&i.Num,
|
||||
pq.Array(&i.TemplateIDs),
|
||||
&i.Name,
|
||||
&i.DisplayName,
|
||||
&i.Description,
|
||||
&i.Options,
|
||||
&i.Value,
|
||||
&i.Count,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getUserLatencyInsights = `-- name: GetUserLatencyInsights :many
|
||||
SELECT
|
||||
workspace_agent_stats.user_id,
|
||||
|
@ -114,3 +114,56 @@ SELECT
|
||||
COUNT(DISTINCT user_id) AS active_users
|
||||
FROM usage_by_day
|
||||
GROUP BY from_, to_;
|
||||
|
||||
|
||||
-- name: GetTemplateParameterInsights :many
|
||||
-- GetTemplateParameterInsights does for each template in a given timeframe,
|
||||
-- look for the latest workspace build (for every workspace) that has been
|
||||
-- created in the timeframe and return the aggregate usage counts of parameter
|
||||
-- values.
|
||||
WITH latest_workspace_builds AS (
|
||||
SELECT
|
||||
wb.id,
|
||||
wbmax.template_id,
|
||||
wb.template_version_id
|
||||
FROM (
|
||||
SELECT
|
||||
tv.template_id, wbmax.workspace_id, MAX(wbmax.build_number) as max_build_number
|
||||
FROM workspace_builds wbmax
|
||||
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
|
||||
WHERE
|
||||
wbmax.created_at >= @start_time::timestamptz
|
||||
AND wbmax.created_at < @end_time::timestamptz
|
||||
AND CASE WHEN COALESCE(array_length(@template_ids::uuid[], 1), 0) > 0 THEN tv.template_id = ANY(@template_ids::uuid[]) ELSE TRUE END
|
||||
GROUP BY tv.template_id, wbmax.workspace_id
|
||||
) wbmax
|
||||
JOIN workspace_builds wb ON (
|
||||
wb.workspace_id = wbmax.workspace_id
|
||||
AND wb.build_number = wbmax.max_build_number
|
||||
)
|
||||
), unique_template_params AS (
|
||||
SELECT
|
||||
ROW_NUMBER() OVER () AS num,
|
||||
array_agg(DISTINCT wb.template_id)::uuid[] AS template_ids,
|
||||
array_agg(wb.id)::uuid[] AS workspace_build_ids,
|
||||
tvp.name,
|
||||
tvp.display_name,
|
||||
tvp.description,
|
||||
tvp.options
|
||||
FROM latest_workspace_builds wb
|
||||
JOIN template_version_parameters tvp ON (tvp.template_version_id = wb.template_version_id)
|
||||
GROUP BY tvp.name, tvp.display_name, tvp.description, tvp.options
|
||||
)
|
||||
|
||||
SELECT
|
||||
utp.num,
|
||||
utp.template_ids,
|
||||
utp.name,
|
||||
utp.display_name,
|
||||
utp.description,
|
||||
utp.options,
|
||||
wbp.value,
|
||||
COUNT(wbp.value) AS count
|
||||
FROM unique_template_params utp
|
||||
JOIN workspace_build_parameters wbp ON (utp.workspace_build_ids @> ARRAY[wbp.workspace_build_id] AND utp.name = wbp.name)
|
||||
GROUP BY utp.num, utp.name, utp.display_name, utp.description, utp.options, utp.template_ids, wbp.value;
|
||||
|
Reference in New Issue
Block a user