Open-source status page, incident response and uptime monitoring
TypeScript alertingincident-managementmcpmcp-servermonitoringmonitoring-as-codeobservabilityon-callopen-sourcesrestatus-pagestatuspagesynthetic-monitoringterraformtinybirdtursouptimeuptime-checkeruptime-monitor
fix: tb metrics 90d order (#2247)
Maximilian Kaskeac3a2dec4f83
diff --git a/apps/dashboard/src/components/metric/global-uptime/section.tsx b/apps/dashboard/src/components/metric/global-uptime/section.tsxindex 567780d99..32f5b29d5 100644--- a/apps/dashboard/src/components/metric/global-uptime/section.tsx+++ b/apps/dashboard/src/components/metric/global-uptime/section.tsx@@ -59,63 +59,68 @@ export function GlobalUptimeSection({if (_metrics.length !== 2) return null;- return _metrics.reverse().reduce(- (acc, metric) => {- Object.entries(metric).forEach(([key, value]) => {- const k = key as keyof typeof acc;- const v = (() => {- if (k === "lastTimestamp") {- if (!value) return "N/A";- return formatDistanceToNow(new Date(value ?? 0), {- addSuffix: true,- });- }- if (k === "uptime") {- return formatPercentage(value ?? 0);- }- if (k.startsWith("p")) {- return formatMilliseconds(value ?? 0);- }- return formatNumber(value ?? 0);- })();+ // The pipes return two rows (current + previous window) via UNION ALL, whose+ // order ClickHouse doesn't guarantee. The current row carries a lastTimestamp,+ // the previous one is null — fold current last so its value + trend win.+ return _metrics+ .sort((a, b) => (a.lastTimestamp ? 1 : 0) - (b.lastTimestamp ? 1 : 0))+ .reduce(+ (acc, metric) => {+ Object.entries(metric).forEach(([key, value]) => {+ const k = key as keyof typeof acc;+ const v = (() => {+ if (k === "lastTimestamp") {+ if (!value) return "N/A";+ return formatDistanceToNow(new Date(value ?? 0), {+ addSuffix: true,+ });+ }+ if (k === "uptime") {+ return formatPercentage(value ?? 0);+ }+ if (k.startsWith("p")) {+ return formatMilliseconds(value ?? 0);+ }+ return formatNumber(value ?? 0);+ })();- if (k in acc) {- // no prior-period baseline (raw === 0) → no trend, so we don't- // render a misleading "0%" badge (e.g. the empty 90d previous window)- const trend = acc[k]?.raw- ? k === "uptime"- ? acc[k]?.raw / (value ?? 0)- : (value ?? 0) / acc[k]?.raw- : Number.NaN;- const hasTrend =- !Number.isNaN(trend) &&- trend !== Number.POSITIVE_INFINITY &&- k !== "total" &&- k !== "lastTimestamp";- acc[k] = {- label: metricsCards[k].label,- variant: metricsCards[k].variant,- value: v ?? "0",- trend: hasTrend ? trend : null,- raw: value ?? 0,- } as (typeof acc)[typeof k & keyof typeof acc];- } else {- acc[k] = {- label: metricsCards[k].label,- variant: metricsCards[k].variant,- value: v ?? "0",- trend: 1,- raw: value ?? 0,- } as (typeof acc)[typeof k & keyof typeof acc];- }- });- return acc;- },- {} as Record<- keyof ReturnType<typeof mapMetrics>[number],- Metric & { raw: number }- >,- );+ if (k in acc) {+ // no prior-period baseline (raw === 0) → no trend, so we don't+ // render a misleading "0%" badge (e.g. the empty 90d previous window)+ const trend = acc[k]?.raw+ ? k === "uptime"+ ? acc[k]?.raw / (value ?? 0)+ : (value ?? 0) / acc[k]?.raw+ : Number.NaN;+ const hasTrend =+ !Number.isNaN(trend) &&+ trend !== Number.POSITIVE_INFINITY &&+ k !== "total" &&+ k !== "lastTimestamp";+ acc[k] = {+ label: metricsCards[k].label,+ variant: metricsCards[k].variant,+ value: v ?? "0",+ trend: hasTrend ? trend : null,+ raw: value ?? 0,+ } as (typeof acc)[typeof k & keyof typeof acc];+ } else {+ acc[k] = {+ label: metricsCards[k].label,+ variant: metricsCards[k].variant,+ value: v ?? "0",+ trend: 1,+ raw: value ?? 0,+ } as (typeof acc)[typeof k & keyof typeof acc];+ }+ });+ return acc;+ },+ {} as Record<+ keyof ReturnType<typeof mapMetrics>[number],+ Metric & { raw: number }+ >,+ );}const refinedMetrics: (Metric | null)[] = (diff --git a/packages/tinybird/endpoints/endpoint__dns_metrics_90d__v0.pipe b/packages/tinybird/endpoints/endpoint__dns_metrics_90d__v0.pipeindex 67c81382f..00f34c91b 100644--- a/packages/tinybird/endpoints/endpoint__dns_metrics_90d__v0.pipe+++ b/packages/tinybird/endpoints/endpoint__dns_metrics_90d__v0.pipe@@ -21,23 +21,18 @@ SQL >AND timestamp >= toUnixTimestamp64Milli(toDateTime64(now() - INTERVAL 90 DAY, 3)){% if regions %} AND region IN {{ Array(regions, 'String', 'ams,fra') }} {% end %}UNION ALL- -- dns_response has no TTL, so the previous 90d window is real data: dns gets a true 90d trend+ -- no 90d comparison: emit an empty row to keep the 2-row contract; count 0+ -- makes the client suppress the trend badge (NaN). matches http/tcp 90d.SELECT- round(quantile(0.50)(latency)) AS p50Latency,- round(quantile(0.75)(latency)) AS p75Latency,- round(quantile(0.90)(latency)) AS p90Latency,- round(quantile(0.95)(latency)) AS p95Latency,- round(quantile(0.99)(latency)) AS p99Latency,- count() as count,- countIf(requestStatus = 'success') AS success,- countIf(requestStatus = 'degraded') AS degraded,- countIf(requestStatus = 'error') AS error,- NULL as lastTimestamp- FROM dns_response__v0- WHERE- monitorId = {{ String(monitorId, '7417', required=True) }}- AND timestamp >= toUnixTimestamp64Milli(toDateTime64(now() - INTERVAL 180 DAY, 3))- AND timestamp < toUnixTimestamp64Milli(toDateTime64(now() - INTERVAL 90 DAY, 3))- {% if regions %} AND region IN {{ Array(regions, 'String', 'ams,fra') }} {% end %}+ 0 AS p50Latency,+ 0 AS p75Latency,+ 0 AS p90Latency,+ 0 AS p95Latency,+ 0 AS p99Latency,+ 0 AS count,+ 0 AS success,+ 0 AS degraded,+ 0 AS error,+ NULL AS lastTimestampTYPE ENDPOINTdiff --git a/packages/tinybird/endpoints/endpoint__http_metrics_90d__v1.pipe b/packages/tinybird/endpoints/endpoint__http_metrics_90d__v1.pipeindex 87d67b58e..af1364187 100644--- a/packages/tinybird/endpoints/endpoint__http_metrics_90d__v1.pipe+++ b/packages/tinybird/endpoints/endpoint__http_metrics_90d__v1.pipe@@ -21,24 +21,19 @@ SQL >AND time >= toDateTime64(now() - INTERVAL 90 DAY, 3){% if regions %} AND region IN {{ Array(regions, 'String', 'ams,fra') }} {% end %}UNION ALL- -- previous 90d window is beyond the 90d TTL, so this row is empty and the- -- client suppresses the trend badge (NaN). keeps the 2-row contract intact.+ -- the previous 90d window (90-180d ago) is past the 90d MV TTL, so there's no+ -- real comparison data. emit an empty row to keep the 2-row contract; count 0+ -- makes the client suppress the trend badge (NaN).SELECT- round(quantile(0.50)(latency)) AS p50Latency,- round(quantile(0.75)(latency)) AS p75Latency,- round(quantile(0.90)(latency)) AS p90Latency,- round(quantile(0.95)(latency)) AS p95Latency,- round(quantile(0.99)(latency)) AS p99Latency,- count() as count,- countIf(requestStatus = 'success') AS success,- countIf(requestStatus = 'degraded') AS degraded,- countIf(requestStatus = 'error') AS error,- NULL as lastTimestamp- FROM mv__http_90d__v1- WHERE- monitorId = {{ String(monitorId, '1', required=True) }}- AND time >= toDateTime64(now() - INTERVAL 180 DAY, 3)- AND time < toDateTime64(now() - INTERVAL 90 DAY, 3)- {% if regions %} AND region IN {{ Array(regions, 'String', 'ams,fra') }} {% end %}+ 0 AS p50Latency,+ 0 AS p75Latency,+ 0 AS p90Latency,+ 0 AS p95Latency,+ 0 AS p99Latency,+ 0 AS count,+ 0 AS success,+ 0 AS degraded,+ 0 AS error,+ NULL AS lastTimestampTYPE ENDPOINTdiff --git a/packages/tinybird/endpoints/endpoint__tcp_metrics_90d__v1.pipe b/packages/tinybird/endpoints/endpoint__tcp_metrics_90d__v1.pipeindex 7c21a27fa..d705347eb 100644--- a/packages/tinybird/endpoints/endpoint__tcp_metrics_90d__v1.pipe+++ b/packages/tinybird/endpoints/endpoint__tcp_metrics_90d__v1.pipe@@ -21,23 +21,19 @@ SQL >AND time >= toDateTime64(now() - INTERVAL 90 DAY, 3){% if regions %} AND region IN {{ Array(regions, 'String', 'ams,fra') }} {% end %}UNION ALL- -- previous 90d window is beyond the 90d TTL: empty row, trend suppressed client-side+ -- the previous 90d window (90-180d ago) is past the 90d MV TTL, so there's no+ -- real comparison data. emit an empty row to keep the 2-row contract; count 0+ -- makes the client suppress the trend badge (NaN).SELECT- round(quantile(0.50)(latency)) AS p50Latency,- round(quantile(0.75)(latency)) AS p75Latency,- round(quantile(0.90)(latency)) AS p90Latency,- round(quantile(0.95)(latency)) AS p95Latency,- round(quantile(0.99)(latency)) AS p99Latency,- count() as count,- countIf(requestStatus = 'success') AS success,- countIf(requestStatus = 'degraded') AS degraded,- countIf(requestStatus = 'error') AS error,- NULL as lastTimestamp- FROM mv__tcp_90d__v1- WHERE- monitorId = {{ String(monitorId, '4433', required=True) }}- AND time >= toDateTime64(now() - INTERVAL 180 DAY, 3)- AND time < toDateTime64(now() - INTERVAL 90 DAY, 3)- {% if regions %} AND region IN {{ Array(regions, 'String', 'ams,fra') }} {% end %}+ 0 AS p50Latency,+ 0 AS p75Latency,+ 0 AS p90Latency,+ 0 AS p95Latency,+ 0 AS p99Latency,+ 0 AS count,+ 0 AS success,+ 0 AS degraded,+ 0 AS error,+ NULL AS lastTimestampTYPE ENDPOINT