From db37060c70912b8901584f28f59080814625dffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sun, 22 Nov 2026 20:13:46 +0100 Subject: [PATCH] fix(store): coalesce NULL avg_latency_ms in HourlyBuckets --- internal/api/statuspages_test.go | 15 +++++++++++++++ internal/store/check_results.sql.go | 2 +- internal/store/queries/check_results.sql | 2 +- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/internal/api/statuspages_test.go b/internal/api/statuspages_test.go index e5f9ee7..b34cd1d 100644 --- a/internal/api/statuspages_test.go +++ b/internal/api/statuspages_test.go @@ -263,6 +263,21 @@ func TestStatusPageGroupsAndDetails(t *testing.T) { } webID := newMonitor("web", up.URL, mgChild["id"]) apiID := newMonitor("api", down.URL, mgRoot["id"]) + // Heartbeat monitors record NULL latency_ms; ensure HourlyBuckets handles them + // cleanly when show_component_details is enabled. + st, hbBody := owner.do("POST", "/api/v1/org/monitors", map[string]any{ + "name": "nightly-job", + "type": "heartbeat", + "interval_seconds": 3600, + }) + if st != http.StatusCreated { + t.Fatalf("create heartbeat monitor = %d %v, want 201", st, hbBody) + } + hbResp, err := e.srv.Client().Post(e.srv.URL+"/hb/"+hbBody["target"].(string), "text/plain", strings.NewReader("")) + if err != nil { + t.Fatalf("ping heartbeat error = %v", err) + } + _ = hbResp.Body.Close() slug := uniq("nested-") st, pageBody := owner.do("POST", "/api/v1/org/statuspages", map[string]any{ diff --git a/internal/store/check_results.sql.go b/internal/store/check_results.sql.go index 64b726e..d9d13b6 100644 --- a/internal/store/check_results.sql.go +++ b/internal/store/check_results.sql.go @@ -200,7 +200,7 @@ func (q *Queries) FirstDataRecent(ctx context.Context, arg FirstDataRecentParams const hourlyBuckets = `-- name: HourlyBuckets :many 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 + coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms FROM check_results_1h WHERE org_id = $1 AND bucket >= $2 GROUP BY monitor_id, bucket diff --git a/internal/store/queries/check_results.sql b/internal/store/queries/check_results.sql index 14c6a1b..63b9685 100644 --- a/internal/store/queries/check_results.sql +++ b/internal/store/queries/check_results.sql @@ -36,7 +36,7 @@ ORDER BY region, time DESC; -- name: HourlyBuckets :many -- Per monitor and hour since $2, across all regions. 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 + coalesce(sum(avg_latency_ms * total) / nullif(sum(total), 0), 0)::real AS avg_latency_ms FROM check_results_1h WHERE org_id = $1 AND bucket >= $2 GROUP BY monitor_id, bucket