Skip to content

Commit 5c4227a

Browse files
committed
Write and test metricscache
1 parent 94efda5 commit 5c4227a

File tree

10 files changed

+266
-204
lines changed

10 files changed

+266
-204
lines changed

coderd/database/databasefake/databasefake.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ func (q *fakeQuerier) GetTemplateDAUs(_ context.Context, templateID uuid.UUID) (
235235
return rs, nil
236236
}
237237

238+
func (q *fakeQuerier) GetTemplateAverageBuildTime(ctx context.Context, arg database.GetTemplateAverageBuildTimeParams) (float64, error) {
239+
var times []float64
240+
for _, wb := range q.workspaceBuilds {
241+
if wb.Transition != database.WorkspaceTransitionStart {
242+
continue
243+
}
244+
version, err := q.GetTemplateVersionByID(ctx, wb.TemplateVersionID)
245+
if err != nil {
246+
return -1, err
247+
}
248+
if version.TemplateID != arg.TemplateID {
249+
continue
250+
}
251+
252+
job, err := q.GetProvisionerJobByID(ctx, wb.JobID)
253+
if err != nil {
254+
return -1, err
255+
}
256+
if job.CompletedAt.Valid {
257+
times = append(times, job.CompletedAt.Time.Sub(job.StartedAt.Time).Seconds())
258+
}
259+
}
260+
sort.Float64s(times)
261+
if len(times) == 0 {
262+
return -1, nil
263+
}
264+
return times[len(times)/2], nil
265+
}
266+
238267
func (q *fakeQuerier) ParameterValue(_ context.Context, id uuid.UUID) (database.ParameterValue, error) {
239268
q.mutex.Lock()
240269
defer q.mutex.Unlock()
@@ -1326,14 +1355,6 @@ func (q *fakeQuerier) GetTemplateVersionByJobID(_ context.Context, jobID uuid.UU
13261355
return database.TemplateVersion{}, sql.ErrNoRows
13271356
}
13281357

1329-
func (q *fakeQuerier) GetTemplatesAverageBuildTime(_ context.Context, _ database.GetTemplatesAverageBuildTimeParams) ([]database.GetTemplatesAverageBuildTimeRow, error) {
1330-
q.mutex.RLock()
1331-
defer q.mutex.RUnlock()
1332-
1333-
// TODO
1334-
return nil, nil
1335-
}
1336-
13371358
func (q *fakeQuerier) GetParameterSchemasByJobID(_ context.Context, jobID uuid.UUID) ([]database.ParameterSchema, error) {
13381359
q.mutex.RLock()
13391360
defer q.mutex.RUnlock()

coderd/database/querier.go

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

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

coderd/database/queries/templates.sql

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -106,57 +106,25 @@ WHERE
106106
RETURNING
107107
*;
108108

109-
-- name: GetTemplatesAverageBuildTime :many
110-
-- Computes average build time for every template.
111-
-- Only considers last moving_average_size successful builds between start_ts and end_ts.
112-
-- If a template does not have at least min_completed_job_count such builds, it gets skipped.
113-
WITH query_with_all_job_count AS (SELECT
114-
DISTINCT t.id,
115-
AVG(pj.exec_time_sec)
116-
OVER(
117-
PARTITION BY t.id
118-
ORDER BY pj.completed_at
119-
ROWS BETWEEN @moving_average_size::integer PRECEDING AND CURRENT ROW)
120-
AS avg_build_time_sec,
121-
COUNT(*) OVER(PARTITION BY t.id) as job_count
122-
FROM
123-
(SELECT
124-
id,
125-
active_version_id
126-
FROM
127-
templates) AS t
128-
INNER JOIN
129-
(SELECT
130-
workspace_id,
131-
template_version_id,
132-
job_id
133-
FROM
134-
workspace_builds)
135-
AS
136-
wb
137-
ON
138-
t.id = wb.workspace_id AND t.active_version_id = wb.template_version_id
139-
INNER JOIN
140-
(SELECT
141-
id,
142-
completed_at,
143-
EXTRACT(EPOCH FROM (completed_at - started_at)) AS exec_time_sec
144-
FROM
145-
provisioner_jobs
146-
WHERE
147-
(completed_at IS NOT NULL) AND (started_at IS NOT NULL) AND
148-
(completed_at >= @start_ts AND completed_at <= @end_ts) AND
149-
(canceled_at IS NULL) AND
150-
((error IS NULL) OR (error = '')))
151-
AS
152-
pj
153-
ON
154-
wb.job_id = pj.id)
109+
-- name: GetTemplateAverageBuildTime :one
110+
WITH build_times AS (
155111
SELECT
156-
id,
157-
avg_build_time_sec
112+
EXTRACT(EPOCH FROM (pj.completed_at - pj.started_at)) AS exec_time_sec
158113
FROM
159-
query_with_all_job_count
114+
workspace_builds
115+
JOIN template_versions ON
116+
workspace_builds.template_version_id = template_versions.id
117+
JOIN provisioner_jobs pj ON
118+
workspace_builds.job_id = pj.id
160119
WHERE
161-
job_count >= @min_completed_job_count::integer
120+
template_versions.template_id = @template_id AND
121+
(workspace_builds.transition = "start") AND
122+
(pj.completed_at IS NOT NULL) AND (pj.started_at IS NOT NULL) AND
123+
(pj.started_at > @start_time) AND
124+
(pj.canceled_at IS NULL) AND
125+
((pj.error IS NULL) OR (pj.error = ''))
126+
ORDER BY
127+
workspace_builds.created_at DESC
128+
)
129+
SELECT CAST(PERCENTILE_DISC(0.5) WITHIN GROUP(ORDER BY exec_time_sec) AS FLOAT) FROM build_times
162130
;

0 commit comments

Comments
 (0)