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
@@ -56,9 +56,9 @@ func (q *Queries) AllMonitorRegions(ctx context.Context) ([]AllMonitorRegionsRow
|
||||
}
|
||||
|
||||
const createMonitor = `-- name: CreateMonitor :one
|
||||
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id, org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, created_at, updated_at
|
||||
INSERT INTO monitors (org_id, name, type, target, interval_seconds, timeout_ms, config, enabled, confirmations)
|
||||
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, confirmations
|
||||
`
|
||||
|
||||
type CreateMonitorParams struct {
|
||||
@@ -70,6 +70,7 @@ type CreateMonitorParams struct {
|
||||
TimeoutMs int32
|
||||
Config []byte
|
||||
Enabled bool
|
||||
Confirmations int32
|
||||
}
|
||||
|
||||
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.Config,
|
||||
arg.Enabled,
|
||||
arg.Confirmations,
|
||||
)
|
||||
var i Monitor
|
||||
err := row.Scan(
|
||||
@@ -96,6 +98,7 @@ func (q *Queries) CreateMonitor(ctx context.Context, arg CreateMonitorParams) (M
|
||||
&i.Enabled,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Confirmations,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -122,7 +125,7 @@ func (q *Queries) DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID)
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -140,6 +143,7 @@ func (q *Queries) GetMonitor(ctx context.Context, id uuid.UUID) (Monitor, error)
|
||||
&i.Enabled,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Confirmations,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -181,7 +185,7 @@ func (q *Queries) ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEna
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -205,6 +209,7 @@ func (q *Queries) ListMonitors(ctx context.Context) ([]Monitor, error) {
|
||||
&i.Enabled,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Confirmations,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -241,7 +246,7 @@ func (q *Queries) MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]st
|
||||
}
|
||||
|
||||
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
|
||||
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.CreatedAt,
|
||||
&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 {
|
||||
return nil, err
|
||||
}
|
||||
@@ -287,9 +346,9 @@ func (q *Queries) MonitorsForJobs(ctx context.Context, arg MonitorsForJobsParams
|
||||
|
||||
const updateMonitor = `-- name: UpdateMonitor :one
|
||||
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
|
||||
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 {
|
||||
@@ -301,6 +360,7 @@ type UpdateMonitorParams struct {
|
||||
TimeoutMs int32
|
||||
Config []byte
|
||||
Enabled bool
|
||||
Confirmations int32
|
||||
}
|
||||
|
||||
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.Config,
|
||||
arg.Enabled,
|
||||
arg.Confirmations,
|
||||
)
|
||||
var i Monitor
|
||||
err := row.Scan(
|
||||
@@ -327,6 +388,7 @@ func (q *Queries) UpdateMonitor(ctx context.Context, arg UpdateMonitorParams) (M
|
||||
&i.Enabled,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.Confirmations,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
Reference in new issue
Block a user