Files
uptimesebi/internal/store/queries/limits.sql
T
Sebastian UnterschützandClaude Opus 5.5 cca4e80e54
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / test (pull_request) Successful in 9m5s
ci / lint (pull_request) Successful in 10m24s
ci / build (pull_request) Successful in 18m10s
ci / image (pull_request) Successful in 5m16s
feat(statuspages): email subscriptions, RSS feed and a JSON API
Visitors can subscribe to a page's incidents by email where the
organization allows it. They confirm their address first; confirmation
and unsubscribe links only show a button, so mail scanners do not act on
them, and mails carry RFC 8058 one-click unsubscribe headers. Subscribers
get a mail when an incident opens or resolves and for every public
update.

Every page also has an RSS feed of incidents and planned maintenance and
a read-only JSON API (status, summary, components, incidents), both
served from the cached snapshot.

Abuse protection: status page servers limit feed and API reads,
subscription requests and link actions per visitor; the API limits
subscription requests per visitor, mailbox and page, and a new limit caps
the subscriptions an organization's pages gain in seven days (platform
default and per-organization override, 1000 in the cloud). Unconfirmed
subscriptions count towards it and are deleted after a week.

The fixed cap of 200 monitors per status page is gone; the configurable
limit alone decides.

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

57 lines
3.0 KiB
SQL

-- name: LockOrgUsage :exec
-- Serializes creations counted against org's limits until the transaction
-- ends.
SELECT pg_advisory_xact_lock(hashtextextended('usage:' || sqlc.arg(org_id)::uuid::text, 0));
-- name: CountMonitors :one
SELECT count(*) FROM monitors WHERE org_id = $1;
-- name: CountStatusPages :one
SELECT count(*) FROM status_pages WHERE org_id = $1;
-- name: CountStatusPageMonitors :one
SELECT count(*) FROM status_page_monitors WHERE page_id = $1;
-- name: SetOrgLimits :one
UPDATE orgs SET limit_monitors = sqlc.narg(monitors), limit_status_pages = sqlc.narg(status_pages),
limit_regions_per_monitor = sqlc.narg(regions_per_monitor), limit_own_regions_count = sqlc.narg(own_regions_count),
limit_monitors_per_page = sqlc.narg(monitors_per_page), limit_subscribers_per_week = sqlc.narg(subscribers_per_week)
WHERE id = sqlc.arg(id)
RETURNING id, slug;
-- name: SetUserOrgLimit :one
UPDATE users SET limit_orgs = sqlc.narg(orgs) WHERE id = sqlc.arg(id)
RETURNING id, email;
-- name: AdminListOrgs :many
-- System scope: organizations with their usage, limit overrides and owner,
-- matching search in name, slug or owner email; newest first, a page from a
-- keyset cursor.
SELECT o.id, o.slug, o.name, o.created_at, o.suspended_at, o.suspended_reason,
o.limit_monitors, o.limit_status_pages, o.limit_regions_per_monitor, o.limit_own_regions_count,
o.limit_monitors_per_page, o.limit_subscribers_per_week,
(SELECT count(*) FROM monitors m WHERE m.org_id = o.id)::integer AS monitors,
(SELECT count(*) FROM status_pages p WHERE p.org_id = o.id)::integer AS status_pages,
coalesce(ow.id, '00000000-0000-0000-0000-000000000000'::uuid)::uuid AS owner_id,
coalesce(ow.email::text, '')::text AS owner_email, ow.disabled_at AS owner_disabled_at,
coalesce(ow.disabled_reason, '')::text AS owner_disabled_reason, coalesce(ow.is_admin, false)::boolean AS owner_is_admin,
ow.limit_orgs AS owner_limit_orgs,
coalesce(ow.owned, 0)::integer AS owner_orgs
FROM orgs o
LEFT JOIN LATERAL (
SELECT u.id, u.email, u.disabled_at, u.disabled_reason, u.is_admin, u.limit_orgs,
(SELECT count(*) FROM memberships m2 WHERE m2.user_id = u.id AND m2.is_owner) AS owned
FROM memberships m JOIN users u ON u.id = m.user_id
WHERE m.org_id = o.id AND m.is_owner ORDER BY m.created_at LIMIT 1
) ow ON true
WHERE (sqlc.arg(search)::text = ''
OR o.name ILIKE '%' || sqlc.arg(search) || '%' OR o.slug::text ILIKE '%' || sqlc.arg(search) || '%'
OR coalesce(ow.email::text, '') ILIKE '%' || sqlc.arg(search) || '%')
AND (sqlc.narg(after_t)::timestamptz IS NULL OR (o.created_at, o.id) < (sqlc.narg(after_t), sqlc.narg(after_id)::uuid))
AND (sqlc.narg(before_t)::timestamptz IS NULL OR (o.created_at, o.id) > (sqlc.narg(before_t), sqlc.narg(before_id)::uuid))
ORDER BY
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN o.created_at END DESC,
CASE WHEN sqlc.narg(before_t)::timestamptz IS NULL THEN o.id END DESC,
o.created_at, o.id
LIMIT sqlc.arg(max_rows);