mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
Preset tables, latest workspace view, state logic improved
Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
51
coderd/database/dump.sql
generated
51
coderd/database/dump.sql
generated
@ -1275,6 +1275,21 @@ CREATE TABLE template_version_preset_parameters (
|
|||||||
value text NOT NULL
|
value text NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE template_version_preset_prebuild_schedules (
|
||||||
|
id uuid NOT NULL,
|
||||||
|
preset_prebuild_id uuid NOT NULL,
|
||||||
|
timezone text NOT NULL,
|
||||||
|
cron_schedule text NOT NULL,
|
||||||
|
desired_instances integer NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE template_version_preset_prebuilds (
|
||||||
|
id uuid NOT NULL,
|
||||||
|
preset_id uuid NOT NULL,
|
||||||
|
desired_instances integer NOT NULL,
|
||||||
|
invalidate_after_secs integer DEFAULT 0
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE template_version_presets (
|
CREATE TABLE template_version_presets (
|
||||||
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
id uuid DEFAULT gen_random_uuid() NOT NULL,
|
||||||
template_version_id uuid NOT NULL,
|
template_version_id uuid NOT NULL,
|
||||||
@ -1773,6 +1788,30 @@ CREATE VIEW workspace_build_with_user AS
|
|||||||
|
|
||||||
COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
|
COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
|
||||||
|
|
||||||
|
CREATE VIEW workspace_latest_build AS
|
||||||
|
SELECT wb.id,
|
||||||
|
wb.created_at,
|
||||||
|
wb.updated_at,
|
||||||
|
wb.workspace_id,
|
||||||
|
wb.template_version_id,
|
||||||
|
wb.build_number,
|
||||||
|
wb.transition,
|
||||||
|
wb.initiator_id,
|
||||||
|
wb.provisioner_state,
|
||||||
|
wb.job_id,
|
||||||
|
wb.deadline,
|
||||||
|
wb.reason,
|
||||||
|
wb.daily_cost,
|
||||||
|
wb.max_deadline,
|
||||||
|
wb.template_version_preset_id
|
||||||
|
FROM (( SELECT tv.template_id,
|
||||||
|
wbmax_1.workspace_id,
|
||||||
|
max(wbmax_1.build_number) AS max_build_number
|
||||||
|
FROM (workspace_builds wbmax_1
|
||||||
|
JOIN template_versions tv ON ((tv.id = wbmax_1.template_version_id)))
|
||||||
|
GROUP BY tv.template_id, wbmax_1.workspace_id) wbmax
|
||||||
|
JOIN workspace_builds wb ON (((wb.workspace_id = wbmax.workspace_id) AND (wb.build_number = wbmax.max_build_number))));
|
||||||
|
|
||||||
CREATE TABLE workspace_modules (
|
CREATE TABLE workspace_modules (
|
||||||
id uuid NOT NULL,
|
id uuid NOT NULL,
|
||||||
job_id uuid NOT NULL,
|
job_id uuid NOT NULL,
|
||||||
@ -2104,6 +2143,12 @@ ALTER TABLE ONLY template_version_parameters
|
|||||||
ALTER TABLE ONLY template_version_preset_parameters
|
ALTER TABLE ONLY template_version_preset_parameters
|
||||||
ADD CONSTRAINT template_version_preset_parameters_pkey PRIMARY KEY (id);
|
ADD CONSTRAINT template_version_preset_parameters_pkey PRIMARY KEY (id);
|
||||||
|
|
||||||
|
ALTER TABLE ONLY template_version_preset_prebuild_schedules
|
||||||
|
ADD CONSTRAINT template_version_preset_prebuild_schedules_pkey PRIMARY KEY (id);
|
||||||
|
|
||||||
|
ALTER TABLE ONLY template_version_preset_prebuilds
|
||||||
|
ADD CONSTRAINT template_version_preset_prebuilds_pkey PRIMARY KEY (id);
|
||||||
|
|
||||||
ALTER TABLE ONLY template_version_presets
|
ALTER TABLE ONLY template_version_presets
|
||||||
ADD CONSTRAINT template_version_presets_pkey PRIMARY KEY (id);
|
ADD CONSTRAINT template_version_presets_pkey PRIMARY KEY (id);
|
||||||
|
|
||||||
@ -2502,6 +2547,12 @@ ALTER TABLE ONLY template_version_parameters
|
|||||||
ALTER TABLE ONLY template_version_preset_parameters
|
ALTER TABLE ONLY template_version_preset_parameters
|
||||||
ADD CONSTRAINT template_version_preset_paramet_template_version_preset_id_fkey FOREIGN KEY (template_version_preset_id) REFERENCES template_version_presets(id) ON DELETE CASCADE;
|
ADD CONSTRAINT template_version_preset_paramet_template_version_preset_id_fkey FOREIGN KEY (template_version_preset_id) REFERENCES template_version_presets(id) ON DELETE CASCADE;
|
||||||
|
|
||||||
|
ALTER TABLE ONLY template_version_preset_prebuild_schedules
|
||||||
|
ADD CONSTRAINT template_version_preset_prebuild_schedu_preset_prebuild_id_fkey FOREIGN KEY (preset_prebuild_id) REFERENCES template_version_preset_prebuilds(id) ON DELETE CASCADE;
|
||||||
|
|
||||||
|
ALTER TABLE ONLY template_version_preset_prebuilds
|
||||||
|
ADD CONSTRAINT template_version_preset_prebuilds_preset_id_fkey FOREIGN KEY (preset_id) REFERENCES template_version_presets(id) ON DELETE CASCADE;
|
||||||
|
|
||||||
ALTER TABLE ONLY template_version_presets
|
ALTER TABLE ONLY template_version_presets
|
||||||
ADD CONSTRAINT template_version_presets_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
ADD CONSTRAINT template_version_presets_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
||||||
|
|
||||||
|
@ -41,6 +41,8 @@ const (
|
|||||||
ForeignKeyTailnetTunnelsCoordinatorID ForeignKeyConstraint = "tailnet_tunnels_coordinator_id_fkey" // ALTER TABLE ONLY tailnet_tunnels ADD CONSTRAINT tailnet_tunnels_coordinator_id_fkey FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators(id) ON DELETE CASCADE;
|
ForeignKeyTailnetTunnelsCoordinatorID ForeignKeyConstraint = "tailnet_tunnels_coordinator_id_fkey" // ALTER TABLE ONLY tailnet_tunnels ADD CONSTRAINT tailnet_tunnels_coordinator_id_fkey FOREIGN KEY (coordinator_id) REFERENCES tailnet_coordinators(id) ON DELETE CASCADE;
|
||||||
ForeignKeyTemplateVersionParametersTemplateVersionID ForeignKeyConstraint = "template_version_parameters_template_version_id_fkey" // ALTER TABLE ONLY template_version_parameters ADD CONSTRAINT template_version_parameters_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
ForeignKeyTemplateVersionParametersTemplateVersionID ForeignKeyConstraint = "template_version_parameters_template_version_id_fkey" // ALTER TABLE ONLY template_version_parameters ADD CONSTRAINT template_version_parameters_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
||||||
ForeignKeyTemplateVersionPresetParametTemplateVersionPresetID ForeignKeyConstraint = "template_version_preset_paramet_template_version_preset_id_fkey" // ALTER TABLE ONLY template_version_preset_parameters ADD CONSTRAINT template_version_preset_paramet_template_version_preset_id_fkey FOREIGN KEY (template_version_preset_id) REFERENCES template_version_presets(id) ON DELETE CASCADE;
|
ForeignKeyTemplateVersionPresetParametTemplateVersionPresetID ForeignKeyConstraint = "template_version_preset_paramet_template_version_preset_id_fkey" // ALTER TABLE ONLY template_version_preset_parameters ADD CONSTRAINT template_version_preset_paramet_template_version_preset_id_fkey FOREIGN KEY (template_version_preset_id) REFERENCES template_version_presets(id) ON DELETE CASCADE;
|
||||||
|
ForeignKeyTemplateVersionPresetPrebuildScheduPresetPrebuildID ForeignKeyConstraint = "template_version_preset_prebuild_schedu_preset_prebuild_id_fkey" // ALTER TABLE ONLY template_version_preset_prebuild_schedules ADD CONSTRAINT template_version_preset_prebuild_schedu_preset_prebuild_id_fkey FOREIGN KEY (preset_prebuild_id) REFERENCES template_version_preset_prebuilds(id) ON DELETE CASCADE;
|
||||||
|
ForeignKeyTemplateVersionPresetPrebuildsPresetID ForeignKeyConstraint = "template_version_preset_prebuilds_preset_id_fkey" // ALTER TABLE ONLY template_version_preset_prebuilds ADD CONSTRAINT template_version_preset_prebuilds_preset_id_fkey FOREIGN KEY (preset_id) REFERENCES template_version_presets(id) ON DELETE CASCADE;
|
||||||
ForeignKeyTemplateVersionPresetsTemplateVersionID ForeignKeyConstraint = "template_version_presets_template_version_id_fkey" // ALTER TABLE ONLY template_version_presets ADD CONSTRAINT template_version_presets_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
ForeignKeyTemplateVersionPresetsTemplateVersionID ForeignKeyConstraint = "template_version_presets_template_version_id_fkey" // ALTER TABLE ONLY template_version_presets ADD CONSTRAINT template_version_presets_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
||||||
ForeignKeyTemplateVersionVariablesTemplateVersionID ForeignKeyConstraint = "template_version_variables_template_version_id_fkey" // ALTER TABLE ONLY template_version_variables ADD CONSTRAINT template_version_variables_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
ForeignKeyTemplateVersionVariablesTemplateVersionID ForeignKeyConstraint = "template_version_variables_template_version_id_fkey" // ALTER TABLE ONLY template_version_variables ADD CONSTRAINT template_version_variables_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
||||||
ForeignKeyTemplateVersionWorkspaceTagsTemplateVersionID ForeignKeyConstraint = "template_version_workspace_tags_template_version_id_fkey" // ALTER TABLE ONLY template_version_workspace_tags ADD CONSTRAINT template_version_workspace_tags_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
ForeignKeyTemplateVersionWorkspaceTagsTemplateVersionID ForeignKeyConstraint = "template_version_workspace_tags_template_version_id_fkey" // ALTER TABLE ONLY template_version_workspace_tags ADD CONSTRAINT template_version_workspace_tags_template_version_id_fkey FOREIGN KEY (template_version_id) REFERENCES template_versions(id) ON DELETE CASCADE;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
-- Revert prebuild views
|
-- Revert prebuild views
|
||||||
DROP VIEW IF EXISTS workspace_prebuild_builds;
|
DROP VIEW IF EXISTS workspace_prebuild_builds;
|
||||||
DROP VIEW IF EXISTS workspace_prebuilds;
|
DROP VIEW IF EXISTS workspace_prebuilds;
|
||||||
|
DROP VIEW IF EXISTS workspace_latest_build;
|
||||||
|
|
||||||
-- Revert user operations
|
-- Revert user operations
|
||||||
DELETE FROM user_status_changes WHERE user_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0';
|
DELETE FROM user_status_changes WHERE user_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0';
|
||||||
|
@ -12,3 +12,17 @@ CREATE VIEW workspace_prebuild_builds AS
|
|||||||
SELECT workspace_id
|
SELECT workspace_id
|
||||||
FROM workspace_builds
|
FROM workspace_builds
|
||||||
WHERE initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0';
|
WHERE initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0';
|
||||||
|
|
||||||
|
CREATE VIEW workspace_latest_build AS
|
||||||
|
SELECT wb.*
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
DROP TABLE IF EXISTS template_version_preset_prebuild_schedules;
|
||||||
|
DROP TABLE IF EXISTS template_version_preset_prebuilds;
|
||||||
|
|
||||||
|
@ -1 +1,21 @@
|
|||||||
CREATE TABLE
|
CREATE TABLE template_version_preset_prebuilds
|
||||||
|
(
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
preset_id UUID NOT NULL,
|
||||||
|
desired_instances INT NOT NULL,
|
||||||
|
invalidate_after_secs INT NULL DEFAULT 0,
|
||||||
|
|
||||||
|
-- Deletion should never occur, but if we allow it we should check no prebuilds exist attached to this preset first.
|
||||||
|
FOREIGN KEY (preset_id) REFERENCES template_version_presets (id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE template_version_preset_prebuild_schedules
|
||||||
|
(
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
preset_prebuild_id UUID NOT NULL,
|
||||||
|
timezone TEXT NOT NULL,
|
||||||
|
cron_schedule TEXT NOT NULL,
|
||||||
|
desired_instances INT NOT NULL,
|
||||||
|
|
||||||
|
FOREIGN KEY (preset_prebuild_id) REFERENCES template_version_preset_prebuilds (id) ON DELETE CASCADE
|
||||||
|
);
|
||||||
|
@ -2968,6 +2968,21 @@ type TemplateVersionPresetParameter struct {
|
|||||||
Value string `db:"value" json:"value"`
|
Value string `db:"value" json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TemplateVersionPresetPrebuild struct {
|
||||||
|
ID uuid.UUID `db:"id" json:"id"`
|
||||||
|
PresetID uuid.UUID `db:"preset_id" json:"preset_id"`
|
||||||
|
DesiredInstances int32 `db:"desired_instances" json:"desired_instances"`
|
||||||
|
InvalidateAfterSecs sql.NullInt32 `db:"invalidate_after_secs" json:"invalidate_after_secs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TemplateVersionPresetPrebuildSchedule struct {
|
||||||
|
ID uuid.UUID `db:"id" json:"id"`
|
||||||
|
PresetPrebuildID uuid.UUID `db:"preset_prebuild_id" json:"preset_prebuild_id"`
|
||||||
|
Timezone string `db:"timezone" json:"timezone"`
|
||||||
|
CronSchedule string `db:"cron_schedule" json:"cron_schedule"`
|
||||||
|
DesiredInstances int32 `db:"desired_instances" json:"desired_instances"`
|
||||||
|
}
|
||||||
|
|
||||||
type TemplateVersionTable struct {
|
type TemplateVersionTable struct {
|
||||||
ID uuid.UUID `db:"id" json:"id"`
|
ID uuid.UUID `db:"id" json:"id"`
|
||||||
TemplateID uuid.NullUUID `db:"template_id" json:"template_id"`
|
TemplateID uuid.NullUUID `db:"template_id" json:"template_id"`
|
||||||
@ -3344,6 +3359,24 @@ type WorkspaceBuildTable struct {
|
|||||||
TemplateVersionPresetID uuid.NullUUID `db:"template_version_preset_id" json:"template_version_preset_id"`
|
TemplateVersionPresetID uuid.NullUUID `db:"template_version_preset_id" json:"template_version_preset_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WorkspaceLatestBuild struct {
|
||||||
|
ID uuid.UUID `db:"id" json:"id"`
|
||||||
|
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||||
|
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||||
|
WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"`
|
||||||
|
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||||
|
BuildNumber int32 `db:"build_number" json:"build_number"`
|
||||||
|
Transition WorkspaceTransition `db:"transition" json:"transition"`
|
||||||
|
InitiatorID uuid.UUID `db:"initiator_id" json:"initiator_id"`
|
||||||
|
ProvisionerState []byte `db:"provisioner_state" json:"provisioner_state"`
|
||||||
|
JobID uuid.UUID `db:"job_id" json:"job_id"`
|
||||||
|
Deadline time.Time `db:"deadline" json:"deadline"`
|
||||||
|
Reason BuildReason `db:"reason" json:"reason"`
|
||||||
|
DailyCost int32 `db:"daily_cost" json:"daily_cost"`
|
||||||
|
MaxDeadline time.Time `db:"max_deadline" json:"max_deadline"`
|
||||||
|
TemplateVersionPresetID uuid.NullUUID `db:"template_version_preset_id" json:"template_version_preset_id"`
|
||||||
|
}
|
||||||
|
|
||||||
type WorkspaceModule struct {
|
type WorkspaceModule struct {
|
||||||
ID uuid.UUID `db:"id" json:"id"`
|
ID uuid.UUID `db:"id" json:"id"`
|
||||||
JobID uuid.UUID `db:"job_id" json:"job_id"`
|
JobID uuid.UUID `db:"job_id" json:"job_id"`
|
||||||
|
@ -265,7 +265,6 @@ type sqlcQuerier interface {
|
|||||||
// created in the timeframe and return the aggregate usage counts of parameter
|
// created in the timeframe and return the aggregate usage counts of parameter
|
||||||
// values.
|
// values.
|
||||||
GetTemplateParameterInsights(ctx context.Context, arg GetTemplateParameterInsightsParams) ([]GetTemplateParameterInsightsRow, error)
|
GetTemplateParameterInsights(ctx context.Context, arg GetTemplateParameterInsightsParams) ([]GetTemplateParameterInsightsRow, error)
|
||||||
// TODO: need to store the desired instances & autoscaling schedules in db; use desired value here
|
|
||||||
GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) ([]GetTemplatePrebuildStateRow, error)
|
GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) ([]GetTemplatePrebuildStateRow, error)
|
||||||
GetTemplateUsageStats(ctx context.Context, arg GetTemplateUsageStatsParams) ([]TemplateUsageStat, error)
|
GetTemplateUsageStats(ctx context.Context, arg GetTemplateUsageStatsParams) ([]TemplateUsageStat, error)
|
||||||
GetTemplateVersionByID(ctx context.Context, id uuid.UUID) (TemplateVersion, error)
|
GetTemplateVersionByID(ctx context.Context, id uuid.UUID) (TemplateVersion, error)
|
||||||
|
@ -5396,44 +5396,45 @@ func (q *sqlQuerier) GetParameterSchemasByJobID(ctx context.Context, jobID uuid.
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getTemplatePrebuildState = `-- name: GetTemplatePrebuildState :many
|
const getTemplatePrebuildState = `-- name: GetTemplatePrebuildState :many
|
||||||
WITH latest_workspace_builds AS (SELECT wb.id,
|
WITH
|
||||||
wb.workspace_id,
|
-- All prebuilds currently running
|
||||||
wbmax.template_id,
|
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
|
||||||
wb.template_version_id
|
FROM workspace_prebuilds p
|
||||||
FROM (SELECT tv.template_id,
|
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
||||||
wbmax.workspace_id,
|
WHERE b.transition = 'start'::workspace_transition),
|
||||||
MAX(wbmax.build_number) as max_build_number
|
-- All templates which have been configured for prebuilds (any version)
|
||||||
FROM workspace_builds wbmax
|
templates_with_prebuilds AS (SELECT t.id AS template_id,
|
||||||
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
|
tv.id AS template_version_id,
|
||||||
GROUP BY tv.template_id, wbmax.workspace_id) wbmax
|
tv.id = t.active_version_id AS using_active_version,
|
||||||
JOIN workspace_builds wb ON (
|
tvpp.desired_instances,
|
||||||
wb.workspace_id = wbmax.workspace_id
|
t.deleted,
|
||||||
AND wb.build_number = wbmax.max_build_number
|
t.deprecated != '' AS deprecated
|
||||||
))
|
FROM templates t
|
||||||
SELECT CAST(1 AS integer) AS desired,
|
INNER JOIN template_versions tv ON tv.template_id = t.id
|
||||||
CAST(COUNT(wp.*) AS integer) AS actual,
|
INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id
|
||||||
CAST(0 AS integer) AS extraneous, -- TODO: calculate this by subtracting actual from count not matching template version
|
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.deleted,
|
||||||
t.deprecated,
|
t.deprecated
|
||||||
tv.id AS template_version_id
|
FROM templates_with_prebuilds t
|
||||||
FROM latest_workspace_builds lwb
|
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
||||||
JOIN template_versions tv ON lwb.template_version_id = tv.id
|
GROUP BY t.template_id, p.id, t.deleted, t.deprecated
|
||||||
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
|
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetTemplatePrebuildStateRow struct {
|
type GetTemplatePrebuildStateRow struct {
|
||||||
Desired int32 `db:"desired" json:"desired"`
|
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||||
Actual int32 `db:"actual" json:"actual"`
|
Actual int64 `db:"actual" json:"actual"`
|
||||||
Extraneous int32 `db:"extraneous" json:"extraneous"`
|
Desired interface{} `db:"desired" json:"desired"`
|
||||||
Deleted bool `db:"deleted" json:"deleted"`
|
Extraneous int64 `db:"extraneous" json:"extraneous"`
|
||||||
Deprecated string `db:"deprecated" json:"deprecated"`
|
Deleted bool `db:"deleted" json:"deleted"`
|
||||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
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) {
|
func (q *sqlQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) ([]GetTemplatePrebuildStateRow, error) {
|
||||||
rows, err := q.db.QueryContext(ctx, getTemplatePrebuildState, templateID)
|
rows, err := q.db.QueryContext(ctx, getTemplatePrebuildState, templateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -5444,12 +5445,12 @@ func (q *sqlQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uu
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var i GetTemplatePrebuildStateRow
|
var i GetTemplatePrebuildStateRow
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&i.Desired,
|
&i.TemplateID,
|
||||||
&i.Actual,
|
&i.Actual,
|
||||||
|
&i.Desired,
|
||||||
&i.Extraneous,
|
&i.Extraneous,
|
||||||
&i.Deleted,
|
&i.Deleted,
|
||||||
&i.Deprecated,
|
&i.Deprecated,
|
||||||
&i.TemplateVersionID,
|
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1,28 +1,29 @@
|
|||||||
-- name: GetTemplatePrebuildState :many
|
-- name: GetTemplatePrebuildState :many
|
||||||
WITH latest_workspace_builds AS (SELECT wb.id,
|
WITH
|
||||||
wb.workspace_id,
|
-- All prebuilds currently running
|
||||||
wbmax.template_id,
|
running_prebuilds AS (SELECT p.*, b.template_version_id
|
||||||
wb.template_version_id
|
FROM workspace_prebuilds p
|
||||||
FROM (SELECT tv.template_id,
|
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
||||||
wbmax.workspace_id,
|
WHERE b.transition = 'start'::workspace_transition),
|
||||||
MAX(wbmax.build_number) as max_build_number
|
-- All templates which have been configured for prebuilds (any version)
|
||||||
FROM workspace_builds wbmax
|
templates_with_prebuilds AS (SELECT t.id AS template_id,
|
||||||
JOIN template_versions tv ON (tv.id = wbmax.template_version_id)
|
tv.id AS template_version_id,
|
||||||
GROUP BY tv.template_id, wbmax.workspace_id) wbmax
|
tv.id = t.active_version_id AS using_active_version,
|
||||||
JOIN workspace_builds wb ON (
|
tvpp.desired_instances,
|
||||||
wb.workspace_id = wbmax.workspace_id
|
t.deleted,
|
||||||
AND wb.build_number = wbmax.max_build_number
|
t.deprecated != '' AS deprecated
|
||||||
))
|
FROM templates t
|
||||||
-- TODO: need to store the desired instances & autoscaling schedules in db; use desired value here
|
INNER JOIN template_versions tv ON tv.template_id = t.id
|
||||||
SELECT CAST(1 AS integer) AS desired,
|
INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id
|
||||||
CAST(COUNT(wp.*) AS integer) AS actual,
|
INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id
|
||||||
CAST(0 AS integer) AS extraneous, -- TODO: calculate this by subtracting actual from count not matching template version
|
WHERE t.id = @template_id::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.deleted,
|
||||||
t.deprecated,
|
t.deprecated
|
||||||
tv.id AS template_version_id
|
FROM templates_with_prebuilds t
|
||||||
FROM latest_workspace_builds lwb
|
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
||||||
JOIN template_versions tv ON lwb.template_version_id = tv.id
|
GROUP BY t.template_id, p.id, t.deleted, t.deprecated;
|
||||||
JOIN templates t ON t.id = lwb.template_id
|
|
||||||
LEFT JOIN workspace_prebuilds wp ON wp.id = lwb.workspace_id
|
|
||||||
WHERE t.id = @template_id::uuid
|
|
||||||
GROUP BY t.id, t.deleted, t.deprecated, tv.id;
|
|
||||||
|
@ -59,6 +59,8 @@ const (
|
|||||||
UniqueTemplateUsageStatsPkey UniqueConstraint = "template_usage_stats_pkey" // ALTER TABLE ONLY template_usage_stats ADD CONSTRAINT template_usage_stats_pkey PRIMARY KEY (start_time, template_id, user_id);
|
UniqueTemplateUsageStatsPkey UniqueConstraint = "template_usage_stats_pkey" // ALTER TABLE ONLY template_usage_stats ADD CONSTRAINT template_usage_stats_pkey PRIMARY KEY (start_time, template_id, user_id);
|
||||||
UniqueTemplateVersionParametersTemplateVersionIDNameKey UniqueConstraint = "template_version_parameters_template_version_id_name_key" // ALTER TABLE ONLY template_version_parameters ADD CONSTRAINT template_version_parameters_template_version_id_name_key UNIQUE (template_version_id, name);
|
UniqueTemplateVersionParametersTemplateVersionIDNameKey UniqueConstraint = "template_version_parameters_template_version_id_name_key" // ALTER TABLE ONLY template_version_parameters ADD CONSTRAINT template_version_parameters_template_version_id_name_key UNIQUE (template_version_id, name);
|
||||||
UniqueTemplateVersionPresetParametersPkey UniqueConstraint = "template_version_preset_parameters_pkey" // ALTER TABLE ONLY template_version_preset_parameters ADD CONSTRAINT template_version_preset_parameters_pkey PRIMARY KEY (id);
|
UniqueTemplateVersionPresetParametersPkey UniqueConstraint = "template_version_preset_parameters_pkey" // ALTER TABLE ONLY template_version_preset_parameters ADD CONSTRAINT template_version_preset_parameters_pkey PRIMARY KEY (id);
|
||||||
|
UniqueTemplateVersionPresetPrebuildSchedulesPkey UniqueConstraint = "template_version_preset_prebuild_schedules_pkey" // ALTER TABLE ONLY template_version_preset_prebuild_schedules ADD CONSTRAINT template_version_preset_prebuild_schedules_pkey PRIMARY KEY (id);
|
||||||
|
UniqueTemplateVersionPresetPrebuildsPkey UniqueConstraint = "template_version_preset_prebuilds_pkey" // ALTER TABLE ONLY template_version_preset_prebuilds ADD CONSTRAINT template_version_preset_prebuilds_pkey PRIMARY KEY (id);
|
||||||
UniqueTemplateVersionPresetsPkey UniqueConstraint = "template_version_presets_pkey" // ALTER TABLE ONLY template_version_presets ADD CONSTRAINT template_version_presets_pkey PRIMARY KEY (id);
|
UniqueTemplateVersionPresetsPkey UniqueConstraint = "template_version_presets_pkey" // ALTER TABLE ONLY template_version_presets ADD CONSTRAINT template_version_presets_pkey PRIMARY KEY (id);
|
||||||
UniqueTemplateVersionVariablesTemplateVersionIDNameKey UniqueConstraint = "template_version_variables_template_version_id_name_key" // ALTER TABLE ONLY template_version_variables ADD CONSTRAINT template_version_variables_template_version_id_name_key UNIQUE (template_version_id, name);
|
UniqueTemplateVersionVariablesTemplateVersionIDNameKey UniqueConstraint = "template_version_variables_template_version_id_name_key" // ALTER TABLE ONLY template_version_variables ADD CONSTRAINT template_version_variables_template_version_id_name_key UNIQUE (template_version_id, name);
|
||||||
UniqueTemplateVersionWorkspaceTagsTemplateVersionIDKeyKey UniqueConstraint = "template_version_workspace_tags_template_version_id_key_key" // ALTER TABLE ONLY template_version_workspace_tags ADD CONSTRAINT template_version_workspace_tags_template_version_id_key_key UNIQUE (template_version_id, key);
|
UniqueTemplateVersionWorkspaceTagsTemplateVersionIDKeyKey UniqueConstraint = "template_version_workspace_tags_template_version_id_key_key" // ALTER TABLE ONLY template_version_workspace_tags ADD CONSTRAINT template_version_workspace_tags_template_version_id_key_key UNIQUE (template_version_id, key);
|
||||||
|
@ -149,12 +149,12 @@ func (c Controller) reconcileTemplate(ctx context.Context, template database.Tem
|
|||||||
|
|
||||||
// If the template has become deleted or deprecated since the last reconciliation, we need to ensure we
|
// If the template has become deleted or deprecated since the last reconciliation, we need to ensure we
|
||||||
// scale those prebuilds down to zero.
|
// scale those prebuilds down to zero.
|
||||||
if result.Deleted || result.Deprecated == "" {
|
if result.Deleted || result.Deprecated {
|
||||||
desired = 0
|
desired = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
c.logger.Info(innerCtx, "template prebuild state retrieved",
|
c.logger.Info(innerCtx, "template prebuild state retrieved",
|
||||||
slog.F("template_id", template.ID), slog.F("template_version_id", result.TemplateVersionID),
|
slog.F("template_id", template.ID),
|
||||||
slog.F("desired", desired), slog.F("actual", actual), slog.F("extraneous", extraneous))
|
slog.F("desired", desired), slog.F("actual", actual), slog.F("extraneous", extraneous))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user