Monitors and groups get one-off or recurring maintenance. While it is in progress, failures neither open incidents nor alert, so the SLA is not affected; successes still resolve earlier incidents. The console shows monitors under maintenance and lists windows on a new page and on monitor pages. Status pages announce maintenance in progress and planned for the next two weeks and mark affected components; the occurrences are part of the snapshot and evaluated when the page is shown, so this also works from cache during an API outage. Maintenance and status page descriptions are rendered as Markdown. Closes #71 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
139 lines
3.3 KiB
Go
139 lines
3.3 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"
|
||
// StatusMaintenance covers planned maintenance, during which failures
|
||
// are expected.
|
||
StatusMaintenance Status = "maintenance"
|
||
)
|
||
|
||
// 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
|
||
}
|