-- latest_results holds the newest check result per monitor and region -- (see migration 00055) and has row-level security. -- name: LatestOK :many -- Whether the newest result of each region of the monitors ids succeeded. SELECT monitor_id, region, ok FROM latest_results WHERE monitor_id = ANY (sqlc.arg(ids)::uuid[]); -- name: UpsertLatestResults :many -- Keeps the newest of results, a JSON array, per monitor and -- region; results arriving late do not replace newer ones. It returns the -- rows it wrote. INSERT INTO latest_results (monitor_id, region, org_id, time, ok, latency_ms, status_code, error) SELECT DISTINCT ON (r.monitor_id, r.region) r.monitor_id, r.region, r.org_id, r.time, r.ok, r.latency_ms, r.status_code, r.error FROM jsonb_to_recordset(sqlc.arg(results)::jsonb) AS r(monitor_id uuid, region text, org_id uuid, time timestamptz, ok boolean, latency_ms integer, status_code integer, error text) ORDER BY r.monitor_id, r.region, r.time DESC ON CONFLICT (monitor_id, region) DO UPDATE SET org_id = excluded.org_id, time = excluded.time, ok = excluded.ok, latency_ms = excluded.latency_ms, status_code = excluded.status_code, error = excluded.error WHERE latest_results.time <= excluded.time RETURNING monitor_id, region, org_id, ok; -- name: LatestResults :many -- Newest result per monitor and region in the org, of the last day. SELECT monitor_id, region, time, ok, latency_ms, status_code, error FROM latest_results WHERE org_id = $1 AND time > now() - interval '1 day'; -- name: LatestResultsForMonitors :many -- LatestResults limited to the monitors ids. SELECT monitor_id, region, time, ok, latency_ms, status_code, error FROM latest_results WHERE org_id = $1 AND monitor_id = ANY (sqlc.arg(ids)::uuid[]) AND time > now() - interval '1 day';