From 6b37cf8e3f700eed962c6bf6d5289eb6181806c5 Mon Sep 17 00:00:00 2001 From: Adam Shiervani Date: Thu, 28 Aug 2025 12:31:12 +0200 Subject: [PATCH] feat: do an actual avg reference calc --- ui/src/components/Metric.tsx | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/ui/src/components/Metric.tsx b/ui/src/components/Metric.tsx index f791e60..a3706e4 100644 --- a/ui/src/components/Metric.tsx +++ b/ui/src/components/Metric.tsx @@ -61,6 +61,18 @@ export function createChartArray( return result; } +function computeReferenceValue(points: ChartPoint[]): number | undefined { + const values = points + .filter(p => p.metric != null && Number.isFinite(p.metric)) + .map(p => Number(p.metric)); + + if (values.length === 0) return undefined; + + const sum = values.reduce((acc, v) => acc + v, 0); + const mean = sum / values.length; + return Math.round(mean); +} + const theme = { light: "bg-white text-black border border-slate-800/20 dark:border dark:border-slate-700 dark:bg-slate-800 dark:text-slate-300", @@ -128,17 +140,9 @@ export function Metric({ // If the consumer provides a map function, we apply it to the raw data. const dataFinal: ChartPoint[] = map ? raw.map(map) : raw; - const recent = dataFinal - .slice(-(raw.length - 1)) - .filter(x => x.metric != null) as ChartPoint[]; - // Average the recent values - const computedReferenceValue = - recent.length > 0 - ? Math.round( - recent.reduce((sum, x) => sum + (x.metric as number), 0) / recent.length, - ) - : undefined; + // Compute the average value of the metric. + const referenceValue = computeReferenceValue(dataFinal); return (
@@ -162,7 +166,7 @@ export function Metric({ data={dataFinal} domain={domain} unit={unit} - referenceValue={computedReferenceValue} + referenceValue={referenceValue} /> ) : (