feat(web): switch charts between 1h, 3h, 12h, 24h and 7d with matching SLA
Bars use a resolution fitting the range and the latency line is placed by time. The SLA is the share of time a monitor was not down, counted from incidents and only since the monitor exists. Monitor pages list the SLA from one hour to 90 days. Closes #59 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
b210c8e5c0
commit
273a958d76
11 files changed
+1419
-810
No files matched your search
+13
-5
@@ -98,20 +98,28 @@ type LatencyLine struct {
|
||||
Height int
|
||||
}
|
||||
|
||||
// Latency plots the average latency of buckets, oldest first.
|
||||
func Latency(buckets []Bucket, width, height int) LatencyLine {
|
||||
// Latency plots the average latency of buckets, oldest first, placing each
|
||||
// point at its time within [from, to] so the line lines up with the bars.
|
||||
// Buckets without results are skipped.
|
||||
func Latency(buckets []Bucket, from, to time.Time, width, height int) LatencyLine {
|
||||
l := LatencyLine{Width: width, Height: height}
|
||||
var max float32
|
||||
var points []Bucket
|
||||
for _, b := range buckets {
|
||||
if b.Total == 0 || b.Start.Before(from) {
|
||||
continue
|
||||
}
|
||||
points = append(points, b)
|
||||
max = maxf(max, b.Latency)
|
||||
}
|
||||
if len(buckets) < 2 || max == 0 {
|
||||
span := to.Sub(from)
|
||||
if len(points) < 2 || max == 0 || span <= 0 {
|
||||
return l
|
||||
}
|
||||
l.MaxMS = int(max)
|
||||
var sb strings.Builder
|
||||
for i, b := range buckets {
|
||||
x := float64(i) * float64(width) / float64(len(buckets)-1)
|
||||
for _, b := range points {
|
||||
x := float64(b.Start.Sub(from)) / float64(span) * float64(width)
|
||||
y := float64(height) - float64(b.Latency/max)*float64(height-2) - 1
|
||||
fmt.Fprintf(&sb, "%.1f,%.1f ", x, y)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -31,3 +32,15 @@ func TestPercent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLatencyPlacesPointsByTime(t *testing.T) {
|
||||
from := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
to := from.Add(10 * time.Minute)
|
||||
l := Latency([]Bucket{
|
||||
{Start: from.Add(5 * time.Minute), Total: 1, Latency: 100},
|
||||
{Start: from.Add(10 * time.Minute), Total: 1, Latency: 50},
|
||||
}, from, to, 100, 10)
|
||||
if !strings.HasPrefix(l.Points, "50.0,") || !strings.Contains(l.Points, " 100.0,") {
|
||||
t.Errorf("Latency().Points = %q, want points at x=50 and x=100", l.Points)
|
||||
}
|
||||
}
|
||||
+64
-17
@@ -51,6 +51,33 @@ type DashboardView struct {
|
||||
Empty bool
|
||||
CanWrite bool
|
||||
Counts map[Status]int
|
||||
Range string
|
||||
Ranges []RangeOption
|
||||
}
|
||||
|
||||
// RangeOption is an entry of the range switcher.
|
||||
type RangeOption struct {
|
||||
Key string
|
||||
Active bool
|
||||
URL string
|
||||
}
|
||||
|
||||
// SLAEntry is one window of the SLA table.
|
||||
type SLAEntry struct {
|
||||
Label string
|
||||
Value string
|
||||
}
|
||||
|
||||
templ rangeSwitch(opts []RangeOption) {
|
||||
<nav class="flex border border-rule font-mono text-xs" aria-label={ i18n.T(ctx, "monitors.range") }>
|
||||
for _, o := range opts {
|
||||
if o.Active {
|
||||
<span class="bg-ink px-2 py-1 text-paper" aria-current="true">{ o.Key }</span>
|
||||
} else {
|
||||
<a href={ templ.SafeURL(o.URL) } class="px-2 py-1 text-muted hover:text-ink">{ o.Key }</a>
|
||||
}
|
||||
}
|
||||
</nav>
|
||||
}
|
||||
|
||||
templ statusDot(s Status) {
|
||||
@@ -96,6 +123,7 @@ templ Dashboard(p Page, me Me, v DashboardView) {
|
||||
<div class="mb-6 flex items-baseline justify-between gap-4">
|
||||
<h1 class="text-lg font-semibold">{ i18n.T(ctx, "console.monitors.title") }</h1>
|
||||
<div class="flex items-center gap-5 font-mono text-xs text-muted">
|
||||
@rangeSwitch(v.Ranges)
|
||||
for _, s := range []Status{StatusUp, StatusDegraded, StatusDown, StatusPaused} {
|
||||
if v.Counts[s] > 0 {
|
||||
<span class="flex items-center gap-1.5">
|
||||
@@ -118,7 +146,7 @@ templ Dashboard(p Page, me Me, v DashboardView) {
|
||||
@groupBlock(g)
|
||||
}
|
||||
</div>
|
||||
<p class="mt-3 text-right text-xs text-muted">{ i18n.T(ctx, "monitors.last_24h") }</p>
|
||||
<p class="mt-3 text-right text-xs text-muted">{ i18n.T(ctx, "monitors.range_note", "Range", v.Range) }</p>
|
||||
}
|
||||
<p class="mt-8 text-sm"><a href="/groups" class="text-muted hover:text-ink">{ i18n.T(ctx, "groups.manage") }</a></p>
|
||||
}
|
||||
@@ -188,6 +216,9 @@ type IncidentRow struct {
|
||||
// MonitorDetailView is the data of a monitor page.
|
||||
type MonitorDetailView struct {
|
||||
Row MonitorRow
|
||||
Range string
|
||||
Ranges []RangeOption
|
||||
SLAs []SLAEntry
|
||||
Interval string
|
||||
Daily []Bar
|
||||
Uptime90 string
|
||||
@@ -212,6 +243,37 @@ templ MonitorDetail(p Page, me Me, v MonitorDetailView) {
|
||||
<a href={ templ.SafeURL("/monitors/" + v.Row.ID + "/edit") } class="border border-ink px-3 py-1.5 text-sm hover:bg-ink hover:text-paper">{ i18n.T(ctx, "sso.action.edit") }</a>
|
||||
}
|
||||
</div>
|
||||
<section class="mb-10">
|
||||
<div class="mb-3 flex items-center justify-between gap-4">
|
||||
<h2 class="text-xs font-medium uppercase tracking-wider text-muted">{ i18n.T(ctx, "monitors.history", "Range", v.Range) }</h2>
|
||||
@rangeSwitch(v.Ranges)
|
||||
</div>
|
||||
<div class="mb-1 flex items-baseline justify-between font-mono text-xs text-muted">
|
||||
<span>{ i18n.T(ctx, "monitors.sla") }</span>
|
||||
<span class="text-sm text-ink">{ v.Row.Uptime }</span>
|
||||
</div>
|
||||
@bars(v.Row.Bars, 28)
|
||||
if v.Latency.Points != "" {
|
||||
<div class="mt-4 mb-1 flex justify-between font-mono text-xs text-muted">
|
||||
<span>{ i18n.T(ctx, "monitors.latency") }</span>
|
||||
<span>max { strconv.Itoa(v.Latency.MaxMS) } ms</span>
|
||||
</div>
|
||||
<svg viewBox={ "0 0 " + strconv.Itoa(v.Latency.Width) + " " + strconv.Itoa(v.Latency.Height) } preserveAspectRatio="none" class="block h-24 w-full border-b border-rule">
|
||||
<polyline points={ v.Latency.Points } fill="none" class="stroke-ink" stroke-width="1.5" vector-effect="non-scaling-stroke"></polyline>
|
||||
</svg>
|
||||
}
|
||||
</section>
|
||||
<section class="mb-10">
|
||||
@sectionHeading("monitors.sla_table")
|
||||
<dl class="grid grid-cols-3 gap-px border border-rule bg-rule sm:grid-cols-7">
|
||||
for _, e := range v.SLAs {
|
||||
<div class="bg-paper px-3 py-2">
|
||||
<dt class="font-mono text-xs text-muted">{ e.Label }</dt>
|
||||
<dd class="font-mono text-sm">{ e.Value }</dd>
|
||||
</div>
|
||||
}
|
||||
</dl>
|
||||
</section>
|
||||
<section class="mb-10">
|
||||
<div class="mb-2 flex items-baseline justify-between">
|
||||
@sectionHeading("monitors.last_90d")
|
||||
@@ -219,7 +281,7 @@ templ MonitorDetail(p Page, me Me, v MonitorDetailView) {
|
||||
</div>
|
||||
@bars(v.Daily, 28)
|
||||
</section>
|
||||
<div class="mb-10 grid gap-10 md:grid-cols-2">
|
||||
<div class="mb-10">
|
||||
<section>
|
||||
@sectionHeading("monitors.regions")
|
||||
<ul class="divide-y divide-rule border-y border-rule text-sm">
|
||||
@@ -232,21 +294,6 @@ templ MonitorDetail(p Page, me Me, v MonitorDetailView) {
|
||||
}
|
||||
</ul>
|
||||
</section>
|
||||
<section>
|
||||
<div class="mb-4 flex items-baseline justify-between">
|
||||
@sectionHeading("monitors.latency_24h")
|
||||
if v.Latency.MaxMS > 0 {
|
||||
<span class="font-mono text-xs text-muted">max { strconv.Itoa(v.Latency.MaxMS) } ms</span>
|
||||
}
|
||||
</div>
|
||||
if v.Latency.Points != "" {
|
||||
<svg viewBox={ "0 0 " + strconv.Itoa(v.Latency.Width) + " " + strconv.Itoa(v.Latency.Height) } preserveAspectRatio="none" class="block h-24 w-full border-b border-rule">
|
||||
<polyline points={ v.Latency.Points } fill="none" class="stroke-ink" stroke-width="1.5" vector-effect="non-scaling-stroke"></polyline>
|
||||
</svg>
|
||||
} else {
|
||||
<p class="text-sm text-muted">–</p>
|
||||
}
|
||||
</section>
|
||||
</div>
|
||||
if len(v.Incidents) > 0 {
|
||||
<section class="mb-10">
|
||||
|
||||
+970
-765
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,15 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// trimPercent cuts v to digits decimals instead of rounding, so 99.9996
|
||||
// never reads as a perfect 100. The epsilon absorbs float noise such as
|
||||
// 99.12 being stored as 99.1199999.
|
||||
func trimPercent(v float64, digits int) string {
|
||||
scale := math.Pow(10, float64(digits))
|
||||
v = math.Floor(v*scale+1e-6) / scale
|
||||
return strconv.FormatFloat(v, 'f', digits, 64) + "%"
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeRange is a selectable chart window. Bars use Bucket as resolution;
|
||||
// Hourly ranges are read from the hourly aggregate instead of raw results.
|
||||
type TimeRange struct {
|
||||
Key string
|
||||
Window time.Duration
|
||||
Bucket time.Duration
|
||||
Hourly bool
|
||||
// Layout formats bar titles.
|
||||
Layout string
|
||||
}
|
||||
|
||||
// Ranges are offered on the dashboard and monitor pages, shortest first.
|
||||
var Ranges = []TimeRange{
|
||||
{Key: "1h", Window: time.Hour, Bucket: time.Minute, Layout: "15:04"},
|
||||
{Key: "3h", Window: 3 * time.Hour, Bucket: 5 * time.Minute, Layout: "15:04"},
|
||||
{Key: "12h", Window: 12 * time.Hour, Bucket: 15 * time.Minute, Layout: "15:04"},
|
||||
{Key: "24h", Window: 24 * time.Hour, Bucket: 30 * time.Minute, Layout: "01-02 15:04"},
|
||||
{Key: "7d", Window: 7 * 24 * time.Hour, Bucket: 4 * time.Hour, Hourly: true, Layout: "01-02 15:04"},
|
||||
}
|
||||
|
||||
// SLAWindows are listed on monitor pages.
|
||||
var SLAWindows = []struct {
|
||||
Key string
|
||||
Window time.Duration
|
||||
}{
|
||||
{"1h", time.Hour}, {"3h", 3 * time.Hour}, {"12h", 12 * time.Hour}, {"24h", 24 * time.Hour},
|
||||
{"7d", 7 * 24 * time.Hour}, {"30d", 30 * 24 * time.Hour}, {"90d", 90 * 24 * time.Hour},
|
||||
}
|
||||
|
||||
// DefaultRange is used when the request names none.
|
||||
const DefaultRange = "24h"
|
||||
|
||||
// RangeByKey returns the range for key, falling back to the default.
|
||||
func RangeByKey(key string) TimeRange {
|
||||
if i := slices.IndexFunc(Ranges, func(r TimeRange) bool { return r.Key == key }); i >= 0 {
|
||||
return Ranges[i]
|
||||
}
|
||||
return RangeByKey(DefaultRange)
|
||||
}
|
||||
|
||||
// Bars returns the number of bars r shows.
|
||||
func (r TimeRange) Bars() int { return int(r.Window / r.Bucket) }
|
||||
|
||||
// Span is a period during which a monitor was down; End is zero while it
|
||||
// still is.
|
||||
type Span struct {
|
||||
Start, End time.Time
|
||||
}
|
||||
|
||||
// SLA returns the fraction of [from, to] not covered by down spans, or -1
|
||||
// if the window is empty, e.g. because the monitor did not exist yet.
|
||||
// Overlapping spans count once.
|
||||
func SLA(down []Span, from, to time.Time) float64 {
|
||||
if !to.After(from) {
|
||||
return -1
|
||||
}
|
||||
clipped := make([]Span, 0, len(down))
|
||||
for _, s := range down {
|
||||
end := s.End
|
||||
if end.IsZero() || end.After(to) {
|
||||
end = to
|
||||
}
|
||||
start := s.Start
|
||||
if start.Before(from) {
|
||||
start = from
|
||||
}
|
||||
if end.After(start) {
|
||||
clipped = append(clipped, Span{start, end})
|
||||
}
|
||||
}
|
||||
slices.SortFunc(clipped, func(a, b Span) int { return a.Start.Compare(b.Start) })
|
||||
var downtime time.Duration
|
||||
var cur Span
|
||||
for i, s := range clipped {
|
||||
switch {
|
||||
case i == 0:
|
||||
cur = s
|
||||
case !s.Start.After(cur.End):
|
||||
if s.End.After(cur.End) {
|
||||
cur.End = s.End
|
||||
}
|
||||
default:
|
||||
downtime += cur.End.Sub(cur.Start)
|
||||
cur = s
|
||||
}
|
||||
}
|
||||
if len(clipped) > 0 {
|
||||
downtime += cur.End.Sub(cur.Start)
|
||||
}
|
||||
return 1 - float64(downtime)/float64(to.Sub(from))
|
||||
}
|
||||
|
||||
// SLAText formats an SLA for display.
|
||||
func SLAText(r float64) string {
|
||||
if r < 0 {
|
||||
return "–"
|
||||
}
|
||||
return PercentPrecise(r)
|
||||
}
|
||||
|
||||
// PercentPrecise shows enough digits for SLA targets such as 99.95%.
|
||||
func PercentPrecise(r float64) string {
|
||||
switch {
|
||||
case r >= 1:
|
||||
return "100%"
|
||||
case r >= 0.999:
|
||||
return trimPercent(r*100, 3)
|
||||
case r >= 0.99:
|
||||
return trimPercent(r*100, 2)
|
||||
}
|
||||
return trimPercent(r*100, 1)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user