feat(alerting): add quiet hours to alert groups
ci / image (pull_request) Successful in 6m35s
ci / test (pull_request) Successful in 7m56s
ci / build (pull_request) Successful in 8m24s
ci / lint (pull_request) Successful in 11m55s

This commit is contained in:
Sebastian Unterschütz committed 2026-11-22 19:18:23 +01:00
1 parent bf529a7711
commit ee3769f578
21 files changed
+1310 -130

No files matched your search

+36 -19
View File
@@ -78,22 +78,24 @@ func (q *Queries) AllMonitorAlertGroups(ctx context.Context) ([]MonitorAlertGrou
}
const createAlertGroup = `-- name: CreateAlertGroup :one
INSERT INTO alert_groups (org_id, name) VALUES ($1, $2) RETURNING id, org_id, name, is_default
INSERT INTO alert_groups (org_id, name, quiet_hours) VALUES ($1, $2, $3) RETURNING id, org_id, name, is_default, quiet_hours
`
type CreateAlertGroupParams struct {
OrgID uuid.UUID
Name string
OrgID uuid.UUID
Name string
QuietHours []byte
}
func (q *Queries) CreateAlertGroup(ctx context.Context, arg CreateAlertGroupParams) (AlertGroup, error) {
row := q.db.QueryRow(ctx, createAlertGroup, arg.OrgID, arg.Name)
row := q.db.QueryRow(ctx, createAlertGroup, arg.OrgID, arg.Name, arg.QuietHours)
var i AlertGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
&i.QuietHours,
)
return i, err
}
@@ -129,7 +131,7 @@ func (q *Queries) DeleteMonitorAlertGroups(ctx context.Context, monitorID uuid.U
}
const listAlertGroups = `-- name: ListAlertGroups :many
SELECT g.id, g.org_id, g.name, g.is_default, ARRAY(SELECT c.channel_id FROM alert_group_channels c WHERE c.group_id = g.id ORDER BY c.channel_id)::uuid[] AS channel_ids
SELECT g.id, g.org_id, g.name, g.is_default, g.quiet_hours, ARRAY(SELECT c.channel_id FROM alert_group_channels c WHERE c.group_id = g.id ORDER BY c.channel_id)::uuid[] AS channel_ids
FROM alert_groups g ORDER BY g.is_default DESC, g.name, g.id
`
@@ -138,6 +140,7 @@ type ListAlertGroupsRow struct {
OrgID uuid.UUID
Name string
IsDefault bool
QuietHours []byte
ChannelIds []uuid.UUID
}
@@ -155,6 +158,7 @@ func (q *Queries) ListAlertGroups(ctx context.Context) ([]ListAlertGroupsRow, er
&i.OrgID,
&i.Name,
&i.IsDefault,
&i.QuietHours,
&i.ChannelIds,
); err != nil {
return nil, err
@@ -191,26 +195,34 @@ func (q *Queries) MonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) (
return items, nil
}
const monitorChannelIDs = `-- name: MonitorChannelIDs :many
SELECT DISTINCT c.id FROM notification_channels c
JOIN alert_group_channels gc ON gc.channel_id = c.id AND gc.org_id = c.org_id
JOIN monitor_alert_groups mg ON mg.group_id = gc.group_id AND mg.org_id = gc.org_id
const monitorAlertRoutes = `-- name: MonitorAlertRoutes :many
SELECT g.id AS group_id, g.quiet_hours, c.id AS channel_id
FROM alert_groups g
JOIN monitor_alert_groups mg ON mg.group_id = g.id AND mg.org_id = g.org_id
JOIN alert_group_channels gc ON gc.group_id = g.id AND gc.org_id = g.org_id
JOIN notification_channels c ON c.id = gc.channel_id AND c.org_id = gc.org_id
WHERE mg.monitor_id = $1 AND c.enabled
`
func (q *Queries) MonitorChannelIDs(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error) {
rows, err := q.db.Query(ctx, monitorChannelIDs, monitorID)
type MonitorAlertRoutesRow struct {
GroupID uuid.UUID
QuietHours []byte
ChannelID uuid.UUID
}
func (q *Queries) MonitorAlertRoutes(ctx context.Context, monitorID uuid.UUID) ([]MonitorAlertRoutesRow, error) {
rows, err := q.db.Query(ctx, monitorAlertRoutes, monitorID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []uuid.UUID{}
items := []MonitorAlertRoutesRow{}
for rows.Next() {
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
var i MonitorAlertRoutesRow
if err := rows.Scan(&i.GroupID, &i.QuietHours, &i.ChannelID); err != nil {
return nil, err
}
items = append(items, id)
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
@@ -219,22 +231,27 @@ func (q *Queries) MonitorChannelIDs(ctx context.Context, monitorID uuid.UUID) ([
}
const updateAlertGroup = `-- name: UpdateAlertGroup :one
UPDATE alert_groups SET name = $2 WHERE id = $1 RETURNING id, org_id, name, is_default
UPDATE alert_groups
SET name = $2, quiet_hours = coalesce($3, quiet_hours)
WHERE id = $1
RETURNING id, org_id, name, is_default, quiet_hours
`
type UpdateAlertGroupParams struct {
ID uuid.UUID
Name string
ID uuid.UUID
Name string
QuietHours []byte
}
func (q *Queries) UpdateAlertGroup(ctx context.Context, arg UpdateAlertGroupParams) (AlertGroup, error) {
row := q.db.QueryRow(ctx, updateAlertGroup, arg.ID, arg.Name)
row := q.db.QueryRow(ctx, updateAlertGroup, arg.ID, arg.Name, arg.QuietHours)
var i AlertGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
&i.QuietHours,
)
return i, err
}