Files
uptimesebi/internal/store/incidents.sql.go
T
Sebastian UnterschützandClaude Opus 5.5 d7c5cd711e
ci / image (pull_request) Successful in 3m18s
ci / build (pull_request) Successful in 8m38s
ci / test (pull_request) Successful in 12m6s
ci / lint (pull_request) Successful in 15m0s
feat: page long and truncated lists
Lists that were cut off by a fixed LIMIT, or could grow without bound,
are paged by keyset: the cursor holds the sort key and an ID, so pages
neither skip nor repeat items as others are added. The audit logs had a
timestamp-only cursor that could skip entries with equal times.

The API returns at most 100 items per list (limit takes up to 500) with
next_cursor and prev_cursor, passed back as after and before. The
console shows Newer/Older (or Previous/Next) links and To the start,
without JavaScript.

Paged: incidents (optionally of one monitor), a monitor's results and
evidence, a checker's results, the audit logs, members, maintenance
windows, firing Alertmanager alerts, the monitors API and the admin
lists of status pages and organizations, which move to their own admin
pages. A monitor's page shows its latest incidents and results and links
to all of them. The dashboard caps every group at 50 of its own monitors
with a link to a paged view of the group, and gains a search by name or
target. Lists filtered by permission after reading may show fewer items
on a page.

Closes #116

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 21:40:25 +01:00

537 lines
12 KiB
Go

// 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 incidentsForMonitors = `-- name: IncidentsForMonitors :many
SELECT id, org_id, monitor_id, started_at, resolved_at, cause FROM incidents
WHERE monitor_id = ANY ($1::uuid[]) AND (resolved_at IS NULL OR resolved_at > $2)
ORDER BY started_at DESC
LIMIT 50
`
type IncidentsForMonitorsParams struct {
Ids []uuid.UUID
Since *time.Time
}
func (q *Queries) IncidentsForMonitors(ctx context.Context, arg IncidentsForMonitorsParams) ([]Incident, error) {
rows, err := q.db.Query(ctx, incidentsForMonitors, arg.Ids, arg.Since)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Incident{}
for rows.Next() {
var i Incident
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.MonitorID,
&i.StartedAt,
&i.ResolvedAt,
&i.Cause,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const incidentsSince = `-- name: IncidentsSince :many
SELECT monitor_id, started_at, resolved_at FROM incidents
WHERE resolved_at IS NULL OR resolved_at > $1
`
type IncidentsSinceRow struct {
MonitorID uuid.UUID
StartedAt time.Time
ResolvedAt *time.Time
}
// Incidents overlapping the time since $1, for SLA calculation.
func (q *Queries) IncidentsSince(ctx context.Context, resolvedAt *time.Time) ([]IncidentsSinceRow, error) {
rows, err := q.db.Query(ctx, incidentsSince, resolvedAt)
if err != nil {
return nil, err
}
defer rows.Close()
items := []IncidentsSinceRow{}
for rows.Next() {
var i IncidentsSinceRow
if err := rows.Scan(&i.MonitorID, &i.StartedAt, &i.ResolvedAt); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
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 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
WHERE ($1::uuid IS NULL OR i.monitor_id = $1)
AND ($2::timestamptz IS NULL OR (i.started_at, i.id) < ($2, $3::uuid))
AND ($4::timestamptz IS NULL OR (i.started_at, i.id) > ($4, $5::uuid))
ORDER BY
CASE WHEN $4::timestamptz IS NULL THEN i.started_at END DESC,
CASE WHEN $4::timestamptz IS NULL THEN i.id END DESC,
i.started_at, i.id
LIMIT $6
`
type ListIncidentsParams struct {
MonitorID *uuid.UUID
AfterT *time.Time
AfterID *uuid.UUID
BeforeT *time.Time
BeforeID *uuid.UUID
MaxRows int32
}
type ListIncidentsRow struct {
ID uuid.UUID
OrgID uuid.UUID
MonitorID uuid.UUID
StartedAt time.Time
ResolvedAt *time.Time
Cause []byte
MonitorName string
}
// Newest first, a page from a keyset cursor (see paging.go); optionally of
// one monitor.
func (q *Queries) ListIncidents(ctx context.Context, arg ListIncidentsParams) ([]ListIncidentsRow, error) {
rows, err := q.db.Query(ctx, listIncidents,
arg.MonitorID,
arg.AfterT,
arg.AfterID,
arg.BeforeT,
arg.BeforeID,
arg.MaxRows,
)
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 openIncidents = `-- name: OpenIncidents :many
SELECT id, org_id, monitor_id, started_at, resolved_at, cause FROM incidents WHERE resolved_at IS NULL
`
func (q *Queries) OpenIncidents(ctx context.Context) ([]Incident, error) {
rows, err := q.db.Query(ctx, openIncidents)
if err != nil {
return nil, err
}
defer rows.Close()
items := []Incident{}
for rows.Next() {
var i Incident
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.MonitorID,
&i.StartedAt,
&i.ResolvedAt,
&i.Cause,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
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
}