feat(alerting): add quiet hours to alert groups
This commit is contained in:
1 parent
bf529a7711
commit
ee3769f578
21 files changed
+1310
-130
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -42,10 +42,11 @@ type AgentStorage struct {
|
||||
}
|
||||
|
||||
type AlertGroup struct {
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Name string
|
||||
IsDefault bool
|
||||
ID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Name string
|
||||
IsDefault bool
|
||||
QuietHours []byte
|
||||
}
|
||||
|
||||
type AlertGroupChannel struct {
|
||||
|
||||
@@ -196,7 +196,7 @@ type Querier interface {
|
||||
// Every role a member holds, with the scope it applies to.
|
||||
MemberGrants(ctx context.Context, arg MemberGrantsParams) ([]MemberGrantsRow, error)
|
||||
MonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
|
||||
MonitorChannelIDs(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
|
||||
MonitorAlertRoutes(ctx context.Context, monitorID uuid.UUID) ([]MonitorAlertRoutesRow, error)
|
||||
MonitorIncidents(ctx context.Context, monitorID uuid.UUID) ([]Incident, error)
|
||||
MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
|
||||
// System scope: monitors to hand to a checker of region, skipping ones that
|
||||
|
||||
@@ -3,10 +3,13 @@ SELECT g.*, ARRAY(SELECT c.channel_id FROM alert_group_channels c WHERE c.group_
|
||||
FROM alert_groups g ORDER BY g.is_default DESC, g.name, g.id;
|
||||
|
||||
-- name: CreateAlertGroup :one
|
||||
INSERT INTO alert_groups (org_id, name) VALUES ($1, $2) RETURNING *;
|
||||
INSERT INTO alert_groups (org_id, name, quiet_hours) VALUES ($1, $2, $3) RETURNING *;
|
||||
|
||||
-- name: UpdateAlertGroup :one
|
||||
UPDATE alert_groups SET name = $2 WHERE id = $1 RETURNING *;
|
||||
UPDATE alert_groups
|
||||
SET name = $2, quiet_hours = coalesce(sqlc.narg(quiet_hours), quiet_hours)
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: DeleteAlertGroup :execrows
|
||||
DELETE FROM alert_groups WHERE id = $1 AND NOT is_default;
|
||||
@@ -35,8 +38,10 @@ SELECT g.org_id, sqlc.arg(monitor_id), g.id FROM alert_groups g
|
||||
WHERE g.id = sqlc.arg(group_id) AND g.org_id = sqlc.arg(org_id)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- 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
|
||||
-- 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;
|
||||
Reference in new issue
Block a user