200 lines
5.1 KiB
Go
200 lines
5.1 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
)
|
|
|
|
type alertGroupRequest struct {
|
|
Name string `json:"name" binding:"required,max=100"`
|
|
ChannelIDs []uuid.UUID `json:"channel_ids" binding:"max=200"`
|
|
// QuietHours suppresses notifications from this group during the given
|
|
// windows. Omitting it on update preserves existing quiet hours; an
|
|
// empty array clears them.
|
|
QuietHours []quietHourRequest `json:"quiet_hours" binding:"max=20"`
|
|
}
|
|
|
|
type alertGroupView struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Name string `json:"name"`
|
|
IsDefault bool `json:"is_default"`
|
|
ChannelIDs []uuid.UUID `json:"channel_ids"`
|
|
QuietHours []quietHourView `json:"quiet_hours"`
|
|
}
|
|
|
|
var errInvalidAlertGroup = errors.New("api: invalid alert group")
|
|
var errInvalidChannel = errors.New("api: invalid notification channel")
|
|
|
|
func (s *Server) listAlertGroups(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
out := []alertGroupView{}
|
|
err := s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
|
rows, err := q.ListAlertGroups(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, row := range rows {
|
|
out = append(out, alertGroupView{
|
|
ID: row.ID, Name: row.Name, IsDefault: row.IsDefault,
|
|
ChannelIDs: row.ChannelIds, QuietHours: decodeQuietHours(row.QuietHours),
|
|
})
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"alert_groups": out})
|
|
}
|
|
|
|
func (s *Server) saveAlertGroup(c *gin.Context) {
|
|
var req alertGroupRequest
|
|
if !bind(c, &req) {
|
|
return
|
|
}
|
|
req.Name = strings.TrimSpace(req.Name)
|
|
if req.Name == "" {
|
|
fail(c, http.StatusUnprocessableEntity, "invalid_input")
|
|
return
|
|
}
|
|
var id uuid.UUID
|
|
if c.Param("id") != "" {
|
|
var err error
|
|
id, err = uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
}
|
|
var quietRaw []byte
|
|
switch {
|
|
case req.QuietHours != nil:
|
|
qh, code := validateQuietHours(req.QuietHours)
|
|
if code != "" {
|
|
fail(c, http.StatusUnprocessableEntity, code)
|
|
return
|
|
}
|
|
var err error
|
|
quietRaw, err = json.Marshal(qh)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
case c.Param("id") == "":
|
|
quietRaw = []byte("[]")
|
|
}
|
|
ctx := c.Request.Context()
|
|
p := current(c)
|
|
var row store.AlertGroup
|
|
// Stable ordering also avoids deadlocks when concurrent edits share channels.
|
|
slices.SortFunc(req.ChannelIDs, func(a, b uuid.UUID) int { return strings.Compare(a.String(), b.String()) })
|
|
req.ChannelIDs = slices.Compact(req.ChannelIDs)
|
|
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
|
var err error
|
|
if c.Param("id") == "" {
|
|
row, err = q.CreateAlertGroup(ctx, store.CreateAlertGroupParams{OrgID: p.Org.ID, Name: req.Name, QuietHours: quietRaw})
|
|
} else {
|
|
row, err = q.UpdateAlertGroup(ctx, store.UpdateAlertGroupParams{ID: id, Name: req.Name, QuietHours: quietRaw})
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := q.DeleteAlertGroupChannels(ctx, row.ID); err != nil {
|
|
return err
|
|
}
|
|
for _, ch := range req.ChannelIDs {
|
|
n, err := q.AddAlertGroupChannel(ctx, store.AddAlertGroupChannelParams{OrgID: p.Org.ID, GroupID: row.ID, ChannelID: ch})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != 1 {
|
|
return errInvalidChannel
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
if errors.Is(err, errInvalidChannel) {
|
|
fail(c, http.StatusUnprocessableEntity, "invalid_channel")
|
|
return
|
|
}
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
status := http.StatusOK
|
|
if c.Param("id") == "" {
|
|
status = http.StatusCreated
|
|
}
|
|
if req.ChannelIDs == nil {
|
|
req.ChannelIDs = []uuid.UUID{}
|
|
}
|
|
c.JSON(status, alertGroupView{
|
|
ID: row.ID, Name: row.Name, IsDefault: row.IsDefault,
|
|
ChannelIDs: req.ChannelIDs, QuietHours: decodeQuietHours(row.QuietHours),
|
|
})
|
|
}
|
|
|
|
func (s *Server) deleteAlertGroup(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
var n int64
|
|
err = s.db.Tx(ctx, current(c).scope(), func(q *store.Queries) error {
|
|
n, err = q.DeleteAlertGroup(ctx, id)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
if n == 0 {
|
|
fail(c, http.StatusNotFound, "alert_group_not_deletable")
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// setMonitorAlertGroups preserves assignments when ids is omitted. On creation,
|
|
// the database assigns the default group; an explicit empty list opts out.
|
|
func setMonitorAlertGroups(ctx context.Context, q *store.Queries, m store.Monitor, ids []uuid.UUID) error {
|
|
if ids == nil {
|
|
return nil
|
|
}
|
|
if err := q.DeleteMonitorAlertGroups(ctx, m.ID); err != nil {
|
|
return err
|
|
}
|
|
seen := make(map[uuid.UUID]bool, len(ids))
|
|
for _, id := range ids {
|
|
if seen[id] {
|
|
continue
|
|
}
|
|
seen[id] = true
|
|
n, err := q.AddMonitorAlertGroup(ctx, store.AddMonitorAlertGroupParams{OrgID: m.OrgID, MonitorID: m.ID, GroupID: id})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n != 1 {
|
|
return errInvalidAlertGroup
|
|
}
|
|
}
|
|
return nil
|
|
}
|