// 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 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 monitorIncidents = `-- name: MonitorIncidents :many SELECT id, org_id, monitor_id, started_at, resolved_at, cause FROM incidents WHERE monitor_id = $1 ORDER BY started_at DESC LIMIT 20 ` func (q *Queries) MonitorIncidents(ctx context.Context, monitorID uuid.UUID) ([]Incident, error) { rows, err := q.db.Query(ctx, monitorIncidents, monitorID) 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 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 }