mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
Filter query: has-agent connecting, connected, disconnected, timeout (#5145)
* WIP * has-agent:connecting, connected * Fix * Fix * has-agent:disconnected, timeout * Fix: typo * Fix * TODOs * databasefake * Fix: typo * More TODOs * databasefake * Timeout tests * Address PR comments * Implement FIXMEs * Renamings * Address PR comments * Fix: readability * Fix: refactor CASE logic * CASE logic * Fix * Use CTE * Polishing * Comment * WIP * IS NOT NULL * Without CTE * One more optimization * 2nd optimization
This commit is contained in:
@@ -45,6 +45,7 @@ FROM
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
workspace_builds.transition,
|
||||
provisioner_jobs.id AS provisioner_job_id,
|
||||
provisioner_jobs.started_at,
|
||||
provisioner_jobs.updated_at,
|
||||
provisioner_jobs.canceled_at,
|
||||
@@ -146,7 +147,7 @@ WHERE
|
||||
-- Use the organization filter to restrict to 1 org if needed.
|
||||
AND CASE
|
||||
WHEN @template_name :: text != '' THEN
|
||||
template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower(@template_name) AND deleted = false)
|
||||
template_id = ANY(SELECT id FROM templates WHERE lower(name) = lower(@template_name) AND deleted = false)
|
||||
ELSE true
|
||||
END
|
||||
-- Filter by template_ids
|
||||
@@ -161,17 +162,54 @@ WHERE
|
||||
name ILIKE '%' || @name || '%'
|
||||
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 @has_agent :: text != '' THEN
|
||||
(
|
||||
SELECT COUNT(*)
|
||||
FROM
|
||||
workspace_resources
|
||||
JOIN
|
||||
workspace_agents
|
||||
ON
|
||||
workspace_agents.resource_id = workspace_resources.id
|
||||
WHERE
|
||||
workspace_resources.job_id = latest_build.provisioner_job_id AND
|
||||
latest_build.transition = 'start'::workspace_transition AND
|
||||
@has_agent = (
|
||||
CASE
|
||||
WHEN workspace_agents.first_connected_at IS NULL THEN
|
||||
CASE
|
||||
WHEN workspace_agents.connection_timeout_seconds > 0 AND NOW() - workspace_agents.created_at > workspace_agents.connection_timeout_seconds * INTERVAL '1 second' THEN
|
||||
'timeout'
|
||||
ELSE
|
||||
'connecting'
|
||||
END
|
||||
WHEN workspace_agents.disconnected_at > workspace_agents.last_connected_at THEN
|
||||
'disconnected'
|
||||
WHEN NOW() - workspace_agents.last_connected_at > INTERVAL '1 second' * @agent_inactive_disconnect_timeout_seconds :: bigint THEN
|
||||
'disconnected'
|
||||
WHEN workspace_agents.last_connected_at IS NOT NULL THEN
|
||||
'connected'
|
||||
ELSE
|
||||
NULL
|
||||
END
|
||||
)
|
||||
) > 0
|
||||
ELSE true
|
||||
END
|
||||
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
|
||||
-- @authorize_filter
|
||||
ORDER BY
|
||||
last_used_at DESC
|
||||
last_used_at DESC
|
||||
LIMIT
|
||||
CASE
|
||||
WHEN @limit_ :: integer > 0 THEN
|
||||
@limit_
|
||||
END
|
||||
CASE
|
||||
WHEN @limit_ :: integer > 0 THEN
|
||||
@limit_
|
||||
END
|
||||
OFFSET
|
||||
@offset_
|
||||
@offset_
|
||||
;
|
||||
|
||||
-- name: GetWorkspaceByOwnerIDAndName :one
|
||||
|
||||
Reference in New Issue
Block a user