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:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-22 09:29:34 +01:00
1 parent b210c8e5c0
commit 273a958d76
11 files changed
+1419 -810

No files matched your search

+79 -19
View File
@@ -54,7 +54,7 @@ func (s *Server) dashboard(c *gin.Context) {
}
v := web.DashboardView{Counts: map[web.Status]int{}, Empty: true}
if p != nil && me.Policy == "" {
data, err := s.loadMonitorData(c.Request.Context(), p)
data, err := s.loadMonitorData(c.Request.Context(), p, web.RangeByKey(c.Query("range")))
if err != nil {
internalError(c, err)
return
@@ -62,6 +62,8 @@ func (s *Server) dashboard(c *gin.Context) {
v.Root = buildGroupNodes(data.tree, data.rows)
v.Empty = len(data.rows) == 0 && len(v.Root.Children) == 0
v.CanWrite = writableAnywhere(p, data.tree)
v.Range = data.rng.Key
v.Ranges = rangeOptions(c, data.rng)
for _, m := range data.rows {
v.Counts[m.Status]++
}
@@ -70,24 +72,63 @@ func (s *Server) dashboard(c *gin.Context) {
}
type monitorData struct {
tree groupTree
rows []web.MonitorRow
byID map[uuid.UUID]store.Monitor
hourly map[uuid.UUID][]web.Bucket
tree groupTree
rows []web.MonitorRow
byID map[uuid.UUID]store.Monitor
// buckets cover the selected range at its resolution.
buckets map[uuid.UUID][]web.Bucket
down map[uuid.UUID][]web.Span
regionOf map[uuid.UUID]store.Region
rng web.TimeRange
now time.Time
}
// rangeBuckets loads the selected range for all monitors, or one if
// monitor is set.
func rangeBuckets(ctx context.Context, q *store.Queries, org uuid.UUID, monitor *uuid.UUID, rng web.TimeRange, now time.Time) (map[uuid.UUID][]web.Bucket, error) {
out := map[uuid.UUID][]web.Bucket{}
since := now.Add(-rng.Window).Truncate(rng.Bucket)
width := fmt.Sprintf("%d seconds", int(rng.Bucket.Seconds()))
if rng.Hourly {
rows, err := q.RangeBucketsHourly(ctx, store.RangeBucketsHourlyParams{OrgID: org, Width: width, Since: since, MonitorID: monitor})
for _, r := range rows {
out[r.MonitorID] = append(out[r.MonitorID], web.Bucket{Start: r.Bucket, Total: r.Total, OK: r.OkCount, Latency: r.AvgLatencyMs})
}
return out, err
}
rows, err := q.RangeBuckets(ctx, store.RangeBucketsParams{OrgID: org, Width: width, Since: since, MonitorID: monitor})
for _, r := range rows {
out[r.MonitorID] = append(out[r.MonitorID], web.Bucket{Start: r.Bucket, Total: r.Total, OK: r.OkCount, Latency: r.AvgLatencyMs})
}
return out, err
}
// slaFor returns m's SLA over the window ending now, not counting time
// before the monitor existed.
func (d monitorData) slaFor(m store.Monitor, window time.Duration) float64 {
from := d.now.Add(-window)
if m.CreatedAt.After(from) {
from = m.CreatedAt
}
return web.SLA(d.down[m.ID], from, d.now)
}
// loadMonitorData assembles everything the monitor list shows.
func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData, error) {
d := monitorData{byID: map[uuid.UUID]store.Monitor{}, hourly: map[uuid.UUID][]web.Bucket{}, regionOf: map[uuid.UUID]store.Region{}}
// loadMonitorData assembles everything the monitor list shows for rng.
// Incidents are loaded far enough back for every SLA window.
func (s *Server) loadMonitorData(ctx context.Context, p *principal, rng web.TimeRange) (monitorData, error) {
now := time.Now()
d := monitorData{
byID: map[uuid.UUID]store.Monitor{}, down: map[uuid.UUID][]web.Span{},
regionOf: map[uuid.UUID]store.Region{}, rng: rng, now: now,
}
var (
monitors []store.Monitor
assigned []store.AllMonitorRegionsRow
regions []store.Region
latest []store.LatestResultsRow
open []store.Incident
hourly []store.HourlyBucketsRow
spans []store.IncidentsSinceRow
)
var groups []store.MonitorGroup
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
@@ -110,13 +151,23 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData
if open, err = q.OpenIncidents(ctx); err != nil {
return err
}
hourly, err = q.HourlyBuckets(ctx, store.HourlyBucketsParams{OrgID: p.Org.ID, Bucket: now.Add(-24 * time.Hour).Truncate(time.Hour)})
if spans, err = q.IncidentsSince(ctx, ptr(now.Add(-web.SLAWindows[len(web.SLAWindows)-1].Window))); err != nil {
return err
}
d.buckets, err = rangeBuckets(ctx, q, p.Org.ID, nil, rng, now)
return err
})
if err != nil {
return d, err
}
d.tree = newGroupTree(groups)
for _, sp := range spans {
span := web.Span{Start: sp.StartedAt}
if sp.ResolvedAt != nil {
span.End = *sp.ResolvedAt
}
d.down[sp.MonitorID] = append(d.down[sp.MonitorID], span)
}
for _, r := range regions {
d.regionOf[r.ID] = r
}
@@ -135,9 +186,6 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData
for _, i := range open {
down[i.MonitorID] = true
}
for _, h := range hourly {
d.hourly[h.MonitorID] = append(d.hourly[h.MonitorID], web.Bucket{Start: h.Bucket, Total: h.Total, OK: h.OkCount, Latency: h.AvgLatencyMs})
}
for _, m := range monitors {
if !canInGroup(p, authz.MonitorsRead, d.tree, m.GroupID) {
continue
@@ -187,10 +235,8 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal) (monitorData
}
row.Latency = fmt.Sprintf("%d ms", sum/int32(len(latencies)))
}
if u := web.Uptime(d.hourly[m.ID]); u >= 0 {
row.Uptime = web.Percent(u)
}
row.Bars = web.UptimeBars(d.hourly[m.ID], now, time.Hour, 24, "15:04")
row.Uptime = web.SLAText(d.slaFor(m, rng.Window))
row.Bars = web.UptimeBars(d.buckets[m.ID], now, rng.Bucket, rng.Bars(), rng.Layout)
d.rows = append(d.rows, row)
}
// Problems first, then by name.
@@ -212,7 +258,7 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
return notFoundPage(c)
}
ctx := c.Request.Context()
d, err := s.loadMonitorData(ctx, p)
d, err := s.loadMonitorData(ctx, p, web.RangeByKey(c.Query("range")))
if err != nil {
return nil, err
}
@@ -224,7 +270,9 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
v := web.MonitorDetailView{
Interval: (time.Duration(m.IntervalSeconds) * time.Second).String(),
CanWrite: canInGroup(p, authz.MonitorsWrite, d.tree, m.GroupID),
Latency: web.Latency(d.hourly[id], 480, 96),
Latency: web.Latency(d.buckets[id], d.now.Add(-d.rng.Window).Truncate(d.rng.Bucket), d.now, 480, 96),
Range: d.rng.Key,
Ranges: rangeOptions(c, d.rng),
Uptime90: "–",
}
for _, r := range d.rows {
@@ -257,6 +305,9 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
buckets[i] = web.Bucket{Start: b.Bucket, Total: b.Total, OK: b.OkCount}
}
v.Daily = web.UptimeBars(buckets, now, 24*time.Hour, 90, "2006-01-02")
for _, w := range web.SLAWindows {
v.SLAs = append(v.SLAs, web.SLAEntry{Label: w.Key, Value: web.SLAText(d.slaFor(m, w.Window))})
}
if u := web.Uptime(buckets); u >= 0 {
v.Uptime90 = web.Percent(u)
}
@@ -427,7 +478,7 @@ func (s *Server) incidentsView(c *gin.Context, p *principal, page web.Page, me w
if err != nil {
return nil, err
}
d, err := s.loadMonitorData(ctx, p)
d, err := s.loadMonitorData(ctx, p, web.RangeByKey(""))
if err != nil {
return nil, err
}
@@ -633,3 +684,12 @@ func (s *Server) settingsIndex(c *gin.Context) {
}
web.Render(c, http.StatusOK, web.SettingsIndex(s.pageFor(c, "console.nav.settings"), me))
}
// rangeOptions builds the range switcher, keeping the current path.
func rangeOptions(c *gin.Context, active web.TimeRange) []web.RangeOption {
out := make([]web.RangeOption, len(web.Ranges))
for i, r := range web.Ranges {
out[i] = web.RangeOption{Key: r.Key, Active: r.Key == active.Key, URL: c.Request.URL.Path + "?range=" + r.Key}
}
return out
}