Files
uptimesebi/internal/store/queries/evidence.sql
T
Sebastian UnterschützandClaude Opus 5.5 e26194d260
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 13m0s
ci / test (pull_request) Successful in 13m56s
ci / build (pull_request) Successful in 19m56s
ci / image (pull_request) Successful in 5m50s
fix(evidence): spread periodic evidence and store a report's in one go
Checkers send evidence of healthy checks every 10 minutes, first with
the first result after they start. When checkers restart together, as
after a self-update, every monitor's evidence came in the same minute,
every 10 minutes. The API stored each in a transaction of its own while
the checker waited, reports took up to 36 seconds, the checkers' slots
filled and runs piled up: the overdue waves seen in prod.

Evidence sent for another reason than the period now puts the next
periodic one at a random point of the period, and the API writes all of
a report's evidence in one transaction, with one query each for the
successes to replace and the failures last kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-29 18:16:05 +01:00

39 lines
1.7 KiB
SQL

-- name: InsertEvidence :exec
INSERT INTO check_evidence (org_id, monitor_id, region, checker_id, time, ok, status_code, error, evidence)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
-- name: DeleteSuccessEvidence :exec
-- Only the latest success per monitor and region is kept.
DELETE FROM check_evidence
WHERE region = sqlc.arg(region) AND ok AND monitor_id = ANY(sqlc.arg(monitor_ids)::uuid[]);
-- name: LastFailureEvidence :many
-- The newest failure per monitor in region.
SELECT DISTINCT ON (monitor_id) monitor_id, time, coalesce(error, '')::text AS error
FROM check_evidence
WHERE region = sqlc.arg(region) AND NOT ok AND monitor_id = ANY(sqlc.arg(monitor_ids)::uuid[])
ORDER BY monitor_id, time DESC;
-- name: ListEvidence :many
-- Newest first, a page from a keyset cursor (see paging.go).
SELECT id, region, checker_id, time, ok, status_code, error FROM check_evidence
WHERE monitor_id = sqlc.arg(monitor_id)
AND (sqlc.narg(after_t)::timestamptz IS NULL OR (time, id) < (sqlc.narg(after_t), sqlc.narg(after_id)::uuid))
AND (sqlc.narg(before_t)::timestamptz IS NULL OR (time, id) > (sqlc.narg(before_t), sqlc.narg(before_id)::uuid))
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 id END DESC,
time, id
LIMIT sqlc.arg(max_rows);
-- name: EvidenceBetween :many
-- Evidence of a monitor in a time span, to link results to it.
SELECT id, region, time FROM check_evidence
WHERE monitor_id = $1 AND time BETWEEN sqlc.arg(since) AND sqlc.arg(until);
-- name: GetEvidence :one
SELECT * FROM check_evidence WHERE id = $1 AND monitor_id = $2;
-- name: PurgeEvidence :execrows
DELETE FROM check_evidence WHERE time < $1;