Skip to content

fix: alerts line chart sorting #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions src/viz/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ import { AlertConversation } from "@/api/generated/types.gen";
const aggregateAlertsByDate = (alerts: { timestamp: string }[]) => {
const dateMap: Record<string, { date: string; alerts: number }> = {};

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 = {
Expand Down
Loading