mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-08-24 14:48:42 +08:00
Show daily stats with line chart
This commit is contained in:
71
ui/src/components/DailyStatsChart.tsx
Normal file
71
ui/src/components/DailyStatsChart.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
import React from "react";
|
||||
import {
|
||||
LineChart,
|
||||
Line,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
} from "recharts";
|
||||
import { useTheme, Theme } from "@material-ui/core/styles";
|
||||
import { DailyStat } from "../api";
|
||||
|
||||
interface Props {
|
||||
data: { [qname: string]: DailyStat[] };
|
||||
numDays: number;
|
||||
}
|
||||
|
||||
interface ChartData {
|
||||
succeeded: number;
|
||||
failed: number;
|
||||
date: string;
|
||||
}
|
||||
|
||||
export default function DailyStatsChart(props: Props) {
|
||||
const data = makeChartData(props.data, props.numDays);
|
||||
const theme = useTheme<Theme>();
|
||||
return (
|
||||
<ResponsiveContainer>
|
||||
<LineChart data={data}>
|
||||
<CartesianGrid strokeDasharray="3 3" />
|
||||
<XAxis dataKey="date" minTickGap={10} />
|
||||
<YAxis />
|
||||
<Tooltip />
|
||||
<Legend />
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey="succeeded"
|
||||
stroke={theme.palette.success.main}
|
||||
/>
|
||||
<Line
|
||||
type="monotone"
|
||||
dataKey="failed"
|
||||
stroke={theme.palette.error.main}
|
||||
/>
|
||||
</LineChart>
|
||||
</ResponsiveContainer>
|
||||
);
|
||||
}
|
||||
|
||||
function makeChartData(
|
||||
queueStats: { [qname: string]: DailyStat[] },
|
||||
numDays: number
|
||||
): ChartData[] {
|
||||
const dataByDate: { [date: string]: ChartData } = {};
|
||||
for (const qname in queueStats) {
|
||||
for (const stat of queueStats[qname]) {
|
||||
if (!dataByDate.hasOwnProperty(stat.date)) {
|
||||
dataByDate[stat.date] = { succeeded: 0, failed: 0, date: stat.date };
|
||||
}
|
||||
dataByDate[stat.date].succeeded += stat.processed - stat.failed;
|
||||
dataByDate[stat.date].failed += stat.failed;
|
||||
}
|
||||
}
|
||||
return Object.values(dataByDate).sort(sortByDate).slice(-numDays);
|
||||
}
|
||||
|
||||
function sortByDate(x: ChartData, y: ChartData): number {
|
||||
return Date.parse(x.date) - Date.parse(y.date);
|
||||
}
|
Reference in New Issue
Block a user