Files
uptimesebi/internal/store/queries/monitors.sql
T
Sebastian UnterschützandClaude Opus 5.5 1138d555aa
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
perf: load console pages in time for organizations with many monitors
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
2026-11-26 08:00:38 +01:00

79 lines
3.3 KiB
SQL

-- name: CreateMonitor :one
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations, group_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *;
-- name: UpdateMonitor :one
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, group_id = $10, updated_at = now()
WHERE id = $1
RETURNING *;
-- name: DeleteMonitor :execrows
DELETE FROM monitors WHERE id = $1;
-- name: GetMonitor :one
SELECT * FROM monitors WHERE id = $1;
-- name: ListMonitors :many
-- The org filter, though row security implies it, lets the index be used.
SELECT * FROM monitors WHERE org_id = $1 ORDER BY name;
-- name: MonitorRegions :many
SELECT region_id FROM monitor_regions WHERE monitor_id = $1 ORDER BY region_id;
-- name: AllMonitorRegions :many
SELECT monitor_id, region_id FROM monitor_regions WHERE org_id = $1;
-- name: DeleteMonitorRegions :exec
DELETE FROM monitor_regions WHERE monitor_id = $1;
-- name: AddMonitorRegion :exec
INSERT INTO monitor_regions (monitor_id, org_id, region_id) VALUES ($1, $2, $3);
-- name: ListEnabledMonitorsForSchedule :many
-- System scope: every enabled monitor with its regions, for reconciling the
-- schedule.
SELECT m.id, m.interval_seconds, array_agg(r.region_id::text ORDER BY r.region_id)::text[] AS regions
FROM monitors m
JOIN monitor_regions r ON r.monitor_id = m.id
JOIN orgs o ON o.id = m.org_id AND o.suspended_at IS NULL
WHERE m.enabled
GROUP BY m.id, m.interval_seconds;
-- name: MonitorsForJobs :many
-- System scope: monitors to hand to a checker of region, skipping ones that
-- were disabled, deleted or moved out of the region since dispatch. An
-- org-owned checker only ever sees its org's monitors.
SELECT m.* FROM monitors m
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region_id = sqlc.arg(region_id)
JOIN orgs o ON o.id = m.org_id AND o.suspended_at IS NULL
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled
AND (sqlc.narg(agent_org)::uuid IS NULL OR m.org_id = sqlc.narg(agent_org));
-- name: MonitorsForResults :many
-- System scope: monitors a checker of region may report, with every region
-- they are assigned to, for evaluation.
SELECT m.id, m.org_id, m.name, m.target, m.interval_seconds, m.confirmations,
(SELECT array_agg(r2.region_id::text ORDER BY r2.region_id) FROM monitor_regions r2 WHERE r2.monitor_id = m.id)::text[] AS regions
FROM monitors m
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region_id = sqlc.arg(region_id)
JOIN orgs o ON o.id = m.org_id AND o.suspended_at IS NULL
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled
AND (sqlc.narg(agent_org)::uuid IS NULL OR m.org_id = sqlc.narg(agent_org));
-- name: ListMonitorsPage :many
-- By name, a page from a keyset cursor (see paging.go).
SELECT * FROM monitors
WHERE true
AND (sqlc.narg(after_s)::text IS NULL OR (name, id) > (sqlc.narg(after_s), sqlc.narg(after_id)::uuid))
AND (sqlc.narg(before_s)::text IS NULL OR (name, id) < (sqlc.narg(before_s), sqlc.narg(before_id)::uuid))
ORDER BY
CASE WHEN sqlc.narg(before_s)::text IS NULL THEN name END,
CASE WHEN sqlc.narg(before_s)::text IS NULL THEN id END,
name DESC, id DESC
LIMIT sqlc.arg(max_rows);
-- name: SetMonitorPosition :execrows
UPDATE monitors SET position = sqlc.arg(position) WHERE id = sqlc.arg(id);