Files
uptimesebi/internal/web/alert_groups.templ
T
Sebastian Unterschütz ee3769f578
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
feat(alerting): add quiet hours to alert groups
2026-11-22 19:18:23 +01:00

163 lines
5.8 KiB
Templ

package web
import (
"strconv"
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
)
// AlertGroupOption is a selectable group or channel in an alert routing form.
type AlertGroupOption struct {
ID string
Name string
Default bool
Checked bool
}
// AlertGroupQuietRow is one quiet-hours window rendered in an alert group form.
type AlertGroupQuietRow struct {
Start string
End string
TimeZone string
Weekdays map[int]bool
}
// AlertGroupRow describes a group, its channels and its quiet hours.
type AlertGroupRow struct {
ID string
Name string
Default bool
QuietNow bool
Channels []AlertGroupOption
QuietHours []AlertGroupQuietRow
}
func quietEndInput(end string) string {
if end == "24:00" {
return "00:00"
}
return end
}
templ alertGroupOptions(name string, options []AlertGroupOption) {
<input type="hidden" name={ name } data-multi/>
for _, opt := range options {
<label class="mb-2 flex items-center gap-2 text-sm">
<input type="checkbox" name={ name } value={ opt.ID } data-multi checked?={ opt.Checked }/>
<span>{ opt.Name }</span>
if opt.Default {
<span class="text-xs text-muted">{ i18n.T(ctx, "alert_groups.default") }</span>
}
</label>
}
}
templ alertGroupQuietRow(prefix string, q AlertGroupQuietRow, canWrite bool) {
<div data-quiet-row class="mb-3 border border-rule p-3">
<div class="grid gap-2 sm:grid-cols-3">
<label class="block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, "alert_groups.quiet_start") }</span>
<input type="time" name={ prefix + ".start" } value={ q.Start } required class={ inputClass }/>
</label>
<label class="block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, "alert_groups.quiet_end") }</span>
<input type="time" name={ prefix + ".end" } value={ quietEndInput(q.End) } required class={ inputClass }/>
</label>
<label class="block">
<span class="mb-1 block text-xs text-muted">{ i18n.T(ctx, "maintenance.field_time_zone") }</span>
<input name={ prefix + ".time_zone" } list="quiet-time-zones" value={ q.TimeZone } required data-tz-default class={ inputClass }/>
</label>
</div>
<div class="mt-2 flex flex-wrap gap-2 text-xs">
<input type="hidden" name={ prefix + ".weekdays" } data-multi/>
for _, d := range []int{1, 2, 3, 4, 5, 6, 0} {
<label class="flex items-center gap-1">
<input type="checkbox" name={ prefix + ".weekdays" } data-multi value={ strconv.Itoa(d) } checked?={ q.Weekdays[d] }/>
{ i18n.T(ctx, "weekday." + strconv.Itoa(d)) }
</label>
}
</div>
if canWrite {
<button type="button" data-quiet-remove class="mt-2 text-xs text-muted hover:text-down">{ i18n.T(ctx, "alert_groups.quiet_remove") }</button>
}
</div>
}
templ alertGroupQuietHours(rows []AlertGroupQuietRow, canWrite bool) {
<div data-quiet-hours class="my-4 border-t border-rule pt-3">
<p class="mb-1 text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "alert_groups.quiet_title") }</p>
<p class="mb-3 text-xs text-muted">{ i18n.T(ctx, "alert_groups.quiet_hint") }</p>
<input type="hidden" name="quiet_hours" data-multi/>
<div data-quiet-list>
for i, q := range rows {
@alertGroupQuietRow("quiet_hours."+strconv.Itoa(i), q, canWrite)
}
</div>
if canWrite {
<template data-quiet-template>
@alertGroupQuietRow("quiet_hours.__INDEX__", AlertGroupQuietRow{Start: "22:00", End: "07:00"}, true)
</template>
<button type="button" data-quiet-add class="mb-2 border border-rule px-2.5 py-1 text-xs hover:border-ink">{ i18n.T(ctx, "alert_groups.quiet_add") }</button>
}
</div>
}
templ alertGroupsSettings(v ChannelsView) {
<section id="alert-groups" class="mb-12">
@sectionHeading("alert_groups.title")
<p class="mb-6 max-w-2xl text-sm text-muted">{ i18n.T(ctx, "alert_groups.intro") }</p>
<datalist id="quiet-time-zones">
for _, tz := range commonTimeZones {
<option value={ tz }></option>
}
</datalist>
<div class="grid gap-6 md:grid-cols-2">
for _, g := range v.AlertGroups {
<form data-api={ "/org/alert-groups/" + g.ID } data-method="put" data-next="/settings/channels" class="border border-rule p-4">
@formError()
if g.Default || g.QuietNow {
<div class="mb-3 flex flex-wrap items-center gap-3 text-xs">
if g.Default {
<span class="text-muted">{ i18n.T(ctx, "alert_groups.default") }</span>
}
if g.QuietNow {
<span class="font-mono text-maintenance">{ i18n.T(ctx, "alert_groups.quiet_active") }</span>
}
</div>
}
<fieldset disabled?={ !v.CanWrite }>
@valueField(i18n.T(ctx, "sso.field.name"), "name", g.Name, "", true)
@alertGroupOptions("channel_ids", g.Channels)
if len(g.Channels) == 0 {
<p class="mb-4 text-sm text-muted">{ i18n.T(ctx, "alert_groups.no_channels") }</p>
}
@alertGroupQuietHours(g.QuietHours, v.CanWrite)
if v.CanWrite {
@submit(i18n.T(ctx, "alert_groups.save"))
}
</fieldset>
if v.CanWrite && !g.Default {
<button type="button" data-delete={ "/org/alert-groups/" + g.ID } data-confirm={ i18n.T(ctx, "alert_groups.delete_confirm") } class="mt-3 text-sm text-muted hover:text-down">{ i18n.T(ctx, "regions.delete") }</button>
}
</form>
}
if v.CanWrite {
<form data-api="/org/alert-groups" data-next="/settings/channels" class="border border-rule p-4">
@sectionHeading("alert_groups.new")
@formError()
@valueField(i18n.T(ctx, "sso.field.name"), "name", "", "", true)
<input type="hidden" name="channel_ids" data-multi/>
for _, ch := range v.Channels {
<label class="mb-2 flex items-center gap-2 text-sm">
<input type="checkbox" name="channel_ids" value={ ch.ID } data-multi/>
<span>{ ch.Name }</span>
</label>
}
@alertGroupQuietHours(nil, true)
@submit(i18n.T(ctx, "alert_groups.create"))
</form>
}
</div>
</section>
}