feat: long time ranges, data-aware range buttons and per-region history #62
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.Empty = len(data.rows) == 0 && len(v.Root.Children) == 0
|
||||||
v.CanWrite = writableAnywhere(p, data.tree)
|
v.CanWrite = writableAnywhere(p, data.tree)
|
||||||
v.Range = data.rng.Key
|
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 {
|
for _, m := range data.rows {
|
||||||
v.Counts[m.Status]++
|
v.Counts[m.Status]++
|
||||||
}
|
}
|
||||||
@@ -81,6 +87,47 @@ type monitorData struct {
|
|||||||
regionOf map[uuid.UUID]store.Region
|
regionOf map[uuid.UUID]store.Region
|
||||||
rng web.TimeRange
|
rng web.TimeRange
|
||||||
now time.Time
|
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
|
// 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{}
|
out := map[uuid.UUID][]web.Bucket{}
|
||||||
since := now.Add(-rng.Window).Truncate(rng.Bucket)
|
since := now.Add(-rng.Window).Truncate(rng.Bucket)
|
||||||
width := fmt.Sprintf("%d seconds", int(rng.Bucket.Seconds()))
|
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})
|
rows, err := q.RangeBucketsHourly(ctx, store.RangeBucketsHourlyParams{OrgID: org, Width: width, Since: since, MonitorID: monitor})
|
||||||
for _, r := range rows {
|
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})
|
out[r.MonitorID] = append(out[r.MonitorID], web.Bucket{Start: r.Bucket, Total: r.Total, OK: r.OkCount, Latency: r.AvgLatencyMs})
|
||||||
}
|
}
|
||||||
return out, err
|
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})
|
rows, err := q.RangeBuckets(ctx, store.RangeBucketsParams{OrgID: org, Width: width, Since: since, MonitorID: monitor})
|
||||||
for _, r := range rows {
|
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 {
|
if spans, err = q.IncidentsSince(ctx, ptr(now.Add(-web.SLAWindows[len(web.SLAWindows)-1].Window))); err != nil {
|
||||||
return err
|
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)
|
d.buckets, err = rangeBuckets(ctx, q, p.Org.ID, nil, rng, now)
|
||||||
return err
|
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),
|
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),
|
Latency: web.Latency(d.buckets[id], d.now.Add(-d.rng.Window).Truncate(d.rng.Bucket), d.now, 480, 96),
|
||||||
Range: d.rng.Key,
|
Range: d.rng.Key,
|
||||||
Ranges: rangeOptions(c, d.rng),
|
Ranges: rangeOptions(c, d.rng, d.first[id], d.now),
|
||||||
Uptime90: "–",
|
Uptime90: "–",
|
||||||
}
|
}
|
||||||
for _, r := range d.rows {
|
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")
|
v.Daily = web.UptimeBars(buckets, now, 24*time.Hour, 90, "2006-01-02")
|
||||||
for _, w := range web.SLAWindows {
|
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 {
|
if u := web.Uptime(buckets); u >= 0 {
|
||||||
v.Uptime90 = web.Percent(u)
|
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.
|
// 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))
|
out := make([]web.RangeOption, len(web.Ranges))
|
||||||
for i, r := range 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
|
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 {
|
for key, n := range bars {
|
||||||
st, _, html := owner.page("/?range=" + key)
|
st, _, html := owner.page("/?range=" + key)
|
||||||
if st != http.StatusOK {
|
if st != http.StatusOK {
|
||||||
@@ -83,3 +83,56 @@ func TestTimeRangesAndSLA(t *testing.T) {
|
|||||||
t.Error("new monitor without incidents does not show 100%")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -184,6 +184,8 @@
|
|||||||
"members.scope_org": "gesamte Organisation",
|
"members.scope_org": "gesamte Organisation",
|
||||||
"members.title": "Mitglieder",
|
"members.title": "Mitglieder",
|
||||||
"members.unassign": "Rolle entfernen",
|
"members.unassign": "Rolle entfernen",
|
||||||
|
"monitors.by_region": "Nach Region",
|
||||||
|
"monitors.by_region_hint": "Anteil erfolgreicher Checks aus jeder Region. Die SLA sinkt nur, wenn alle Regionen gleichzeitig ausfallen.",
|
||||||
"monitors.confirmations": "Fehlschläge bis zum Alarm",
|
"monitors.confirmations": "Fehlschläge bis zum Alarm",
|
||||||
"monitors.confirmations_hint": "Aufeinanderfolgende Fehlschläge pro Region. Alarm gibt es erst, wenn alle gewählten Regionen ausfallen.",
|
"monitors.confirmations_hint": "Aufeinanderfolgende Fehlschläge pro Region. Alarm gibt es erst, wenn alle gewählten Regionen ausfallen.",
|
||||||
"monitors.delete": "Monitor löschen",
|
"monitors.delete": "Monitor löschen",
|
||||||
@@ -204,6 +206,7 @@
|
|||||||
"monitors.method": "Methode",
|
"monitors.method": "Methode",
|
||||||
"monitors.new": "Neuer Monitor",
|
"monitors.new": "Neuer Monitor",
|
||||||
"monitors.range": "Zeitraum",
|
"monitors.range": "Zeitraum",
|
||||||
|
"monitors.range_not_enough": "Für weniger als die Hälfte dieses Zeitraums liegen bisher Daten vor.",
|
||||||
"monitors.range_note": "SLA und Balken: letzte {{.Range}}",
|
"monitors.range_note": "SLA und Balken: letzte {{.Range}}",
|
||||||
"monitors.recent": "Letzte Checks",
|
"monitors.recent": "Letzte Checks",
|
||||||
"monitors.regions": "Regionen",
|
"monitors.regions": "Regionen",
|
||||||
|
|||||||
@@ -184,6 +184,8 @@
|
|||||||
"members.scope_org": "whole organization",
|
"members.scope_org": "whole organization",
|
||||||
"members.title": "Members",
|
"members.title": "Members",
|
||||||
"members.unassign": "Remove role",
|
"members.unassign": "Remove role",
|
||||||
|
"monitors.by_region": "By region",
|
||||||
|
"monitors.by_region_hint": "Share of successful checks from each region. The SLA only drops when all regions fail at the same time.",
|
||||||
"monitors.confirmations": "Failures before alerting",
|
"monitors.confirmations": "Failures before alerting",
|
||||||
"monitors.confirmations_hint": "Consecutive failures per region. An alert needs every selected region to fail.",
|
"monitors.confirmations_hint": "Consecutive failures per region. An alert needs every selected region to fail.",
|
||||||
"monitors.delete": "Delete monitor",
|
"monitors.delete": "Delete monitor",
|
||||||
@@ -204,6 +206,7 @@
|
|||||||
"monitors.method": "Method",
|
"monitors.method": "Method",
|
||||||
"monitors.new": "New monitor",
|
"monitors.new": "New monitor",
|
||||||
"monitors.range": "Time range",
|
"monitors.range": "Time range",
|
||||||
|
"monitors.range_not_enough": "Less than half of this period is covered by data yet.",
|
||||||
"monitors.range_note": "SLA and bars: last {{.Range}}",
|
"monitors.range_note": "SLA and bars: last {{.Range}}",
|
||||||
"monitors.recent": "Recent checks",
|
"monitors.recent": "Recent checks",
|
||||||
"monitors.regions": "Regions",
|
"monitors.regions": "Regions",
|
||||||
|
|||||||
@@ -98,6 +98,80 @@ func (q *Queries) DailyBucketsForMonitors(ctx context.Context, arg DailyBucketsF
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const firstDataDaily = `-- name: FirstDataDaily :many
|
||||||
|
SELECT monitor_id, region, min(bucket)::timestamptz AS first
|
||||||
|
FROM check_results_1d
|
||||||
|
WHERE org_id = $1
|
||||||
|
GROUP BY 1, 2
|
||||||
|
`
|
||||||
|
|
||||||
|
type FirstDataDailyRow struct {
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Region string
|
||||||
|
First time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// When each monitor and region first reported, to the day.
|
||||||
|
func (q *Queries) FirstDataDaily(ctx context.Context, orgID uuid.UUID) ([]FirstDataDailyRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, firstDataDaily, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []FirstDataDailyRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i FirstDataDailyRow
|
||||||
|
if err := rows.Scan(&i.MonitorID, &i.Region, &i.First); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const firstDataRecent = `-- name: FirstDataRecent :many
|
||||||
|
SELECT monitor_id, region, min(time)::timestamptz AS first
|
||||||
|
FROM check_results
|
||||||
|
WHERE org_id = $1 AND time >= $2
|
||||||
|
GROUP BY 1, 2
|
||||||
|
`
|
||||||
|
|
||||||
|
type FirstDataRecentParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Time time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type FirstDataRecentRow struct {
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Region string
|
||||||
|
First time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// When each monitor and region first reported since $2, exactly. Used for
|
||||||
|
// data younger than the daily aggregate's resolution.
|
||||||
|
func (q *Queries) FirstDataRecent(ctx context.Context, arg FirstDataRecentParams) ([]FirstDataRecentRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, firstDataRecent, arg.OrgID, arg.Time)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []FirstDataRecentRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i FirstDataRecentRow
|
||||||
|
if err := rows.Scan(&i.MonitorID, &i.Region, &i.First); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const hourlyBuckets = `-- name: HourlyBuckets :many
|
const hourlyBuckets = `-- name: HourlyBuckets :many
|
||||||
SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
SELECT monitor_id, bucket::timestamptz AS bucket, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
||||||
(sum(avg_latency_ms * total) / nullif(sum(total), 0))::real AS avg_latency_ms
|
(sum(avg_latency_ms * total) / nullif(sum(total), 0))::real AS avg_latency_ms
|
||||||
@@ -301,6 +375,64 @@ func (q *Queries) RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]R
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const rangeBucketsDaily = `-- name: RangeBucketsDaily :many
|
||||||
|
SELECT monitor_id, time_bucket($2::text::interval, bucket)::timestamptz AS bucket,
|
||||||
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
||||||
|
coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms
|
||||||
|
FROM check_results_1d
|
||||||
|
WHERE org_id = $1 AND bucket >= $3
|
||||||
|
AND ($4::uuid IS NULL OR monitor_id = $4)
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2
|
||||||
|
`
|
||||||
|
|
||||||
|
type RangeBucketsDailyParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Width string
|
||||||
|
Since interface{}
|
||||||
|
MonitorID *uuid.UUID
|
||||||
|
}
|
||||||
|
|
||||||
|
type RangeBucketsDailyRow struct {
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Bucket time.Time
|
||||||
|
Total int64
|
||||||
|
OkCount int64
|
||||||
|
AvgLatencyMs float32
|
||||||
|
}
|
||||||
|
|
||||||
|
// The daily aggregate rebucketed by width, for ranges of months.
|
||||||
|
func (q *Queries) RangeBucketsDaily(ctx context.Context, arg RangeBucketsDailyParams) ([]RangeBucketsDailyRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, rangeBucketsDaily,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.Width,
|
||||||
|
arg.Since,
|
||||||
|
arg.MonitorID,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []RangeBucketsDailyRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i RangeBucketsDailyRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.MonitorID,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Total,
|
||||||
|
&i.OkCount,
|
||||||
|
&i.AvgLatencyMs,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const rangeBucketsHourly = `-- name: RangeBucketsHourly :many
|
const rangeBucketsHourly = `-- name: RangeBucketsHourly :many
|
||||||
SELECT monitor_id, time_bucket($2::text::interval, bucket)::timestamptz AS bucket,
|
SELECT monitor_id, time_bucket($2::text::interval, bucket)::timestamptz AS bucket,
|
||||||
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
||||||
@@ -409,6 +541,166 @@ func (q *Queries) RecentResults(ctx context.Context, arg RecentResultsParams) ([
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const regionBuckets = `-- name: RegionBuckets :many
|
||||||
|
SELECT region, time_bucket($2::text::interval, time)::timestamptz AS bucket,
|
||||||
|
count(*)::bigint AS total, count(*) FILTER (WHERE ok)::bigint AS ok_count
|
||||||
|
FROM check_results
|
||||||
|
WHERE org_id = $1 AND monitor_id = $3 AND time >= $4
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2
|
||||||
|
`
|
||||||
|
|
||||||
|
type RegionBucketsParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Width string
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Since time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegionBucketsRow struct {
|
||||||
|
Region string
|
||||||
|
Bucket time.Time
|
||||||
|
Total int64
|
||||||
|
OkCount int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// One monitor's raw results per region.
|
||||||
|
func (q *Queries) RegionBuckets(ctx context.Context, arg RegionBucketsParams) ([]RegionBucketsRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, regionBuckets,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.Width,
|
||||||
|
arg.MonitorID,
|
||||||
|
arg.Since,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []RegionBucketsRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i RegionBucketsRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Region,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Total,
|
||||||
|
&i.OkCount,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const regionBucketsDaily = `-- name: RegionBucketsDaily :many
|
||||||
|
SELECT region, time_bucket($2::text::interval, bucket)::timestamptz AS bucket,
|
||||||
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||||
|
FROM check_results_1d
|
||||||
|
WHERE org_id = $1 AND monitor_id = $3 AND bucket >= $4
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2
|
||||||
|
`
|
||||||
|
|
||||||
|
type RegionBucketsDailyParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Width string
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Since interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegionBucketsDailyRow struct {
|
||||||
|
Region string
|
||||||
|
Bucket time.Time
|
||||||
|
Total int64
|
||||||
|
OkCount int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) RegionBucketsDaily(ctx context.Context, arg RegionBucketsDailyParams) ([]RegionBucketsDailyRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, regionBucketsDaily,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.Width,
|
||||||
|
arg.MonitorID,
|
||||||
|
arg.Since,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []RegionBucketsDailyRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i RegionBucketsDailyRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Region,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Total,
|
||||||
|
&i.OkCount,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const regionBucketsHourly = `-- name: RegionBucketsHourly :many
|
||||||
|
SELECT region, time_bucket($2::text::interval, bucket)::timestamptz AS bucket,
|
||||||
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||||
|
FROM check_results_1h
|
||||||
|
WHERE org_id = $1 AND monitor_id = $3 AND bucket >= $4
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2
|
||||||
|
`
|
||||||
|
|
||||||
|
type RegionBucketsHourlyParams struct {
|
||||||
|
OrgID uuid.UUID
|
||||||
|
Width string
|
||||||
|
MonitorID uuid.UUID
|
||||||
|
Since interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegionBucketsHourlyRow struct {
|
||||||
|
Region string
|
||||||
|
Bucket time.Time
|
||||||
|
Total int64
|
||||||
|
OkCount int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) RegionBucketsHourly(ctx context.Context, arg RegionBucketsHourlyParams) ([]RegionBucketsHourlyRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, regionBucketsHourly,
|
||||||
|
arg.OrgID,
|
||||||
|
arg.Width,
|
||||||
|
arg.MonitorID,
|
||||||
|
arg.Since,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []RegionBucketsHourlyRow{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i RegionBucketsHourlyRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Region,
|
||||||
|
&i.Bucket,
|
||||||
|
&i.Total,
|
||||||
|
&i.OkCount,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const uptimeSince = `-- name: UptimeSince :many
|
const uptimeSince = `-- name: UptimeSince :many
|
||||||
SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
SELECT monitor_id, sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||||
FROM check_results_1h
|
FROM check_results_1h
|
||||||
|
|||||||
@@ -66,6 +66,11 @@ type Querier interface {
|
|||||||
// System scope: resolves a request to a page by verified custom domain or
|
// System scope: resolves a request to a page by verified custom domain or
|
||||||
// by slug.
|
// by slug.
|
||||||
FindPublishedPage(ctx context.Context, arg FindPublishedPageParams) (StatusPage, error)
|
FindPublishedPage(ctx context.Context, arg FindPublishedPageParams) (StatusPage, error)
|
||||||
|
// When each monitor and region first reported, to the day.
|
||||||
|
FirstDataDaily(ctx context.Context, orgID uuid.UUID) ([]FirstDataDailyRow, error)
|
||||||
|
// When each monitor and region first reported since $2, exactly. Used for
|
||||||
|
// data younger than the daily aggregate's resolution.
|
||||||
|
FirstDataRecent(ctx context.Context, arg FirstDataRecentParams) ([]FirstDataRecentRow, error)
|
||||||
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
GetAPIToken(ctx context.Context, id uuid.UUID) (ApiToken, error)
|
||||||
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
GetAPITokenByHash(ctx context.Context, tokenHash []byte) (ApiToken, error)
|
||||||
// System scope.
|
// System scope.
|
||||||
@@ -148,9 +153,15 @@ type Querier interface {
|
|||||||
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
OrgIDBySlug(ctx context.Context, slug string) (uuid.UUID, error)
|
||||||
// Raw results bucketed by width since a point in time, for short ranges.
|
// Raw results bucketed by width since a point in time, for short ranges.
|
||||||
RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error)
|
RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error)
|
||||||
|
// The daily aggregate rebucketed by width, for ranges of months.
|
||||||
|
RangeBucketsDaily(ctx context.Context, arg RangeBucketsDailyParams) ([]RangeBucketsDailyRow, error)
|
||||||
// The hourly aggregate rebucketed by width, for ranges of days.
|
// The hourly aggregate rebucketed by width, for ranges of days.
|
||||||
RangeBucketsHourly(ctx context.Context, arg RangeBucketsHourlyParams) ([]RangeBucketsHourlyRow, error)
|
RangeBucketsHourly(ctx context.Context, arg RangeBucketsHourlyParams) ([]RangeBucketsHourlyRow, error)
|
||||||
RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error)
|
RecentResults(ctx context.Context, arg RecentResultsParams) ([]RecentResultsRow, error)
|
||||||
|
// One monitor's raw results per region.
|
||||||
|
RegionBuckets(ctx context.Context, arg RegionBucketsParams) ([]RegionBucketsRow, error)
|
||||||
|
RegionBucketsDaily(ctx context.Context, arg RegionBucketsDailyParams) ([]RegionBucketsDailyRow, error)
|
||||||
|
RegionBucketsHourly(ctx context.Context, arg RegionBucketsHourlyParams) ([]RegionBucketsHourlyRow, error)
|
||||||
RegroupMonitors(ctx context.Context, arg RegroupMonitorsParams) error
|
RegroupMonitors(ctx context.Context, arg RegroupMonitorsParams) error
|
||||||
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
||||||
// Moves the subgroups of a group that is about to be deleted.
|
// Moves the subgroups of a group that is about to be deleted.
|
||||||
|
|||||||
@@ -77,3 +77,54 @@ WHERE org_id = $1 AND bucket >= sqlc.arg(since)
|
|||||||
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
|
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
|
||||||
GROUP BY 1, 2
|
GROUP BY 1, 2
|
||||||
ORDER BY 1, 2;
|
ORDER BY 1, 2;
|
||||||
|
|
||||||
|
-- name: RangeBucketsDaily :many
|
||||||
|
-- The daily aggregate rebucketed by width, for ranges of months.
|
||||||
|
SELECT monitor_id, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
|
||||||
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count,
|
||||||
|
coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms
|
||||||
|
FROM check_results_1d
|
||||||
|
WHERE org_id = $1 AND bucket >= sqlc.arg(since)
|
||||||
|
AND (sqlc.narg(monitor_id)::uuid IS NULL OR monitor_id = sqlc.narg(monitor_id))
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2;
|
||||||
|
|
||||||
|
-- name: RegionBuckets :many
|
||||||
|
-- One monitor's raw results per region.
|
||||||
|
SELECT region, time_bucket(sqlc.arg(width)::text::interval, time)::timestamptz AS bucket,
|
||||||
|
count(*)::bigint AS total, count(*) FILTER (WHERE ok)::bigint AS ok_count
|
||||||
|
FROM check_results
|
||||||
|
WHERE org_id = $1 AND monitor_id = sqlc.arg(monitor_id) AND time >= sqlc.arg(since)
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2;
|
||||||
|
|
||||||
|
-- name: RegionBucketsHourly :many
|
||||||
|
SELECT region, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
|
||||||
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||||
|
FROM check_results_1h
|
||||||
|
WHERE org_id = $1 AND monitor_id = sqlc.arg(monitor_id) AND bucket >= sqlc.arg(since)
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2;
|
||||||
|
|
||||||
|
-- name: RegionBucketsDaily :many
|
||||||
|
SELECT region, time_bucket(sqlc.arg(width)::text::interval, bucket)::timestamptz AS bucket,
|
||||||
|
sum(total)::bigint AS total, sum(ok_count)::bigint AS ok_count
|
||||||
|
FROM check_results_1d
|
||||||
|
WHERE org_id = $1 AND monitor_id = sqlc.arg(monitor_id) AND bucket >= sqlc.arg(since)
|
||||||
|
GROUP BY 1, 2
|
||||||
|
ORDER BY 1, 2;
|
||||||
|
|
||||||
|
-- name: FirstDataDaily :many
|
||||||
|
-- When each monitor and region first reported, to the day.
|
||||||
|
SELECT monitor_id, region, min(bucket)::timestamptz AS first
|
||||||
|
FROM check_results_1d
|
||||||
|
WHERE org_id = $1
|
||||||
|
GROUP BY 1, 2;
|
||||||
|
|
||||||
|
-- name: FirstDataRecent :many
|
||||||
|
-- When each monitor and region first reported since $2, exactly. Used for
|
||||||
|
-- data younger than the daily aggregate's resolution.
|
||||||
|
SELECT monitor_id, region, min(time)::timestamptz AS first
|
||||||
|
FROM check_results
|
||||||
|
WHERE org_id = $1 AND time >= $2
|
||||||
|
GROUP BY 1, 2;
|
||||||
@@ -38,9 +38,9 @@ type Bucket struct {
|
|||||||
func UptimeBars(buckets []Bucket, end time.Time, step time.Duration, n int, layout string) []Bar {
|
func UptimeBars(buckets []Bucket, end time.Time, step time.Duration, n int, layout string) []Bar {
|
||||||
byStart := make(map[int64]Bucket, len(buckets))
|
byStart := make(map[int64]Bucket, len(buckets))
|
||||||
for _, b := range 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)
|
bars := make([]Bar, n)
|
||||||
for i := range bars {
|
for i := range bars {
|
||||||
t := start.Add(time.Duration(i) * step)
|
t := start.Add(time.Duration(i) * step)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -55,17 +56,30 @@ type DashboardView struct {
|
|||||||
Ranges []RangeOption
|
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 {
|
type RangeOption struct {
|
||||||
Key string
|
Key string
|
||||||
Active bool
|
Active bool
|
||||||
URL string
|
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 {
|
type SLAEntry struct {
|
||||||
Label string
|
Label string
|
||||||
Value 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) {
|
templ rangeSwitch(opts []RangeOption) {
|
||||||
@@ -73,6 +87,8 @@ templ rangeSwitch(opts []RangeOption) {
|
|||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
if o.Active {
|
if o.Active {
|
||||||
<span class="bg-ink px-2 py-1 text-paper" aria-current="true">{ o.Key }</span>
|
<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 {
|
} else {
|
||||||
<a href={ templ.SafeURL(o.URL) } class="px-2 py-1 text-muted hover:text-ink">{ o.Key }</a>
|
<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
|
Range string
|
||||||
Ranges []RangeOption
|
Ranges []RangeOption
|
||||||
SLAs []SLAEntry
|
SLAs []SLAEntry
|
||||||
|
Regions []RegionHistory
|
||||||
Interval string
|
Interval string
|
||||||
Daily []Bar
|
Daily []Bar
|
||||||
Uptime90 string
|
Uptime90 string
|
||||||
@@ -263,11 +280,26 @@ templ MonitorDetail(p Page, me Me, v MonitorDetailView) {
|
|||||||
</svg>
|
</svg>
|
||||||
}
|
}
|
||||||
</section>
|
</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">
|
<section class="mb-10">
|
||||||
@sectionHeading("monitors.sla_table")
|
@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 {
|
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>
|
<dt class="font-mono text-xs text-muted">{ e.Label }</dt>
|
||||||
<dd class="font-mono text-sm">{ e.Value }</dd>
|
<dd class="font-mono text-sm">{ e.Value }</dd>
|
||||||
</div>
|
</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"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TimeRange is a selectable chart window. Bars use Bucket as resolution;
|
// Source names where a range reads its data. Raw results are kept for 90
|
||||||
// Hourly ranges are read from the hourly aggregate instead of raw results.
|
// 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 {
|
type TimeRange struct {
|
||||||
Key string
|
Key string
|
||||||
Window time.Duration
|
Window time.Duration
|
||||||
Bucket time.Duration
|
Bucket time.Duration
|
||||||
Hourly bool
|
Source Source
|
||||||
// Layout formats bar titles.
|
// Layout formats bar titles.
|
||||||
Layout string
|
Layout string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const day = 24 * time.Hour
|
||||||
|
|
||||||
// Ranges are offered on the dashboard and monitor pages, shortest first.
|
// Ranges are offered on the dashboard and monitor pages, shortest first.
|
||||||
var Ranges = []TimeRange{
|
var Ranges = []TimeRange{
|
||||||
{Key: "1h", Window: time.Hour, Bucket: time.Minute, Layout: "15:04"},
|
{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: "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: "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: "24h", Window: day, 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: "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.
|
// SLAWindows are listed on monitor pages.
|
||||||
@@ -30,8 +46,29 @@ var SLAWindows = []struct {
|
|||||||
Key string
|
Key string
|
||||||
Window time.Duration
|
Window time.Duration
|
||||||
}{
|
}{
|
||||||
{"1h", time.Hour}, {"3h", 3 * time.Hour}, {"12h", 12 * time.Hour}, {"24h", 24 * time.Hour},
|
{"1h", time.Hour}, {"3h", 3 * time.Hour}, {"12h", 12 * time.Hour}, {"24h", day},
|
||||||
{"7d", 7 * 24 * time.Hour}, {"30d", 30 * 24 * time.Hour}, {"90d", 90 * 24 * time.Hour},
|
{"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.
|
// 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")
|
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