ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m38s
ci / test (pull_request) Successful in 14m21s
ci / build (pull_request) Successful in 16m36s
ci / image (pull_request) Successful in 3m37s
Organizations add statuses of their own, with a name and a color, next to the built-in ones (Settings, incident statuses; API under /org/incident-statuses, managed with org.manage). Updates pick them by ID; the console and status pages show them in their color. Renaming or recoloring carries into the history; a status in use cannot be deleted (409 status_in_use). API responses give status "custom" with status_id, status_name and status_color. Status pages now list recent incidents before planned maintenance. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
858 lines
20 KiB
Go
858 lines
20 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 addIncidentUpdate = `-- name: AddIncidentUpdate :one
|
|
INSERT INTO incident_updates (org_id, incident_id, status, status_id, message, public, author_id)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
RETURNING id, org_id, incident_id, status, message, public, author_id, created_at, status_id
|
|
`
|
|
|
|
type AddIncidentUpdateParams struct {
|
|
OrgID uuid.UUID
|
|
IncidentID uuid.UUID
|
|
Status string
|
|
StatusID *uuid.UUID
|
|
Message string
|
|
Public bool
|
|
AuthorID *uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) AddIncidentUpdate(ctx context.Context, arg AddIncidentUpdateParams) (IncidentUpdate, error) {
|
|
row := q.db.QueryRow(ctx, addIncidentUpdate,
|
|
arg.OrgID,
|
|
arg.IncidentID,
|
|
arg.Status,
|
|
arg.StatusID,
|
|
arg.Message,
|
|
arg.Public,
|
|
arg.AuthorID,
|
|
)
|
|
var i IncidentUpdate
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.IncidentID,
|
|
&i.Status,
|
|
&i.Message,
|
|
&i.Public,
|
|
&i.AuthorID,
|
|
&i.CreatedAt,
|
|
&i.StatusID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
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 createIncidentStatus = `-- name: CreateIncidentStatus :one
|
|
INSERT INTO incident_statuses (org_id, name, color) VALUES ($1, $2, $3) RETURNING id, org_id, name, color, created_at
|
|
`
|
|
|
|
type CreateIncidentStatusParams struct {
|
|
OrgID uuid.UUID
|
|
Name string
|
|
Color string
|
|
}
|
|
|
|
func (q *Queries) CreateIncidentStatus(ctx context.Context, arg CreateIncidentStatusParams) (IncidentStatus, error) {
|
|
row := q.db.QueryRow(ctx, createIncidentStatus, arg.OrgID, arg.Name, arg.Color)
|
|
var i IncidentStatus
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Color,
|
|
&i.CreatedAt,
|
|
)
|
|
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 deleteIncidentStatus = `-- name: DeleteIncidentStatus :execrows
|
|
DELETE FROM incident_statuses WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteIncidentStatus(ctx context.Context, id uuid.UUID) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteIncidentStatus, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteIncidentUpdate = `-- name: DeleteIncidentUpdate :execrows
|
|
DELETE FROM incident_updates WHERE id = $1 AND incident_id = $2
|
|
`
|
|
|
|
type DeleteIncidentUpdateParams struct {
|
|
ID uuid.UUID
|
|
IncidentID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) DeleteIncidentUpdate(ctx context.Context, arg DeleteIncidentUpdateParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteIncidentUpdate, arg.ID, arg.IncidentID)
|
|
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 getIncident = `-- name: GetIncident :one
|
|
SELECT i.id, i.org_id, i.monitor_id, i.started_at, i.resolved_at, i.cause, i.title, i.status, i.status_id, m.name AS monitor_name, m.group_id AS monitor_group_id
|
|
FROM incidents i JOIN monitors m ON m.id = i.monitor_id
|
|
WHERE i.id = $1
|
|
`
|
|
|
|
type GetIncidentRow struct {
|
|
ID uuid.UUID
|
|
OrgID uuid.UUID
|
|
MonitorID uuid.UUID
|
|
StartedAt time.Time
|
|
ResolvedAt *time.Time
|
|
Cause []byte
|
|
Title string
|
|
Status *string
|
|
StatusID *uuid.UUID
|
|
MonitorName string
|
|
MonitorGroupID *uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) GetIncident(ctx context.Context, id uuid.UUID) (GetIncidentRow, error) {
|
|
row := q.db.QueryRow(ctx, getIncident, id)
|
|
var i GetIncidentRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.MonitorID,
|
|
&i.StartedAt,
|
|
&i.ResolvedAt,
|
|
&i.Cause,
|
|
&i.Title,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
&i.MonitorName,
|
|
&i.MonitorGroupID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const incidentsForMonitors = `-- name: IncidentsForMonitors :many
|
|
SELECT id, org_id, monitor_id, started_at, resolved_at, cause, title, status, status_id 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,
|
|
&i.Title,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
); 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 listIncidentStatuses = `-- name: ListIncidentStatuses :many
|
|
SELECT id, org_id, name, color, created_at FROM incident_statuses ORDER BY name
|
|
`
|
|
|
|
func (q *Queries) ListIncidentStatuses(ctx context.Context) ([]IncidentStatus, error) {
|
|
rows, err := q.db.Query(ctx, listIncidentStatuses)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []IncidentStatus{}
|
|
for rows.Next() {
|
|
var i IncidentStatus
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.Color,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listIncidentUpdates = `-- name: ListIncidentUpdates :many
|
|
SELECT u.id, u.org_id, u.incident_id, u.status, u.message, u.public, u.author_id, u.created_at, u.status_id, coalesce(nullif(usr.display_name, ''), usr.email::text, '')::text AS author_name
|
|
FROM incident_updates u LEFT JOIN users usr ON usr.id = u.author_id
|
|
WHERE u.incident_id = $1
|
|
ORDER BY u.created_at DESC, u.id DESC
|
|
`
|
|
|
|
type ListIncidentUpdatesRow struct {
|
|
ID uuid.UUID
|
|
OrgID uuid.UUID
|
|
IncidentID uuid.UUID
|
|
Status string
|
|
Message string
|
|
Public bool
|
|
AuthorID *uuid.UUID
|
|
CreatedAt time.Time
|
|
StatusID *uuid.UUID
|
|
AuthorName string
|
|
}
|
|
|
|
// Newest first, with the author's name.
|
|
func (q *Queries) ListIncidentUpdates(ctx context.Context, incidentID uuid.UUID) ([]ListIncidentUpdatesRow, error) {
|
|
rows, err := q.db.Query(ctx, listIncidentUpdates, incidentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListIncidentUpdatesRow{}
|
|
for rows.Next() {
|
|
var i ListIncidentUpdatesRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.IncidentID,
|
|
&i.Status,
|
|
&i.Message,
|
|
&i.Public,
|
|
&i.AuthorID,
|
|
&i.CreatedAt,
|
|
&i.StatusID,
|
|
&i.AuthorName,
|
|
); 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, i.title, i.status, i.status_id, 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
|
|
Title string
|
|
Status *string
|
|
StatusID *uuid.UUID
|
|
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.Title,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
&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, title, status, status_id
|
|
`
|
|
|
|
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,
|
|
&i.Title,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const openIncidents = `-- name: OpenIncidents :many
|
|
SELECT id, org_id, monitor_id, started_at, resolved_at, cause, title, status, status_id 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,
|
|
&i.Title,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const publicIncidentUpdates = `-- name: PublicIncidentUpdates :many
|
|
SELECT incident_id, status, status_id, message, created_at FROM incident_updates
|
|
WHERE incident_id = ANY($1::uuid[]) AND public
|
|
ORDER BY created_at DESC, id DESC
|
|
`
|
|
|
|
type PublicIncidentUpdatesRow struct {
|
|
IncidentID uuid.UUID
|
|
Status string
|
|
StatusID *uuid.UUID
|
|
Message string
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// The public updates of incidents shown on a status page, newest first.
|
|
func (q *Queries) PublicIncidentUpdates(ctx context.Context, ids []uuid.UUID) ([]PublicIncidentUpdatesRow, error) {
|
|
rows, err := q.db.Query(ctx, publicIncidentUpdates, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []PublicIncidentUpdatesRow{}
|
|
for rows.Next() {
|
|
var i PublicIncidentUpdatesRow
|
|
if err := rows.Scan(
|
|
&i.IncidentID,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
&i.Message,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const refreshIncidentStatus = `-- name: RefreshIncidentStatus :exec
|
|
UPDATE incidents SET (status, status_id) = (
|
|
SELECT u.status, u.status_id FROM incident_updates u WHERE u.incident_id = incidents.id
|
|
ORDER BY u.created_at DESC, u.id DESC LIMIT 1
|
|
) WHERE incidents.id = $1
|
|
`
|
|
|
|
// The incident shows the status of its latest update.
|
|
func (q *Queries) RefreshIncidentStatus(ctx context.Context, id uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, refreshIncidentStatus, id)
|
|
return 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, title, status, status_id
|
|
`
|
|
|
|
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,
|
|
&i.Title,
|
|
&i.Status,
|
|
&i.StatusID,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const setIncidentTitle = `-- name: SetIncidentTitle :execrows
|
|
UPDATE incidents SET title = $1 WHERE id = $2
|
|
`
|
|
|
|
type SetIncidentTitleParams struct {
|
|
Title string
|
|
ID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) SetIncidentTitle(ctx context.Context, arg SetIncidentTitleParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, setIncidentTitle, arg.Title, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
const updateIncidentStatus = `-- name: UpdateIncidentStatus :execrows
|
|
UPDATE incident_statuses SET name = $1, color = $2 WHERE id = $3
|
|
`
|
|
|
|
type UpdateIncidentStatusParams struct {
|
|
Name string
|
|
Color string
|
|
ID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) UpdateIncidentStatus(ctx context.Context, arg UpdateIncidentStatusParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, updateIncidentStatus, arg.Name, arg.Color, arg.ID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|