Generating short ID for prebuilds

Also dropped unnecessary CASTs

Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
Danny Kopping
2025-01-28 10:26:14 +00:00
parent c227bb842f
commit 9d5c6633de
3 changed files with 63 additions and 44 deletions

View File

@ -38,25 +38,25 @@ WITH
GROUP BY wpb.template_version_id, wpb.transition)
SELECT t.template_id,
t.template_version_id,
t.using_active_version AS is_active,
CAST(COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END),
'') AS TEXT) AS running_prebuild_ids,
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
0) AS INT) AS actual, -- running prebuilds for active version
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
0) AS INT) AS desired, -- we only care about the active version's desired instances
CAST(COALESCE(MAX(CASE
WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false
THEN p.count END),
0) AS INT) AS extraneous, -- running prebuilds for inactive version
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END),
0) AS INT) AS starting,
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END),
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END),
0) AS INT) AS deleting,
t.deleted AS template_deleted,
t.deprecated AS template_deprecated
t.using_active_version AS is_active,
COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END),
'')::text AS running_prebuild_ids,
COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
0)::int AS actual, -- running prebuilds for active version
COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
0)::int AS desired, -- we only care about the active version's desired instances
COALESCE(MAX(CASE
WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false
THEN p.count END),
0)::int AS extraneous, -- running prebuilds for inactive version
COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END),
0)::int AS starting,
COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END),
0)::int AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END),
0)::int AS deleting,
t.deleted AS template_deleted,
t.deprecated AS template_deprecated
FROM templates_with_prebuilds t
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id