feat(statuspages): email subscriptions, RSS feed and a JSON API
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
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
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
This commit is contained in:
1 parent
df39b765cb
commit
cca4e80e54
39 files changed
+4332
-1408
No files matched your search
@@ -15,7 +15,7 @@ import (
|
||||
const adminListOrgs = `-- name: AdminListOrgs :many
|
||||
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_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,
|
||||
@@ -52,26 +52,27 @@ type AdminListOrgsParams struct {
|
||||
}
|
||||
|
||||
type AdminListOrgsRow struct {
|
||||
ID uuid.UUID
|
||||
Slug string
|
||||
Name string
|
||||
CreatedAt time.Time
|
||||
SuspendedAt *time.Time
|
||||
SuspendedReason string
|
||||
LimitMonitors *int32
|
||||
LimitStatusPages *int32
|
||||
LimitRegionsPerMonitor *int32
|
||||
LimitOwnRegionsCount *bool
|
||||
LimitMonitorsPerPage *int32
|
||||
Monitors int32
|
||||
StatusPages int32
|
||||
OwnerID uuid.UUID
|
||||
OwnerEmail string
|
||||
OwnerDisabledAt *time.Time
|
||||
OwnerDisabledReason string
|
||||
OwnerIsAdmin bool
|
||||
OwnerLimitOrgs *int32
|
||||
OwnerOrgs int32
|
||||
ID uuid.UUID
|
||||
Slug string
|
||||
Name string
|
||||
CreatedAt time.Time
|
||||
SuspendedAt *time.Time
|
||||
SuspendedReason string
|
||||
LimitMonitors *int32
|
||||
LimitStatusPages *int32
|
||||
LimitRegionsPerMonitor *int32
|
||||
LimitOwnRegionsCount *bool
|
||||
LimitMonitorsPerPage *int32
|
||||
LimitSubscribersPerWeek *int32
|
||||
Monitors int32
|
||||
StatusPages int32
|
||||
OwnerID uuid.UUID
|
||||
OwnerEmail string
|
||||
OwnerDisabledAt *time.Time
|
||||
OwnerDisabledReason string
|
||||
OwnerIsAdmin bool
|
||||
OwnerLimitOrgs *int32
|
||||
OwnerOrgs int32
|
||||
}
|
||||
|
||||
// System scope: organizations with their usage, limit overrides and owner,
|
||||
@@ -105,6 +106,7 @@ func (q *Queries) AdminListOrgs(ctx context.Context, arg AdminListOrgsParams) ([
|
||||
&i.LimitRegionsPerMonitor,
|
||||
&i.LimitOwnRegionsCount,
|
||||
&i.LimitMonitorsPerPage,
|
||||
&i.LimitSubscribersPerWeek,
|
||||
&i.Monitors,
|
||||
&i.StatusPages,
|
||||
&i.OwnerID,
|
||||
@@ -172,18 +174,19 @@ func (q *Queries) LockOrgUsage(ctx context.Context, orgID uuid.UUID) error {
|
||||
const setOrgLimits = `-- name: SetOrgLimits :one
|
||||
UPDATE orgs SET limit_monitors = $1, limit_status_pages = $2,
|
||||
limit_regions_per_monitor = $3, limit_own_regions_count = $4,
|
||||
limit_monitors_per_page = $5
|
||||
WHERE id = $6
|
||||
limit_monitors_per_page = $5, limit_subscribers_per_week = $6
|
||||
WHERE id = $7
|
||||
RETURNING id, slug
|
||||
`
|
||||
|
||||
type SetOrgLimitsParams struct {
|
||||
Monitors *int32
|
||||
StatusPages *int32
|
||||
RegionsPerMonitor *int32
|
||||
OwnRegionsCount *bool
|
||||
MonitorsPerPage *int32
|
||||
ID uuid.UUID
|
||||
Monitors *int32
|
||||
StatusPages *int32
|
||||
RegionsPerMonitor *int32
|
||||
OwnRegionsCount *bool
|
||||
MonitorsPerPage *int32
|
||||
SubscribersPerWeek *int32
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
type SetOrgLimitsRow struct {
|
||||
@@ -198,6 +201,7 @@ func (q *Queries) SetOrgLimits(ctx context.Context, arg SetOrgLimitsParams) (Set
|
||||
arg.RegionsPerMonitor,
|
||||
arg.OwnRegionsCount,
|
||||
arg.MonitorsPerPage,
|
||||
arg.SubscribersPerWeek,
|
||||
arg.ID,
|
||||
)
|
||||
var i SetOrgLimitsRow
|
||||
|
||||
+44
-16
@@ -318,22 +318,23 @@ type NotificationChannel struct {
|
||||
}
|
||||
|
||||
type Org struct {
|
||||
ID uuid.UUID
|
||||
Slug string
|
||||
Name string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
RequireTotp bool
|
||||
RequireSso bool
|
||||
ApiTokenMaxDays *int32
|
||||
DisabledCheckTypes []string
|
||||
SuspendedAt *time.Time
|
||||
SuspendedReason string
|
||||
LimitMonitors *int32
|
||||
LimitStatusPages *int32
|
||||
LimitRegionsPerMonitor *int32
|
||||
LimitOwnRegionsCount *bool
|
||||
LimitMonitorsPerPage *int32
|
||||
ID uuid.UUID
|
||||
Slug string
|
||||
Name string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
RequireTotp bool
|
||||
RequireSso bool
|
||||
ApiTokenMaxDays *int32
|
||||
DisabledCheckTypes []string
|
||||
SuspendedAt *time.Time
|
||||
SuspendedReason string
|
||||
LimitMonitors *int32
|
||||
LimitStatusPages *int32
|
||||
LimitRegionsPerMonitor *int32
|
||||
LimitOwnRegionsCount *bool
|
||||
LimitMonitorsPerPage *int32
|
||||
LimitSubscribersPerWeek *int32
|
||||
}
|
||||
|
||||
type PlatformIdentity struct {
|
||||
@@ -435,6 +436,7 @@ type StatusPage struct {
|
||||
DomainCheckedAt *time.Time
|
||||
DomainFailingSince *time.Time
|
||||
DomainVerifyMethod *string
|
||||
Subscriptions bool
|
||||
}
|
||||
|
||||
type StatusPageGroup struct {
|
||||
@@ -458,6 +460,32 @@ type StatusPageMonitor struct {
|
||||
GroupID *uuid.UUID
|
||||
}
|
||||
|
||||
type StatusPageSubscriber struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
PageID uuid.UUID
|
||||
Email string
|
||||
Locale string
|
||||
PageUrl string
|
||||
CreatedAt time.Time
|
||||
ConfirmedAt *time.Time
|
||||
}
|
||||
|
||||
type SubscriberNotification struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
SubscriberID uuid.UUID
|
||||
IncidentID uuid.UUID
|
||||
UpdateID *uuid.UUID
|
||||
Event string
|
||||
Status string
|
||||
Attempts int32
|
||||
NextAttemptAt time.Time
|
||||
LastError *string
|
||||
CreatedAt time.Time
|
||||
SentAt *time.Time
|
||||
}
|
||||
|
||||
type SupportMessage struct {
|
||||
ID uuid.UUID
|
||||
TicketID uuid.UUID
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
const createOrg = `-- name: CreateOrg :one
|
||||
INSERT INTO orgs (id, slug, name) VALUES ($1, $2, $3)
|
||||
RETURNING id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page
|
||||
RETURNING id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page, limit_subscribers_per_week
|
||||
`
|
||||
|
||||
type CreateOrgParams struct {
|
||||
@@ -42,6 +42,7 @@ func (q *Queries) CreateOrg(ctx context.Context, arg CreateOrgParams) (Org, erro
|
||||
&i.LimitRegionsPerMonitor,
|
||||
&i.LimitOwnRegionsCount,
|
||||
&i.LimitMonitorsPerPage,
|
||||
&i.LimitSubscribersPerWeek,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -59,7 +60,7 @@ func (q *Queries) DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
}
|
||||
|
||||
const getOrg = `-- name: GetOrg :one
|
||||
SELECT id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page FROM orgs WHERE id = $1
|
||||
SELECT id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page, limit_subscribers_per_week FROM orgs WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetOrg(ctx context.Context, id uuid.UUID) (Org, error) {
|
||||
@@ -82,12 +83,13 @@ func (q *Queries) GetOrg(ctx context.Context, id uuid.UUID) (Org, error) {
|
||||
&i.LimitRegionsPerMonitor,
|
||||
&i.LimitOwnRegionsCount,
|
||||
&i.LimitMonitorsPerPage,
|
||||
&i.LimitSubscribersPerWeek,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getOrgBySlug = `-- name: GetOrgBySlug :one
|
||||
SELECT id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page FROM orgs WHERE slug = $1
|
||||
SELECT id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page, limit_subscribers_per_week FROM orgs WHERE slug = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetOrgBySlug(ctx context.Context, slug string) (Org, error) {
|
||||
@@ -110,12 +112,13 @@ func (q *Queries) GetOrgBySlug(ctx context.Context, slug string) (Org, error) {
|
||||
&i.LimitRegionsPerMonitor,
|
||||
&i.LimitOwnRegionsCount,
|
||||
&i.LimitMonitorsPerPage,
|
||||
&i.LimitSubscribersPerWeek,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listOrgs = `-- name: ListOrgs :many
|
||||
SELECT id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page FROM orgs ORDER BY name
|
||||
SELECT id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page, limit_subscribers_per_week FROM orgs ORDER BY name
|
||||
`
|
||||
|
||||
func (q *Queries) ListOrgs(ctx context.Context) ([]Org, error) {
|
||||
@@ -144,6 +147,7 @@ func (q *Queries) ListOrgs(ctx context.Context) ([]Org, error) {
|
||||
&i.LimitRegionsPerMonitor,
|
||||
&i.LimitOwnRegionsCount,
|
||||
&i.LimitMonitorsPerPage,
|
||||
&i.LimitSubscribersPerWeek,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -172,7 +176,7 @@ func (q *Queries) SetOrgDisabledCheckTypes(ctx context.Context, arg SetOrgDisabl
|
||||
const updateOrgPolicy = `-- name: UpdateOrgPolicy :one
|
||||
UPDATE orgs SET require_totp = $2, require_sso = $3, api_token_max_days = $4, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page
|
||||
RETURNING id, slug, name, created_at, updated_at, require_totp, require_sso, api_token_max_days, disabled_check_types, suspended_at, suspended_reason, limit_monitors, limit_status_pages, limit_regions_per_monitor, limit_own_regions_count, limit_monitors_per_page, limit_subscribers_per_week
|
||||
`
|
||||
|
||||
type UpdateOrgPolicyParams struct {
|
||||
@@ -207,6 +211,7 @@ func (q *Queries) UpdateOrgPolicy(ctx context.Context, arg UpdateOrgPolicyParams
|
||||
&i.LimitRegionsPerMonitor,
|
||||
&i.LimitOwnRegionsCount,
|
||||
&i.LimitMonitorsPerPage,
|
||||
&i.LimitSubscribersPerWeek,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -46,15 +46,22 @@ type Querier interface {
|
||||
// System scope. Leases due rows for five minutes so a crashed sender's rows
|
||||
// come back; SKIP LOCKED lets every API replica work the outbox.
|
||||
ClaimNotifications(ctx context.Context, max int32) ([]uuid.UUID, error)
|
||||
// System scope; leased like ClaimNotifications.
|
||||
ClaimSubscriberNotifications(ctx context.Context, max int32) ([]uuid.UUID, error)
|
||||
// Shortens tokens that would outlive a newly set maximum lifetime.
|
||||
ClampAPITokenExpiry(ctx context.Context, arg ClampAPITokenExpiryParams) (int64, error)
|
||||
ClearMaintenanceTargets(ctx context.Context, windowID uuid.UUID) error
|
||||
ClearStatusPageGroups(ctx context.Context, pageID uuid.UUID) error
|
||||
ClearStatusPageMonitors(ctx context.Context, pageID uuid.UUID) error
|
||||
ConfirmPageSubscriber(ctx context.Context, id uuid.UUID) error
|
||||
CountMonitors(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountOpenSupportTickets(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
CountOwnedOrgs(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
CountOwners(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountPageSubscribers(ctx context.Context, pageID uuid.UUID) (CountPageSubscribersRow, error)
|
||||
// Subscriptions the organization's pages gained since $2, confirmed or
|
||||
// not, for the weekly limit.
|
||||
CountRecentSubscribers(ctx context.Context, arg CountRecentSubscribersParams) (int64, error)
|
||||
CountRoleAssignments(ctx context.Context, roleID uuid.UUID) (int64, error)
|
||||
CountStatusPageMonitors(ctx context.Context, pageID uuid.UUID) (int64, error)
|
||||
CountStatusPages(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
@@ -75,6 +82,7 @@ type Querier interface {
|
||||
CreateMaintenance(ctx context.Context, arg CreateMaintenanceParams) (MaintenanceWindow, error)
|
||||
CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error)
|
||||
CreateOrg(ctx context.Context, arg CreateOrgParams) (Org, error)
|
||||
CreatePageSubscriber(ctx context.Context, arg CreatePageSubscriberParams) (StatusPageSubscriber, error)
|
||||
CreatePlatformIDP(ctx context.Context, arg CreatePlatformIDPParams) (PlatformIdentityProvider, error)
|
||||
CreatePlatformIdentity(ctx context.Context, arg CreatePlatformIdentityParams) error
|
||||
CreateRegion(ctx context.Context, arg CreateRegionParams) (Region, error)
|
||||
@@ -107,6 +115,7 @@ type Querier interface {
|
||||
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
|
||||
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
DeleteOrgStorage(ctx context.Context, orgID uuid.UUID) error
|
||||
DeletePageSubscriber(ctx context.Context, arg DeletePageSubscriberParams) (int64, error)
|
||||
DeletePlatformIDP(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
DeletePlatformIdentity(ctx context.Context, arg DeletePlatformIdentityParams) (int64, error)
|
||||
DeleteRecoveryCodes(ctx context.Context, userID uuid.UUID) error
|
||||
@@ -133,6 +142,9 @@ type Querier interface {
|
||||
EmailKeyTaken(ctx context.Context, emailKey *string) (bool, error)
|
||||
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
||||
EnqueueNotification(ctx context.Context, arg EnqueueNotificationParams) error
|
||||
// Queues the event of incident for every confirmed subscriber of the
|
||||
// published pages showing its monitor that take subscriptions.
|
||||
EnqueueSubscriberNotifications(ctx context.Context, arg EnqueueSubscriberNotificationsParams) (int64, error)
|
||||
// An empty name names a new region after its key and keeps an existing name.
|
||||
EnsurePlatformRegion(ctx context.Context, arg EnsurePlatformRegionParams) (Region, error)
|
||||
// Evidence of a monitor in a time span, to link results to it.
|
||||
@@ -170,6 +182,8 @@ type Querier interface {
|
||||
GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error)
|
||||
GetOrg(ctx context.Context, id uuid.UUID) (Org, error)
|
||||
GetOrgBySlug(ctx context.Context, slug string) (Org, error)
|
||||
GetPageSubscriber(ctx context.Context, id uuid.UUID) (GetPageSubscriberRow, error)
|
||||
GetPageSubscriberByEmail(ctx context.Context, arg GetPageSubscriberByEmailParams) (StatusPageSubscriber, error)
|
||||
GetPlatformIDP(ctx context.Context, id uuid.UUID) (PlatformIdentityProvider, error)
|
||||
GetPlatformIdentity(ctx context.Context, arg GetPlatformIdentityParams) (PlatformIdentity, error)
|
||||
GetPlatformSetting(ctx context.Context, key string) ([]byte, error)
|
||||
@@ -249,6 +263,8 @@ type Querier interface {
|
||||
// By email, a page from a keyset cursor (see paging.go).
|
||||
ListOrgMembersPage(ctx context.Context, arg ListOrgMembersPageParams) ([]ListOrgMembersPageRow, error)
|
||||
ListOrgs(ctx context.Context) ([]Org, error)
|
||||
// The newest subscribers of a page first.
|
||||
ListPageSubscribers(ctx context.Context, arg ListPageSubscribersParams) ([]ListPageSubscribersRow, error)
|
||||
ListPendingInvitations(ctx context.Context) ([]ListPendingInvitationsRow, error)
|
||||
ListPlatformIDPs(ctx context.Context) ([]PlatformIdentityProvider, error)
|
||||
ListPlatformRegions(ctx context.Context) ([]Region, error)
|
||||
@@ -289,6 +305,8 @@ type Querier interface {
|
||||
MarkInvitationAccepted(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
MarkNotificationFailed(ctx context.Context, arg MarkNotificationFailedParams) error
|
||||
MarkNotificationSent(ctx context.Context, id uuid.UUID) error
|
||||
MarkSubscriberNotificationFailed(ctx context.Context, arg MarkSubscriberNotificationFailedParams) error
|
||||
MarkSubscriberNotificationSent(ctx context.Context, id uuid.UUID) error
|
||||
// Every role a member holds, with the scope it applies to.
|
||||
MemberGrants(ctx context.Context, arg MemberGrantsParams) ([]MemberGrantsRow, error)
|
||||
MonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
|
||||
@@ -332,6 +350,10 @@ type Querier interface {
|
||||
// Delivered and given-up notifications; pending ones are still being worked.
|
||||
PurgeNotifications(ctx context.Context, createdAt time.Time) (int64, error)
|
||||
PurgeOrgCheckData(ctx context.Context, org uuid.UUID) error
|
||||
PurgeSubscriberNotifications(ctx context.Context, createdAt time.Time) (int64, error)
|
||||
// Unconfirmed subscriptions are kept for as long as they count towards the
|
||||
// weekly limit, so letting them lapse frees no room for more.
|
||||
PurgeUnconfirmedSubscribers(ctx context.Context, createdAt time.Time) (int64, error)
|
||||
// Accounts never confirmed; they belong to no organization.
|
||||
PurgeUnverifiedUsers(ctx context.Context, createdAt time.Time) (int64, error)
|
||||
// Raw results bucketed by width since a point in time, for short ranges.
|
||||
@@ -394,6 +416,8 @@ type Querier interface {
|
||||
StorageGet(ctx context.Context, arg StorageGetParams) (StorageGetRow, error)
|
||||
StorageList(ctx context.Context, arg StorageListParams) ([]StorageListRow, error)
|
||||
StoragePut(ctx context.Context, arg StoragePutParams) error
|
||||
// System scope: what a mail to a subscriber says.
|
||||
SubscriberNotificationDetails(ctx context.Context, ids []uuid.UUID) ([]SubscriberNotificationDetailsRow, error)
|
||||
SupportMessages(ctx context.Context, ticketID uuid.UUID) ([]SupportMessagesRow, error)
|
||||
SupportOptionsAlertGroups(ctx context.Context, orgID uuid.UUID) ([]SupportOptionsAlertGroupsRow, error)
|
||||
SupportOptionsChannels(ctx context.Context, orgID uuid.UUID) ([]SupportOptionsChannelsRow, error)
|
||||
@@ -414,6 +438,9 @@ type Querier interface {
|
||||
// Writes at most once a minute per instance; agents poll far more often.
|
||||
TouchAgentInstance(ctx context.Context, arg TouchAgentInstanceParams) error
|
||||
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
|
||||
// Keeps a pending subscription's language and link current when the
|
||||
// visitor asks again.
|
||||
TouchPendingSubscriber(ctx context.Context, arg TouchPendingSubscriberParams) error
|
||||
TouchPlatformIdentity(ctx context.Context, arg TouchPlatformIdentityParams) error
|
||||
// A verified domain whose record stayed missing past the grace period.
|
||||
UnverifyDomain(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
@@ -15,7 +15,7 @@ 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_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;
|
||||
|
||||
@@ -29,7 +29,7 @@ RETURNING id, email;
|
||||
-- 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_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,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
-- name: CreateStatusPage :one
|
||||
INSERT INTO status_pages (org_id, slug, title, description, published, show_component_details, domain_token, theme)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
INSERT INTO status_pages (org_id, slug, title, description, published, show_component_details, domain_token, theme, subscriptions)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateStatusPage :one
|
||||
@@ -11,6 +11,7 @@ SET slug = $2,
|
||||
published = $5,
|
||||
show_component_details = $6,
|
||||
theme = $7,
|
||||
subscriptions = $8,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
-- name: CountRecentSubscribers :one
|
||||
-- Subscriptions the organization's pages gained since $2, confirmed or
|
||||
-- not, for the weekly limit.
|
||||
SELECT count(*) FROM status_page_subscribers WHERE org_id = $1 AND created_at > $2;
|
||||
|
||||
-- name: GetPageSubscriberByEmail :one
|
||||
SELECT * FROM status_page_subscribers WHERE page_id = $1 AND email = $2;
|
||||
|
||||
-- name: CreatePageSubscriber :one
|
||||
INSERT INTO status_page_subscribers (org_id, page_id, email, locale, page_url)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING *;
|
||||
|
||||
-- name: TouchPendingSubscriber :exec
|
||||
-- Keeps a pending subscription's language and link current when the
|
||||
-- visitor asks again.
|
||||
UPDATE status_page_subscribers SET locale = $2, page_url = $3
|
||||
WHERE id = $1 AND confirmed_at IS NULL;
|
||||
|
||||
-- name: GetPageSubscriber :one
|
||||
SELECT s.*, p.title AS page_title FROM status_page_subscribers s
|
||||
JOIN status_pages p ON p.id = s.page_id
|
||||
WHERE s.id = $1;
|
||||
|
||||
-- name: ConfirmPageSubscriber :exec
|
||||
UPDATE status_page_subscribers SET confirmed_at = coalesce(confirmed_at, now()) WHERE id = $1;
|
||||
|
||||
-- name: DeletePageSubscriber :execrows
|
||||
DELETE FROM status_page_subscribers WHERE id = $1 AND page_id = $2;
|
||||
|
||||
-- name: ListPageSubscribers :many
|
||||
-- The newest subscribers of a page first.
|
||||
SELECT id, email, locale, created_at, confirmed_at FROM status_page_subscribers
|
||||
WHERE page_id = $1
|
||||
ORDER BY created_at DESC, id
|
||||
LIMIT sqlc.arg(max_rows);
|
||||
|
||||
-- name: CountPageSubscribers :one
|
||||
SELECT count(*) FILTER (WHERE confirmed_at IS NOT NULL)::integer AS confirmed,
|
||||
count(*) FILTER (WHERE confirmed_at IS NULL)::integer AS pending
|
||||
FROM status_page_subscribers WHERE page_id = $1;
|
||||
|
||||
-- name: EnqueueSubscriberNotifications :execrows
|
||||
-- Queues the event of incident for every confirmed subscriber of the
|
||||
-- published pages showing its monitor that take subscriptions.
|
||||
INSERT INTO subscriber_notifications (org_id, subscriber_id, incident_id, update_id, event)
|
||||
SELECT s.org_id, s.id, i.id, sqlc.narg(update_id)::uuid, sqlc.arg(event)
|
||||
FROM incidents i
|
||||
JOIN status_page_monitors spm ON spm.monitor_id = i.monitor_id
|
||||
JOIN status_pages p ON p.id = spm.page_id
|
||||
JOIN status_page_subscribers s ON s.page_id = p.id
|
||||
WHERE i.id = sqlc.arg(incident_id) AND s.confirmed_at IS NOT NULL
|
||||
AND p.published AND p.subscriptions AND p.suspended_at IS NULL
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: ClaimSubscriberNotifications :many
|
||||
-- System scope; leased like ClaimNotifications.
|
||||
UPDATE subscriber_notifications SET next_attempt_at = now() + interval '5 minutes'
|
||||
WHERE id IN (
|
||||
SELECT id FROM subscriber_notifications
|
||||
WHERE status = 'pending' AND next_attempt_at <= now()
|
||||
ORDER BY next_attempt_at
|
||||
LIMIT sqlc.arg(max)
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
RETURNING id;
|
||||
|
||||
-- name: SubscriberNotificationDetails :many
|
||||
-- System scope: what a mail to a subscriber says.
|
||||
SELECT n.id, n.event, n.attempts,
|
||||
s.id AS subscriber_id, s.email, s.locale, s.page_url,
|
||||
p.id AS page_id, p.slug, p.title AS page_title, p.custom_domain, p.domain_verified_at,
|
||||
EXISTS (SELECT 1 FROM agents a WHERE a.org_id = p.org_id AND a.kind = 'statuspage' AND a.last_seen_at IS NOT NULL) AS own_servers,
|
||||
i.started_at, i.resolved_at, i.title AS incident_title,
|
||||
coalesce(nullif(spm.label, ''), m.name)::text AS component,
|
||||
coalesce(u.status, '')::text AS update_status, coalesce(st.name, '')::text AS update_status_name,
|
||||
coalesce(u.message, '')::text AS update_message
|
||||
FROM subscriber_notifications n
|
||||
JOIN status_page_subscribers s ON s.id = n.subscriber_id
|
||||
JOIN status_pages p ON p.id = s.page_id
|
||||
JOIN incidents i ON i.id = n.incident_id
|
||||
JOIN monitors m ON m.id = i.monitor_id
|
||||
LEFT JOIN status_page_monitors spm ON spm.page_id = p.id AND spm.monitor_id = m.id
|
||||
LEFT JOIN incident_updates u ON u.id = n.update_id
|
||||
LEFT JOIN incident_statuses st ON st.id = u.status_id
|
||||
WHERE n.id = ANY (sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: MarkSubscriberNotificationSent :exec
|
||||
UPDATE subscriber_notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: MarkSubscriberNotificationFailed :exec
|
||||
UPDATE subscriber_notifications SET attempts = attempts + 1, last_error = sqlc.arg(last_error),
|
||||
status = CASE WHEN attempts + 1 >= sqlc.arg(max_attempts)::int THEN 'failed' ELSE 'pending' END,
|
||||
next_attempt_at = now() + make_interval(secs => sqlc.arg(retry_in_seconds)::int)
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: PurgeUnconfirmedSubscribers :execrows
|
||||
-- Unconfirmed subscriptions are kept for as long as they count towards the
|
||||
-- weekly limit, so letting them lapse frees no room for more.
|
||||
DELETE FROM status_page_subscribers WHERE confirmed_at IS NULL AND created_at < $1;
|
||||
|
||||
-- name: PurgeSubscriberNotifications :execrows
|
||||
DELETE FROM subscriber_notifications WHERE status IN ('sent', 'failed') AND created_at < $1;
|
||||
@@ -59,9 +59,9 @@ func (q *Queries) ClearStatusPageMonitors(ctx context.Context, pageID uuid.UUID)
|
||||
}
|
||||
|
||||
const createStatusPage = `-- name: CreateStatusPage :one
|
||||
INSERT INTO status_pages (org_id, slug, title, description, published, show_component_details, domain_token, theme)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method
|
||||
INSERT INTO status_pages (org_id, slug, title, description, published, show_component_details, domain_token, theme, subscriptions)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
RETURNING id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method, subscriptions
|
||||
`
|
||||
|
||||
type CreateStatusPageParams struct {
|
||||
@@ -73,6 +73,7 @@ type CreateStatusPageParams struct {
|
||||
ShowComponentDetails bool
|
||||
DomainToken string
|
||||
Theme []byte
|
||||
Subscriptions bool
|
||||
}
|
||||
|
||||
func (q *Queries) CreateStatusPage(ctx context.Context, arg CreateStatusPageParams) (StatusPage, error) {
|
||||
@@ -85,6 +86,7 @@ func (q *Queries) CreateStatusPage(ctx context.Context, arg CreateStatusPagePara
|
||||
arg.ShowComponentDetails,
|
||||
arg.DomainToken,
|
||||
arg.Theme,
|
||||
arg.Subscriptions,
|
||||
)
|
||||
var i StatusPage
|
||||
err := row.Scan(
|
||||
@@ -107,6 +109,7 @@ func (q *Queries) CreateStatusPage(ctx context.Context, arg CreateStatusPagePara
|
||||
&i.DomainCheckedAt,
|
||||
&i.DomainFailingSince,
|
||||
&i.DomainVerifyMethod,
|
||||
&i.Subscriptions,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -242,7 +245,7 @@ func (q *Queries) DomainsToCheck(ctx context.Context, arg DomainsToCheckParams)
|
||||
}
|
||||
|
||||
const findPublishedPage = `-- name: FindPublishedPage :one
|
||||
SELECT id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method FROM status_pages
|
||||
SELECT id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method, subscriptions FROM status_pages
|
||||
WHERE published AND suspended_at IS NULL
|
||||
AND NOT EXISTS (SELECT 1 FROM orgs o WHERE o.id = status_pages.org_id AND o.suspended_at IS NOT NULL)
|
||||
AND (($1::citext IS NOT NULL AND custom_domain = $1 AND domain_verified_at IS NOT NULL)
|
||||
@@ -279,12 +282,13 @@ func (q *Queries) FindPublishedPage(ctx context.Context, arg FindPublishedPagePa
|
||||
&i.DomainCheckedAt,
|
||||
&i.DomainFailingSince,
|
||||
&i.DomainVerifyMethod,
|
||||
&i.Subscriptions,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getStatusPage = `-- name: GetStatusPage :one
|
||||
SELECT id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method FROM status_pages WHERE id = $1
|
||||
SELECT id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method, subscriptions FROM status_pages WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetStatusPage(ctx context.Context, id uuid.UUID) (StatusPage, error) {
|
||||
@@ -310,6 +314,7 @@ func (q *Queries) GetStatusPage(ctx context.Context, id uuid.UUID) (StatusPage,
|
||||
&i.DomainCheckedAt,
|
||||
&i.DomainFailingSince,
|
||||
&i.DomainVerifyMethod,
|
||||
&i.Subscriptions,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -350,7 +355,7 @@ func (q *Queries) ListStatusPageGroups(ctx context.Context, pageID uuid.UUID) ([
|
||||
}
|
||||
|
||||
const listStatusPages = `-- name: ListStatusPages :many
|
||||
SELECT id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method FROM status_pages ORDER BY title
|
||||
SELECT id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method, subscriptions FROM status_pages ORDER BY title
|
||||
`
|
||||
|
||||
func (q *Queries) ListStatusPages(ctx context.Context) ([]StatusPage, error) {
|
||||
@@ -382,6 +387,7 @@ func (q *Queries) ListStatusPages(ctx context.Context) ([]StatusPage, error) {
|
||||
&i.DomainCheckedAt,
|
||||
&i.DomainFailingSince,
|
||||
&i.DomainVerifyMethod,
|
||||
&i.Subscriptions,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -520,7 +526,7 @@ UPDATE status_pages SET
|
||||
custom_domain = $1,
|
||||
updated_at = now()
|
||||
WHERE id = $2
|
||||
RETURNING id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method
|
||||
RETURNING id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method, subscriptions
|
||||
`
|
||||
|
||||
type SetStatusPageDomainParams struct {
|
||||
@@ -552,6 +558,7 @@ func (q *Queries) SetStatusPageDomain(ctx context.Context, arg SetStatusPageDoma
|
||||
&i.DomainCheckedAt,
|
||||
&i.DomainFailingSince,
|
||||
&i.DomainVerifyMethod,
|
||||
&i.Subscriptions,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -652,9 +659,10 @@ SET slug = $2,
|
||||
published = $5,
|
||||
show_component_details = $6,
|
||||
theme = $7,
|
||||
subscriptions = $8,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method
|
||||
RETURNING id, org_id, slug, title, description, published, custom_domain, domain_token, domain_verified_at, theme, created_at, updated_at, show_component_details, suspended_at, suspended_reason, domain_set_at, domain_checked_at, domain_failing_since, domain_verify_method, subscriptions
|
||||
`
|
||||
|
||||
type UpdateStatusPageParams struct {
|
||||
@@ -665,6 +673,7 @@ type UpdateStatusPageParams struct {
|
||||
Published bool
|
||||
ShowComponentDetails bool
|
||||
Theme []byte
|
||||
Subscriptions bool
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateStatusPage(ctx context.Context, arg UpdateStatusPageParams) (StatusPage, error) {
|
||||
@@ -676,6 +685,7 @@ func (q *Queries) UpdateStatusPage(ctx context.Context, arg UpdateStatusPagePara
|
||||
arg.Published,
|
||||
arg.ShowComponentDetails,
|
||||
arg.Theme,
|
||||
arg.Subscriptions,
|
||||
)
|
||||
var i StatusPage
|
||||
err := row.Scan(
|
||||
@@ -698,6 +708,7 @@ func (q *Queries) UpdateStatusPage(ctx context.Context, arg UpdateStatusPagePara
|
||||
&i.DomainCheckedAt,
|
||||
&i.DomainFailingSince,
|
||||
&i.DomainVerifyMethod,
|
||||
&i.Subscriptions,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,442 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: subscribers.sql
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const claimSubscriberNotifications = `-- name: ClaimSubscriberNotifications :many
|
||||
UPDATE subscriber_notifications SET next_attempt_at = now() + interval '5 minutes'
|
||||
WHERE id IN (
|
||||
SELECT id FROM subscriber_notifications
|
||||
WHERE status = 'pending' AND next_attempt_at <= now()
|
||||
ORDER BY next_attempt_at
|
||||
LIMIT $1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
// System scope; leased like ClaimNotifications.
|
||||
func (q *Queries) ClaimSubscriberNotifications(ctx context.Context, max int32) ([]uuid.UUID, error) {
|
||||
rows, err := q.db.Query(ctx, claimSubscriberNotifications, max)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []uuid.UUID{}
|
||||
for rows.Next() {
|
||||
var id uuid.UUID
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const confirmPageSubscriber = `-- name: ConfirmPageSubscriber :exec
|
||||
UPDATE status_page_subscribers SET confirmed_at = coalesce(confirmed_at, now()) WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) ConfirmPageSubscriber(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, confirmPageSubscriber, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const countPageSubscribers = `-- name: CountPageSubscribers :one
|
||||
SELECT count(*) FILTER (WHERE confirmed_at IS NOT NULL)::integer AS confirmed,
|
||||
count(*) FILTER (WHERE confirmed_at IS NULL)::integer AS pending
|
||||
FROM status_page_subscribers WHERE page_id = $1
|
||||
`
|
||||
|
||||
type CountPageSubscribersRow struct {
|
||||
Confirmed int32
|
||||
Pending int32
|
||||
}
|
||||
|
||||
func (q *Queries) CountPageSubscribers(ctx context.Context, pageID uuid.UUID) (CountPageSubscribersRow, error) {
|
||||
row := q.db.QueryRow(ctx, countPageSubscribers, pageID)
|
||||
var i CountPageSubscribersRow
|
||||
err := row.Scan(&i.Confirmed, &i.Pending)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const countRecentSubscribers = `-- name: CountRecentSubscribers :one
|
||||
SELECT count(*) FROM status_page_subscribers WHERE org_id = $1 AND created_at > $2
|
||||
`
|
||||
|
||||
type CountRecentSubscribersParams struct {
|
||||
OrgID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// Subscriptions the organization's pages gained since $2, confirmed or
|
||||
// not, for the weekly limit.
|
||||
func (q *Queries) CountRecentSubscribers(ctx context.Context, arg CountRecentSubscribersParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countRecentSubscribers, arg.OrgID, arg.CreatedAt)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const createPageSubscriber = `-- name: CreatePageSubscriber :one
|
||||
INSERT INTO status_page_subscribers (org_id, page_id, email, locale, page_url)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, org_id, page_id, email, locale, page_url, created_at, confirmed_at
|
||||
`
|
||||
|
||||
type CreatePageSubscriberParams struct {
|
||||
OrgID uuid.UUID
|
||||
PageID uuid.UUID
|
||||
Email string
|
||||
Locale string
|
||||
PageUrl string
|
||||
}
|
||||
|
||||
func (q *Queries) CreatePageSubscriber(ctx context.Context, arg CreatePageSubscriberParams) (StatusPageSubscriber, error) {
|
||||
row := q.db.QueryRow(ctx, createPageSubscriber,
|
||||
arg.OrgID,
|
||||
arg.PageID,
|
||||
arg.Email,
|
||||
arg.Locale,
|
||||
arg.PageUrl,
|
||||
)
|
||||
var i StatusPageSubscriber
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.PageID,
|
||||
&i.Email,
|
||||
&i.Locale,
|
||||
&i.PageUrl,
|
||||
&i.CreatedAt,
|
||||
&i.ConfirmedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deletePageSubscriber = `-- name: DeletePageSubscriber :execrows
|
||||
DELETE FROM status_page_subscribers WHERE id = $1 AND page_id = $2
|
||||
`
|
||||
|
||||
type DeletePageSubscriberParams struct {
|
||||
ID uuid.UUID
|
||||
PageID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) DeletePageSubscriber(ctx context.Context, arg DeletePageSubscriberParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deletePageSubscriber, arg.ID, arg.PageID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const enqueueSubscriberNotifications = `-- name: EnqueueSubscriberNotifications :execrows
|
||||
INSERT INTO subscriber_notifications (org_id, subscriber_id, incident_id, update_id, event)
|
||||
SELECT s.org_id, s.id, i.id, $1::uuid, $2
|
||||
FROM incidents i
|
||||
JOIN status_page_monitors spm ON spm.monitor_id = i.monitor_id
|
||||
JOIN status_pages p ON p.id = spm.page_id
|
||||
JOIN status_page_subscribers s ON s.page_id = p.id
|
||||
WHERE i.id = $3 AND s.confirmed_at IS NOT NULL
|
||||
AND p.published AND p.subscriptions AND p.suspended_at IS NULL
|
||||
ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
type EnqueueSubscriberNotificationsParams struct {
|
||||
UpdateID *uuid.UUID
|
||||
Event string
|
||||
IncidentID uuid.UUID
|
||||
}
|
||||
|
||||
// Queues the event of incident for every confirmed subscriber of the
|
||||
// published pages showing its monitor that take subscriptions.
|
||||
func (q *Queries) EnqueueSubscriberNotifications(ctx context.Context, arg EnqueueSubscriberNotificationsParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, enqueueSubscriberNotifications, arg.UpdateID, arg.Event, arg.IncidentID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getPageSubscriber = `-- name: GetPageSubscriber :one
|
||||
SELECT s.id, s.org_id, s.page_id, s.email, s.locale, s.page_url, s.created_at, s.confirmed_at, p.title AS page_title FROM status_page_subscribers s
|
||||
JOIN status_pages p ON p.id = s.page_id
|
||||
WHERE s.id = $1
|
||||
`
|
||||
|
||||
type GetPageSubscriberRow struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
PageID uuid.UUID
|
||||
Email string
|
||||
Locale string
|
||||
PageUrl string
|
||||
CreatedAt time.Time
|
||||
ConfirmedAt *time.Time
|
||||
PageTitle string
|
||||
}
|
||||
|
||||
func (q *Queries) GetPageSubscriber(ctx context.Context, id uuid.UUID) (GetPageSubscriberRow, error) {
|
||||
row := q.db.QueryRow(ctx, getPageSubscriber, id)
|
||||
var i GetPageSubscriberRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.PageID,
|
||||
&i.Email,
|
||||
&i.Locale,
|
||||
&i.PageUrl,
|
||||
&i.CreatedAt,
|
||||
&i.ConfirmedAt,
|
||||
&i.PageTitle,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getPageSubscriberByEmail = `-- name: GetPageSubscriberByEmail :one
|
||||
SELECT id, org_id, page_id, email, locale, page_url, created_at, confirmed_at FROM status_page_subscribers WHERE page_id = $1 AND email = $2
|
||||
`
|
||||
|
||||
type GetPageSubscriberByEmailParams struct {
|
||||
PageID uuid.UUID
|
||||
Email string
|
||||
}
|
||||
|
||||
func (q *Queries) GetPageSubscriberByEmail(ctx context.Context, arg GetPageSubscriberByEmailParams) (StatusPageSubscriber, error) {
|
||||
row := q.db.QueryRow(ctx, getPageSubscriberByEmail, arg.PageID, arg.Email)
|
||||
var i StatusPageSubscriber
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.PageID,
|
||||
&i.Email,
|
||||
&i.Locale,
|
||||
&i.PageUrl,
|
||||
&i.CreatedAt,
|
||||
&i.ConfirmedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listPageSubscribers = `-- name: ListPageSubscribers :many
|
||||
SELECT id, email, locale, created_at, confirmed_at FROM status_page_subscribers
|
||||
WHERE page_id = $1
|
||||
ORDER BY created_at DESC, id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListPageSubscribersParams struct {
|
||||
PageID uuid.UUID
|
||||
MaxRows int32
|
||||
}
|
||||
|
||||
type ListPageSubscribersRow struct {
|
||||
ID uuid.UUID
|
||||
Email string
|
||||
Locale string
|
||||
CreatedAt time.Time
|
||||
ConfirmedAt *time.Time
|
||||
}
|
||||
|
||||
// The newest subscribers of a page first.
|
||||
func (q *Queries) ListPageSubscribers(ctx context.Context, arg ListPageSubscribersParams) ([]ListPageSubscribersRow, error) {
|
||||
rows, err := q.db.Query(ctx, listPageSubscribers, arg.PageID, arg.MaxRows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListPageSubscribersRow{}
|
||||
for rows.Next() {
|
||||
var i ListPageSubscribersRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.Locale,
|
||||
&i.CreatedAt,
|
||||
&i.ConfirmedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markSubscriberNotificationFailed = `-- name: MarkSubscriberNotificationFailed :exec
|
||||
UPDATE subscriber_notifications SET attempts = attempts + 1, last_error = $1,
|
||||
status = CASE WHEN attempts + 1 >= $2::int THEN 'failed' ELSE 'pending' END,
|
||||
next_attempt_at = now() + make_interval(secs => $3::int)
|
||||
WHERE id = $4
|
||||
`
|
||||
|
||||
type MarkSubscriberNotificationFailedParams struct {
|
||||
LastError *string
|
||||
MaxAttempts int32
|
||||
RetryInSeconds int32
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) MarkSubscriberNotificationFailed(ctx context.Context, arg MarkSubscriberNotificationFailedParams) error {
|
||||
_, err := q.db.Exec(ctx, markSubscriberNotificationFailed,
|
||||
arg.LastError,
|
||||
arg.MaxAttempts,
|
||||
arg.RetryInSeconds,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const markSubscriberNotificationSent = `-- name: MarkSubscriberNotificationSent :exec
|
||||
UPDATE subscriber_notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) MarkSubscriberNotificationSent(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, markSubscriberNotificationSent, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const purgeSubscriberNotifications = `-- name: PurgeSubscriberNotifications :execrows
|
||||
DELETE FROM subscriber_notifications WHERE status IN ('sent', 'failed') AND created_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) PurgeSubscriberNotifications(ctx context.Context, createdAt time.Time) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, purgeSubscriberNotifications, createdAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const purgeUnconfirmedSubscribers = `-- name: PurgeUnconfirmedSubscribers :execrows
|
||||
DELETE FROM status_page_subscribers WHERE confirmed_at IS NULL AND created_at < $1
|
||||
`
|
||||
|
||||
// Unconfirmed subscriptions are kept for as long as they count towards the
|
||||
// weekly limit, so letting them lapse frees no room for more.
|
||||
func (q *Queries) PurgeUnconfirmedSubscribers(ctx context.Context, createdAt time.Time) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, purgeUnconfirmedSubscribers, createdAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const subscriberNotificationDetails = `-- name: SubscriberNotificationDetails :many
|
||||
SELECT n.id, n.event, n.attempts,
|
||||
s.id AS subscriber_id, s.email, s.locale, s.page_url,
|
||||
p.id AS page_id, p.slug, p.title AS page_title, p.custom_domain, p.domain_verified_at,
|
||||
EXISTS (SELECT 1 FROM agents a WHERE a.org_id = p.org_id AND a.kind = 'statuspage' AND a.last_seen_at IS NOT NULL) AS own_servers,
|
||||
i.started_at, i.resolved_at, i.title AS incident_title,
|
||||
coalesce(nullif(spm.label, ''), m.name)::text AS component,
|
||||
coalesce(u.status, '')::text AS update_status, coalesce(st.name, '')::text AS update_status_name,
|
||||
coalesce(u.message, '')::text AS update_message
|
||||
FROM subscriber_notifications n
|
||||
JOIN status_page_subscribers s ON s.id = n.subscriber_id
|
||||
JOIN status_pages p ON p.id = s.page_id
|
||||
JOIN incidents i ON i.id = n.incident_id
|
||||
JOIN monitors m ON m.id = i.monitor_id
|
||||
LEFT JOIN status_page_monitors spm ON spm.page_id = p.id AND spm.monitor_id = m.id
|
||||
LEFT JOIN incident_updates u ON u.id = n.update_id
|
||||
LEFT JOIN incident_statuses st ON st.id = u.status_id
|
||||
WHERE n.id = ANY ($1::uuid[])
|
||||
`
|
||||
|
||||
type SubscriberNotificationDetailsRow struct {
|
||||
ID uuid.UUID
|
||||
Event string
|
||||
Attempts int32
|
||||
SubscriberID uuid.UUID
|
||||
Email string
|
||||
Locale string
|
||||
PageUrl string
|
||||
PageID uuid.UUID
|
||||
Slug string
|
||||
PageTitle string
|
||||
CustomDomain *string
|
||||
DomainVerifiedAt *time.Time
|
||||
OwnServers bool
|
||||
StartedAt time.Time
|
||||
ResolvedAt *time.Time
|
||||
IncidentTitle string
|
||||
Component string
|
||||
UpdateStatus string
|
||||
UpdateStatusName string
|
||||
UpdateMessage string
|
||||
}
|
||||
|
||||
// System scope: what a mail to a subscriber says.
|
||||
func (q *Queries) SubscriberNotificationDetails(ctx context.Context, ids []uuid.UUID) ([]SubscriberNotificationDetailsRow, error) {
|
||||
rows, err := q.db.Query(ctx, subscriberNotificationDetails, ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []SubscriberNotificationDetailsRow{}
|
||||
for rows.Next() {
|
||||
var i SubscriberNotificationDetailsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Event,
|
||||
&i.Attempts,
|
||||
&i.SubscriberID,
|
||||
&i.Email,
|
||||
&i.Locale,
|
||||
&i.PageUrl,
|
||||
&i.PageID,
|
||||
&i.Slug,
|
||||
&i.PageTitle,
|
||||
&i.CustomDomain,
|
||||
&i.DomainVerifiedAt,
|
||||
&i.OwnServers,
|
||||
&i.StartedAt,
|
||||
&i.ResolvedAt,
|
||||
&i.IncidentTitle,
|
||||
&i.Component,
|
||||
&i.UpdateStatus,
|
||||
&i.UpdateStatusName,
|
||||
&i.UpdateMessage,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const touchPendingSubscriber = `-- name: TouchPendingSubscriber :exec
|
||||
UPDATE status_page_subscribers SET locale = $2, page_url = $3
|
||||
WHERE id = $1 AND confirmed_at IS NULL
|
||||
`
|
||||
|
||||
type TouchPendingSubscriberParams struct {
|
||||
ID uuid.UUID
|
||||
Locale string
|
||||
PageUrl string
|
||||
}
|
||||
|
||||
// Keeps a pending subscription's language and link current when the
|
||||
// visitor asks again.
|
||||
func (q *Queries) TouchPendingSubscriber(ctx context.Context, arg TouchPendingSubscriberParams) error {
|
||||
_, err := q.db.Exec(ctx, touchPendingSubscriber, arg.ID, arg.Locale, arg.PageUrl)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user