feat(store): store Alertmanager receivers and the alerts they deliver
Refs #91 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
98bdb3c282
commit
16dfba817e
5 files changed
+429
No files matched your search
@@ -0,0 +1,294 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: alertmanager.sql
|
||||
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const alertmanagerMonitors = `-- name: AlertmanagerMonitors :many
|
||||
SELECT m.id, m.org_id, m.name, m.group_id, m.interval_seconds, m.confirmations, m.config,
|
||||
coalesce((SELECT max(r.time) FROM check_results r
|
||||
WHERE r.org_id = m.org_id AND r.monitor_id = m.id AND r.time > now() - interval '2 days'), 'epoch')::timestamptz AS last_result
|
||||
FROM monitors m
|
||||
WHERE m.type = 'alertmanager' AND m.enabled AND ($1::uuid IS NULL OR m.org_id = $1)
|
||||
`
|
||||
|
||||
type AlertmanagerMonitorsRow struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Name string
|
||||
GroupID *uuid.UUID
|
||||
IntervalSeconds int32
|
||||
Confirmations int32
|
||||
Config []byte
|
||||
LastResult time.Time
|
||||
}
|
||||
|
||||
// System scope: enabled alertmanager monitors of org, with when they last
|
||||
// recorded a result.
|
||||
func (q *Queries) AlertmanagerMonitors(ctx context.Context, orgID *uuid.UUID) ([]AlertmanagerMonitorsRow, error) {
|
||||
rows, err := q.db.Query(ctx, alertmanagerMonitors, orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []AlertmanagerMonitorsRow{}
|
||||
for rows.Next() {
|
||||
var i AlertmanagerMonitorsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.Name,
|
||||
&i.GroupID,
|
||||
&i.IntervalSeconds,
|
||||
&i.Confirmations,
|
||||
&i.Config,
|
||||
&i.LastResult,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const alertmanagerReceiverByHash = `-- name: AlertmanagerReceiverByHash :one
|
||||
UPDATE alertmanager_receivers SET last_received_at = now()
|
||||
WHERE token_hash = $1
|
||||
RETURNING id, org_id
|
||||
`
|
||||
|
||||
type AlertmanagerReceiverByHashRow struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
}
|
||||
|
||||
// System scope: the receiver a webhook token belongs to.
|
||||
func (q *Queries) AlertmanagerReceiverByHash(ctx context.Context, tokenHash []byte) (AlertmanagerReceiverByHashRow, error) {
|
||||
row := q.db.QueryRow(ctx, alertmanagerReceiverByHash, tokenHash)
|
||||
var i AlertmanagerReceiverByHashRow
|
||||
err := row.Scan(&i.ID, &i.OrgID)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createAlertmanagerReceiver = `-- name: CreateAlertmanagerReceiver :one
|
||||
INSERT INTO alertmanager_receivers (org_id, name, token_hash, prefix)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, org_id, name, prefix, created_at, last_received_at
|
||||
`
|
||||
|
||||
type CreateAlertmanagerReceiverParams struct {
|
||||
OrgID uuid.UUID
|
||||
Name string
|
||||
TokenHash []byte
|
||||
Prefix string
|
||||
}
|
||||
|
||||
type CreateAlertmanagerReceiverRow struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Name string
|
||||
Prefix string
|
||||
CreatedAt time.Time
|
||||
LastReceivedAt *time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAlertmanagerReceiver(ctx context.Context, arg CreateAlertmanagerReceiverParams) (CreateAlertmanagerReceiverRow, error) {
|
||||
row := q.db.QueryRow(ctx, createAlertmanagerReceiver,
|
||||
arg.OrgID,
|
||||
arg.Name,
|
||||
arg.TokenHash,
|
||||
arg.Prefix,
|
||||
)
|
||||
var i CreateAlertmanagerReceiverRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.Name,
|
||||
&i.Prefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastReceivedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const deleteAlertmanagerReceiver = `-- name: DeleteAlertmanagerReceiver :execrows
|
||||
DELETE FROM alertmanager_receivers WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteAlertmanagerReceiver(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteAlertmanagerReceiver, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const firingAlerts = `-- name: FiringAlerts :many
|
||||
SELECT fingerprint, labels, annotations, starts_at, generator_url, updated_at
|
||||
FROM alertmanager_alerts
|
||||
WHERE org_id = $1 AND status = 'firing' AND updated_at > $2
|
||||
ORDER BY starts_at
|
||||
`
|
||||
|
||||
type FiringAlertsParams struct {
|
||||
OrgID uuid.UUID
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type FiringAlertsRow struct {
|
||||
Fingerprint string
|
||||
Labels []byte
|
||||
Annotations []byte
|
||||
StartsAt time.Time
|
||||
GeneratorUrl string
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
// Alerts of org that fire and were repeated within the stale period.
|
||||
func (q *Queries) FiringAlerts(ctx context.Context, arg FiringAlertsParams) ([]FiringAlertsRow, error) {
|
||||
rows, err := q.db.Query(ctx, firingAlerts, arg.OrgID, arg.UpdatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []FiringAlertsRow{}
|
||||
for rows.Next() {
|
||||
var i FiringAlertsRow
|
||||
if err := rows.Scan(
|
||||
&i.Fingerprint,
|
||||
&i.Labels,
|
||||
&i.Annotations,
|
||||
&i.StartsAt,
|
||||
&i.GeneratorUrl,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const latestResultForMonitor = `-- name: LatestResultForMonitor :one
|
||||
SELECT ok, coalesce(error, '')::text AS error FROM check_results
|
||||
WHERE org_id = $1 AND monitor_id = $2 AND time > now() - interval '2 days'
|
||||
ORDER BY time DESC LIMIT 1
|
||||
`
|
||||
|
||||
type LatestResultForMonitorParams struct {
|
||||
OrgID uuid.UUID
|
||||
MonitorID uuid.UUID
|
||||
}
|
||||
|
||||
type LatestResultForMonitorRow struct {
|
||||
Ok bool
|
||||
Error string
|
||||
}
|
||||
|
||||
func (q *Queries) LatestResultForMonitor(ctx context.Context, arg LatestResultForMonitorParams) (LatestResultForMonitorRow, error) {
|
||||
row := q.db.QueryRow(ctx, latestResultForMonitor, arg.OrgID, arg.MonitorID)
|
||||
var i LatestResultForMonitorRow
|
||||
err := row.Scan(&i.Ok, &i.Error)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAlertmanagerReceivers = `-- name: ListAlertmanagerReceivers :many
|
||||
SELECT id, org_id, name, prefix, created_at, last_received_at FROM alertmanager_receivers ORDER BY name
|
||||
`
|
||||
|
||||
type ListAlertmanagerReceiversRow struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Name string
|
||||
Prefix string
|
||||
CreatedAt time.Time
|
||||
LastReceivedAt *time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListAlertmanagerReceivers(ctx context.Context) ([]ListAlertmanagerReceiversRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAlertmanagerReceivers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAlertmanagerReceiversRow{}
|
||||
for rows.Next() {
|
||||
var i ListAlertmanagerReceiversRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.OrgID,
|
||||
&i.Name,
|
||||
&i.Prefix,
|
||||
&i.CreatedAt,
|
||||
&i.LastReceivedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const purgeAlertmanagerAlerts = `-- name: PurgeAlertmanagerAlerts :execrows
|
||||
DELETE FROM alertmanager_alerts WHERE updated_at < $1
|
||||
`
|
||||
|
||||
// Resolved and long silent alerts are history the monitors' results keep.
|
||||
func (q *Queries) PurgeAlertmanagerAlerts(ctx context.Context, updatedAt time.Time) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, purgeAlertmanagerAlerts, updatedAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const upsertAlertmanagerAlert = `-- name: UpsertAlertmanagerAlert :exec
|
||||
INSERT INTO alertmanager_alerts (org_id, fingerprint, receiver_id, status, labels, annotations, starts_at, generator_url, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, now())
|
||||
ON CONFLICT (org_id, fingerprint) DO UPDATE SET
|
||||
receiver_id = EXCLUDED.receiver_id, status = EXCLUDED.status, labels = EXCLUDED.labels,
|
||||
annotations = EXCLUDED.annotations, starts_at = EXCLUDED.starts_at,
|
||||
generator_url = EXCLUDED.generator_url, updated_at = now()
|
||||
`
|
||||
|
||||
type UpsertAlertmanagerAlertParams struct {
|
||||
OrgID uuid.UUID
|
||||
Fingerprint string
|
||||
ReceiverID uuid.UUID
|
||||
Status string
|
||||
Labels []byte
|
||||
Annotations []byte
|
||||
StartsAt time.Time
|
||||
GeneratorUrl string
|
||||
}
|
||||
|
||||
func (q *Queries) UpsertAlertmanagerAlert(ctx context.Context, arg UpsertAlertmanagerAlertParams) error {
|
||||
_, err := q.db.Exec(ctx, upsertAlertmanagerAlert,
|
||||
arg.OrgID,
|
||||
arg.Fingerprint,
|
||||
arg.ReceiverID,
|
||||
arg.Status,
|
||||
arg.Labels,
|
||||
arg.Annotations,
|
||||
arg.StartsAt,
|
||||
arg.GeneratorUrl,
|
||||
)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user