69 lines
3.4 KiB
SQL
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, park_blocker FROM tasks WHERE id = $1;
|