feat(store): add incidents, notification channels and delivery outbox
At most one open incident per monitor is enforced by a partial unique index. Refs #15, #16 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
4b4e134bfd
commit
ffef63e586
9 files changed
+770
-11
No files matched your search
@@ -0,0 +1,75 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- How many consecutive failures a region needs before it counts as down.
|
||||||
|
ALTER TABLE monitors ADD COLUMN confirmations integer NOT NULL DEFAULT 2 CHECK (confirmations BETWEEN 1 AND 10);
|
||||||
|
|
||||||
|
CREATE TABLE incidents (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
||||||
|
monitor_id uuid NOT NULL REFERENCES monitors (id) ON DELETE CASCADE,
|
||||||
|
started_at timestamptz NOT NULL,
|
||||||
|
resolved_at timestamptz,
|
||||||
|
-- Last error per region when the incident opened.
|
||||||
|
cause jsonb NOT NULL DEFAULT '{}',
|
||||||
|
CHECK (resolved_at IS NULL OR resolved_at >= started_at)
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Guards against duplicate incidents even if two evaluations race.
|
||||||
|
CREATE UNIQUE INDEX incidents_one_open_idx ON incidents (monitor_id) WHERE resolved_at IS NULL;
|
||||||
|
CREATE INDEX incidents_org_idx ON incidents (org_id, started_at DESC);
|
||||||
|
|
||||||
|
CREATE TABLE notification_channels (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
||||||
|
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 100),
|
||||||
|
type text NOT NULL CHECK (type IN ('webhook', 'email')),
|
||||||
|
-- Type-specific settings; secrets inside are sealed by the application.
|
||||||
|
config jsonb NOT NULL,
|
||||||
|
enabled boolean NOT NULL DEFAULT true,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
updated_at timestamptz NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX notification_channels_org_idx ON notification_channels (org_id);
|
||||||
|
|
||||||
|
-- Outbox: a row per channel and incident event, delivered with retries by
|
||||||
|
-- any API replica.
|
||||||
|
CREATE TABLE notifications (
|
||||||
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
org_id uuid NOT NULL REFERENCES orgs (id) ON DELETE CASCADE,
|
||||||
|
channel_id uuid NOT NULL REFERENCES notification_channels (id) ON DELETE CASCADE,
|
||||||
|
incident_id uuid NOT NULL REFERENCES incidents (id) ON DELETE CASCADE,
|
||||||
|
event text NOT NULL CHECK (event IN ('opened', 'resolved')),
|
||||||
|
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'sent', 'failed')),
|
||||||
|
attempts integer NOT NULL DEFAULT 0,
|
||||||
|
next_attempt_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
last_error text,
|
||||||
|
created_at timestamptz NOT NULL DEFAULT now(),
|
||||||
|
sent_at timestamptz,
|
||||||
|
UNIQUE (channel_id, incident_id, event)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX notifications_due_idx ON notifications (next_attempt_at) WHERE status = 'pending';
|
||||||
|
|
||||||
|
ALTER TABLE incidents ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE incidents FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY incidents_tenant ON incidents FOR ALL
|
||||||
|
USING (app_scope_system() OR org_id = app_org_id())
|
||||||
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
||||||
|
|
||||||
|
ALTER TABLE notification_channels ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE notification_channels FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY notification_channels_tenant ON notification_channels FOR ALL
|
||||||
|
USING (app_scope_system() OR org_id = app_org_id())
|
||||||
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
||||||
|
|
||||||
|
ALTER TABLE notifications ENABLE ROW LEVEL SECURITY;
|
||||||
|
ALTER TABLE notifications FORCE ROW LEVEL SECURITY;
|
||||||
|
CREATE POLICY notifications_tenant ON notifications FOR ALL
|
||||||
|
USING (app_scope_system() OR org_id = app_org_id())
|
||||||
|
WITH CHECK (app_scope_system() OR org_id = app_org_id());
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
DROP TABLE notifications;
|
||||||
|
DROP TABLE notification_channels;
|
||||||
|
DROP TABLE incidents;
|
||||||
|
ALTER TABLE monitors DROP COLUMN confirmations;
|
||||||
@@ -70,6 +70,44 @@ func (q *Queries) LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestR
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const latestResultsForMonitor = `-- name: LatestResultsForMonitor :many
|
||||||
|
SELECT DISTINCT ON (region) region, ok, error
|
||||||
|
FROM check_results
|
||||||
|
WHERE org_id = $1 AND monitor_id = $2 AND time > now() - interval '1 day'
|
||||||
|
ORDER BY region, time DESC
|
||||||
|
`
|
||||||
|
|
||||||
|
type LatestResultsForMonitorParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
type LatestResultsForMonitorRow struct {
|
||||||
|
Region string
|
||||||
|
Ok bool
|
||||||
|
Error *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) LatestResultsForMonitor(ctx context.Context, arg LatestResultsForMonitorParams) ([]LatestResultsForMonitorRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, latestResultsForMonitor, arg.OrgID, arg.MonitorID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []LatestResultsForMonitorRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i LatestResultsForMonitorRow
|
||||||
|
if err := rows.Scan(&i.Region, &i.Ok, &i.Error); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const recentResults = `-- name: RecentResults :many
|
const recentResults = `-- name: RecentResults :many
|
||||||
SELECT time, region, ok, latency_ms, status_code, error
|
SELECT time, region, ok, latency_ms, status_code, error
|
||||||
FROM check_results
|
FROM check_results
|
||||||
|
|||||||
@@ -0,0 +1,434 @@
|
|||||||
|
// Code generated by sqlc. DO NOT EDIT.
|
||||||
|
// versions:
|
||||||
|
// sqlc v1.31.1
|
||||||
|
// source: incidents.sql
|
||||||
|
|
||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
const claimNotifications = `-- name: ClaimNotifications :many
|
||||||
|
UPDATE notifications SET next_attempt_at = now() + interval '5 minutes'
|
||||||
|
WHERE id IN (
|
||||||
|
SELECT id FROM notifications
|
||||||
|
WHERE status = 'pending' AND next_attempt_at <= now()
|
||||||
|
ORDER BY next_attempt_at
|
||||||
|
LIMIT $1
|
||||||
|
FOR UPDATE SKIP LOCKED
|
||||||
|
)
|
||||||
|
RETURNING id
|
||||||
|
`
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
func (q *Queries) ClaimNotifications(ctx context.Context, max int32) ([]uuid.UUID, error) {
|
||||||
|
rows, err := q.db.Query(ctx, claimNotifications, 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 createChannel = `-- name: CreateChannel :one
|
||||||
|
INSERT INTO notification_channels (org_id, name, type, config, enabled)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
RETURNING id, org_id, name, type, config, enabled, created_at, updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type CreateChannelParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
Config []byte
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) CreateChannel(ctx context.Context, arg CreateChannelParams) (NotificationChannel, error) {
|
||||||
|
row := q.db.QueryRow(ctx, createChannel,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Type,
|
||||||
|
arg.Config,
|
||||||
|
arg.Enabled,
|
||||||
|
)
|
||||||
|
var i NotificationChannel
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Type,
|
||||||
|
&i.Config,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteChannel = `-- name: DeleteChannel :execrows
|
||||||
|
DELETE FROM notification_channels WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) DeleteChannel(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||||
|
result, err := q.db.Exec(ctx, deleteChannel, id)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const enqueueNotification = `-- name: EnqueueNotification :exec
|
||||||
|
INSERT INTO notifications (org_id, channel_id, incident_id, event)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT DO NOTHING
|
||||||
|
`
|
||||||
|
|
||||||
|
type EnqueueNotificationParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
ChannelID uuid.UUID
|
||||||
|
IncidentID uuid.UUID
|
||||||
|
Event string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) EnqueueNotification(ctx context.Context, arg EnqueueNotificationParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, enqueueNotification,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.ChannelID,
|
||||||
|
arg.IncidentID,
|
||||||
|
arg.Event,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const getChannel = `-- name: GetChannel :one
|
||||||
|
SELECT id, org_id, name, type, config, enabled, created_at, updated_at FROM notification_channels WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetChannel(ctx context.Context, id uuid.UUID) (NotificationChannel, error) {
|
||||||
|
row := q.db.QueryRow(ctx, getChannel, id)
|
||||||
|
var i NotificationChannel
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Type,
|
||||||
|
&i.Config,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const listChannels = `-- name: ListChannels :many
|
||||||
|
SELECT id, org_id, name, type, config, enabled, created_at, updated_at FROM notification_channels ORDER BY name
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListChannels(ctx context.Context) ([]NotificationChannel, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listChannels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []NotificationChannel{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i NotificationChannel
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Type,
|
||||||
|
&i.Config,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const listEnabledChannelIDs = `-- name: ListEnabledChannelIDs :many
|
||||||
|
SELECT id FROM notification_channels WHERE org_id = $1 AND enabled
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) ListEnabledChannelIDs(ctx context.Context, orgID uuid.UUID) ([]uuid.UUID, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listEnabledChannelIDs, orgID)
|
||||||
|
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 listIncidents = `-- name: ListIncidents :many
|
||||||
|
SELECT i.id, i.org_id, i.monitor_id, i.started_at, i.resolved_at, i.cause, m.name AS monitor_name
|
||||||
|
FROM incidents i
|
||||||
|
JOIN monitors m ON m.id = i.monitor_id
|
||||||
|
ORDER BY i.started_at DESC
|
||||||
|
LIMIT 200
|
||||||
|
`
|
||||||
|
|
||||||
|
type ListIncidentsRow struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
StartedAt time.Time
|
||||||
|
ResolvedAt *time.Time
|
||||||
|
Cause []byte
|
||||||
|
MonitorName string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ListIncidents(ctx context.Context) ([]ListIncidentsRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, listIncidents)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []ListIncidentsRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i ListIncidentsRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.MonitorID,
|
||||||
|
&i.StartedAt,
|
||||||
|
&i.ResolvedAt,
|
||||||
|
&i.Cause,
|
||||||
|
&i.MonitorName,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const markNotificationFailed = `-- name: MarkNotificationFailed :exec
|
||||||
|
UPDATE 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 MarkNotificationFailedParams struct {
|
||||||
|
LastError *string
|
||||||
|
MaxAttempts int32
|
||||||
|
RetryInSeconds int32
|
||||||
|
ID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) MarkNotificationFailed(ctx context.Context, arg MarkNotificationFailedParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, markNotificationFailed,
|
||||||
|
arg.LastError,
|
||||||
|
arg.MaxAttempts,
|
||||||
|
arg.RetryInSeconds,
|
||||||
|
arg.ID,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const markNotificationSent = `-- name: MarkNotificationSent :exec
|
||||||
|
UPDATE notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL
|
||||||
|
WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) MarkNotificationSent(ctx context.Context, id uuid.UUID) error {
|
||||||
|
_, err := q.db.Exec(ctx, markNotificationSent, id)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
const notificationDetails = `-- name: NotificationDetails :many
|
||||||
|
SELECT n.id, n.org_id, n.event, n.attempts,
|
||||||
|
c.type AS channel_type, c.config AS channel_config, c.id AS channel_id,
|
||||||
|
i.id AS incident_id, i.started_at, i.resolved_at, i.cause,
|
||||||
|
m.id AS monitor_id, m.name AS monitor_name, m.target AS monitor_target, m.type AS monitor_type
|
||||||
|
FROM notifications n
|
||||||
|
JOIN notification_channels c ON c.id = n.channel_id
|
||||||
|
JOIN incidents i ON i.id = n.incident_id
|
||||||
|
JOIN monitors m ON m.id = i.monitor_id
|
||||||
|
WHERE n.id = ANY ($1::uuid[])
|
||||||
|
`
|
||||||
|
|
||||||
|
type NotificationDetailsRow struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Event string
|
||||||
|
Attempts int32
|
||||||
|
ChannelType string
|
||||||
|
ChannelConfig []byte
|
||||||
|
ChannelID uuid.UUID
|
||||||
|
IncidentID uuid.UUID
|
||||||
|
StartedAt time.Time
|
||||||
|
ResolvedAt *time.Time
|
||||||
|
Cause []byte
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
MonitorName string
|
||||||
|
MonitorTarget string
|
||||||
|
MonitorType string
|
||||||
|
}
|
||||||
|
|
||||||
|
// System scope.
|
||||||
|
func (q *Queries) NotificationDetails(ctx context.Context, ids []uuid.UUID) ([]NotificationDetailsRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, notificationDetails, ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []NotificationDetailsRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i NotificationDetailsRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Event,
|
||||||
|
&i.Attempts,
|
||||||
|
&i.ChannelType,
|
||||||
|
&i.ChannelConfig,
|
||||||
|
&i.ChannelID,
|
||||||
|
&i.IncidentID,
|
||||||
|
&i.StartedAt,
|
||||||
|
&i.ResolvedAt,
|
||||||
|
&i.Cause,
|
||||||
|
&i.MonitorID,
|
||||||
|
&i.MonitorName,
|
||||||
|
&i.MonitorTarget,
|
||||||
|
&i.MonitorType,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const openIncident = `-- name: OpenIncident :one
|
||||||
|
INSERT INTO incidents (org_id, monitor_id, started_at, cause)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (monitor_id) WHERE resolved_at IS NULL DO NOTHING
|
||||||
|
RETURNING id, org_id, monitor_id, started_at, resolved_at, cause
|
||||||
|
`
|
||||||
|
|
||||||
|
type OpenIncidentParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
StartedAt time.Time
|
||||||
|
Cause []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) OpenIncident(ctx context.Context, arg OpenIncidentParams) (Incident, error) {
|
||||||
|
row := q.db.QueryRow(ctx, openIncident,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.MonitorID,
|
||||||
|
arg.StartedAt,
|
||||||
|
arg.Cause,
|
||||||
|
)
|
||||||
|
var i Incident
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.MonitorID,
|
||||||
|
&i.StartedAt,
|
||||||
|
&i.ResolvedAt,
|
||||||
|
&i.Cause,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolveIncident = `-- name: ResolveIncident :one
|
||||||
|
UPDATE incidents SET resolved_at = $1
|
||||||
|
WHERE monitor_id = $2 AND resolved_at IS NULL
|
||||||
|
RETURNING id, org_id, monitor_id, started_at, resolved_at, cause
|
||||||
|
`
|
||||||
|
|
||||||
|
type ResolveIncidentParams struct {
|
||||||
|
ResolvedAt *time.Time
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ResolveIncident(ctx context.Context, arg ResolveIncidentParams) (Incident, error) {
|
||||||
|
row := q.db.QueryRow(ctx, resolveIncident, arg.ResolvedAt, arg.MonitorID)
|
||||||
|
var i Incident
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.MonitorID,
|
||||||
|
&i.StartedAt,
|
||||||
|
&i.ResolvedAt,
|
||||||
|
&i.Cause,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateChannel = `-- name: UpdateChannel :one
|
||||||
|
UPDATE notification_channels SET name = $2, config = $3, enabled = $4, updated_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING id, org_id, name, type, config, enabled, created_at, updated_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type UpdateChannelParams struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Config []byte
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) UpdateChannel(ctx context.Context, arg UpdateChannelParams) (NotificationChannel, error) {
|
||||||
|
row := q.db.QueryRow(ctx, updateChannel,
|
||||||
|
arg.ID,
|
||||||
|
arg.Name,
|
||||||
|
arg.Config,
|
||||||
|
arg.Enabled,
|
||||||
|
)
|
||||||
|
var i NotificationChannel
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Type,
|
||||||
|
&i.Config,
|
||||||
|
&i.Enabled,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
@@ -70,6 +70,15 @@ type CheckResults1h struct {
|
|||||||
MaxLatencyMs interface{}
|
MaxLatencyMs interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Incident struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
StartedAt time.Time
|
||||||
|
ResolvedAt *time.Time
|
||||||
|
Cause []byte
|
||||||
|
}
|
||||||
|
|
||||||
type Invitation struct {
|
type Invitation struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
OrgID uuid.UUID
|
OrgID uuid.UUID
|
||||||
@@ -101,6 +110,7 @@ type Monitor struct {
|
|||||||
Enabled bool
|
Enabled bool
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
|
Confirmations int32
|
||||||
}
|
}
|
||||||
|
|
||||||
type MonitorRegion struct {
|
type MonitorRegion struct {
|
||||||
@@ -109,6 +119,31 @@ type MonitorRegion struct {
|
|||||||
Region string
|
Region string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Notification struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
ChannelID uuid.UUID
|
||||||
|
IncidentID uuid.UUID
|
||||||
|
Event string
|
||||||
|
Status string
|
||||||
|
Attempts int32
|
||||||
|
NextAttemptAt time.Time
|
||||||
|
LastError *string
|
||||||
|
CreatedAt time.Time
|
||||||
|
SentAt *time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type NotificationChannel struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Type string
|
||||||
|
Config []byte
|
||||||
|
Enabled bool
|
||||||
|
CreatedAt time.Time
|
||||||
|
UpdatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
type Org struct {
|
type Org struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Slug string
|
Slug string
|
||||||
|
|||||||
@@ -56,9 +56,9 @@ func (q *Queries) AllMonitorRegions(ctx context.Context) ([]AllMonitorRegionsRow
|
|||||||
}
|
}
|
||||||
|
|
||||||
const createMonitor = `-- name: CreateMonitor :one
|
const createMonitor = `-- name: CreateMonitor :one
|
||||||
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled)
|
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at
|
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateMonitorParams struct {
|
type CreateMonitorParams struct {
|
||||||
@@ -70,6 +70,7 @@ type CreateMonitorParams struct {
|
|||||||
TimeoutMs int32
|
TimeoutMs int32
|
||||||
Config []byte
|
Config []byte
|
||||||
Enabled bool
|
Enabled bool
|
||||||
|
Confirmations int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error) {
|
func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error) {
|
||||||
@@ -82,6 +83,7 @@ func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (M
|
|||||||
arg.TimeoutMs,
|
arg.TimeoutMs,
|
||||||
arg.Config,
|
arg.Config,
|
||||||
arg.Enabled,
|
arg.Enabled,
|
||||||
|
arg.Confirmations,
|
||||||
)
|
)
|
||||||
var i Monitor
|
var i Monitor
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
@@ -96,6 +98,7 @@ func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (M
|
|||||||
&i.Enabled,
|
&i.Enabled,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.Confirmations,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -122,7 +125,7 @@ func (q *Queries) DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID)
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getMonitor = `-- name: GetMonitor :one
|
const getMonitor = `-- name: GetMonitor :one
|
||||||
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at FROM monitors WHERE id = $1
|
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations FROM monitors WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error) {
|
func (q *Queries) GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error) {
|
||||||
@@ -140,6 +143,7 @@ func (q *Queries) GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error)
|
|||||||
&i.Enabled,
|
&i.Enabled,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.Confirmations,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -181,7 +185,7 @@ func (q *Queries) ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEna
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listMonitors = `-- name: ListMonitors :many
|
const listMonitors = `-- name: ListMonitors :many
|
||||||
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at FROM monitors ORDER BY name
|
SELECT id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations FROM monitors ORDER BY name
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) ListMonitors(ctx context.Context) ([]Monitor, error) {
|
func (q *Queries) ListMonitors(ctx context.Context) ([]Monitor, error) {
|
||||||
@@ -205,6 +209,7 @@ func (q *Queries) ListMonitors(ctx context.Context) ([]Monitor, error) {
|
|||||||
&i.Enabled,
|
&i.Enabled,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.Confirmations,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -241,7 +246,7 @@ func (q *Queries) MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]st
|
|||||||
}
|
}
|
||||||
|
|
||||||
const monitorsForJobs = `-- name: MonitorsForJobs :many
|
const monitorsForJobs = `-- name: MonitorsForJobs :many
|
||||||
SELECT m.id, m.org_id, m.name, m.type, m.target, m.interval_seconds, m.timeout_ms, m.config, m.enabled, m.created_at, m.updated_at FROM monitors m
|
SELECT m.id, m.org_id, m.name, m.type, m.target, m.interval_seconds, m.timeout_ms, m.config, m.enabled, m.created_at, m.updated_at, m.confirmations FROM monitors m
|
||||||
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region = $1
|
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region = $1
|
||||||
WHERE m.id = ANY ($2::uuid[]) AND m.enabled
|
WHERE m.id = ANY ($2::uuid[]) AND m.enabled
|
||||||
`
|
`
|
||||||
@@ -274,6 +279,60 @@ func (q *Queries) MonitorsForJobs(ctx context.Context, arg MonitorsForJobsParams
|
|||||||
&i.Enabled,
|
&i.Enabled,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.Confirmations,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const monitorsForResults = `-- name: MonitorsForResults :many
|
||||||
|
SELECT m.id, m.org_id, m.name, m.target, m.interval_seconds, m.confirmations,
|
||||||
|
(SELECT array_agg(r2.region ORDER BY r2.region) 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 = $1
|
||||||
|
WHERE m.id = ANY ($2::uuid[]) AND m.enabled
|
||||||
|
`
|
||||||
|
|
||||||
|
type MonitorsForResultsParams struct {
|
||||||
|
Region string
|
||||||
|
Ids []uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
type MonitorsForResultsRow struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Name string
|
||||||
|
Target string
|
||||||
|
IntervalSeconds int32
|
||||||
|
Confirmations int32
|
||||||
|
Regions []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// System scope: monitors a checker of region may report, with every region
|
||||||
|
// they are assigned to, for evaluation.
|
||||||
|
func (q *Queries) MonitorsForResults(ctx context.Context, arg MonitorsForResultsParams) ([]MonitorsForResultsRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, monitorsForResults, arg.Region, arg.Ids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []MonitorsForResultsRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i MonitorsForResultsRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.Name,
|
||||||
|
&i.Target,
|
||||||
|
&i.IntervalSeconds,
|
||||||
|
&i.Confirmations,
|
||||||
|
&i.Regions,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -287,9 +346,9 @@ func (q *Queries) MonitorsForJobs(ctx context.Context, arg MonitorsForJobsParams
|
|||||||
|
|
||||||
const updateMonitor = `-- name: UpdateMonitor :one
|
const updateMonitor = `-- name: UpdateMonitor :one
|
||||||
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
|
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
|
||||||
timeout_ms = $6, config = $7, enabled = $8, updated_at = now()
|
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, updated_at = now()
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at
|
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at, confirmations
|
||||||
`
|
`
|
||||||
|
|
||||||
type UpdateMonitorParams struct {
|
type UpdateMonitorParams struct {
|
||||||
@@ -301,6 +360,7 @@ type UpdateMonitorParams struct {
|
|||||||
TimeoutMs int32
|
TimeoutMs int32
|
||||||
Config []byte
|
Config []byte
|
||||||
Enabled bool
|
Enabled bool
|
||||||
|
Confirmations int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error) {
|
func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error) {
|
||||||
@@ -313,6 +373,7 @@ func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (M
|
|||||||
arg.TimeoutMs,
|
arg.TimeoutMs,
|
||||||
arg.Config,
|
arg.Config,
|
||||||
arg.Enabled,
|
arg.Enabled,
|
||||||
|
arg.Confirmations,
|
||||||
)
|
)
|
||||||
var i Monitor
|
var i Monitor
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
@@ -327,6 +388,7 @@ func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (M
|
|||||||
&i.Enabled,
|
&i.Enabled,
|
||||||
&i.CreatedAt,
|
&i.CreatedAt,
|
||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
|
&i.Confirmations,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,9 @@ type Querier interface {
|
|||||||
AddMonitorRegion(ctx context.Context, arg AddMonitorRegionParams) error
|
AddMonitorRegion(ctx context.Context, arg AddMonitorRegionParams) error
|
||||||
AllMonitorRegions(ctx context.Context) ([]AllMonitorRegionsRow, error)
|
AllMonitorRegions(ctx context.Context) ([]AllMonitorRegionsRow, error)
|
||||||
AnyUserExists(ctx context.Context) (bool, error)
|
AnyUserExists(ctx context.Context) (bool, error)
|
||||||
|
// 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)
|
||||||
// Shortens tokens that would outlive a newly set maximum lifetime.
|
// Shortens tokens that would outlive a newly set maximum lifetime.
|
||||||
ClampAPITokenExpiry(ctx context.Context, arg ClampAPITokenExpiryParams) (int64, error)
|
ClampAPITokenExpiry(ctx context.Context, arg ClampAPITokenExpiryParams) (int64, error)
|
||||||
CountRoleAssignments(ctx context.Context, roleID uuid.UUID) (int64, error)
|
CountRoleAssignments(ctx context.Context, roleID uuid.UUID) (int64, error)
|
||||||
@@ -23,6 +26,7 @@ type Querier interface {
|
|||||||
CreateAPIToken(ctx context.Context, arg CreateAPITokenParams) (ApiToken, error)
|
CreateAPIToken(ctx context.Context, arg CreateAPITokenParams) (ApiToken, error)
|
||||||
CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent, error)
|
CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent, error)
|
||||||
CreateAssignment(ctx context.Context, arg CreateAssignmentParams) (RoleAssignment, error)
|
CreateAssignment(ctx context.Context, arg CreateAssignmentParams) (RoleAssignment, error)
|
||||||
|
CreateChannel(ctx context.Context, arg CreateChannelParams) (NotificationChannel, error)
|
||||||
CreateIdentity(ctx context.Context, arg CreateIdentityParams) error
|
CreateIdentity(ctx context.Context, arg CreateIdentityParams) error
|
||||||
CreateInvitation(ctx context.Context, arg CreateInvitationParams) (Invitation, error)
|
CreateInvitation(ctx context.Context, arg CreateInvitationParams) (Invitation, error)
|
||||||
CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error)
|
CreateMonitor(ctx context.Context, arg CreateMonitorParams) (Monitor, error)
|
||||||
@@ -33,6 +37,7 @@ type Querier interface {
|
|||||||
DeleteAPIToken(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteAPIToken(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteAgent(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteAgent(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteAssignment(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteAssignment(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
DeleteChannel(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
|
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
|
||||||
@@ -43,10 +48,12 @@ type Querier interface {
|
|||||||
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
||||||
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
||||||
|
EnqueueNotification(ctx context.Context, arg EnqueueNotificationParams) error
|
||||||
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
||||||
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
||||||
GetAgentByHash(ctx context.Context, tokenHash []byte) (Agent, error)
|
GetAgentByHash(ctx context.Context, tokenHash []byte) (Agent, error)
|
||||||
GetAssignment(ctx context.Context, id uuid.UUID) (RoleAssignment, error)
|
GetAssignment(ctx context.Context, id uuid.UUID) (RoleAssignment, error)
|
||||||
|
GetChannel(ctx context.Context, id uuid.UUID) (NotificationChannel, error)
|
||||||
GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error)
|
GetIdentity(ctx context.Context, arg GetIdentityParams) (UserIdentity, error)
|
||||||
GetInvitation(ctx context.Context, id uuid.UUID) (Invitation, error)
|
GetInvitation(ctx context.Context, id uuid.UUID) (Invitation, error)
|
||||||
GetInvitationByHash(ctx context.Context, tokenHash []byte) (GetInvitationByHashRow, error)
|
GetInvitationByHash(ctx context.Context, tokenHash []byte) (GetInvitationByHashRow, error)
|
||||||
@@ -66,13 +73,17 @@ type Querier interface {
|
|||||||
IsMember(ctx context.Context, arg IsMemberParams) (bool, error)
|
IsMember(ctx context.Context, arg IsMemberParams) (bool, error)
|
||||||
// Newest result per monitor and region in the org.
|
// Newest result per monitor and region in the org.
|
||||||
LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error)
|
LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error)
|
||||||
|
LatestResultsForMonitor(ctx context.Context, arg LatestResultsForMonitorParams) ([]LatestResultsForMonitorRow, error)
|
||||||
ListAPITokens(ctx context.Context, arg ListAPITokensParams) ([]ListAPITokensRow, error)
|
ListAPITokens(ctx context.Context, arg ListAPITokensParams) ([]ListAPITokensRow, error)
|
||||||
ListAgents(ctx context.Context) ([]Agent, error)
|
ListAgents(ctx context.Context) ([]Agent, error)
|
||||||
ListAssignments(ctx context.Context, orgID uuid.UUID) ([]ListAssignmentsRow, error)
|
ListAssignments(ctx context.Context, orgID uuid.UUID) ([]ListAssignmentsRow, error)
|
||||||
|
ListChannels(ctx context.Context) ([]NotificationChannel, error)
|
||||||
|
ListEnabledChannelIDs(ctx context.Context, orgID uuid.UUID) ([]uuid.UUID, error)
|
||||||
// System scope: every enabled monitor with its regions, for reconciling the
|
// System scope: every enabled monitor with its regions, for reconciling the
|
||||||
// schedule.
|
// schedule.
|
||||||
ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEnabledMonitorsForScheduleRow, error)
|
ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEnabledMonitorsForScheduleRow, error)
|
||||||
ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error)
|
ListEnabledSSOProviders(ctx context.Context) ([]ListEnabledSSOProvidersRow, error)
|
||||||
|
ListIncidents(ctx context.Context) ([]ListIncidentsRow, error)
|
||||||
ListMonitors(ctx context.Context) ([]Monitor, error)
|
ListMonitors(ctx context.Context) ([]Monitor, error)
|
||||||
ListOrgMembers(ctx context.Context, orgID uuid.UUID) ([]ListOrgMembersRow, error)
|
ListOrgMembers(ctx context.Context, orgID uuid.UUID) ([]ListOrgMembersRow, error)
|
||||||
ListOrgs(ctx context.Context) ([]Org, error)
|
ListOrgs(ctx context.Context) ([]Org, error)
|
||||||
@@ -83,20 +94,30 @@ type Querier interface {
|
|||||||
ListUserIdentitiesInOrg(ctx context.Context, userID uuid.UUID) ([]ListUserIdentitiesInOrgRow, error)
|
ListUserIdentitiesInOrg(ctx context.Context, userID uuid.UUID) ([]ListUserIdentitiesInOrgRow, error)
|
||||||
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
||||||
MarkInvitationAccepted(ctx context.Context, id uuid.UUID) (int64, error)
|
MarkInvitationAccepted(ctx context.Context, id uuid.UUID) (int64, error)
|
||||||
|
MarkNotificationFailed(ctx context.Context, arg MarkNotificationFailedParams) error
|
||||||
|
MarkNotificationSent(ctx context.Context, id uuid.UUID) error
|
||||||
// Every role a member holds, with the scope it applies to.
|
// Every role a member holds, with the scope it applies to.
|
||||||
MemberGrants(ctx context.Context, arg MemberGrantsParams) ([]MemberGrantsRow, error)
|
MemberGrants(ctx context.Context, arg MemberGrantsParams) ([]MemberGrantsRow, error)
|
||||||
MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]string, error)
|
MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]string, error)
|
||||||
// System scope: monitors to hand to a checker of region, skipping ones that
|
// System scope: monitors to hand to a checker of region, skipping ones that
|
||||||
// were disabled, deleted or moved out of the region since dispatch.
|
// were disabled, deleted or moved out of the region since dispatch.
|
||||||
MonitorsForJobs(ctx context.Context, arg MonitorsForJobsParams) ([]Monitor, error)
|
MonitorsForJobs(ctx context.Context, arg MonitorsForJobsParams) ([]Monitor, error)
|
||||||
|
// System scope: monitors a checker of region may report, with every region
|
||||||
|
// they are assigned to, for evaluation.
|
||||||
|
MonitorsForResults(ctx context.Context, arg MonitorsForResultsParams) ([]MonitorsForResultsRow, error)
|
||||||
|
// System scope.
|
||||||
|
NotificationDetails(ctx context.Context, ids []uuid.UUID) ([]NotificationDetailsRow, error)
|
||||||
|
OpenIncident(ctx context.Context, arg OpenIncidentParams) (Incident, error)
|
||||||
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
||||||
RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error)
|
RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error)
|
||||||
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
||||||
|
ResolveIncident(ctx context.Context, arg ResolveIncidentParams) (Incident, error)
|
||||||
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
||||||
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
||||||
TouchAPIToken(ctx context.Context, id uuid.UUID) error
|
TouchAPIToken(ctx context.Context, id uuid.UUID) error
|
||||||
TouchAgent(ctx context.Context, arg TouchAgentParams) error
|
TouchAgent(ctx context.Context, arg TouchAgentParams) error
|
||||||
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
|
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
|
||||||
|
UpdateChannel(ctx context.Context, arg UpdateChannelParams) (NotificationChannel, error)
|
||||||
UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error)
|
UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (Monitor, error)
|
||||||
UpdateOrgPolicy(ctx context.Context, arg UpdateOrgPolicyParams) (Org, error)
|
UpdateOrgPolicy(ctx context.Context, arg UpdateOrgPolicyParams) (Org, error)
|
||||||
UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error)
|
UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error)
|
||||||
|
|||||||
@@ -26,3 +26,9 @@ SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_coun
|
|||||||
FROM check_results_1h
|
FROM check_results_1h
|
||||||
WHERE org_id = $1 AND bucket >= $2
|
WHERE org_id = $1 AND bucket >= $2
|
||||||
GROUP BY monitor_id;
|
GROUP BY monitor_id;
|
||||||
|
|
||||||
|
-- name: LatestResultsForMonitor :many
|
||||||
|
SELECT DISTINCT ON (region) region, ok, error
|
||||||
|
FROM check_results
|
||||||
|
WHERE org_id = $1 AND monitor_id = $2 AND time > now() - interval '1 day'
|
||||||
|
ORDER BY region, time DESC;
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
-- name: OpenIncident :one
|
||||||
|
INSERT INTO incidents (org_id, monitor_id, started_at, cause)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT (monitor_id) WHERE resolved_at IS NULL DO NOTHING
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ResolveIncident :one
|
||||||
|
UPDATE incidents SET resolved_at = sqlc.arg(resolved_at)
|
||||||
|
WHERE monitor_id = sqlc.arg(monitor_id) AND resolved_at IS NULL
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: ListIncidents :many
|
||||||
|
SELECT i.*, m.name AS monitor_name
|
||||||
|
FROM incidents i
|
||||||
|
JOIN monitors m ON m.id = i.monitor_id
|
||||||
|
ORDER BY i.started_at DESC
|
||||||
|
LIMIT 200;
|
||||||
|
|
||||||
|
-- name: ListEnabledChannelIDs :many
|
||||||
|
SELECT id FROM notification_channels WHERE org_id = $1 AND enabled;
|
||||||
|
|
||||||
|
-- name: EnqueueNotification :exec
|
||||||
|
INSERT INTO notifications (org_id, channel_id, incident_id, event)
|
||||||
|
VALUES ($1, $2, $3, $4)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
|
-- name: ClaimNotifications :many
|
||||||
|
-- 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.
|
||||||
|
UPDATE notifications SET next_attempt_at = now() + interval '5 minutes'
|
||||||
|
WHERE id IN (
|
||||||
|
SELECT id FROM 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: NotificationDetails :many
|
||||||
|
-- System scope.
|
||||||
|
SELECT n.id, n.org_id, n.event, n.attempts,
|
||||||
|
c.type AS channel_type, c.config AS channel_config, c.id AS channel_id,
|
||||||
|
i.id AS incident_id, i.started_at, i.resolved_at, i.cause,
|
||||||
|
m.id AS monitor_id, m.name AS monitor_name, m.target AS monitor_target, m.type AS monitor_type
|
||||||
|
FROM notifications n
|
||||||
|
JOIN notification_channels c ON c.id = n.channel_id
|
||||||
|
JOIN incidents i ON i.id = n.incident_id
|
||||||
|
JOIN monitors m ON m.id = i.monitor_id
|
||||||
|
WHERE n.id = ANY (sqlc.arg(ids)::uuid[]);
|
||||||
|
|
||||||
|
-- name: MarkNotificationSent :exec
|
||||||
|
UPDATE notifications SET status = 'sent', sent_at = now(), attempts = attempts + 1, last_error = NULL
|
||||||
|
WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: MarkNotificationFailed :exec
|
||||||
|
UPDATE 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: CreateChannel :one
|
||||||
|
INSERT INTO notification_channels (org_id, name, type, config, enabled)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: UpdateChannel :one
|
||||||
|
UPDATE notification_channels SET name = $2, config = $3, enabled = $4, updated_at = now()
|
||||||
|
WHERE id = $1
|
||||||
|
RETURNING *;
|
||||||
|
|
||||||
|
-- name: GetChannel :one
|
||||||
|
SELECT * FROM notification_channels WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: ListChannels :many
|
||||||
|
SELECT * FROM notification_channels ORDER BY name;
|
||||||
|
|
||||||
|
-- name: DeleteChannel :execrows
|
||||||
|
DELETE FROM notification_channels WHERE id = $1;
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
-- name: CreateMonitor :one
|
-- name: CreateMonitor :one
|
||||||
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled)
|
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
-- name: UpdateMonitor :one
|
-- name: UpdateMonitor :one
|
||||||
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
|
UPDATE monitors SET name = $2, type = $3, target = $4, interval_seconds = $5,
|
||||||
timeout_ms = $6, config = $7, enabled = $8, updated_at = now()
|
timeout_ms = $6, config = $7, enabled = $8, confirmations = $9, updated_at = now()
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
@@ -45,3 +45,12 @@ GROUP BY m.id, m.interval_seconds;
|
|||||||
SELECT m.* FROM monitors m
|
SELECT m.* FROM monitors m
|
||||||
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region = sqlc.arg(region)
|
JOIN monitor_regions r ON r.monitor_id = m.id AND r.region = sqlc.arg(region)
|
||||||
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled;
|
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled;
|
||||||
|
|
||||||
|
-- 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 ORDER BY r2.region) 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 = sqlc.arg(region)
|
||||||
|
WHERE m.id = ANY (sqlc.arg(ids)::uuid[]) AND m.enabled;
|
||||||
Reference in new issue
Block a user