Files
Sebastian UnterschützandClaude Opus 5.5 d7c5cd711e
ci / image (pull_request) Successful in 3m18s
ci / build (pull_request) Successful in 8m38s
ci / test (pull_request) Successful in 12m6s
ci / lint (pull_request) Successful in 15m0s
feat: page long and truncated lists
Lists that were cut off by a fixed LIMIT, or could grow without bound,
are paged by keyset: the cursor holds the sort key and an ID, so pages
neither skip nor repeat items as others are added. The audit logs had a
timestamp-only cursor that could skip entries with equal times.

The API returns at most 100 items per list (limit takes up to 500) with
next_cursor and prev_cursor, passed back as after and before. The
console shows Newer/Older (or Previous/Next) links and To the start,
without JavaScript.

Paged: incidents (optionally of one monitor), a monitor's results and
evidence, a checker's results, the audit logs, members, maintenance
windows, firing Alertmanager alerts, the monitors API and the admin
lists of status pages and organizations, which move to their own admin
pages. A monitor's page shows its latest incidents and results and links
to all of them. The dashboard caps every group at 50 of its own monitors
with a link to a paged view of the group, and gains a search by name or
target. Lists filtered by permission after reading may show fewer items
on a page.

Closes #116

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

164 lines
7.4 KiB
SQL

-- check_results and its aggregates have no row-level security (see
-- migration 00013). Every query here must filter by org_id.
-- name: InsertCheckResults :copyfrom
INSERT INTO check_results (time, org_id, monitor_id, region, checker_id, ok, latency_ms, status_code, error)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
-- name: LatestResults :many
-- Newest result per monitor and region in the org.
SELECT DISTINCT ON (monitor_id, region) monitor_id, region, time, ok, latency_ms, status_code, error
FROM check_results
WHERE org_id = $1 AND time > now() - interval '1 day'
ORDER BY monitor_id, region, time DESC;
-- name: ListResults :many
-- A monitor's results newest first, a page from a keyset cursor (see
-- paging.go) whose string key is region and checker.
SELECT time, region, ok, latency_ms, status_code, error, checker_id,
(region || '|' || checker_id::text)::text AS position
FROM check_results
WHERE org_id = sqlc.arg(org_id) AND monitor_id = sqlc.arg(monitor_id)
AND (sqlc.narg(after_t)::timestamptz IS NULL
OR time <= sqlc.narg(after_t) AND (time, region || '|' || checker_id::text) < (sqlc.narg(after_t), sqlc.narg(after_s)::text))
AND (sqlc.narg(before_t)::timestamptz IS NULL
OR time >= sqlc.narg(before_t) AND (time, region || '|' || checker_id::text) > (sqlc.narg(before_t), sqlc.narg(before_s)::text))
ORDER BY
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN time END DESC,
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN region || '|' || checker_id::text END DESC,
time, region || '|' || checker_id::text
LIMIT sqlc.arg(max_rows);
-- name: UptimeSince :many
-- Uptime per monitor from the hourly aggregate, including the not yet
-- materialized most recent hours (real-time aggregation).
SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
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;
-- name: HourlyBuckets :many
-- Per monitor and hour since $2, across all regions.
SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms
FROM check_results_1h
WHERE org_id = $1 AND bucket >= $2
GROUP BY monitor_id, bucket
ORDER BY monitor_id, bucket;
-- name: DailyBuckets :many
SELECT bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
FROM check_results_1d
WHERE org_id = $1 AND monitor_id = $2 AND bucket >= $3
GROUP BY bucket
ORDER BY bucket;
-- name: DailyBucketsForMonitors :many
SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
FROM check_results_1d
WHERE org_id = $1 AND monitor_id = ANY (sqlc.arg(ids)::uuid[]) AND bucket >= $2
GROUP BY monitor_id, bucket
ORDER BY monitor_id, bucket;
-- name: RangeBuckets :many
-- Raw results bucketed by width since a point in time, for short ranges.
SELECT monitor_id, time_bucket(sqlc.arg(width)::text::interval, time)::timestamptz AS bucket,
count(*)::bigint AS total, count(*) FILTER (WHERE ok)::bigint AS ok_count,
coalesce(avg(latency_ms), 0)::real AS avg_latency_ms
FROM check_results
WHERE org_id = $1 AND time >= sqlc.arg(since)
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
GROUP BY 1, 2
ORDER BY 1, 2;
-- name: RangeBucketsHourly :many
-- The hourly aggregate rebucketed by width, for ranges of days.
SELECT monitor_id, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms
FROM check_results_1h
WHERE org_id = $1 AND bucket >= sqlc.arg(since)
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
GROUP BY 1, 2
ORDER BY 1, 2;
-- name: RangeBucketsDaily :many
-- The daily aggregate rebucketed by width, for ranges of months.
SELECT monitor_id, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms
FROM check_results_1d
WHERE org_id = $1 AND bucket >= sqlc.arg(since)
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
GROUP BY 1, 2
ORDER BY 1, 2;
-- name: RegionBuckets :many
-- One monitor's raw results per region.
SELECT region, time_bucket(sqlc.arg(width)::text::interval, time)::timestamptz AS bucket,
count(*)::bigint AS total, count(*) FILTER (WHERE ok)::bigint AS ok_count
FROM check_results
WHERE org_id = $1 AND monitor_id = sqlc.arg(monitor_id) AND time >= sqlc.arg(since)
GROUP BY 1, 2
ORDER BY 1, 2;
-- name: RegionBucketsHourly :many
SELECT region, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
FROM check_results_1h
WHERE org_id = $1 AND monitor_id = sqlc.arg(monitor_id) AND bucket >= sqlc.arg(since)
GROUP BY 1, 2
ORDER BY 1, 2;
-- name: RegionBucketsDaily :many
SELECT region, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
FROM check_results_1d
WHERE org_id = $1 AND monitor_id = sqlc.arg(monitor_id) AND bucket >= sqlc.arg(since)
GROUP BY 1, 2
ORDER BY 1, 2;
-- name: FirstDataDaily :many
-- When each monitor and region first reported, to the day.
SELECT monitor_id, region, min(bucket)::timestamptz AS first
FROM check_results_1d
WHERE org_id = $1
GROUP BY 1, 2;
-- name: FirstDataRecent :many
-- When each monitor and region first reported since $2, exactly. Used for
-- data younger than the daily aggregate's resolution.
SELECT monitor_id, region, min(time)::timestamptz AS first
FROM check_results
WHERE org_id = $1 AND time >= $2
GROUP BY 1, 2;
-- name: ResultsByChecker :many
-- The organization's results run by one checker instance, newest first, a
-- page from a keyset cursor whose string key is monitor and region.
SELECT r.time, r.monitor_id, r.region, r.ok, r.latency_ms, r.status_code, r.error
FROM check_results r
WHERE r.org_id = sqlc.arg(org_id) AND r.checker_id = sqlc.arg(checker_id)
AND (sqlc.narg(after_t)::timestamptz IS NULL
OR r.time <= sqlc.narg(after_t) AND (r.time, r.monitor_id::text || '|' || r.region) < (sqlc.narg(after_t), sqlc.narg(after_s)::text))
AND (sqlc.narg(before_t)::timestamptz IS NULL
OR r.time >= sqlc.narg(before_t) AND (r.time, r.monitor_id::text || '|' || r.region) > (sqlc.narg(before_t), sqlc.narg(before_s)::text))
ORDER BY
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN r.time END DESC,
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN r.monitor_id::text || '|' || r.region END DESC,
r.time, r.monitor_id::text || '|' || r.region
LIMIT sqlc.arg(max_rows);
-- name: CheckerStats :one
-- How much one instance checked for the organization in the last hour.
SELECT count(*)::bigint AS total, count(*) FILTER (WHERE NOT ok)::bigint AS failed,
coalesce(avg(latency_ms), 0)::real AS avg_latency_ms
FROM check_results
WHERE org_id = $1 AND checker_id = sqlc.arg(checker_id) AND time > now() - interval '1 hour';