146 lines
6.0 KiB
SQL
146 lines
6.0 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: RecentResults :many
|
|
SELECT time, region, ok, latency_ms, status_code, error, checker_id
|
|
FROM check_results
|
|
WHERE org_id = $1 AND monitor_id = $2 AND time > $3
|
|
ORDER BY time DESC
|
|
LIMIT 500;
|
|
|
|
-- 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 latest results run by one checker instance.
|
|
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 = $1 AND r.checker_id = sqlc.arg(checker_id) AND r.time > now() - interval '1 day'
|
|
ORDER BY r.time DESC
|
|
LIMIT 50;
|
|
|
|
-- 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';
|