ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m10s
ci / test (pull_request) Successful in 12m19s
ci / build (pull_request) Successful in 18m13s
ci / image (pull_request) Successful in 5m20s
Every console page that shows monitor status (monitors, incidents, monitor details, evidence) went through one loader that read, for all of the organization's monitors: - the newest result per monitor and region, by scanning a day of raw results; - the bars of the selected range, from raw results for ranges up to 24h, though only one page of monitors is shown; - when each monitor first reported, scanning two days of results. With 3000 monitors that took about a second, growing with the number of results. Now: - latest_results keeps the newest result per monitor and region, updated as results are stored (migration 00055 fills it from the last day); - bars are loaded only for the monitors a page shows; - first data is loaded per monitor on its page, and the monitor list asks only for the organization's earliest data, by index; - monitor, region and incident queries name the organization, so their indexes are used instead of row security filtering whole tables. Public status pages also read only their own monitors' latest results and hourly buckets instead of the whole organization's. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
175 lines
7.8 KiB
SQL
175 lines
7.8 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: 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
|
|
AND (sqlc.narg(ids)::uuid[] IS NULL OR monitor_id = ANY (sqlc.narg(ids)::uuid[]))
|
|
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 monitor_id = ANY (sqlc.arg(ids)::uuid[])
|
|
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 monitor_id = ANY (sqlc.arg(ids)::uuid[])
|
|
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 monitor_id = ANY (sqlc.arg(ids)::uuid[])
|
|
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 region of a monitor first reported, to the day.
|
|
SELECT monitor_id, region, min(bucket)::timestamptz AS first
|
|
FROM check_results_1d
|
|
WHERE org_id = $1 AND monitor_id = $2
|
|
GROUP BY 1, 2;
|
|
|
|
-- name: EarliestDataDaily :one
|
|
-- The day the organization's monitors first reported, if any did.
|
|
SELECT bucket::timestamptz AS first FROM check_results_1d WHERE org_id = $1 ORDER BY bucket LIMIT 1;
|
|
|
|
-- name: EarliestDataRecent :one
|
|
-- When the organization's monitors first reported since $2, exactly: the
|
|
-- first result of each monitor is found through its index, so the cost
|
|
-- does not grow with the days of results.
|
|
SELECT f.time FROM monitors m
|
|
CROSS JOIN LATERAL (
|
|
SELECT c.time FROM check_results c
|
|
WHERE c.org_id = $1 AND c.monitor_id = m.id AND c.time >= $2
|
|
ORDER BY c.time LIMIT 1
|
|
) f
|
|
WHERE m.org_id = $1
|
|
ORDER BY f.time LIMIT 1;
|
|
|
|
-- name: FirstDataRecent :many
|
|
-- When each region of a monitor first reported since $3, 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 monitor_id = $2 AND time >= $3
|
|
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';
|