|
1 |
| --- name: GetTemplatePrebuildState :one |
| 1 | +-- name: GetTemplatePrebuildState :many |
2 | 2 | WITH
|
3 | 3 | -- All prebuilds currently running
|
4 | 4 | running_prebuilds AS (SELECT p.template_id,
|
|
8 | 8 | FROM workspace_prebuilds p
|
9 | 9 | INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
10 | 10 | INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
|
| 11 | + INNER JOIN templates t ON p.template_id = t.id |
11 | 12 | WHERE (b.transition = 'start'::workspace_transition
|
12 | 13 | -- if a deletion job fails, the workspace will still be running
|
13 | 14 | OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
|
28 | 29 | GROUP BY t.id, tv.id, tvpp.id),
|
29 | 30 | -- Jobs relating to prebuilds current in-flight
|
30 | 31 | prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count
|
31 |
| - FROM workspace_prebuild_builds wpb |
32 |
| - INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id |
| 32 | + FROM workspace_latest_build wlb |
33 | 33 | INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id
|
| 34 | + INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id |
34 | 35 | WHERE pj.job_status NOT IN
|
35 | 36 | ('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
36 | 37 | 'failed'::provisioner_job_status)
|
37 | 38 | GROUP BY wpb.template_version_id, wpb.transition)
|
38 | 39 | SELECT t.template_id,
|
39 | 40 | t.template_version_id,
|
40 |
| - t.using_active_version AS is_active, |
41 |
| - p.ids AS running_prebuild_ids, |
42 |
| - CAST(COALESCE( |
43 |
| - MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END), |
44 |
| - 0) AS INT) AS actual, -- running prebuilds for active version |
| 41 | + t.using_active_version AS is_active, |
| 42 | + CAST(COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END), |
| 43 | + '') AS TEXT) AS running_prebuild_ids, |
| 44 | + CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END), |
| 45 | + 0) AS INT) AS actual, -- running prebuilds for active version |
45 | 46 | CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
|
46 |
| - 0) AS INT) AS desired, -- we only care about the active version's desired instances |
47 |
| - CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN 0 ELSE p.count END), |
48 |
| - 0) AS INT) AS extraneous, -- running prebuilds for inactive version |
| 47 | + 0) AS INT) AS desired, -- we only care about the active version's desired instances |
49 | 48 | CAST(COALESCE(MAX(CASE
|
50 |
| - WHEN pip.transition = 'start'::workspace_transition THEN pip.count |
51 |
| - ELSE 0 END), 0) AS INT) AS starting, |
52 |
| - CAST(COALESCE(MAX(CASE |
53 |
| - WHEN pip.transition = 'stop'::workspace_transition THEN pip.count |
54 |
| - ELSE 0 END), |
55 |
| - 0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know |
56 |
| - CAST(COALESCE(MAX(CASE |
57 |
| - WHEN pip.transition = 'delete'::workspace_transition THEN pip.count |
58 |
| - ELSE 0 END), 0) AS INT) AS deleting, |
59 |
| - t.deleted AS template_deleted, |
60 |
| - t.deprecated AS template_deprecated |
| 49 | + WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false |
| 50 | + THEN p.count END), |
| 51 | + 0) AS INT) AS extraneous, -- running prebuilds for inactive version |
| 52 | + CAST(COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END), |
| 53 | + 0) AS INT) AS starting, |
| 54 | + CAST(COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END), |
| 55 | + 0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know |
| 56 | + CAST(COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END), |
| 57 | + 0) AS INT) AS deleting, |
| 58 | + t.deleted AS template_deleted, |
| 59 | + t.deprecated AS template_deprecated |
61 | 60 | FROM templates_with_prebuilds t
|
62 |
| - LEFT JOIN running_prebuilds p ON p.template_id = t.template_id |
| 61 | + LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id |
63 | 62 | LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
|
64 |
| -GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids, t.deleted, t.deprecated; |
| 63 | +GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids, |
| 64 | + p.template_version_id, t.deleted, t.deprecated; |
0 commit comments