feat(web): add month and year ranges, data-aware range buttons and region history
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
This commit is contained in:
1 parent
24147ec31d
commit
7a085e22d0
9 files changed
+1368
-857
No files matched your search
@@ -38,9 +38,9 @@ type Bucket struct {
|
||||
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
|
||||
byStart[AlignBucket(b.Start, step).Unix()] = b
|
||||
}
|
||||
start := end.Truncate(step).Add(-time.Duration(n-1) * step)
|
||||
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)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -55,17 +56,30 @@ type DashboardView struct {
|
||||
Ranges []RangeOption
|
||||
}
|
||||
|
||||
// RangeOption is an entry of the range switcher.
|
||||
// RangeOption is an entry of the range switcher. Disabled ranges are not
|
||||
// yet covered by enough data.
|
||||
type RangeOption struct {
|
||||
Key string
|
||||
Active bool
|
||||
URL string
|
||||
Key string
|
||||
Active bool
|
||||
URL string
|
||||
Disabled bool
|
||||
}
|
||||
|
||||
// SLAEntry is one window of the SLA table.
|
||||
// SLAEntry is one window of the SLA table. Partial windows are less than
|
||||
// half covered by data.
|
||||
type SLAEntry struct {
|
||||
Label string
|
||||
Value string
|
||||
Label string
|
||||
Value string
|
||||
Partial bool
|
||||
}
|
||||
|
||||
// RegionHistory is one region's view of a monitor in the selected range.
|
||||
type RegionHistory struct {
|
||||
Name string
|
||||
Uptime string
|
||||
Bars []Bar
|
||||
// Enough is false when the region reported for less than half the range.
|
||||
Enough bool
|
||||
}
|
||||
|
||||
templ rangeSwitch(opts []RangeOption) {
|
||||
@@ -73,6 +87,8 @@ templ rangeSwitch(opts []RangeOption) {
|
||||
for _, o := range opts {
|
||||
if o.Active {
|
||||
<span class="bg-ink px-2 py-1 text-paper" aria-current="true">{ o.Key }</span>
|
||||
} else if o.Disabled {
|
||||
<span class="cursor-not-allowed px-2 py-1 text-rule" title={ i18n.T(ctx, "monitors.range_not_enough") } aria-disabled="true">{ o.Key }</span>
|
||||
} else {
|
||||
<a href={ templ.SafeURL(o.URL) } class="px-2 py-1 text-muted hover:text-ink">{ o.Key }</a>
|
||||
}
|
||||
@@ -219,6 +235,7 @@ type MonitorDetailView struct {
|
||||
Range string
|
||||
Ranges []RangeOption
|
||||
SLAs []SLAEntry
|
||||
Regions []RegionHistory
|
||||
Interval string
|
||||
Daily []Bar
|
||||
Uptime90 string
|
||||
@@ -263,11 +280,26 @@ templ MonitorDetail(p Page, me Me, v MonitorDetailView) {
|
||||
</svg>
|
||||
}
|
||||
</section>
|
||||
if len(v.Regions) > 1 {
|
||||
<section class="mb-10">
|
||||
@sectionHeading("monitors.by_region")
|
||||
<ul class="divide-y divide-rule border-y border-rule">
|
||||
for _, r := range v.Regions {
|
||||
<li class="grid grid-cols-[8rem_1fr_5rem] items-center gap-4 py-2 text-sm">
|
||||
<span class="truncate">{ r.Name }</span>
|
||||
@bars(r.Bars, 14)
|
||||
<span class={ "text-right font-mono text-xs", templ.KV("text-unknown", !r.Enough) } title={ partialTitle(ctx, !r.Enough) }>{ r.Uptime }</span>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
<p class="mt-2 text-xs text-muted">{ i18n.T(ctx, "monitors.by_region_hint") }</p>
|
||||
</section>
|
||||
}
|
||||
<section class="mb-10">
|
||||
@sectionHeading("monitors.sla_table")
|
||||
<dl class="grid grid-cols-3 gap-px border border-rule bg-rule sm:grid-cols-7">
|
||||
<dl class="grid grid-cols-3 gap-px border border-rule bg-rule sm:grid-cols-9">
|
||||
for _, e := range v.SLAs {
|
||||
<div class="bg-paper px-3 py-2">
|
||||
<div class={ "bg-paper px-3 py-2", templ.KV("text-unknown", e.Partial) } title={ partialTitle(ctx, e.Partial) }>
|
||||
<dt class="font-mono text-xs text-muted">{ e.Label }</dt>
|
||||
<dd class="font-mono text-sm">{ e.Value }</dd>
|
||||
</div>
|
||||
@@ -697,3 +729,10 @@ templ GroupsPage(p Page, me Me, v GroupsView) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func partialTitle(ctx context.Context, partial bool) string {
|
||||
if !partial {
|
||||
return ""
|
||||
}
|
||||
return i18n.T(ctx, "monitors.range_not_enough")
|
||||
}
|
||||
+1058
-832
File diff suppressed because it is too large.
Load diff
+44
-7
@@ -5,24 +5,40 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeRange is a selectable chart window. Bars use Bucket as resolution;
|
||||
// Hourly ranges are read from the hourly aggregate instead of raw results.
|
||||
// Source names where a range reads its data. Raw results are kept for 90
|
||||
// days, the aggregates much longer.
|
||||
type Source int
|
||||
|
||||
// Sources.
|
||||
const (
|
||||
SourceRaw Source = iota
|
||||
SourceHourly
|
||||
SourceDaily
|
||||
)
|
||||
|
||||
// TimeRange is a selectable chart window with Bucket as bar resolution.
|
||||
type TimeRange struct {
|
||||
Key string
|
||||
Window time.Duration
|
||||
Bucket time.Duration
|
||||
Hourly bool
|
||||
Source Source
|
||||
// Layout formats bar titles.
|
||||
Layout string
|
||||
}
|
||||
|
||||
const day = 24 * time.Hour
|
||||
|
||||
// Ranges are offered on the dashboard and monitor pages, shortest first.
|
||||
var Ranges = []TimeRange{
|
||||
{Key: "1h", Window: time.Hour, Bucket: time.Minute, Layout: "15:04"},
|
||||
{Key: "3h", Window: 3 * time.Hour, Bucket: 5 * time.Minute, Layout: "15:04"},
|
||||
{Key: "12h", Window: 12 * time.Hour, Bucket: 15 * time.Minute, Layout: "15:04"},
|
||||
{Key: "24h", Window: 24 * time.Hour, Bucket: 30 * time.Minute, Layout: "01-02 15:04"},
|
||||
{Key: "7d", Window: 7 * 24 * time.Hour, Bucket: 4 * time.Hour, Hourly: true, Layout: "01-02 15:04"},
|
||||
{Key: "24h", Window: day, Bucket: 30 * time.Minute, Layout: "01-02 15:04"},
|
||||
{Key: "7d", Window: 7 * day, Bucket: 4 * time.Hour, Source: SourceHourly, Layout: "01-02 15:04"},
|
||||
{Key: "30d", Window: 30 * day, Bucket: 12 * time.Hour, Source: SourceHourly, Layout: "2006-01-02 15:04"},
|
||||
{Key: "90d", Window: 90 * day, Bucket: 2 * day, Source: SourceDaily, Layout: "2006-01-02"},
|
||||
{Key: "180d", Window: 180 * day, Bucket: 4 * day, Source: SourceDaily, Layout: "2006-01-02"},
|
||||
{Key: "1y", Window: 364 * day, Bucket: 7 * day, Source: SourceDaily, Layout: "2006-01-02"},
|
||||
}
|
||||
|
||||
// SLAWindows are listed on monitor pages.
|
||||
@@ -30,8 +46,29 @@ var SLAWindows = []struct {
|
||||
Key string
|
||||
Window time.Duration
|
||||
}{
|
||||
{"1h", time.Hour}, {"3h", 3 * time.Hour}, {"12h", 12 * time.Hour}, {"24h", 24 * time.Hour},
|
||||
{"7d", 7 * 24 * time.Hour}, {"30d", 30 * 24 * time.Hour}, {"90d", 90 * 24 * time.Hour},
|
||||
{"1h", time.Hour}, {"3h", 3 * time.Hour}, {"12h", 12 * time.Hour}, {"24h", day},
|
||||
{"7d", 7 * day}, {"30d", 30 * day}, {"90d", 90 * day}, {"180d", 180 * day}, {"1y", 365 * day},
|
||||
}
|
||||
|
||||
// HasEnoughData reports whether data since first covers at least half of
|
||||
// window, below which a chart of that window would be mostly empty.
|
||||
func HasEnoughData(first, now time.Time, window time.Duration) bool {
|
||||
return !first.IsZero() && now.Sub(first) >= window/2
|
||||
}
|
||||
|
||||
// bucketOrigin is TimescaleDB's default time_bucket origin. Bars are aligned
|
||||
// to it so they match buckets of several days, which Go's Truncate would
|
||||
// place differently.
|
||||
var bucketOrigin = time.Date(2000, 1, 3, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
// AlignBucket returns the start of the bucket of width step containing t.
|
||||
func AlignBucket(t time.Time, step time.Duration) time.Time {
|
||||
d := t.Sub(bucketOrigin)
|
||||
n := d / step
|
||||
if d < 0 && d%step != 0 {
|
||||
n--
|
||||
}
|
||||
return bucketOrigin.Add(n * step)
|
||||
}
|
||||
|
||||
// DefaultRange is used when the request names none.
|
||||
|
||||
@@ -47,3 +47,30 @@ func TestRanges(t *testing.T) {
|
||||
t.Error("unknown range does not fall back to the default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlignBucketMatchesTimescale(t *testing.T) {
|
||||
// Reference values from TimescaleDB:
|
||||
// time_bucket('2 days', '2026-01-02 12:00Z') = 2026-01-02 00:00Z,
|
||||
// time_bucket('7 days', '2026-01-02 12:00Z') = 2025-12-29 00:00Z.
|
||||
ts := time.Date(2026, 1, 2, 12, 0, 0, 0, time.UTC)
|
||||
if got, want := AlignBucket(ts, 48*time.Hour), time.Date(2026, 1, 2, 0, 0, 0, 0, time.UTC); !got.Equal(want) {
|
||||
t.Errorf("AlignBucket(2 days) = %v, want %v", got, want)
|
||||
}
|
||||
if got, want := AlignBucket(ts, 7*24*time.Hour), time.Date(2025, 12, 29, 0, 0, 0, 0, time.UTC); !got.Equal(want) {
|
||||
t.Errorf("AlignBucket(7 days) = %v, want %v", got, want)
|
||||
}
|
||||
h := time.Date(2026, 1, 2, 12, 34, 0, 0, time.UTC)
|
||||
if got := AlignBucket(h, time.Hour); !got.Equal(h.Truncate(time.Hour)) {
|
||||
t.Errorf("AlignBucket(1h) = %v, want %v", got, h.Truncate(time.Hour))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasEnoughData(t *testing.T) {
|
||||
now := time.Now()
|
||||
if HasEnoughData(time.Time{}, now, time.Hour) {
|
||||
t.Error("no data counts as enough")
|
||||
}
|
||||
if !HasEnoughData(now.Add(-31*time.Minute), now, time.Hour) || HasEnoughData(now.Add(-29*time.Minute), now, time.Hour) {
|
||||
t.Error("half-window threshold wrong for 1h")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user