mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: add filtering options to provisioners list (#19378)
## Summary In this pull request we're adding support for additional filtering options to the `provisioners list` CLI command and the `/provisionerdaemons` API endpoint. Resolves: https://github.com/coder/coder/issues/18783 ### Changes #### Added CLI Options - `--show-offline`: When this option is provided, all provisioner daemons will be returned. This means that when `--show-offline` is not provided only `idle` and `busy` provisioner daemons will be returned. - `--status=<list_of_statuses>`: When this option is provided with a comma-separated list of valid statuses (`idle`, `busy`, or `offline`) only provisioner daemons that have these statuses will be returned. - `--max-age=<duration>`: When this option is provided with a valid duration value (e.g., `24h`, `30s`) only provisioner daemons with a `last_seen_at` timestamp within the provided max age will be returned. #### Query Params - `?offline=true`: Include offline provisioner daemons in the results. Offline provisioner daemons will be excluded if `?offline=false` or if offline is not provided. - `?status=<list_of_statuses>`: Include provisioner daemons with the specified statuses. - `?max_age=<duration>`: Include provisioner daemons with a `last_seen_at` timestamp within the max age duration. #### Frontend - Since offline provisioners will not be returned by default anymore (`--show-offline` has to be provided to see them), a checkbox was added to the provisioners list page to allow for offline provisioners to be displayed - A revamp of the provisioners page will be done in: https://github.com/coder/coder/issues/17156, this checkbox change was just added to maintain currently functionality with the backend updates Current provisioners page (without checkbox) <img width="1329" height="574" alt="Screenshot 2025-08-20 at 10 51 00 AM" src="https://github.com/user-attachments/assets/77b73650-0b62-44f0-a77f-acbe5710809f" /> Provisioners page with checkbox (unchecked) <img width="1314" height="626" alt="Screenshot 2025-08-20 at 10 48 40 AM" src="https://github.com/user-attachments/assets/7ba164ad-6d3f-417b-bd39-338c0161b145" /> Provisioner page with checkbox (checked) and URL updated with query parameters <img width="1306" height="597" alt="Screenshot 2025-08-20 at 10 50 14 AM" src="https://github.com/user-attachments/assets/e78d0986-bbf8-491b-9d56-b682973237a0" /> ### Show Offline vs Offline Status To list offline provisioner daemons, users can either: 1. Include the `--show-offline` option OR 2. Include `offline` in the list of values provided to the `--status` option
This commit is contained in:
@@ -8263,13 +8263,13 @@ const getProvisionerDaemonsWithStatusByOrganization = `-- name: GetProvisionerDa
|
||||
SELECT
|
||||
pd.id, pd.created_at, pd.name, pd.provisioners, pd.replica_id, pd.tags, pd.last_seen_at, pd.version, pd.api_version, pd.organization_id, pd.key_id,
|
||||
CASE
|
||||
WHEN pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - ($1::bigint || ' ms')::interval)
|
||||
THEN 'offline'
|
||||
ELSE CASE
|
||||
WHEN current_job.id IS NOT NULL THEN 'busy'
|
||||
ELSE 'idle'
|
||||
END
|
||||
END::provisioner_daemon_status AS status,
|
||||
WHEN current_job.id IS NOT NULL THEN 'busy'::provisioner_daemon_status
|
||||
WHEN (COALESCE($1::bool, false) = true
|
||||
OR 'offline'::provisioner_daemon_status = ANY($2::provisioner_daemon_status[]))
|
||||
AND (pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - ($3::bigint || ' ms')::interval))
|
||||
THEN 'offline'::provisioner_daemon_status
|
||||
ELSE 'idle'::provisioner_daemon_status
|
||||
END AS status,
|
||||
pk.name AS key_name,
|
||||
-- NOTE(mafredri): sqlc.embed doesn't support nullable tables nor renaming them.
|
||||
current_job.id AS current_job_id,
|
||||
@@ -8336,21 +8336,56 @@ LEFT JOIN
|
||||
AND previous_template.organization_id = pd.organization_id
|
||||
)
|
||||
WHERE
|
||||
pd.organization_id = $2::uuid
|
||||
AND (COALESCE(array_length($3::uuid[], 1), 0) = 0 OR pd.id = ANY($3::uuid[]))
|
||||
AND ($4::tagset = 'null'::tagset OR provisioner_tagset_contains(pd.tags::tagset, $4::tagset))
|
||||
pd.organization_id = $4::uuid
|
||||
AND (COALESCE(array_length($5::uuid[], 1), 0) = 0 OR pd.id = ANY($5::uuid[]))
|
||||
AND ($6::tagset = 'null'::tagset OR provisioner_tagset_contains(pd.tags::tagset, $6::tagset))
|
||||
-- Filter by max age if provided
|
||||
AND (
|
||||
$7::bigint IS NULL
|
||||
OR pd.last_seen_at IS NULL
|
||||
OR pd.last_seen_at >= (NOW() - ($7::bigint || ' ms')::interval)
|
||||
)
|
||||
AND (
|
||||
-- Always include online daemons
|
||||
(pd.last_seen_at IS NOT NULL AND pd.last_seen_at >= (NOW() - ($3::bigint || ' ms')::interval))
|
||||
-- Include offline daemons if offline param is true or 'offline' status is requested
|
||||
OR (
|
||||
(pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - ($3::bigint || ' ms')::interval))
|
||||
AND (
|
||||
COALESCE($1::bool, false) = true
|
||||
OR 'offline'::provisioner_daemon_status = ANY($2::provisioner_daemon_status[])
|
||||
)
|
||||
)
|
||||
)
|
||||
AND (
|
||||
-- Filter daemons by any statuses if provided
|
||||
COALESCE(array_length($2::provisioner_daemon_status[], 1), 0) = 0
|
||||
OR (current_job.id IS NOT NULL AND 'busy'::provisioner_daemon_status = ANY($2::provisioner_daemon_status[]))
|
||||
OR (current_job.id IS NULL AND 'idle'::provisioner_daemon_status = ANY($2::provisioner_daemon_status[]))
|
||||
OR (
|
||||
'offline'::provisioner_daemon_status = ANY($2::provisioner_daemon_status[])
|
||||
AND (pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - ($3::bigint || ' ms')::interval))
|
||||
)
|
||||
OR (
|
||||
COALESCE($1::bool, false) = true
|
||||
AND (pd.last_seen_at IS NULL OR pd.last_seen_at < (NOW() - ($3::bigint || ' ms')::interval))
|
||||
)
|
||||
)
|
||||
ORDER BY
|
||||
pd.created_at DESC
|
||||
LIMIT
|
||||
$5::int
|
||||
$8::int
|
||||
`
|
||||
|
||||
type GetProvisionerDaemonsWithStatusByOrganizationParams struct {
|
||||
StaleIntervalMS int64 `db:"stale_interval_ms" json:"stale_interval_ms"`
|
||||
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
|
||||
IDs []uuid.UUID `db:"ids" json:"ids"`
|
||||
Tags StringMap `db:"tags" json:"tags"`
|
||||
Limit sql.NullInt32 `db:"limit" json:"limit"`
|
||||
Offline sql.NullBool `db:"offline" json:"offline"`
|
||||
Statuses []ProvisionerDaemonStatus `db:"statuses" json:"statuses"`
|
||||
StaleIntervalMS int64 `db:"stale_interval_ms" json:"stale_interval_ms"`
|
||||
OrganizationID uuid.UUID `db:"organization_id" json:"organization_id"`
|
||||
IDs []uuid.UUID `db:"ids" json:"ids"`
|
||||
Tags StringMap `db:"tags" json:"tags"`
|
||||
MaxAgeMs sql.NullInt64 `db:"max_age_ms" json:"max_age_ms"`
|
||||
Limit sql.NullInt32 `db:"limit" json:"limit"`
|
||||
}
|
||||
|
||||
type GetProvisionerDaemonsWithStatusByOrganizationRow struct {
|
||||
@@ -8373,10 +8408,13 @@ type GetProvisionerDaemonsWithStatusByOrganizationRow struct {
|
||||
// Previous job information.
|
||||
func (q *sqlQuerier) GetProvisionerDaemonsWithStatusByOrganization(ctx context.Context, arg GetProvisionerDaemonsWithStatusByOrganizationParams) ([]GetProvisionerDaemonsWithStatusByOrganizationRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getProvisionerDaemonsWithStatusByOrganization,
|
||||
arg.Offline,
|
||||
pq.Array(arg.Statuses),
|
||||
arg.StaleIntervalMS,
|
||||
arg.OrganizationID,
|
||||
pq.Array(arg.IDs),
|
||||
arg.Tags,
|
||||
arg.MaxAgeMs,
|
||||
arg.Limit,
|
||||
)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user