258 lines
6.7 KiB
Go
258 lines
6.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: alert_groups.sql
|
|
|
|
package store
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const addAlertGroupChannel = `-- name: AddAlertGroupChannel :execrows
|
|
INSERT INTO alert_group_channels (org_id, group_id, channel_id)
|
|
SELECT c.org_id, $1, c.id FROM notification_channels c
|
|
WHERE c.id = $2 AND c.org_id = $3
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type AddAlertGroupChannelParams struct {
|
|
GroupID uuid.UUID
|
|
ChannelID uuid.UUID
|
|
OrgID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) AddAlertGroupChannel(ctx context.Context, arg AddAlertGroupChannelParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, addAlertGroupChannel, arg.GroupID, arg.ChannelID, arg.OrgID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const addMonitorAlertGroup = `-- name: AddMonitorAlertGroup :execrows
|
|
INSERT INTO monitor_alert_groups (org_id, monitor_id, group_id)
|
|
SELECT g.org_id, $1, g.id FROM alert_groups g
|
|
WHERE g.id = $2 AND g.org_id = $3
|
|
ON CONFLICT DO NOTHING
|
|
`
|
|
|
|
type AddMonitorAlertGroupParams struct {
|
|
MonitorID uuid.UUID
|
|
GroupID uuid.UUID
|
|
OrgID uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) AddMonitorAlertGroup(ctx context.Context, arg AddMonitorAlertGroupParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, addMonitorAlertGroup, arg.MonitorID, arg.GroupID, arg.OrgID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const allMonitorAlertGroups = `-- name: AllMonitorAlertGroups :many
|
|
SELECT org_id, monitor_id, group_id FROM monitor_alert_groups ORDER BY monitor_id, group_id
|
|
`
|
|
|
|
func (q *Queries) AllMonitorAlertGroups(ctx context.Context) ([]MonitorAlertGroup, error) {
|
|
rows, err := q.db.Query(ctx, allMonitorAlertGroups)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []MonitorAlertGroup{}
|
|
for rows.Next() {
|
|
var i MonitorAlertGroup
|
|
if err := rows.Scan(&i.OrgID, &i.MonitorID, &i.GroupID); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const createAlertGroup = `-- name: CreateAlertGroup :one
|
|
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
|
|
QuietHours []byte
|
|
}
|
|
|
|
func (q *Queries) CreateAlertGroup(ctx context.Context, arg CreateAlertGroupParams) (AlertGroup, error) {
|
|
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
|
|
}
|
|
|
|
const deleteAlertGroup = `-- name: DeleteAlertGroup :execrows
|
|
DELETE FROM alert_groups WHERE id = $1 AND NOT is_default
|
|
`
|
|
|
|
func (q *Queries) DeleteAlertGroup(ctx context.Context, id uuid.UUID) (int64, error) {
|
|
result, err := q.db.Exec(ctx, deleteAlertGroup, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const deleteAlertGroupChannels = `-- name: DeleteAlertGroupChannels :exec
|
|
DELETE FROM alert_group_channels WHERE group_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteAlertGroupChannels(ctx context.Context, groupID uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteAlertGroupChannels, groupID)
|
|
return err
|
|
}
|
|
|
|
const deleteMonitorAlertGroups = `-- name: DeleteMonitorAlertGroups :exec
|
|
DELETE FROM monitor_alert_groups WHERE monitor_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteMonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteMonitorAlertGroups, monitorID)
|
|
return err
|
|
}
|
|
|
|
const listAlertGroups = `-- name: ListAlertGroups :many
|
|
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
|
|
`
|
|
|
|
type ListAlertGroupsRow struct {
|
|
ID uuid.UUID
|
|
OrgID uuid.UUID
|
|
Name string
|
|
IsDefault bool
|
|
QuietHours []byte
|
|
ChannelIds []uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) ListAlertGroups(ctx context.Context) ([]ListAlertGroupsRow, error) {
|
|
rows, err := q.db.Query(ctx, listAlertGroups)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListAlertGroupsRow{}
|
|
for rows.Next() {
|
|
var i ListAlertGroupsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.OrgID,
|
|
&i.Name,
|
|
&i.IsDefault,
|
|
&i.QuietHours,
|
|
&i.ChannelIds,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const monitorAlertGroups = `-- name: MonitorAlertGroups :many
|
|
SELECT group_id FROM monitor_alert_groups WHERE monitor_id = $1 ORDER BY group_id
|
|
`
|
|
|
|
func (q *Queries) MonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error) {
|
|
rows, err := q.db.Query(ctx, monitorAlertGroups, monitorID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []uuid.UUID{}
|
|
for rows.Next() {
|
|
var group_id uuid.UUID
|
|
if err := rows.Scan(&group_id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, group_id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
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
|
|
`
|
|
|
|
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 := []MonitorAlertRoutesRow{}
|
|
for rows.Next() {
|
|
var i MonitorAlertRoutesRow
|
|
if err := rows.Scan(&i.GroupID, &i.QuietHours, &i.ChannelID); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateAlertGroup = `-- name: UpdateAlertGroup :one
|
|
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
|
|
QuietHours []byte
|
|
}
|
|
|
|
func (q *Queries) UpdateAlertGroup(ctx context.Context, arg UpdateAlertGroupParams) (AlertGroup, error) {
|
|
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
|
|
}
|