|
1 |
| --- name: GetTemplatePrebuildState :many |
| 1 | +-- name: GetTemplatePrebuildState :one |
2 | 2 | WITH
|
3 | 3 | -- All prebuilds currently running
|
4 |
| - running_prebuilds AS (SELECT p.*, b.template_version_id |
| 4 | + running_prebuilds AS (SELECT p.template_id, b.template_version_id, COUNT(*) AS count, STRING_AGG(p.id::text, ',') AS ids |
5 | 5 | FROM workspace_prebuilds p
|
6 | 6 | INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
7 |
| - WHERE b.transition = 'start'::workspace_transition), |
| 7 | + WHERE b.transition = 'start'::workspace_transition |
| 8 | + GROUP BY p.template_id, b.template_version_id), |
8 | 9 | -- All templates which have been configured for prebuilds (any version)
|
9 | 10 | templates_with_prebuilds AS (SELECT t.id AS template_id,
|
10 | 11 | tv.id AS template_version_id,
|
|
18 | 19 | INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id
|
19 | 20 | WHERE t.id = @template_id::uuid
|
20 | 21 | GROUP BY t.id, tv.id, tvpp.id),
|
21 |
| - prebuilds_in_progress AS (SELECT wpb.template_version_id, pj.id AS job_id, pj.type, pj.job_status, wpb.transition |
| 22 | + prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count |
22 | 23 | FROM workspace_prebuild_builds wpb
|
23 | 24 | INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id
|
24 | 25 | INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id
|
25 | 26 | WHERE pj.job_status NOT IN
|
26 | 27 | ('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
27 |
| - 'failed'::provisioner_job_status)) |
| 28 | + 'failed'::provisioner_job_status) |
| 29 | + GROUP BY wpb.template_version_id, wpb.transition) |
28 | 30 | SELECT t.template_id,
|
29 |
| - CAST(COUNT(p.id) AS INT) AS actual, -- running prebuilds for active version |
30 |
| - CAST(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END) AS int) AS desired, -- we only care about the active version's desired instances |
31 |
| - CAST(SUM(CASE WHEN t.using_active_version THEN 0 ELSE 1 END) AS INT) AS extraneous, -- running prebuilds for inactive version |
32 |
| - CAST(SUM(CASE |
33 |
| - WHEN pip.transition = 'start'::workspace_transition THEN 1 |
34 |
| - ELSE 0 END) AS INT) AS starting, |
35 |
| - CAST(SUM(CASE |
36 |
| - WHEN pip.transition = 'delete'::workspace_transition THEN 1 |
37 |
| - ELSE 0 END) AS INT) AS deleting, |
38 |
| - t.deleted AS template_deleted, |
39 |
| - t.deprecated AS template_deprecated |
| 31 | + p.ids AS running_prebuild_ids, |
| 32 | + CAST(SUM(CASE WHEN t.using_active_version THEN p.count ELSE 0 END) AS INT) AS actual, -- running prebuilds for active version |
| 33 | + CAST(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END) AS int) AS desired, -- we only care about the active version's desired instances |
| 34 | + CAST(SUM(CASE WHEN t.using_active_version THEN 0 ELSE p.count END) AS INT) AS extraneous, -- running prebuilds for inactive version |
| 35 | + CAST(MAX(CASE |
| 36 | + WHEN pip.transition = 'start'::workspace_transition THEN pip.count |
| 37 | + ELSE 0 END) AS INT) AS starting, |
| 38 | + CAST(MAX(CASE |
| 39 | + WHEN pip.transition = 'stop'::workspace_transition THEN pip.count |
| 40 | + ELSE 0 END) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know |
| 41 | + CAST(MAX(CASE |
| 42 | + WHEN pip.transition = 'delete'::workspace_transition THEN pip.count |
| 43 | + ELSE 0 END) AS INT) AS deleting, |
| 44 | + t.deleted AS template_deleted, |
| 45 | + t.deprecated AS template_deprecated |
40 | 46 | FROM templates_with_prebuilds t
|
41 | 47 | LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
42 | 48 | LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
|
43 |
| -GROUP BY t.template_id, p.id, t.deleted, t.deprecated, pip.template_version_id; |
| 49 | +GROUP BY t.template_id, p.count, p.ids, t.deleted, t.deprecated; |
0 commit comments