Files
uptimesebi/internal/web/charts.go
T
Sebastian UnterschützandClaude Opus 5.5 b831478143 feat(web): console pages for monitors, incidents and notification channels
The dashboard lists monitors with status, latency and 24-hour uptime
bars rendered as SVG on the server; monitor pages add 90-day uptime,
latency and recent checks; an editor shows the settings of the selected
check type.

Closes #17
Closes #19

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 01:50:04 +01:00

128 lines
2.8 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.
func Latency(buckets []Bucket, width, height int) LatencyLine {
l := LatencyLine{Width: width, Height: height}
var max float32
for _, b := range buckets {
max = maxf(max, b.Latency)
}
if len(buckets) < 2 || max == 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)
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
}