feat(store): add incidents, notification channels and delivery outbox
At most one open incident per monitor is enforced by a partial unique index. Refs #15, #16 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
4b4e134bfd
commit
ffef63e586
9 files changed
+770
-11
No files matched your search
@@ -26,3 +26,9 @@ SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_coun
|
||||
FROM check_results_1h
|
||||
WHERE org_id = $1 AND bucket >= $2
|
||||
GROUP BY monitor_id;
|
||||
|
||||
-- name: LatestResultsForMonitor :many
|
||||
SELECT DISTINCT ON (region) region, ok, error
|
||||
FROM check_results
|
||||
WHERE org_id = $1 AND monitor_id = $2 AND time > now() - interval '1 day'
|
||||
ORDER BY region, time DESC;
|
||||
@@ -0,0 +1,79 @@
|
||||
-- name: OpenIncident :one
|
||||
INSERT INTO incidents (org_id, monitor_id, started_at, cause)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (monitor_id) WHERE resolved_at IS NULL DO NOTHING
|
||||
RETURNING *;
|
||||
|
||||
-- name: ResolveIncident :one
|
||||
UPDATE incidents SET resolved_at = sqlc.arg(resolved_at)
|
||||
WHERE monitor_id = sqlc.arg(monitor_id) AND resolved_at IS NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListIncidents :many
|
||||
SELECT i.*, m.name AS monitor_name
|
||||
FROM incidents i
|
||||
JOIN monitors m ON m.id = i.monitor_id
|
||||
ORDER BY i.started_at DESC
|
||||
LIMIT 200;
|
||||
|
||||
-- name: ListEnabledChannelIDs :many
|
||||
SELECT id FROM notification_channels WHERE org_id = $1 AND enabled;
|
||||
|
||||
-- name: EnqueueNotification :exec
|
||||
INSERT INTO notifications (org_id, channel_id, incident_id, event)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: ClaimNotifications :many
|
||||
-- System scope. Leases due rows for five minutes so a crashed sender's rows
|
||||
-- come back; SKIP LOCKED lets every API replica work the outbox.
|
||||
UPDATE notifications SET next_attempt_at = now() + interval '5 minutes'
|
||||
WHERE id IN (
|
||||
SELECT id FROM notifications
|
||||
WHERE status = 'pending' AND next_attempt_at <= now()
|
||||
ORDER BY next_attempt_at
|
||||
LIMIT sqlc.arg(max)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
RETURNING id;
|
||||
|
||||
-- name: NotificationDetails :many
|
||||
-- System scope.
|
||||
SELECT n.id, n.org_id, n.event, n.attempts,
|
||||
c.type AS channel_type, c.config AS channel_config, c.id AS channel_id,
|
||||
i.id AS incident_id, i.started_at, i.resolved_at, i.cause,
|
||||
m.id AS monitor_id, m.name AS monitor_name, m.target AS monitor_target, m.type AS monitor_type
|
||||
FROM notifications n
|
||||
JOIN notification_channels c ON c.id = n.channel_id
|
||||
JOIN incidents i ON i.id = n.incident_id
|
||||
JOIN monitors m ON m.id = i.monitor_id
|
||||
WHERE n.id = ANY (sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: MarkNotificationSent :exec
|
||||
UPDATE notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: MarkNotificationFailed :exec
|
||||
UPDATE notifications SET attempts = attempts + 1, last_error = sqlc.arg(last_error),
|
||||
status = CASE WHEN attempts + 1 >= sqlc.arg(max_attempts)::int THEN 'failed' ELSE 'pending' END,
|
||||
next_attempt_at = now() + make_interval(secs => sqlc.arg(retry_in_seconds)::int)
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: CreateChannel :one
|
||||
INSERT INTO notification_channels (org_id, name, type, config, enabled)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateChannel :one
|
||||
UPDATE notification_channels SET name = $2, config = $3, enabled = $4, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetChannel :one
|
||||
SELECT * FROM notification_channels WHERE id = $1;
|
||||
|
||||
-- name: ListChannels :many
|
||||
SELECT * FROM notification_channels ORDER BY name;
|
||||
|
||||
-- name: DeleteChannel :execrows
|
||||
DELETE FROM notification_channels WHERE id = $1;
|
||||
@@ -1,11 +1,11 @@
|
||||
-- name: CreateMonitor :one
|
||||
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateMonitor :one
|
||||
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
|
||||
timeout_ms = $6, config = $7, enabled = $8, updated_at = now()
|
||||
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
@@ -45,3 +45,12 @@ GROUP BY m.id, m.interval_seconds;
|
||||
SELECT m.* FROM monitors m
|
||||
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region = sqlc.arg(region)
|
||||
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled;
|
||||
|
||||
-- name: MonitorsForResults :many
|
||||
-- System scope: monitors a checker of region may report, with every region
|
||||
-- they are assigned to, for evaluation.
|
||||
SELECT m.id, m.org_id, m.name, m.target, m.interval_seconds, m.confirmations,
|
||||
(SELECT array_agg(r2.region ORDER BY r2.region) FROM monitor_regions r2 WHERE r2.monitor_id = m.id)::text[] AS regions
|
||||
FROM monitors m
|
||||
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region = sqlc.arg(region)
|
||||
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled;
|
||||
Reference in new issue
Block a user