Files
archie/internal/infrastructure/postgres/queries/alerts.sql
T
sam 747a7cbfd7 Feat/operator alerts (#1635)
* feat(state): durable operator alerts owned by the State Store

Alerts dedup on org+kind+subject, resolve themselves when a condition
clears, and are raised, resolved, dismissed and listed through the state
contract. Task transitions raise needs-human and failed alerts in the same
transaction; the store checks service health and prunes closed alerts.
Each raise and resolve queues a webhook delivery whose attempts are kept.

* feat(notify): deliver operator alerts to the webhook; raise config and release alerts

notify.webhook now receives raised and resolved alerts above a severity
threshold with HMAC-signed payloads, bounded retry and every attempt recorded
on the alert. archied raises model alias, provider and credential-binding
alerts from the effective configuration, an update alert from the release
channel, and a credential-rejected alert from the egress resolver. The
dashboard API lists and dismisses the caller's org's alerts.

* feat(ui): alert bell in the top bar and webhook alert settings

The bell lists the org's open alerts by severity with dismiss and a link per
alert, and refreshes from the event stream. The task settings page gains the
webhook severity threshold and signing secret. ui/dist rebuilt.
2026-10-12 03:30:46 +11:00

69 lines
3.4 KiB
SQL

-- Operator alerts and their outbound deliveries (internal/domain/alert).
-- name: LockAlertKey :exec
-- Serialises raisers of one alert, so two first raises cannot both insert.
SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg(lock_key)::text, 0));
-- name: GetAlertByKey :one
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts WHERE org_id = $1 AND kind = $2 AND subject = $3;
-- name: GetOrgAlert :one
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts WHERE org_id = $1 AND id = $2;
-- name: GetAlert :one
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts WHERE id = $1;
-- name: UpsertAlert :one
INSERT INTO alerts (org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
ON CONFLICT (org_id, kind, subject) DO UPDATE SET
severity = excluded.severity, title = excluded.title, body = excluded.body, link = excluded.link,
is_condition = excluded.is_condition, state = excluded.state, first_raised = excluded.first_raised,
last_seen = excluded.last_seen, count = excluded.count, closed_by = excluded.closed_by, closed_at = excluded.closed_at
RETURNING id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at;
-- name: ListAlerts :many
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts
WHERE org_id = $1 AND state = ANY(sqlc.arg(states)::text[])
ORDER BY CASE severity WHEN 'action' THEN 3 WHEN 'warning' THEN 2 ELSE 1 END DESC, last_seen DESC, id DESC
LIMIT sqlc.arg(page_limit);
-- name: PruneAlerts :execrows
DELETE FROM alerts WHERE state <> 'open' AND closed_at < $1;
-- name: InsertAlertDelivery :exec
INSERT INTO alert_deliveries (alert_id, event) VALUES ($1, $2);
-- name: ClaimAlertDeliveries :many
UPDATE alert_deliveries SET next_attempt_at = now() + interval '2 minutes'
WHERE id IN (
SELECT d.id FROM alert_deliveries d
WHERE d.state = 'pending' AND d.next_attempt_at <= now()
ORDER BY d.id LIMIT sqlc.arg(page_limit) FOR UPDATE SKIP LOCKED)
RETURNING id, alert_id, event, state, created_at, next_attempt_at, attempts;
-- name: LockAlertDelivery :one
SELECT id, alert_id, event, state, created_at, next_attempt_at, attempts
FROM alert_deliveries WHERE id = $1 FOR UPDATE;
-- name: SettleAlertDelivery :exec
UPDATE alert_deliveries SET state = $2, attempts = $3, next_attempt_at = $4 WHERE id = $1;
-- name: InsertAlertAttempt :exec
INSERT INTO alert_delivery_attempts (delivery_id, attempt, status_code, error) VALUES ($1, $2, $3, $4);
-- name: ListAlertDeliveries :many
SELECT id, alert_id, event, state, created_at, next_attempt_at, attempts
FROM alert_deliveries WHERE alert_id = ANY(sqlc.arg(alert_ids)::bigint[]) ORDER BY alert_id, id DESC;
-- name: ListAlertAttempts :many
SELECT id, delivery_id, attempt, at, status_code, error
FROM alert_delivery_attempts WHERE delivery_id = ANY(sqlc.arg(delivery_ids)::bigint[]) ORDER BY delivery_id, id;
-- name: TaskAlertInfo :one
SELECT org_id, title, workflow, park_class FROM tasks WHERE id = $1;