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

+149
View File
@@ -0,0 +1,149 @@
package api
import (
"encoding/json"
"slices"
"strconv"
"strings"
"time"
)
// quietHourRequest is one recurring time window during which an alert group
// suppresses notifications.
type quietHourRequest struct {
// Weekdays are days of the week (0 = Sunday .. 6 = Saturday); empty
// means every day.
Weekdays flexInts `json:"weekdays" binding:"max=7"`
// Start and End are wall-clock times "HH:MM" in TimeZone. End may be
// "24:00"; Start > End wraps overnight into the next day, and
// "00:00" to "00:00" (or "24:00") covers the entire day.
Start string `json:"start" binding:"required,max=5"`
End string `json:"end" binding:"required,max=5"`
TimeZone string `json:"time_zone" binding:"required,max=64"`
}
// quietHourView is one normalized quiet window returned by the API and stored
// on an alert group.
type quietHourView struct {
Weekdays []int `json:"weekdays"`
Start string `json:"start"`
End string `json:"end"`
TimeZone string `json:"time_zone"`
}
// validateQuietHours validates and normalizes quiet windows, returning an
// error code on failure.
func validateQuietHours(in []quietHourRequest) ([]quietHourView, string) {
out := make([]quietHourView, 0, len(in))
for _, w := range in {
tz := strings.TrimSpace(w.TimeZone)
if tz == "" || tz == "Local" {
return nil, "invalid_time_zone"
}
if _, err := time.LoadLocation(tz); err != nil {
return nil, "invalid_time_zone"
}
start := strings.TrimSpace(w.Start)
end := strings.TrimSpace(w.End)
sMin, okS := parseClockMinutes(start, false)
eMin, okE := parseClockMinutes(end, true)
if !okS || !okE {
return nil, "invalid_quiet_hours"
}
// Identical non-zero times would be an empty window; 00:00-00:00
// (like 00:00-24:00) means the whole day.
if sMin == eMin && sMin != 0 {
return nil, "invalid_quiet_hours"
}
days := make([]int, 0, len(w.Weekdays))
for _, d := range w.Weekdays {
if d < 0 || d > 6 {
return nil, "invalid_quiet_hours"
}
days = append(days, d)
}
slices.Sort(days)
days = slices.Compact(days)
out = append(out, quietHourView{
Weekdays: days,
Start: start,
End: end,
TimeZone: tz,
})
}
return out, ""
}
// parseClockMinutes parses "HH:MM" into minutes since midnight. When allow24
// is true, "24:00" is also accepted as 1440.
func parseClockMinutes(s string, allow24 bool) (int, bool) {
if len(s) != 5 || s[2] != ':' {
return 0, false
}
h, err1 := strconv.Atoi(s[:2])
m, err2 := strconv.Atoi(s[3:])
if err1 != nil || err2 != nil || m < 0 || m > 59 {
return 0, false
}
if h == 24 && m == 0 && allow24 {
return 24 * 60, true
}
if h < 0 || h > 23 {
return 0, false
}
return h*60 + m, true
}
func decodeQuietHours(raw []byte) []quietHourView {
if len(raw) == 0 {
return []quietHourView{}
}
var out []quietHourView
if err := json.Unmarshal(raw, &out); err != nil || out == nil {
return []quietHourView{}
}
for i := range out {
if out[i].Weekdays == nil {
out[i].Weekdays = []int{}
}
}
return out
}
// contains reports whether t falls inside w in w's time zone.
func (w quietHourView) contains(t time.Time) bool {
sMin, okS := parseClockMinutes(w.Start, false)
eMin, okE := parseClockMinutes(w.End, true)
if !okS || !okE {
return false
}
lt := t.In(loadLocation(w.TimeZone))
nowMin := lt.Hour()*60 + lt.Minute()
today := int(lt.Weekday())
hasDay := func(d int) bool {
return len(w.Weekdays) == 0 || slices.Contains(w.Weekdays, d)
}
if sMin == 0 && (eMin == 0 || eMin == 24*60) {
return hasDay(today)
}
if sMin < eMin {
return hasDay(today) && nowMin >= sMin && nowMin < eMin
}
// Overnight window (Start > End), e.g. 22:00-07:00 or 18:00-00:00:
// covers [Start, 24:00) on matching days and [00:00, End) on the next day.
yesterday := (today + 6) % 7
return (hasDay(today) && nowMin >= sMin) || (hasDay(yesterday) && nowMin < eMin)
}
func isQuietAt(windows []quietHourView, t time.Time) bool {
for _, w := range windows {
if w.contains(t) {
return true
}
}
return false
}
func isQuietRawAt(raw []byte, t time.Time) bool {
return isQuietAt(decodeQuietHours(raw), t)
}