ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m12s
ci / test (pull_request) Successful in 12m47s
ci / build (pull_request) Canceled after 1m41s
ci / image (pull_request) Canceled after 0s
- The range picker no longer looks for the first result: on the real-time daily aggregate that aggregated the organization's recent raw results on every request. The oldest monitor's creation bounds the ranges now. - Sorting worked out each row's key per comparison; it does so once now and moves each row once (40-60 ms to 5 ms for 3,500 monitors). - The queries behind the monitor list run in parallel. - The monitor API reads the latest results of the page's monitors only. - The dashboard reports its steps in a Server-Timing header and logs them when it takes a second or more. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
158 lines
7.1 KiB
SQL
158 lines
7.1 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: 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';
|