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 = {