Files

451 lines
10 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: alerts.sql
package postgresdb
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
const claimAlertDeliveries = `-- name: ClaimAlertDeliveries :many
UPDATE alert_deliveries SET next_attempt_at = now() + interval '2 minutes'
WHERE id IN (
SELECT d.id FROM alert_deliveries d
WHERE d.state = 'pending' AND d.next_attempt_at <= now()
ORDER BY d.id LIMIT $1 FOR UPDATE SKIP LOCKED)
RETURNING id, alert_id, event, state, created_at, next_attempt_at, attempts
`
func (q *Queries) ClaimAlertDeliveries(ctx context.Context, pageLimit int32) ([]AlertDelivery, error) {
rows, err := q.db.Query(ctx, claimAlertDeliveries, pageLimit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AlertDelivery
for rows.Next() {
var i AlertDelivery
if err := rows.Scan(
&i.ID,
&i.AlertID,
&i.Event,
&i.State,
&i.CreatedAt,
&i.NextAttemptAt,
&i.Attempts,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAlert = `-- name: GetAlert :one
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts WHERE id = $1
`
func (q *Queries) GetAlert(ctx context.Context, id int64) (Alert, error) {
row := q.db.QueryRow(ctx, getAlert, id)
var i Alert
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Subject,
&i.Severity,
&i.Title,
&i.Body,
&i.Link,
&i.IsCondition,
&i.State,
&i.FirstRaised,
&i.LastSeen,
&i.Count,
&i.ClosedBy,
&i.ClosedAt,
)
return i, err
}
const getAlertByKey = `-- name: GetAlertByKey :one
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts WHERE org_id = $1 AND kind = $2 AND subject = $3
`
type GetAlertByKeyParams struct {
OrgID string
Kind string
Subject string
}
func (q *Queries) GetAlertByKey(ctx context.Context, arg GetAlertByKeyParams) (Alert, error) {
row := q.db.QueryRow(ctx, getAlertByKey, arg.OrgID, arg.Kind, arg.Subject)
var i Alert
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Subject,
&i.Severity,
&i.Title,
&i.Body,
&i.Link,
&i.IsCondition,
&i.State,
&i.FirstRaised,
&i.LastSeen,
&i.Count,
&i.ClosedBy,
&i.ClosedAt,
)
return i, err
}
const getOrgAlert = `-- name: GetOrgAlert :one
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts WHERE org_id = $1 AND id = $2
`
type GetOrgAlertParams struct {
OrgID string
ID int64
}
func (q *Queries) GetOrgAlert(ctx context.Context, arg GetOrgAlertParams) (Alert, error) {
row := q.db.QueryRow(ctx, getOrgAlert, arg.OrgID, arg.ID)
var i Alert
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Subject,
&i.Severity,
&i.Title,
&i.Body,
&i.Link,
&i.IsCondition,
&i.State,
&i.FirstRaised,
&i.LastSeen,
&i.Count,
&i.ClosedBy,
&i.ClosedAt,
)
return i, err
}
const insertAlertAttempt = `-- name: InsertAlertAttempt :exec
INSERT INTO alert_delivery_attempts (delivery_id, attempt, status_code, error) VALUES ($1, $2, $3, $4)
`
type InsertAlertAttemptParams struct {
DeliveryID int64
Attempt int64
StatusCode int64
Error string
}
func (q *Queries) InsertAlertAttempt(ctx context.Context, arg InsertAlertAttemptParams) error {
_, err := q.db.Exec(ctx, insertAlertAttempt,
arg.DeliveryID,
arg.Attempt,
arg.StatusCode,
arg.Error,
)
return err
}
const insertAlertDelivery = `-- name: InsertAlertDelivery :exec
INSERT INTO alert_deliveries (alert_id, event) VALUES ($1, $2)
`
type InsertAlertDeliveryParams struct {
AlertID int64
Event string
}
func (q *Queries) InsertAlertDelivery(ctx context.Context, arg InsertAlertDeliveryParams) error {
_, err := q.db.Exec(ctx, insertAlertDelivery, arg.AlertID, arg.Event)
return err
}
const listAlertAttempts = `-- name: ListAlertAttempts :many
SELECT id, delivery_id, attempt, at, status_code, error
FROM alert_delivery_attempts WHERE delivery_id = ANY($1::bigint[]) ORDER BY delivery_id, id
`
func (q *Queries) ListAlertAttempts(ctx context.Context, deliveryIds []int64) ([]AlertDeliveryAttempt, error) {
rows, err := q.db.Query(ctx, listAlertAttempts, deliveryIds)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AlertDeliveryAttempt
for rows.Next() {
var i AlertDeliveryAttempt
if err := rows.Scan(
&i.ID,
&i.DeliveryID,
&i.Attempt,
&i.At,
&i.StatusCode,
&i.Error,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listAlertDeliveries = `-- name: ListAlertDeliveries :many
SELECT id, alert_id, event, state, created_at, next_attempt_at, attempts
FROM alert_deliveries WHERE alert_id = ANY($1::bigint[]) ORDER BY alert_id, id DESC
`
func (q *Queries) ListAlertDeliveries(ctx context.Context, alertIds []int64) ([]AlertDelivery, error) {
rows, err := q.db.Query(ctx, listAlertDeliveries, alertIds)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AlertDelivery
for rows.Next() {
var i AlertDelivery
if err := rows.Scan(
&i.ID,
&i.AlertID,
&i.Event,
&i.State,
&i.CreatedAt,
&i.NextAttemptAt,
&i.Attempts,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listAlerts = `-- name: ListAlerts :many
SELECT id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
FROM alerts
WHERE org_id = $1 AND state = ANY($2::text[])
ORDER BY CASE severity WHEN 'action' THEN 3 WHEN 'warning' THEN 2 ELSE 1 END DESC, last_seen DESC, id DESC
LIMIT $3
`
type ListAlertsParams struct {
OrgID string
States []string
PageLimit int32
}
func (q *Queries) ListAlerts(ctx context.Context, arg ListAlertsParams) ([]Alert, error) {
rows, err := q.db.Query(ctx, listAlerts, arg.OrgID, arg.States, arg.PageLimit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Alert
for rows.Next() {
var i Alert
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Subject,
&i.Severity,
&i.Title,
&i.Body,
&i.Link,
&i.IsCondition,
&i.State,
&i.FirstRaised,
&i.LastSeen,
&i.Count,
&i.ClosedBy,
&i.ClosedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const lockAlertDelivery = `-- name: LockAlertDelivery :one
SELECT id, alert_id, event, state, created_at, next_attempt_at, attempts
FROM alert_deliveries WHERE id = $1 FOR UPDATE
`
func (q *Queries) LockAlertDelivery(ctx context.Context, id int64) (AlertDelivery, error) {
row := q.db.QueryRow(ctx, lockAlertDelivery, id)
var i AlertDelivery
err := row.Scan(
&i.ID,
&i.AlertID,
&i.Event,
&i.State,
&i.CreatedAt,
&i.NextAttemptAt,
&i.Attempts,
)
return i, err
}
const lockAlertKey = `-- name: LockAlertKey :exec
SELECT pg_advisory_xact_lock(hashtextextended($1::text, 0))
`
// Operator alerts and their outbound deliveries (internal/domain/alert).
// Serialises raisers of one alert, so two first raises cannot both insert.
func (q *Queries) LockAlertKey(ctx context.Context, lockKey string) error {
_, err := q.db.Exec(ctx, lockAlertKey, lockKey)
return err
}
const pruneAlerts = `-- name: PruneAlerts :execrows
DELETE FROM alerts WHERE state <> 'open' AND closed_at < $1
`
func (q *Queries) PruneAlerts(ctx context.Context, closedAt pgtype.Timestamptz) (int64, error) {
result, err := q.db.Exec(ctx, pruneAlerts, closedAt)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const settleAlertDelivery = `-- name: SettleAlertDelivery :exec
UPDATE alert_deliveries SET state = $2, attempts = $3, next_attempt_at = $4 WHERE id = $1
`
type SettleAlertDeliveryParams struct {
ID int64
State string
Attempts int64
NextAttemptAt time.Time
}
func (q *Queries) SettleAlertDelivery(ctx context.Context, arg SettleAlertDeliveryParams) error {
_, err := q.db.Exec(ctx, settleAlertDelivery,
arg.ID,
arg.State,
arg.Attempts,
arg.NextAttemptAt,
)
return err
}
const taskAlertInfo = `-- name: TaskAlertInfo :one
SELECT org_id, title, workflow, park_class, park_blocker FROM tasks WHERE id = $1
`
type TaskAlertInfoRow struct {
OrgID string
Title string
Workflow string
ParkClass string
ParkBlocker string
}
func (q *Queries) TaskAlertInfo(ctx context.Context, id int64) (TaskAlertInfoRow, error) {
row := q.db.QueryRow(ctx, taskAlertInfo, id)
var i TaskAlertInfoRow
err := row.Scan(
&i.OrgID,
&i.Title,
&i.Workflow,
&i.ParkClass,
&i.ParkBlocker,
)
return i, err
}
const upsertAlert = `-- name: UpsertAlert :one
INSERT INTO alerts (org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
ON CONFLICT (org_id, kind, subject) DO UPDATE SET
severity = excluded.severity, title = excluded.title, body = excluded.body, link = excluded.link,
is_condition = excluded.is_condition, state = excluded.state, first_raised = excluded.first_raised,
last_seen = excluded.last_seen, count = excluded.count, closed_by = excluded.closed_by, closed_at = excluded.closed_at
RETURNING id, org_id, kind, subject, severity, title, body, link, is_condition, state, first_raised, last_seen, count, closed_by, closed_at
`
type UpsertAlertParams struct {
OrgID string
Kind string
Subject string
Severity string
Title string
Body string
Link string
IsCondition bool
State string
FirstRaised time.Time
LastSeen time.Time
Count int64
ClosedBy string
ClosedAt pgtype.Timestamptz
}
func (q *Queries) UpsertAlert(ctx context.Context, arg UpsertAlertParams) (Alert, error) {
row := q.db.QueryRow(ctx, upsertAlert,
arg.OrgID,
arg.Kind,
arg.Subject,
arg.Severity,
arg.Title,
arg.Body,
arg.Link,
arg.IsCondition,
arg.State,
arg.FirstRaised,
arg.LastSeen,
arg.Count,
arg.ClosedBy,
arg.ClosedAt,
)
var i Alert
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Kind,
&i.Subject,
&i.Severity,
&i.Title,
&i.Body,
&i.Link,
&i.IsCondition,
&i.State,
&i.FirstRaised,
&i.LastSeen,
&i.Count,
&i.ClosedBy,
&i.ClosedAt,
)
return i, err
}