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
136 lines
3.1 KiB
Go
136 lines
3.1 KiB
Go
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[AlignBucket(b.Start, step).Unix()] = b
|
||
}
|
||
start := AlignBucket(end, 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
|
||
}
|