Files
uptimesebi/internal/store/queries/status_pages.sql
T
Sebastian UnterschützandClaude Opus 5.5 538c43b563
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 6m29s
ci / test (pull_request) Successful in 12m55s
ci / build (pull_request) Successful in 16m45s
ci / image (pull_request) Successful in 5m41s
feat(agents): talk to the API over a WebSocket and push page changes
Checkers and status page servers keep one WebSocket open to the API at
/agent/v1/stream and send their requests over it. The API answers them
through the agent API as if they had come over HTTP, each authenticated
again, so the protocol stays the same and a revoked token ends the
stream. While the stream is down agents fall back to plain HTTP, and an
API without it is asked again every five minutes.

Over the stream the API tells status page servers which of their pages
changed, so they fetch them at once instead of on their next refresh:
when a region of a monitor changes between up and down, an incident
opens or resolves, or anything of the organization is edited. Changes
are announced through Redis to every API instance and sent at most every
five seconds per stream. A page that changes while it is fetched is
fetched once more.

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

158 lines
6.5 KiB
SQL

-- name: CreateStatusPage :one
INSERT INTO status_pages (org_id, slug, title, description, published, show_component_details, domain_token, theme)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
-- name: UpdateStatusPage :one
UPDATE status_pages
SET slug = $2,
title = $3,
description = $4,
published = $5,
show_component_details = $6,
theme = $7,
updated_at = now()
WHERE id = $1
RETURNING *;
-- name: SetStatusPageDomain :one
-- Changing the domain drops a previous verification and its checks.
UPDATE status_pages SET
domain_verified_at = CASE WHEN custom_domain IS NOT DISTINCT FROM sqlc.narg(custom_domain) THEN domain_verified_at END,
domain_verify_method = CASE WHEN custom_domain IS NOT DISTINCT FROM sqlc.narg(custom_domain) THEN domain_verify_method END,
domain_failing_since = CASE WHEN custom_domain IS NOT DISTINCT FROM sqlc.narg(custom_domain) THEN domain_failing_since END,
domain_checked_at = CASE WHEN custom_domain IS NOT DISTINCT FROM sqlc.narg(custom_domain) THEN domain_checked_at END,
domain_set_at = CASE WHEN custom_domain IS NOT DISTINCT FROM sqlc.narg(custom_domain) THEN domain_set_at ELSE now() END,
custom_domain = sqlc.narg(custom_domain),
updated_at = now()
WHERE id = sqlc.arg(id)
RETURNING *;
-- name: MarkDomainVerified :exec
-- The domain's record was found: verified, or still in place.
UPDATE status_pages SET
domain_verified_at = coalesce(domain_verified_at, sqlc.arg(now)::timestamptz),
domain_verify_method = sqlc.arg(method)::text,
domain_checked_at = sqlc.arg(now)::timestamptz,
domain_failing_since = NULL
WHERE id = sqlc.arg(id);
-- name: MarkDomainUnconfirmed :one
-- The record of a domain was not found. Returns since when it has not
-- been, for a verified domain.
UPDATE status_pages SET
domain_checked_at = sqlc.arg(now)::timestamptz,
domain_failing_since = CASE WHEN domain_verified_at IS NULL THEN NULL
ELSE coalesce(domain_failing_since, sqlc.arg(now)::timestamptz) END
WHERE id = sqlc.arg(id)
RETURNING domain_failing_since;
-- name: UnverifyDomain :exec
-- A verified domain whose record stayed missing past the grace period.
UPDATE status_pages SET
domain_verified_at = NULL, domain_verify_method = NULL, domain_failing_since = NULL
WHERE id = $1;
-- name: DomainsToCheck :many
-- System scope: domains due for a look at their DNS records. A new one is
-- looked at every minute for a quarter of an hour, then every ten minutes
-- for two days, after which only the verify button tries again. A verified
-- one is checked daily, hourly once its record went missing.
SELECT id, org_id, title, custom_domain::text AS domain, domain_token,
domain_verified_at, domain_failing_since
FROM status_pages
WHERE custom_domain IS NOT NULL
AND (sqlc.narg(page_id)::uuid IS NULL OR id = sqlc.narg(page_id))
AND (
(domain_verified_at IS NULL AND domain_set_at > sqlc.arg(now)::timestamptz - interval '48 hours'
AND (domain_checked_at IS NULL OR domain_checked_at <= sqlc.arg(now)::timestamptz - CASE
WHEN domain_set_at > sqlc.arg(now)::timestamptz - interval '15 minutes' THEN interval '50 seconds'
ELSE interval '10 minutes' END))
OR (domain_verified_at IS NOT NULL AND domain_verify_method IS NOT NULL
AND (domain_checked_at IS NULL OR domain_checked_at <= sqlc.arg(now)::timestamptz - CASE
WHEN domain_failing_since IS NULL THEN interval '24 hours' ELSE interval '1 hour' END)))
ORDER BY domain_checked_at NULLS FIRST
LIMIT 500;
-- name: GetStatusPage :one
SELECT * FROM status_pages WHERE id = $1;
-- name: ListStatusPages :many
SELECT * FROM status_pages ORDER BY title;
-- name: DeleteStatusPage :execrows
DELETE FROM status_pages WHERE id = $1;
-- name: StatusPageMonitors :many
SELECT spm.monitor_id, spm.section, spm.group_id, spm.label, spm.position,
m.name AS monitor_name, m.enabled, m.group_id AS monitor_group_id,
m.type AS monitor_type, m.interval_seconds
FROM status_page_monitors spm
JOIN monitors m ON m.id = spm.monitor_id
WHERE spm.page_id = $1
ORDER BY spm.position, m.name;
-- name: ClearStatusPageMonitors :exec
DELETE FROM status_page_monitors WHERE page_id = $1;
-- name: AddStatusPageMonitor :exec
INSERT INTO status_page_monitors (page_id, monitor_id, org_id, section, group_id, label, position)
VALUES ($1, $2, $3, $4, $5, $6, $7);
-- name: SetStatusPageMonitorGroup :exec
UPDATE status_page_monitors SET group_id = $3, section = $4
WHERE page_id = $1 AND monitor_id = $2;
-- name: ListStatusPageGroups :many
SELECT * FROM status_page_groups
WHERE page_id = $1
ORDER BY position, name, id;
-- name: CreateStatusPageGroup :one
INSERT INTO status_page_groups (page_id, org_id, parent_id, name, position)
VALUES ($1, $2, $3, $4, $5)
RETURNING *;
-- name: UpdateStatusPageGroup :one
UPDATE status_page_groups
SET name = $3, parent_id = $4, position = $5, updated_at = now()
WHERE id = $1 AND page_id = $2
RETURNING *;
-- name: ReparentStatusPageGroupChildren :exec
UPDATE status_page_groups SET parent_id = $3, updated_at = now()
WHERE page_id = $1 AND parent_id = $2;
-- name: ReparentStatusPageGroupMonitors :exec
UPDATE status_page_monitors SET group_id = $3
WHERE page_id = $1 AND group_id = $2;
-- name: DeleteStatusPageGroup :execrows
DELETE FROM status_page_groups WHERE id = $1 AND page_id = $2;
-- name: ClearStatusPageGroups :exec
DELETE FROM status_page_groups WHERE page_id = $1;
-- name: FindPublishedPage :one
-- System scope: resolves a request to a page by verified custom domain or
-- by slug.
SELECT * FROM status_pages
WHERE published AND suspended_at IS NULL
AND NOT EXISTS (SELECT 1 FROM orgs o WHERE o.id = status_pages.org_id AND o.suspended_at IS NOT NULL)
AND ((sqlc.narg(host)::citext IS NOT NULL AND custom_domain = sqlc.narg(host) AND domain_verified_at IS NOT NULL)
OR (sqlc.narg(slug)::citext IS NOT NULL AND slug = sqlc.narg(slug)));
-- name: ServablePages :many
-- System scope: the published pages a status page server serves, those
-- of agent_org for an organization's server; the platform's leave the
-- pages of organizations serving their own.
SELECT p.slug, COALESCE(CASE WHEN p.domain_verified_at IS NOT NULL THEN p.custom_domain END, '')::text AS host, p.org_id
FROM status_pages p
JOIN orgs o ON o.id = p.org_id
WHERE p.published AND p.suspended_at IS NULL AND o.suspended_at IS NULL
AND CASE WHEN sqlc.narg(agent_org)::uuid IS NULL
THEN NOT EXISTS (SELECT 1 FROM agents a WHERE a.org_id = p.org_id AND a.kind = 'statuspage' AND a.last_seen_at IS NOT NULL)
ELSE p.org_id = sqlc.narg(agent_org)::uuid
END
ORDER BY p.slug;