ui: alerts list page + alert row partial + nav badge
This commit is contained in:
@@ -38,6 +38,68 @@ func funcMap() template.FuncMap {
|
||||
// list packs strings into a slice — handy for inline ranges
|
||||
// in templates (e.g. quick-pick cron presets).
|
||||
"list": func(items ...string) []string { return items },
|
||||
// dict builds a map[string]any from alternating key-value pairs.
|
||||
// Useful for passing multiple named values to a sub-template:
|
||||
// {{template "foo" (dict "A" $a "B" $b)}}
|
||||
"dict": func(pairs ...any) map[string]any {
|
||||
m := make(map[string]any, len(pairs)/2)
|
||||
for i := 0; i+1 < len(pairs); i += 2 {
|
||||
if k, ok := pairs[i].(string); ok {
|
||||
m[k] = pairs[i+1]
|
||||
}
|
||||
}
|
||||
return m
|
||||
},
|
||||
// mapGet retrieves a string value from a map[string]string by key.
|
||||
// Returns "" when the key is absent or the map is nil. Used by the
|
||||
// alert_row partial to resolve host_id → host name.
|
||||
"mapGet": func(m map[string]string, key *string) string {
|
||||
if m == nil || key == nil {
|
||||
return ""
|
||||
}
|
||||
return m[*key]
|
||||
},
|
||||
// alertStatus derives the display status of an alert from its DB
|
||||
// fields: "open", "acknowledged", or "resolved".
|
||||
// Accepts any value — returns "" for unrecognised input so templates
|
||||
// can still render safely.
|
||||
"alertStatus": func(resolvedAt, acknowledgedAt any) string {
|
||||
isSet := func(v any) bool {
|
||||
if v == nil {
|
||||
return false
|
||||
}
|
||||
switch t := v.(type) {
|
||||
case *time.Time:
|
||||
return t != nil
|
||||
}
|
||||
return false
|
||||
}
|
||||
if isSet(resolvedAt) {
|
||||
return "resolved"
|
||||
}
|
||||
if isSet(acknowledgedAt) {
|
||||
return "acknowledged"
|
||||
}
|
||||
return "open"
|
||||
},
|
||||
// stillHappening returns true when last_seen_at is within the last
|
||||
// 60 seconds — used to render the "still happening · Ns ago" pill
|
||||
// on alert rows where the signal is still firing.
|
||||
"stillHappening": func(v any) bool {
|
||||
var t time.Time
|
||||
switch x := v.(type) {
|
||||
case time.Time:
|
||||
t = x
|
||||
case *time.Time:
|
||||
if x == nil {
|
||||
return false
|
||||
}
|
||||
t = *x
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return time.Since(t) < 60*time.Second
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user