Skip to content

feat: add has-ai-task filters to the /workspaces and /templates endpoints #18387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13233,6 +13233,17 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
continue
}
}

if arg.HasAITask.Valid {
build, err := q.getTemplateVersionByIDNoLock(ctx, template.ActiveVersionID)
if err != nil {
return nil, xerrors.Errorf("get latest build: %w", err)
}
if build.HasAITask != arg.HasAITask.Bool {
continue
}
}

templates = append(templates, template)
}
if len(templates) > 0 {
Expand Down Expand Up @@ -13562,6 +13573,16 @@ func (q *FakeQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg database.
}
}

if arg.HasAITask.Valid {
build, err := q.getLatestWorkspaceBuildByWorkspaceIDNoLock(ctx, workspace.ID)
if err != nil {
return nil, xerrors.Errorf("get latest build: %w", err)
}
if build.HasAITask != arg.HasAITask.Bool {
continue
}
}

// If the filter exists, ensure the object is authorized.
if prepared != nil && prepared.Authorize(ctx, workspace.RBACObject()) != nil {
continue
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
arg.FuzzyName,
pq.Array(arg.IDs),
arg.Deprecated,
arg.HasAITask,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -264,6 +265,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
arg.LastUsedBefore,
arg.LastUsedAfter,
arg.UsingActive,
arg.HasAITask,
arg.RequesterID,
arg.Offset,
arg.Limit,
Expand Down Expand Up @@ -311,6 +313,7 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
&i.LatestBuildError,
&i.LatestBuildTransition,
&i.LatestBuildStatus,
&i.LatestBuildHasAITask,
&i.Count,
); err != nil {
return nil, err
Expand Down
63 changes: 43 additions & 20 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,58 @@ LIMIT

-- name: GetTemplatesWithFilter :many
SELECT
*
t.*
FROM
template_with_names AS templates
template_with_names AS t
LEFT JOIN
template_versions tv ON t.active_version_id = tv.id
WHERE
-- Optionally include deleted templates
templates.deleted = @deleted
t.deleted = @deleted
-- Filter by organization_id
AND CASE
WHEN @organization_id :: uuid != '00000000-0000-0000-0000-000000000000'::uuid THEN
organization_id = @organization_id
t.organization_id = @organization_id
ELSE true
END
-- Filter by exact name
AND CASE
WHEN @exact_name :: text != '' THEN
LOWER("name") = LOWER(@exact_name)
LOWER(t.name) = LOWER(@exact_name)
ELSE true
END
-- Filter by name, matching on substring
AND CASE
WHEN @fuzzy_name :: text != '' THEN
lower(name) ILIKE '%' || lower(@fuzzy_name) || '%'
lower(t.name) ILIKE '%' || lower(@fuzzy_name) || '%'
ELSE true
END
-- Filter by ids
AND CASE
WHEN array_length(@ids :: uuid[], 1) > 0 THEN
id = ANY(@ids)
t.id = ANY(@ids)
ELSE true
END
-- Filter by deprecated
AND CASE
WHEN sqlc.narg('deprecated') :: boolean IS NOT NULL THEN
CASE
WHEN sqlc.narg('deprecated') :: boolean THEN
deprecated != ''
t.deprecated != ''
ELSE
deprecated = ''
t.deprecated = ''
END
ELSE true
END
-- Filter by has_ai_task in latest version
AND CASE
WHEN sqlc.narg('has_ai_task') :: boolean IS NOT NULL THEN
tv.has_ai_task = sqlc.narg('has_ai_task') :: boolean
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedTemplates
-- @authorize_filter
ORDER BY (name, id) ASC
ORDER BY (t.name, t.id) ASC
;

-- name: GetTemplateByOrganizationAndName :one
Expand Down
13 changes: 11 additions & 2 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ SELECT
latest_build.canceled_at as latest_build_canceled_at,
latest_build.error as latest_build_error,
latest_build.transition as latest_build_transition,
latest_build.job_status as latest_build_status
latest_build.job_status as latest_build_status,
latest_build.has_ai_task as latest_build_has_ai_task
FROM
workspaces_expanded as workspaces
JOIN
Expand All @@ -128,6 +129,7 @@ LEFT JOIN LATERAL (
workspace_builds.id,
workspace_builds.transition,
workspace_builds.template_version_id,
workspace_builds.has_ai_task,
template_versions.name AS template_version_name,
provisioner_jobs.id AS provisioner_job_id,
provisioner_jobs.started_at,
Expand Down Expand Up @@ -345,6 +347,12 @@ WHERE
(latest_build.template_version_id = template.active_version_id) = sqlc.narg('using_active') :: boolean
ELSE true
END
-- Filter by has_ai_task in latest build
AND CASE
WHEN sqlc.narg('has_ai_task') :: boolean IS NOT NULL THEN
latest_build.has_ai_task = sqlc.narg('has_ai_task') :: boolean
ELSE true
END
-- Authorize Filter clause will be injected below in GetAuthorizedWorkspaces
-- @authorize_filter
), filtered_workspaces_order AS (
Expand Down Expand Up @@ -411,7 +419,8 @@ WHERE
'0001-01-01 00:00:00+00'::timestamptz, -- latest_build_canceled_at,
'', -- latest_build_error
'start'::workspace_transition, -- latest_build_transition
'unknown'::provisioner_job_status -- latest_build_status
'unknown'::provisioner_job_status, -- latest_build_status
false -- latest_build_has_ai_task
WHERE
@with_summary :: boolean = true
), total_count AS (
Expand Down
1 change: 1 addition & 0 deletions coderd/database/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ sql:
stale_interval_ms: StaleIntervalMS
has_ai_task: HasAITask
ai_tasks_sidebar_app_id: AITasksSidebarAppID
latest_build_has_ai_task: LatestBuildHasAITask
rules:
- name: do-not-use-public-schema-in-queries
message: "do not use public schema in queries"
Expand Down
Loading
Loading