Ranges grow to 30d, 90d, 180d and 1y, read from the aggregates, and the SLA table to 180d and 1y. Ranges and SLA windows less than half covered by data are shown disabled. Monitor pages chart each region separately with its share of successful checks. Bars align to TimescaleDB's bucket origin so multi-day buckets match. Closes #61 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
77 lines
2.6 KiB
Go
77 lines
2.6 KiB
Go
package web
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSLA(t *testing.T) {
|
|
t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
at := func(m int) time.Time { return t0.Add(time.Duration(m) * time.Minute) }
|
|
tests := []struct {
|
|
name string
|
|
down []Span
|
|
from, to time.Time
|
|
want float64
|
|
}{
|
|
{"no incidents", nil, at(0), at(60), 1},
|
|
{"six minutes down", []Span{{at(10), at(16)}}, at(0), at(60), 0.9},
|
|
{"clipped at window start", []Span{{at(-30), at(6)}}, at(0), at(60), 0.9},
|
|
{"ongoing", []Span{{at(54), time.Time{}}}, at(0), at(60), 0.9},
|
|
{"overlapping counted once", []Span{{at(10), at(20)}, {at(15), at(22)}}, at(0), at(60), 0.8},
|
|
{"outside window", []Span{{at(70), at(80)}}, at(0), at(60), 1},
|
|
{"empty window", nil, at(60), at(60), -1},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := SLA(tt.down, tt.from, tt.to); got < tt.want-1e-9 || got > tt.want+1e-9 {
|
|
t.Errorf("%s: SLA() = %v, want %v", tt.name, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPercentPrecise(t *testing.T) {
|
|
for r, want := range map[float64]string{1: "100%", 0.999996: "99.999%", 0.9995: "99.950%", 0.9912: "99.12%", 0.5: "50.0%"} {
|
|
if got := PercentPrecise(r); got != want {
|
|
t.Errorf("PercentPrecise(%v) = %q, want %q", r, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRanges(t *testing.T) {
|
|
for _, r := range Ranges {
|
|
if n := r.Bars(); n < 24 || n > 72 {
|
|
t.Errorf("range %s has %d bars, want a readable number", r.Key, n)
|
|
}
|
|
}
|
|
if RangeByKey("bogus").Key != DefaultRange {
|
|
t.Error("unknown range does not fall back to the default")
|
|
}
|
|
}
|
|
|
|
func TestAlignBucketMatchesTimescale(t *testing.T) {
|
|
// Reference values from TimescaleDB:
|
|
// time_bucket('2 days', '2026-01-02 12:00Z') = 2026-01-02 00:00Z,
|
|
// time_bucket('7 days', '2026-01-02 12:00Z') = 2025-12-29 00:00Z.
|
|
ts := time.Date(2026, 1, 2, 12, 0, 0, 0, time.UTC)
|
|
if got, want := AlignBucket(ts, 48*time.Hour), time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC); !got.Equal(want) {
|
|
t.Errorf("AlignBucket(2 days) = %v, want %v", got, want)
|
|
}
|
|
if got, want := AlignBucket(ts, 7*24*time.Hour), time.Date(2025, 12, 29, 0, 0, 0, 0, time.UTC); !got.Equal(want) {
|
|
t.Errorf("AlignBucket(7 days) = %v, want %v", got, want)
|
|
}
|
|
h := time.Date(2026, 1, 2, 12, 34, 0, 0, time.UTC)
|
|
if got := AlignBucket(h, time.Hour); !got.Equal(h.Truncate(time.Hour)) {
|
|
t.Errorf("AlignBucket(1h) = %v, want %v", got, h.Truncate(time.Hour))
|
|
}
|
|
}
|
|
|
|
func TestHasEnoughData(t *testing.T) {
|
|
now := time.Now()
|
|
if HasEnoughData(time.Time{}, now, time.Hour) {
|
|
t.Error("no data counts as enough")
|
|
}
|
|
if !HasEnoughData(now.Add(-31*time.Minute), now, time.Hour) || HasEnoughData(now.Add(-29*time.Minute), now, time.Hour) {
|
|
t.Error("half-window threshold wrong for 1h")
|
|
}
|
|
}
|