chore: add organization search query to workspaces (#14474)

* chore: add organization search query to workspaces
This commit is contained in:
Steven Masley
2024-08-28 15:18:45 -05:00
committed by GitHub
parent 54fe082551
commit b96ac677f1
7 changed files with 109 additions and 78 deletions
+35 -27
View File
@@ -13726,9 +13726,15 @@ WHERE
workspaces.owner_id = $5
ELSE true
END
-- Filter by organization_id
AND CASE
WHEN $6 :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
workspaces.organization_id = $6
ELSE true
END
-- Filter by build parameter
-- @has_param will match any build that includes the parameter.
AND CASE WHEN array_length($6 :: text[], 1) > 0 THEN
AND CASE WHEN array_length($7 :: text[], 1) > 0 THEN
EXISTS (
SELECT
1
@@ -13737,7 +13743,7 @@ WHERE
WHERE
workspace_build_parameters.workspace_build_id = latest_build.id AND
-- ILIKE is case insensitive
workspace_build_parameters.name ILIKE ANY($6)
workspace_build_parameters.name ILIKE ANY($7)
)
ELSE true
END
@@ -13762,40 +13768,40 @@ WHERE
-- Filter by owner_name
AND CASE
WHEN $7 :: text != '' THEN
workspaces.owner_id = (SELECT id FROM users WHERE lower(username) = lower($7) AND deleted = false)
WHEN $8 :: text != '' THEN
workspaces.owner_id = (SELECT id FROM users WHERE lower(username) = lower($8) AND deleted = false)
ELSE true
END
-- Filter by template_name
-- There can be more than 1 template with the same name across organizations.
-- Use the organization filter to restrict to 1 org if needed.
AND CASE
WHEN $8 :: text != '' THEN
workspaces.template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower($8) AND deleted = false)
WHEN $9 :: text != '' THEN
workspaces.template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower($9) AND deleted = false)
ELSE true
END
-- Filter by template_ids
AND CASE
WHEN array_length($9 :: uuid[], 1) > 0 THEN
workspaces.template_id = ANY($9)
WHEN array_length($10 :: uuid[], 1) > 0 THEN
workspaces.template_id = ANY($10)
ELSE true
END
-- Filter by workspace_ids
AND CASE
WHEN array_length($10 :: uuid[], 1) > 0 THEN
workspaces.id = ANY($10)
WHEN array_length($11 :: uuid[], 1) > 0 THEN
workspaces.id = ANY($11)
ELSE true
END
-- Filter by name, matching on substring
AND CASE
WHEN $11 :: text != '' THEN
workspaces.name ILIKE '%' || $11 || '%'
WHEN $12 :: text != '' THEN
workspaces.name ILIKE '%' || $12 || '%'
ELSE true
END
-- Filter by agent status
-- has-agent: is only applicable for workspaces in "start" transition. Stopped and deleted workspaces don't have agents.
AND CASE
WHEN $12 :: text != '' THEN
WHEN $13 :: text != '' THEN
(
SELECT COUNT(*)
FROM
@@ -13807,7 +13813,7 @@ WHERE
WHERE
workspace_resources.job_id = latest_build.provisioner_job_id AND
latest_build.transition = 'start'::workspace_transition AND
$12 = (
$13 = (
CASE
WHEN workspace_agents.first_connected_at IS NULL THEN
CASE
@@ -13818,7 +13824,7 @@ WHERE
END
WHEN workspace_agents.disconnected_at > workspace_agents.last_connected_at THEN
'disconnected'
WHEN NOW() - workspace_agents.last_connected_at > INTERVAL '1 second' * $13 :: bigint THEN
WHEN NOW() - workspace_agents.last_connected_at > INTERVAL '1 second' * $14 :: bigint THEN
'disconnected'
WHEN workspace_agents.last_connected_at IS NOT NULL THEN
'connected'
@@ -13831,24 +13837,24 @@ WHERE
END
-- Filter by dormant workspaces.
AND CASE
WHEN $14 :: boolean != 'false' THEN
WHEN $15 :: boolean != 'false' THEN
dormant_at IS NOT NULL
ELSE true
END
-- Filter by last_used
AND CASE
WHEN $15 :: timestamp with time zone > '0001-01-01 00:00:00Z' THEN
workspaces.last_used_at <= $15
WHEN $16 :: timestamp with time zone > '0001-01-01 00:00:00Z' THEN
workspaces.last_used_at <= $16
ELSE true
END
AND CASE
WHEN $16 :: timestamp with time zone > '0001-01-01 00:00:00Z' THEN
workspaces.last_used_at >= $16
WHEN $17 :: timestamp with time zone > '0001-01-01 00:00:00Z' THEN
workspaces.last_used_at >= $17
ELSE true
END
AND CASE
WHEN $17 :: boolean IS NOT NULL THEN
(latest_build.template_version_id = template.active_version_id) = $17 :: boolean
WHEN $18 :: boolean IS NOT NULL THEN
(latest_build.template_version_id = template.active_version_id) = $18 :: boolean
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
@@ -13860,7 +13866,7 @@ WHERE
filtered_workspaces fw
ORDER BY
-- To ensure that 'favorite' workspaces show up first in the list only for their owner.
CASE WHEN owner_id = $18 AND favorite THEN 0 ELSE 1 END ASC,
CASE WHEN owner_id = $19 AND favorite THEN 0 ELSE 1 END ASC,
(latest_build_completed_at IS NOT NULL AND
latest_build_canceled_at IS NULL AND
latest_build_error IS NULL AND
@@ -13869,11 +13875,11 @@ WHERE
LOWER(name) ASC
LIMIT
CASE
WHEN $20 :: integer > 0 THEN
$20
WHEN $21 :: integer > 0 THEN
$21
END
OFFSET
$19
$20
), filtered_workspaces_order_with_summary AS (
SELECT
fwo.id, fwo.created_at, fwo.updated_at, fwo.owner_id, fwo.organization_id, fwo.template_id, fwo.deleted, fwo.name, fwo.autostart_schedule, fwo.ttl, fwo.last_used_at, fwo.dormant_at, fwo.deleting_at, fwo.automatic_updates, fwo.favorite, fwo.template_name, fwo.template_version_id, fwo.template_version_name, fwo.username, fwo.latest_build_completed_at, fwo.latest_build_canceled_at, fwo.latest_build_error, fwo.latest_build_transition, fwo.latest_build_status
@@ -13909,7 +13915,7 @@ WHERE
'start'::workspace_transition, -- latest_build_transition
'unknown'::provisioner_job_status -- latest_build_status
WHERE
$21 :: boolean = true
$22 :: boolean = true
), total_count AS (
SELECT
count(*) AS count
@@ -13931,6 +13937,7 @@ type GetWorkspacesParams struct {
Deleted bool `db:"deleted" json:"deleted"`
Status string `db:"status" json:"status"`
OwnerID uuid.UUID `db:"owner_id" json:"owner_id"`
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
HasParam []string `db:"has_param" json:"has_param"`
OwnerUsername string `db:"owner_username" json:"owner_username"`
TemplateName string `db:"template_name" json:"template_name"`
@@ -13987,6 +13994,7 @@ func (q *sqlQuerier) GetWorkspaces(ctx context.Context, arg GetWorkspacesParams)
arg.Deleted,
arg.Status,
arg.OwnerID,
arg.OrganizationID,
pq.Array(arg.HasParam),
arg.OwnerUsername,
arg.TemplateName,