@@ -15,22 +15,23 @@ SELECT
15
15
t .deleted ,
16
16
t .deprecated != ' ' AS deprecated
17
17
FROM templates t
18
- INNER JOIN template_versions tv ON tv .template_id = t .id
19
- INNER JOIN template_version_presets tvp ON tvp .template_version_id = tv .id
20
- INNER JOIN organizations o ON o .id = t .organization_id
18
+ INNER JOIN template_versions tv ON tv .template_id = t .id
19
+ INNER JOIN template_version_presets tvp ON tvp .template_version_id = tv .id
20
+ INNER JOIN organizations o ON o .id = t .organization_id
21
21
WHERE tvp .desired_instances IS NOT NULL -- Consider only presets that have a prebuild configuration.
22
- AND (t .id = sqlc .narg (' template_id' )::uuid OR sqlc .narg (' template_id' ) IS NULL );
22
+ AND (t .id = sqlc .narg (' template_id' )::uuid OR sqlc .narg (' template_id' ) IS NULL );
23
23
24
24
-- name: GetRunningPrebuiltWorkspaces :many
25
- SELECT p .id ,
26
- p .name ,
27
- p .template_id ,
28
- b .template_version_id ,
29
- p .current_preset_id AS current_preset_id,
30
- p .ready ,
31
- p .created_at
25
+ SELECT
26
+ p .id ,
27
+ p .name ,
28
+ p .template_id ,
29
+ b .template_version_id ,
30
+ p .current_preset_id AS current_preset_id,
31
+ p .ready ,
32
+ p .created_at
32
33
FROM workspace_prebuilds p
33
- INNER JOIN workspace_latest_builds b ON b .workspace_id = p .id
34
+ INNER JOIN workspace_latest_builds b ON b .workspace_id = p .id
34
35
WHERE (b .transition = ' start' ::workspace_transition
35
36
AND b .job_status = ' succeeded' ::provisioner_job_status);
36
37
@@ -39,14 +40,14 @@ WHERE (b.transition = 'start'::workspace_transition
39
40
-- Prebuild considered in-progress if it's in the "starting", "stopping", or "deleting" state.
40
41
SELECT t .id AS template_id, wpb .template_version_id , wpb .transition , COUNT (wpb .transition )::int AS count
41
42
FROM workspace_latest_builds wlb
42
- INNER JOIN workspace_prebuild_builds wpb ON wpb .id = wlb .id
43
- -- We only need these counts for active template versions.
44
- -- It doesn't influence whether we create or delete prebuilds
45
- -- for inactive template versions. This is because we never create
46
- -- prebuilds for inactive template versions, we always delete
47
- -- running prebuilds for inactive template versions, and we ignore
48
- -- prebuilds that are still building.
49
- INNER JOIN templates t ON t .active_version_id = wlb .template_version_id
43
+ INNER JOIN workspace_prebuild_builds wpb ON wpb .id = wlb .id
44
+ -- We only need these counts for active template versions.
45
+ -- It doesn't influence whether we create or delete prebuilds
46
+ -- for inactive template versions. This is because we never create
47
+ -- prebuilds for inactive template versions, we always delete
48
+ -- running prebuilds for inactive template versions, and we ignore
49
+ -- prebuilds that are still building.
50
+ INNER JOIN templates t ON t .active_version_id = wlb .template_version_id
50
51
WHERE wlb .job_status IN (' pending' ::provisioner_job_status, ' running' ::provisioner_job_status)
51
52
GROUP BY t .id , wpb .template_version_id , wpb .transition ;
52
53
@@ -69,37 +70,38 @@ WITH filtered_builds AS (
69
70
-- Only select builds which are for prebuild creations
70
71
SELECT wlb .template_version_id , wlb .created_at , tvp .id AS preset_id, wlb .job_status , tvp .desired_instances
71
72
FROM template_version_presets tvp
72
- INNER JOIN workspace_latest_builds wlb ON wlb .template_version_preset_id = tvp .id
73
- INNER JOIN workspaces w ON wlb .workspace_id = w .id
74
- INNER JOIN template_versions tv ON wlb .template_version_id = tv .id
75
- INNER JOIN templates t ON tv .template_id = t .id AND t .active_version_id = tv .id
73
+ INNER JOIN workspace_latest_builds wlb ON wlb .template_version_preset_id = tvp .id
74
+ INNER JOIN workspaces w ON wlb .workspace_id = w .id
75
+ INNER JOIN template_versions tv ON wlb .template_version_id = tv .id
76
+ INNER JOIN templates t ON tv .template_id = t .id AND t .active_version_id = tv .id
76
77
WHERE tvp .desired_instances IS NOT NULL -- Consider only presets that have a prebuild configuration.
77
- AND wlb .transition = ' start' ::workspace_transition
78
- AND w .owner_id = ' c42fdf75-3097-471c-8c33-fb52454d81c0'
78
+ AND wlb .transition = ' start' ::workspace_transition
79
+ AND w .owner_id = ' c42fdf75-3097-471c-8c33-fb52454d81c0'
79
80
),
80
81
time_sorted_builds AS (
81
- -- Group builds by preset, then sort each group by created_at.
82
+ -- Group builds by preset, then sort each group by created_at.
82
83
SELECT fb .template_version_id , fb .created_at , fb .preset_id , fb .job_status , fb .desired_instances ,
83
- ROW_NUMBER() OVER (PARTITION BY fb .preset_id ORDER BY fb .created_at DESC ) as rn
84
+ ROW_NUMBER() OVER (PARTITION BY fb .preset_id ORDER BY fb .created_at DESC ) as rn
84
85
FROM filtered_builds fb
85
86
),
86
87
failed_count AS (
87
- -- Count failed builds per preset in the given period
88
+ -- Count failed builds per preset in the given period
88
89
SELECT preset_id, COUNT (* ) AS num_failed
89
90
FROM filtered_builds
90
91
WHERE job_status = ' failed' ::provisioner_job_status
91
92
AND created_at >= @lookback::timestamptz
92
93
GROUP BY preset_id
93
94
)
94
- SELECT tsb .template_version_id ,
95
- tsb .preset_id ,
96
- COALESCE(fc .num_failed , 0 )::int AS num_failed,
97
- MAX (tsb .created_at )::timestamptz AS last_build_at
95
+ SELECT
96
+ tsb .template_version_id ,
97
+ tsb .preset_id ,
98
+ COALESCE(fc .num_failed , 0 )::int AS num_failed,
99
+ MAX (tsb .created_at )::timestamptz AS last_build_at
98
100
FROM time_sorted_builds tsb
99
- LEFT JOIN failed_count fc ON fc .preset_id = tsb .preset_id
101
+ LEFT JOIN failed_count fc ON fc .preset_id = tsb .preset_id
100
102
WHERE tsb .rn <= tsb .desired_instances -- Fetch the last N builds, where N is the number of desired instances; if any fail, we backoff
101
- AND tsb .job_status = ' failed' ::provisioner_job_status
102
- AND created_at >= @lookback::timestamptz
103
+ AND tsb .job_status = ' failed' ::provisioner_job_status
104
+ AND created_at >= @lookback::timestamptz
103
105
GROUP BY tsb .template_version_id , tsb .preset_id , fc .num_failed ;
104
106
105
107
-- name: ClaimPrebuiltWorkspace :one
@@ -114,24 +116,24 @@ WHERE w.id IN (
114
116
INNER JOIN templates t ON p .template_id = t .id
115
117
WHERE (b .transition = ' start' ::workspace_transition
116
118
AND b .job_status IN (' succeeded' ::provisioner_job_status))
117
- -- The prebuilds system should never try to claim a prebuild for an inactive template version.
118
- -- Nevertheless, this filter is here as a defensive measure:
119
- AND b .template_version_id = t .active_version_id
120
- AND p .current_preset_id = @preset_id::uuid
121
- AND p .ready
119
+ -- The prebuilds system should never try to claim a prebuild for an inactive template version.
120
+ -- Nevertheless, this filter is here as a defensive measure:
121
+ AND b .template_version_id = t .active_version_id
122
+ AND p .current_preset_id = @preset_id::uuid
123
+ AND p .ready
122
124
LIMIT 1 FOR UPDATE OF p SKIP LOCKED -- Ensure that a concurrent request will not select the same prebuild.
123
125
)
124
126
RETURNING w .id , w .name ;
125
127
126
128
-- name: GetPrebuildMetrics :many
127
129
SELECT
128
- t .name as template_name,
129
- tvp .name as preset_name,
130
+ t .name as template_name,
131
+ tvp .name as preset_name,
130
132
o .name as organization_name,
131
- COUNT (* ) as created_count,
132
- COUNT (* ) FILTER (WHERE pj .job_status = ' failed' ::provisioner_job_status) as failed_count,
133
- COUNT (* ) FILTER (
134
- WHERE w .owner_id != ' c42fdf75-3097-471c-8c33-fb52454d81c0' ::uuid -- The system user responsible for prebuilds.
133
+ COUNT (* ) as created_count,
134
+ COUNT (* ) FILTER (WHERE pj .job_status = ' failed' ::provisioner_job_status) as failed_count,
135
+ COUNT (* ) FILTER (
136
+ WHERE w .owner_id != ' c42fdf75-3097-471c-8c33-fb52454d81c0' ::uuid -- The system user responsible for prebuilds.
135
137
) as claimed_count
136
138
FROM workspaces w
137
139
INNER JOIN workspace_prebuild_builds wpb ON wpb .workspace_id = w .id
0 commit comments