mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
Preset tables, latest workspace view, state logic improved
Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
@ -5396,44 +5396,45 @@ func (q *sqlQuerier) GetParameterSchemasByJobID(ctx context.Context, jobID uuid.
|
||||
}
|
||||
|
||||
const getTemplatePrebuildState = `-- name: GetTemplatePrebuildState :many
|
||||
WITH latest_workspace_builds AS (SELECT wb.id,
|
||||
wb.workspace_id,
|
||||
wbmax.template_id,
|
||||
wb.template_version_id
|
||||
FROM (SELECT tv.template_id,
|
||||
wbmax.workspace_id,
|
||||
MAX(wbmax.build_number) as max_build_number
|
||||
FROM workspace_builds wbmax
|
||||
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
|
||||
GROUP BY tv.template_id, wbmax.workspace_id) wbmax
|
||||
JOIN workspace_builds wb ON (
|
||||
wb.workspace_id = wbmax.workspace_id
|
||||
AND wb.build_number = wbmax.max_build_number
|
||||
))
|
||||
SELECT CAST(1 AS integer) AS desired,
|
||||
CAST(COUNT(wp.*) AS integer) AS actual,
|
||||
CAST(0 AS integer) AS extraneous, -- TODO: calculate this by subtracting actual from count not matching template version
|
||||
WITH
|
||||
-- All prebuilds currently running
|
||||
running_prebuilds AS (SELECT p.id, p.created_at, p.updated_at, p.owner_id, p.organization_id, p.template_id, p.deleted, p.name, p.autostart_schedule, p.ttl, p.last_used_at, p.dormant_at, p.deleting_at, p.automatic_updates, p.favorite, p.next_start_at, b.template_version_id
|
||||
FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
||||
WHERE b.transition = 'start'::workspace_transition),
|
||||
-- All templates which have been configured for prebuilds (any version)
|
||||
templates_with_prebuilds AS (SELECT t.id AS template_id,
|
||||
tv.id AS template_version_id,
|
||||
tv.id = t.active_version_id AS using_active_version,
|
||||
tvpp.desired_instances,
|
||||
t.deleted,
|
||||
t.deprecated != '' AS deprecated
|
||||
FROM templates t
|
||||
INNER JOIN template_versions tv ON tv.template_id = t.id
|
||||
INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id
|
||||
INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id
|
||||
WHERE t.id = $1::uuid
|
||||
GROUP BY t.id, tv.id, tvpp.id)
|
||||
SELECT t.template_id,
|
||||
COUNT(p.id) AS actual, -- running prebuilds for active version
|
||||
MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END) AS desired, -- we only care about the active version's desired instances
|
||||
SUM(CASE WHEN t.using_active_version THEN 0 ELSE 1 END) AS extraneous, -- running prebuilds for inactive version
|
||||
t.deleted,
|
||||
t.deprecated,
|
||||
tv.id AS template_version_id
|
||||
FROM latest_workspace_builds lwb
|
||||
JOIN template_versions tv ON lwb.template_version_id = tv.id
|
||||
JOIN templates t ON t.id = lwb.template_id
|
||||
LEFT JOIN workspace_prebuilds wp ON wp.id = lwb.workspace_id
|
||||
WHERE t.id = $1::uuid
|
||||
GROUP BY t.id, t.deleted, t.deprecated, tv.id
|
||||
t.deprecated
|
||||
FROM templates_with_prebuilds t
|
||||
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
||||
GROUP BY t.template_id, p.id, t.deleted, t.deprecated
|
||||
`
|
||||
|
||||
type GetTemplatePrebuildStateRow struct {
|
||||
Desired int32 `db:"desired" json:"desired"`
|
||||
Actual int32 `db:"actual" json:"actual"`
|
||||
Extraneous int32 `db:"extraneous" json:"extraneous"`
|
||||
Deleted bool `db:"deleted" json:"deleted"`
|
||||
Deprecated string `db:"deprecated" json:"deprecated"`
|
||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||
Actual int64 `db:"actual" json:"actual"`
|
||||
Desired interface{} `db:"desired" json:"desired"`
|
||||
Extraneous int64 `db:"extraneous" json:"extraneous"`
|
||||
Deleted bool `db:"deleted" json:"deleted"`
|
||||
Deprecated bool `db:"deprecated" json:"deprecated"`
|
||||
}
|
||||
|
||||
// TODO: need to store the desired instances & autoscaling schedules in db; use desired value here
|
||||
func (q *sqlQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) ([]GetTemplatePrebuildStateRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getTemplatePrebuildState, templateID)
|
||||
if err != nil {
|
||||
@ -5444,12 +5445,12 @@ func (q *sqlQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uu
|
||||
for rows.Next() {
|
||||
var i GetTemplatePrebuildStateRow
|
||||
if err := rows.Scan(
|
||||
&i.Desired,
|
||||
&i.TemplateID,
|
||||
&i.Actual,
|
||||
&i.Desired,
|
||||
&i.Extraneous,
|
||||
&i.Deleted,
|
||||
&i.Deprecated,
|
||||
&i.TemplateVersionID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user