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
+129
-6
@@ -63,7 +63,13 @@ func (s *Server) dashboard(c *gin.Context) {
|
||||
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)
|
||||
var earliest time.Time
|
||||
for _, m := range data.byID {
|
||||
if f, ok := data.first[m.ID]; ok && (earliest.IsZero() || f.Before(earliest)) {
|
||||
earliest = f
|
||||
}
|
||||
}
|
||||
v.Ranges = rangeOptions(c, data.rng, earliest, data.now)
|
||||
for _, m := range data.rows {
|
||||
v.Counts[m.Status]++
|
||||
}
|
||||
@@ -81,6 +87,47 @@ type monitorData struct {
|
||||
regionOf map[uuid.UUID]store.Region
|
||||
rng web.TimeRange
|
||||
now time.Time
|
||||
// first is when each monitor first reported, from any region;
|
||||
// firstRegion the same per region.
|
||||
first map[uuid.UUID]time.Time
|
||||
firstRegion map[uuid.UUID]map[string]time.Time
|
||||
}
|
||||
|
||||
// loadFirstData finds when monitors and their regions first reported. The
|
||||
// daily aggregate answers to the day, which is precise enough for anything
|
||||
// older than two days; younger data is looked up in the raw results.
|
||||
func loadFirstData(ctx context.Context, q *store.Queries, org uuid.UUID, now time.Time) (map[uuid.UUID]time.Time, map[uuid.UUID]map[string]time.Time, error) {
|
||||
first := map[uuid.UUID]time.Time{}
|
||||
perRegion := map[uuid.UUID]map[string]time.Time{}
|
||||
note := func(m uuid.UUID, region string, t time.Time) {
|
||||
if perRegion[m] == nil {
|
||||
perRegion[m] = map[string]time.Time{}
|
||||
}
|
||||
if cur, ok := perRegion[m][region]; !ok || t.Before(cur) {
|
||||
perRegion[m][region] = t
|
||||
}
|
||||
if cur, ok := first[m]; !ok || t.Before(cur) {
|
||||
first[m] = t
|
||||
}
|
||||
}
|
||||
recentSince := now.Add(-2 * 24 * time.Hour)
|
||||
daily, err := q.FirstDataDaily(ctx, org)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, r := range daily {
|
||||
if r.First.Before(recentSince) {
|
||||
note(r.MonitorID, r.Region, r.First)
|
||||
}
|
||||
}
|
||||
recent, err := q.FirstDataRecent(ctx, store.FirstDataRecentParams{OrgID: org, Time: recentSince})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, r := range recent {
|
||||
note(r.MonitorID, r.Region, r.First)
|
||||
}
|
||||
return first, perRegion, nil
|
||||
}
|
||||
|
||||
// rangeBuckets loads the selected range for all monitors, or one if
|
||||
@@ -89,12 +136,19 @@ func rangeBuckets(ctx context.Context, q *store.Queries, org uuid.UUID, monitor
|
||||
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 {
|
||||
switch rng.Source {
|
||||
case web.SourceHourly:
|
||||
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
|
||||
case web.SourceDaily:
|
||||
rows, err := q.RangeBucketsDaily(ctx, store.RangeBucketsDailyParams{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 {
|
||||
@@ -154,6 +208,9 @@ func (s *Server) loadMonitorData(ctx context.Context, p *principal, rng web.Time
|
||||
if spans, err = q.IncidentsSince(ctx, ptr(now.Add(-web.SLAWindows[len(web.SLAWindows)-1].Window))); err != nil {
|
||||
return err
|
||||
}
|
||||
if d.first, d.firstRegion, err = loadFirstData(ctx, q, p.Org.ID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
d.buckets, err = rangeBuckets(ctx, q, p.Org.ID, nil, rng, now)
|
||||
return err
|
||||
})
|
||||
@@ -272,7 +329,7 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
|
||||
CanWrite: canInGroup(p, authz.MonitorsWrite, d.tree, m.GroupID),
|
||||
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),
|
||||
Ranges: rangeOptions(c, d.rng, d.first[id], d.now),
|
||||
Uptime90: "–",
|
||||
}
|
||||
for _, r := range d.rows {
|
||||
@@ -306,8 +363,16 @@ func (s *Server) monitorDetail(c *gin.Context, p *principal, page web.Page, me w
|
||||
}
|
||||
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))})
|
||||
v.SLAs = append(v.SLAs, web.SLAEntry{
|
||||
Label: w.Key, Value: web.SLAText(d.slaFor(m, w.Window)),
|
||||
Partial: !web.HasEnoughData(d.first[id], d.now, w.Window),
|
||||
})
|
||||
}
|
||||
regions, err := s.regionHistory(ctx, p, d, m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.Regions = regions
|
||||
if u := web.Uptime(buckets); u >= 0 {
|
||||
v.Uptime90 = web.Percent(u)
|
||||
}
|
||||
@@ -686,10 +751,68 @@ func (s *Server) settingsIndex(c *gin.Context) {
|
||||
}
|
||||
|
||||
// rangeOptions builds the range switcher, keeping the current path.
|
||||
func rangeOptions(c *gin.Context, active web.TimeRange) []web.RangeOption {
|
||||
// Ranges less than half covered by data since first are disabled.
|
||||
func rangeOptions(c *gin.Context, active web.TimeRange, first, now time.Time) []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}
|
||||
out[i] = web.RangeOption{
|
||||
Key: r.Key, Active: r.Key == active.Key, URL: c.Request.URL.Path + "?range=" + r.Key,
|
||||
Disabled: r.Key != active.Key && !web.HasEnoughData(first, now, r.Window),
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// regionHistory returns bars and availability per region of m for the
|
||||
// selected range. Availability is the share of successful checks from that
|
||||
// region; the monitor's SLA only drops when all regions fail together.
|
||||
func (s *Server) regionHistory(ctx context.Context, p *principal, d monitorData, m store.Monitor) ([]web.RegionHistory, error) {
|
||||
rng := d.rng
|
||||
since := d.now.Add(-rng.Window).Truncate(rng.Bucket)
|
||||
width := fmt.Sprintf("%d seconds", int(rng.Bucket.Seconds()))
|
||||
byRegion := map[string][]web.Bucket{}
|
||||
err := s.db.Tx(ctx, p.scope(), func(q *store.Queries) error {
|
||||
add := func(region string, b web.Bucket) { byRegion[region] = append(byRegion[region], b) }
|
||||
switch rng.Source {
|
||||
case web.SourceHourly:
|
||||
rows, err := q.RegionBucketsHourly(ctx, store.RegionBucketsHourlyParams{OrgID: p.Org.ID, MonitorID: m.ID, Width: width, Since: since})
|
||||
for _, r := range rows {
|
||||
add(r.Region, web.Bucket{Start: r.Bucket, Total: r.Total, OK: r.OkCount})
|
||||
}
|
||||
return err
|
||||
case web.SourceDaily:
|
||||
rows, err := q.RegionBucketsDaily(ctx, store.RegionBucketsDailyParams{OrgID: p.Org.ID, MonitorID: m.ID, Width: width, Since: since})
|
||||
for _, r := range rows {
|
||||
add(r.Region, web.Bucket{Start: r.Bucket, Total: r.Total, OK: r.OkCount})
|
||||
}
|
||||
return err
|
||||
}
|
||||
rows, err := q.RegionBuckets(ctx, store.RegionBucketsParams{OrgID: p.Org.ID, MonitorID: m.ID, Width: width, Since: since})
|
||||
for _, r := range rows {
|
||||
add(r.Region, web.Bucket{Start: r.Bucket, Total: r.Total, OK: r.OkCount})
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
regions := make([]string, 0, len(byRegion))
|
||||
for r := range byRegion {
|
||||
regions = append(regions, r)
|
||||
}
|
||||
slices.SortFunc(regions, func(a, b string) int { return strings.Compare(d.regionNameString(a), d.regionNameString(b)) })
|
||||
out := make([]web.RegionHistory, 0, len(regions))
|
||||
for _, r := range regions {
|
||||
h := web.RegionHistory{
|
||||
Name: d.regionNameString(r),
|
||||
Bars: web.UptimeBars(byRegion[r], d.now, rng.Bucket, rng.Bars(), rng.Layout),
|
||||
Uptime: "–",
|
||||
Enough: web.HasEnoughData(d.firstRegion[m.ID][r], d.now, rng.Window),
|
||||
}
|
||||
if u := web.Uptime(byRegion[r]); u >= 0 {
|
||||
h.Uptime = web.PercentPrecise(u)
|
||||
}
|
||||
out = append(out, h)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -53,7 +53,7 @@ func TestTimeRangesAndSLA(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
bars := map[string]int{"1h": 60, "3h": 36, "12h": 48, "24h": 48, "7d": 42}
|
||||
bars := map[string]int{"1h": 60, "3h": 36, "12h": 48, "24h": 48, "7d": 42, "30d": 60, "90d": 45, "180d": 45, "1y": 52}
|
||||
for key, n := range bars {
|
||||
st, _, html := owner.page("/?range=" + key)
|
||||
if st != http.StatusOK {
|
||||
@@ -83,3 +83,56 @@ func TestTimeRangesAndSLA(t *testing.T) {
|
||||
t.Error("new monitor without incidents does not show 100%")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRangeAvailabilityAndRegions(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
_, eu := e.platformChecker(t, uniq("eu-"))
|
||||
_, us := e.platformChecker(t, uniq("us-"))
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("ra")+".test", uniq("ra-"))
|
||||
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
||||
"name": "API", "type": "tcp", "target": "example.com:443", "interval_seconds": 3600, "regions": []string{eu, us},
|
||||
})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create monitor = %d %v", st, body)
|
||||
}
|
||||
id := uuid.MustParse(body["id"].(string))
|
||||
org := uuid.MustParse(owner.orgID())
|
||||
|
||||
// Two hours of results: eu always fine, us always failing.
|
||||
ctx := context.Background()
|
||||
now := time.Now()
|
||||
checker := uuid.New()
|
||||
for m := 0; m <= 120; m += 5 {
|
||||
at := now.Add(-time.Duration(m) * time.Minute)
|
||||
for region, ok := range map[string]bool{eu: true, us: false} {
|
||||
if _, err := e.dbPool.Exec(ctx,
|
||||
`INSERT INTO check_results (time, org_id, monitor_id, region, checker_id, ok, latency_ms) VALUES ($1, $2, $3, $4, $5, $6, 50)`,
|
||||
at, org, id, region, checker, ok); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, _, html := owner.page("/monitors/" + id.String() + "?range=1h")
|
||||
for key, disabled := range map[string]bool{"3h": false, "12h": true, "24h": true, "1y": true} {
|
||||
got := strings.Contains(html, `aria-disabled="true">`+key+"<")
|
||||
if got != disabled {
|
||||
t.Errorf("range %s disabled = %t, want %t", key, got, disabled)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(html, `href="/monitors/`+id.String()+`?range=3h"`) {
|
||||
t.Error("3h is not selectable with two hours of data")
|
||||
}
|
||||
if !strings.Contains(html, "By region") && !strings.Contains(html, "Nach Region") {
|
||||
t.Fatal("monitor page lacks the per-region section")
|
||||
}
|
||||
if !strings.Contains(html, ">100%<") || !strings.Contains(html, ">0.0%<") {
|
||||
t.Error("per-region availability does not show 100% for eu and 0.0% for us")
|
||||
}
|
||||
|
||||
// The dashboard follows the monitor with the oldest data.
|
||||
if _, _, dash := owner.page("/?range=1h"); !strings.Contains(dash, `aria-disabled="true">12h<`) || strings.Contains(dash, `aria-disabled="true">3h<`) {
|
||||
t.Error("dashboard range availability does not follow the data")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user