Skip to content

Commit ce8146c

Browse files
authored
fix: alerts line chart sorting (#63)
1 parent 345350d commit ce8146c

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/viz/LineChart.tsx

+18-15
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,27 @@ import { AlertConversation } from "@/api/generated/types.gen";
2121
const aggregateAlertsByDate = (alerts: { timestamp: string }[]) => {
2222
const dateMap: Record<string, { date: string; alerts: number }> = {};
2323

24-
alerts.reduce((acc, alert) => {
25-
const timestamp = new Date(alert.timestamp);
26-
const formattedDate = timestamp.toLocaleDateString("en-US", {
27-
month: "short",
28-
day: "2-digit",
29-
});
24+
alerts
25+
.sort(
26+
(a, b) =>
27+
new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime(),
28+
)
29+
.reduce((acc, alert) => {
30+
const timestamp = new Date(alert.timestamp);
31+
const formattedDate = timestamp.toLocaleDateString("en-US", {
32+
month: "short",
33+
day: "2-digit",
34+
});
3035

31-
if (!acc[formattedDate]) {
32-
acc[formattedDate] = { date: formattedDate, alerts: 0 };
33-
}
34-
(acc[formattedDate] as { alerts: number }).alerts += 1;
36+
if (!acc[formattedDate]) {
37+
acc[formattedDate] = { date: formattedDate, alerts: 0 };
38+
}
39+
(acc[formattedDate] as { alerts: number }).alerts += 1;
3540

36-
return acc;
37-
}, dateMap);
41+
return acc;
42+
}, dateMap);
3843

39-
return Object.values(dateMap).sort(
40-
(a, b) => new Date(a.date).getTime() - new Date(b.date).getTime(),
41-
);
44+
return Object.values(dateMap);
4245
};
4346

4447
const chartConfig = {

0 commit comments

Comments
 (0)