Files
uptimesebi/internal/store/queries/agents.sql
T
Sebastian UnterschützandClaude Opus 5.5 3562f8224e
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m48s
ci / test (pull_request) Successful in 12m42s
ci / build (pull_request) Canceled after 30s
ci / image (pull_request) Canceled after 0s
feat(regions): show checker regions on a map
Regions get an optional location. Platform admins set it for platform
regions and org admins for their own, by clicking the map on the regions
page or entering the coordinates; PATCH /org/regions/:id and
/admin/regions/:id set or remove it.

The monitor form shows the located regions on a map from the
self-hosted ZB-Server Maps: a click on a marker ticks or unticks the
region, and the checkbox list stays the source of truth. MapLibre loads
only on pages with a map, and the map stays hidden if the map server
cannot be reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-28 20:17:27 +01:00

86 lines
3.2 KiB
SQL

-- name: CreateRegion :one
INSERT INTO regions (org_id, key, name, latitude, longitude) VALUES ($1, $2, $3, $4, $5)
RETURNING *;
-- name: UpdateRegionLocation :execrows
-- Null coordinates remove the location.
UPDATE regions SET latitude = sqlc.narg(latitude), longitude = sqlc.narg(longitude)
WHERE id = sqlc.arg(id);
-- name: EnsurePlatformRegion :one
-- An empty name names a new region after its key and keeps an existing name.
INSERT INTO regions (org_id, key, name)
VALUES (NULL, sqlc.arg(key), coalesce(nullif(sqlc.arg(name)::text, ''), sqlc.arg(key)))
ON CONFLICT ON CONSTRAINT regions_owner_key_key
DO UPDATE SET name = CASE WHEN sqlc.arg(name)::text = '' THEN regions.name ELSE EXCLUDED.name END
RETURNING *;
-- name: GetRegion :one
SELECT * FROM regions WHERE id = $1;
-- name: ListVisibleRegions :many
-- Row-level security limits this to platform regions and the scope's org;
-- the org's own regions come first.
SELECT * FROM regions ORDER BY org_id IS NULL, name, key;
-- name: ListPlatformRegions :many
SELECT * FROM regions WHERE org_id IS NULL ORDER BY name, key;
-- name: DeleteRegion :execrows
DELETE FROM regions WHERE id = $1;
-- name: CreateAgent :one
INSERT INTO agents (org_id, kind, name, region_id, token_hash, prefix, allow_private)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;
-- name: GetAgentByHash :one
-- System scope.
SELECT a.*, r.key AS region_key, r.name AS region_name
FROM agents a
LEFT JOIN regions r ON r.id = a.region_id
WHERE a.token_hash = $1;
-- name: ListAgents :many
-- Agents owned by org, or platform agents when org is NULL.
SELECT a.*, r.key AS region_key, r.name AS region_name
FROM agents a
LEFT JOIN regions r ON r.id = a.region_id
WHERE a.org_id IS NOT DISTINCT FROM sqlc.narg(org_id)::uuid
ORDER BY a.kind, r.name, a.name;
-- name: DeleteAgent :execrows
DELETE FROM agents WHERE id = sqlc.arg(id) AND org_id IS NOT DISTINCT FROM sqlc.narg(org_id)::uuid;
-- name: TouchAgent :exec
UPDATE agents SET last_seen_at = now(), release = $2
WHERE id = $1 AND (last_seen_at IS NULL OR last_seen_at < now() - interval '30 seconds' OR release IS DISTINCT FROM $2);
-- name: SetAgentAllowPrivate :execrows
UPDATE agents SET allow_private = sqlc.arg(allow_private)
WHERE id = sqlc.arg(id) AND kind = 'checker' AND org_id = sqlc.arg(org_id);
-- name: OrgServesOwnPages :one
-- An organization serves its status pages itself once one of its own
-- status page servers has connected.
SELECT EXISTS (
SELECT 1 FROM agents
WHERE org_id = $1 AND kind = 'statuspage' AND last_seen_at IS NOT NULL
);
-- name: RegionsToWatch :many
-- System scope: regions that have checkers and enabled monitors, so a
-- region without a live checker leaves monitors unchecked.
SELECT r.id, r.name, r.org_id, o.name AS org_name
FROM regions r
LEFT JOIN orgs o ON o.id = r.org_id
WHERE EXISTS (SELECT 1 FROM agents a WHERE a.kind = 'checker' AND a.region_id = r.id)
AND EXISTS (SELECT 1 FROM monitor_regions mr JOIN monitors m ON m.id = mr.monitor_id
WHERE mr.region_id = r.id AND m.enabled)
ORDER BY r.name;
-- name: OrgOwnerEmails :many
-- System scope.
SELECT u.email FROM memberships m JOIN users u ON u.id = m.user_id
WHERE m.org_id = $1 AND m.is_owner AND u.disabled_at IS NULL;