From f8e38a89796947cb4904e399c91762f7e4c504b7 Mon Sep 17 00:00:00 2001 From: Giuseppe Scuglia Date: Tue, 14 Jan 2025 19:43:12 +0100 Subject: [PATCH] fix: alerts line chart sorting --- src/viz/LineChart.tsx | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/viz/LineChart.tsx b/src/viz/LineChart.tsx index f9c8ecc6..0edc5c79 100644 --- a/src/viz/LineChart.tsx +++ b/src/viz/LineChart.tsx @@ -21,24 +21,27 @@ import { AlertConversation } from "@/api/generated/types.gen"; const aggregateAlertsByDate = (alerts: { timestamp: string }[]) => { const dateMap: Record = {}; - alerts.reduce((acc, alert) => { - const timestamp = new Date(alert.timestamp); - const formattedDate = timestamp.toLocaleDateString("en-US", { - month: "short", - day: "2-digit", - }); + alerts + .sort( + (a, b) => + new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(), + ) + .reduce((acc, alert) => { + const timestamp = new Date(alert.timestamp); + const formattedDate = timestamp.toLocaleDateString("en-US", { + month: "short", + day: "2-digit", + }); - if (!acc[formattedDate]) { - acc[formattedDate] = { date: formattedDate, alerts: 0 }; - } - (acc[formattedDate] as { alerts: number }).alerts += 1; + if (!acc[formattedDate]) { + acc[formattedDate] = { date: formattedDate, alerts: 0 }; + } + (acc[formattedDate] as { alerts: number }).alerts += 1; - return acc; - }, dateMap); + return acc; + }, dateMap); - return Object.values(dateMap).sort( - (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(), - ); + return Object.values(dateMap); }; const chartConfig = {