Files
uptimesebi/internal/store/queries/incidents.sql
T
Sebastian UnterschützandClaude Opus 5.5 2409a919c3
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m38s
ci / test (pull_request) Successful in 14m21s
ci / build (pull_request) Successful in 16m36s
ci / image (pull_request) Successful in 3m37s
feat: own incident statuses with a color; incidents before maintenance
Organizations add statuses of their own, with a name and a color, next
to the built-in ones (Settings, incident statuses; API under
/org/incident-statuses, managed with org.manage). Updates pick them by
ID; the console and status pages show them in their color. Renaming or
recoloring carries into the history; a status in use cannot be deleted
(409 status_in_use). API responses give status "custom" with status_id,
status_name and status_color.

Status pages now list recent incidents before planned maintenance.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-23 23:49:58 +01:00

147 lines
5.5 KiB
SQL

-- 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
-- Newest first, a page from a keyset cursor (see paging.go); optionally of
-- one monitor.
SELECT i.*, m.name AS monitor_name
FROM incidents i
JOIN monitors m ON m.id = i.monitor_id
WHERE (sqlc.narg(monitor_id)::uuid IS NULL OR i.monitor_id = sqlc.narg(monitor_id))
AND (sqlc.narg(after_t)::timestamptz IS NULL OR (i.started_at, i.id) < (sqlc.narg(after_t), sqlc.narg(after_id)::uuid))
AND (sqlc.narg(before_t)::timestamptz IS NULL OR (i.started_at, i.id) > (sqlc.narg(before_t), sqlc.narg(before_id)::uuid))
ORDER BY
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN i.started_at END DESC,
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN i.id END DESC,
i.started_at, i.id
LIMIT sqlc.arg(max_rows);
-- 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;
-- name: OpenIncidents :many
SELECT * FROM incidents WHERE resolved_at IS NULL;
-- name: IncidentsForMonitors :many
SELECT * FROM incidents
WHERE monitor_id = ANY (sqlc.arg(ids)::uuid[]) AND (resolved_at IS NULL OR resolved_at > sqlc.arg(since))
ORDER BY started_at DESC
LIMIT 50;
-- name: IncidentsSince :many
-- Incidents overlapping the time since $1, for SLA calculation.
SELECT monitor_id, started_at, resolved_at FROM incidents
WHERE resolved_at IS NULL OR resolved_at > $1;
-- name: GetIncident :one
SELECT i.*, m.name AS monitor_name, m.group_id AS monitor_group_id
FROM incidents i JOIN monitors m ON m.id = i.monitor_id
WHERE i.id = $1;
-- name: SetIncidentTitle :execrows
UPDATE incidents SET title = sqlc.arg(title) WHERE id = sqlc.arg(id);
-- name: AddIncidentUpdate :one
INSERT INTO incident_updates (org_id, incident_id, status, status_id, message, public, author_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING *;
-- name: DeleteIncidentUpdate :execrows
DELETE FROM incident_updates WHERE id = sqlc.arg(id) AND incident_id = sqlc.arg(incident_id);
-- name: RefreshIncidentStatus :exec
-- The incident shows the status of its latest update.
UPDATE incidents SET (status, status_id) = (
SELECT u.status, u.status_id FROM incident_updates u WHERE u.incident_id = incidents.id
ORDER BY u.created_at DESC, u.id DESC LIMIT 1
) WHERE incidents.id = $1;
-- name: ListIncidentUpdates :many
-- Newest first, with the author's name.
SELECT u.*, coalesce(nullif(usr.display_name, ''), usr.email::text, '')::text AS author_name
FROM incident_updates u LEFT JOIN users usr ON usr.id = u.author_id
WHERE u.incident_id = $1
ORDER BY u.created_at DESC, u.id DESC;
-- name: PublicIncidentUpdates :many
-- The public updates of incidents shown on a status page, newest first.
SELECT incident_id, status, status_id, message, created_at FROM incident_updates
WHERE incident_id = ANY(sqlc.arg(ids)::uuid[]) AND public
ORDER BY created_at DESC, id DESC;
-- name: ListIncidentStatuses :many
SELECT * FROM incident_statuses ORDER BY name;
-- name: CreateIncidentStatus :one
INSERT INTO incident_statuses (org_id, name, color) VALUES ($1, $2, $3) RETURNING *;
-- name: UpdateIncidentStatus :execrows
UPDATE incident_statuses SET name = sqlc.arg(name), color = sqlc.arg(color) WHERE id = sqlc.arg(id);
-- name: DeleteIncidentStatus :execrows
DELETE FROM incident_statuses WHERE id = $1;