GET /api/v1/devices/{slug}/overview (traffic, ports, availability, rtt, events);
metrics/graph gains aggregate=avg|sum|max when component is omitted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
146 lines
3.9 KiB
Go
146 lines
3.9 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/v10networks/nmon/internal/store"
|
|
)
|
|
|
|
const (
|
|
adminDown = 2
|
|
operUp = 1
|
|
overviewEvents = 20
|
|
year = 365 * 24 * time.Hour
|
|
)
|
|
|
|
// availabilityPeriods are the keys of the availability block and their lengths.
|
|
var availabilityPeriods = []time.Duration{24 * time.Hour, 7 * 24 * time.Hour, 30 * 24 * time.Hour, year}
|
|
|
|
type span struct{ from, to time.Time }
|
|
|
|
// downtime sums the time inside [from, to] covered by the incidents; overlaps count once.
|
|
func downtime(incidents []span, from, to time.Time) time.Duration {
|
|
clipped := make([]span, 0, len(incidents))
|
|
for _, s := range incidents {
|
|
s.from, s.to = laterOf(s.from, from), earlierOf(s.to, to)
|
|
if s.from.Before(s.to) {
|
|
clipped = append(clipped, s)
|
|
}
|
|
}
|
|
sort.Slice(clipped, func(i, j int) bool { return clipped[i].from.Before(clipped[j].from) })
|
|
var total time.Duration
|
|
var end time.Time
|
|
for _, s := range clipped {
|
|
if s.from.Before(end) {
|
|
s.from = end
|
|
}
|
|
if s.to.After(s.from) {
|
|
total += s.to.Sub(s.from)
|
|
end = s.to
|
|
}
|
|
}
|
|
return total
|
|
}
|
|
|
|
func laterOf(a, b time.Time) time.Time {
|
|
if a.After(b) {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
func earlierOf(a, b time.Time) time.Time {
|
|
if a.Before(b) {
|
|
return a
|
|
}
|
|
return b
|
|
}
|
|
|
|
// availabilityPct is 100 x (1 - downtime/period) over the period ending at now, counted from the device's creation
|
|
// if that is later; nil when the device has existed for no time.
|
|
func availabilityPct(incidents []span, now, created time.Time, period time.Duration) *float64 {
|
|
start := laterOf(now.Add(-period), created)
|
|
if !start.Before(now) {
|
|
return nil
|
|
}
|
|
v := 100 * (1 - float64(downtime(incidents, start, now))/float64(now.Sub(start)))
|
|
return &v
|
|
}
|
|
|
|
// incidentSpans turns device_down rows into spans; an open row lasts until now.
|
|
func incidentSpans(rows []store.Alert, now time.Time) []span {
|
|
out := make([]span, 0, len(rows))
|
|
for _, a := range rows {
|
|
if a.FiredAt == nil {
|
|
continue
|
|
}
|
|
end := now
|
|
if a.ClearedAt != nil {
|
|
end = *a.ClearedAt
|
|
}
|
|
out = append(out, span{from: *a.FiredAt, to: end})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func availabilityOf(rows []store.Alert, now, created time.Time) OverviewAvailability {
|
|
sp := incidentSpans(rows, now)
|
|
at := func(i int) *float64 { return availabilityPct(sp, now, created, availabilityPeriods[i]) }
|
|
return OverviewAvailability{Day: at(0), Week: at(1), Month: at(2), Year: at(3)}
|
|
}
|
|
|
|
// portCounts classifies the ports that count: admin down first, then oper up, else down.
|
|
func portCounts(ifs []store.Interface) OverviewPorts {
|
|
var p OverviewPorts
|
|
for _, i := range ifs {
|
|
if i.RetiredAt != nil || i.Excluded {
|
|
continue
|
|
}
|
|
p.Total++
|
|
switch {
|
|
case i.AdminStatus == adminDown:
|
|
p.AdminDown++
|
|
case i.OperStatus == operUp:
|
|
p.Up++
|
|
default:
|
|
p.Down++
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
// mergeEvents turns alerts (a fired and a resolved line each) and audit entries into the newest-first list.
|
|
func mergeEvents(alerts []store.Alert, audit []store.AuditEntry) []OverviewEvent {
|
|
ev := []OverviewEvent{}
|
|
for _, a := range alerts {
|
|
if a.FiredAt != nil {
|
|
ev = append(ev, OverviewEvent{At: a.FiredAt.UTC(), Kind: "alert", Severity: a.Severity, Summary: "Fired: " + a.Summary})
|
|
}
|
|
if a.ClearedAt != nil {
|
|
sum := closedWord(a.State) + ": " + a.Summary
|
|
ev = append(ev, OverviewEvent{At: a.ClearedAt.UTC(), Kind: "alert", Severity: a.Severity, Summary: sum})
|
|
}
|
|
}
|
|
for _, e := range audit {
|
|
ev = append(ev, OverviewEvent{At: e.At.UTC(), Kind: "audit", Summary: auditSummary(e)})
|
|
}
|
|
sort.SliceStable(ev, func(i, j int) bool { return ev[i].At.After(ev[j].At) })
|
|
return ev[:min(len(ev), overviewEvents)]
|
|
}
|
|
|
|
func closedWord(state string) string {
|
|
if state == store.AlertRetired {
|
|
return "Retired"
|
|
}
|
|
return "Resolved"
|
|
}
|
|
|
|
func auditSummary(e store.AuditEntry) string {
|
|
return fmt.Sprintf("%s by %s", e.Action, e.Actor)
|
|
}
|
|
|
|
// ifaceMatcher is a regexp alternation of interface ids, for an iface=~ matcher.
|
|
func ifaceMatcher(ids []string) string { return quote(regexAlt(ids)) }
|