Files
uptimesebi/internal/store/alert_groups.sql.go
T

241 lines
6.2 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) VALUES ($1, $2) RETURNING id, org_id, name, is_default
`
type CreateAlertGroupParams struct {
OrgID uuid.UUID
Name string
}
func (q *Queries) CreateAlertGroup(ctx context.Context, arg CreateAlertGroupParams) (AlertGroup, error) {
row := q.db.QueryRow(ctx, createAlertGroup, arg.OrgID, arg.Name)
var i AlertGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
)
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, 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
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.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 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
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)
if err != nil {
return nil, err
}
defer rows.Close()
items := []uuid.UUID{}
for rows.Next() {
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateAlertGroup = `-- name: UpdateAlertGroup :one
UPDATE alert_groups SET name = $2 WHERE id = $1 RETURNING id, org_id, name, is_default
`
type UpdateAlertGroupParams struct {
ID uuid.UUID
Name string
}
func (q *Queries) UpdateAlertGroup(ctx context.Context, arg UpdateAlertGroupParams) (AlertGroup, error) {
row := q.db.QueryRow(ctx, updateAlertGroup, arg.ID, arg.Name)
var i AlertGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
)
return i, err
}