Files
uptimesebi/internal/web/charts.go
T
Sebastian UnterschützandClaude Opus 5.5 273a958d76 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
2026-11-22 09:29:34 +01:00

136 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package web
import (
"fmt"
"strings"
"time"
)
// Status is how a monitor, region or time bucket fared. The values double
// as CSS class suffixes.
type Status string
// Statuses.
const (
StatusUp Status = "up"
StatusDegraded Status = "degraded"
StatusDown Status = "down"
StatusUnknown Status = "unknown"
StatusPaused Status = "paused"
)
// Bar is one bucket of an uptime bar chart.
type Bar struct {
Status Status
Title string
}
// Bucket is the raw data behind one bar.
type Bucket struct {
Start time.Time
Total int64
OK int64
Latency float32
}
// UptimeBars turns buckets into n bars of length step ending at end; gaps
// become unknown bars. layout formats bar titles.
func UptimeBars(buckets []Bucket, end time.Time, step time.Duration, n int, layout string) []Bar {
byStart := make(map[int64]Bucket, len(buckets))
for _, b := range buckets {
byStart[b.Start.Truncate(step).Unix()] = b
}
start := end.Truncate(step).Add(-time.Duration(n-1) * step)
bars := make([]Bar, n)
for i := range bars {
t := start.Add(time.Duration(i) * step)
b, ok := byStart[t.Unix()]
if !ok || b.Total == 0 {
bars[i] = Bar{Status: StatusUnknown, Title: t.Format(layout) + " · –"}
continue
}
ratio := float64(b.OK) / float64(b.Total)
bars[i] = Bar{Status: ratioStatus(ratio), Title: t.Format(layout) + " · " + Percent(ratio)}
}
return bars
}
func ratioStatus(r float64) Status {
switch {
case r >= 0.999:
return StatusUp
case r >= 0.95:
return StatusDegraded
}
return StatusDown
}
// Percent formats an uptime ratio with the precision people expect from
// uptime figures.
func Percent(r float64) string {
switch {
case r >= 1:
return "100%"
case r >= 0.99:
return fmt.Sprintf("%.2f%%", r*100)
}
return fmt.Sprintf("%.1f%%", r*100)
}
// Uptime returns the overall ratio of buckets, or -1 without data.
func Uptime(buckets []Bucket) float64 {
var total, ok int64
for _, b := range buckets {
total += b.Total
ok += b.OK
}
if total == 0 {
return -1
}
return float64(ok) / float64(total)
}
// LatencyLine is a polyline for an SVG of the given size.
type LatencyLine struct {
Points string
MaxMS int
Width int
Height int
}
// 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)
}
span := to.Sub(from)
if len(points) < 2 || max == 0 || span <= 0 {
return l
}
l.MaxMS = int(max)
var sb strings.Builder
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)
}
l.Points = strings.TrimSpace(sb.String())
return l
}
func maxf(a, b float32) float32 {
if a > b {
return a
}
return b
}