enhancement: add average and max response time for histogram
This commit is contained in:
1 parent
d88c73bb20
commit
974d33b3de
2 files changed
+96
-13
No files matched your search
@@ -1,43 +1,120 @@
|
|||||||
import Box from "@mui/material/Box";
|
import Box from "@mui/material/Box";
|
||||||
|
import Stack from "@mui/material/Stack";
|
||||||
|
import Typography from "@mui/material/Typography";
|
||||||
import { useTheme } from "@mui/material/styles";
|
import { useTheme } from "@mui/material/styles";
|
||||||
import type { ResponsiveStyleValue } from "@mui/system";
|
import type { ResponsiveStyleValue } from "@mui/system";
|
||||||
import type { CheckSnapshot } from "@/Types/Check";
|
import type { CheckSnapshot } from "@/Types/Check";
|
||||||
import { HeatmapResponseTimeTooltip } from "./HeatmapResponseTimeTooltip";
|
import { HeatmapResponseTimeTooltip } from "./HeatmapResponseTimeTooltip";
|
||||||
|
import { useMemo } from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
interface HistogramResponseTimeProps {
|
interface HistogramResponseTimeProps {
|
||||||
checks: CheckSnapshot[];
|
checks: CheckSnapshot[];
|
||||||
height?: ResponsiveStyleValue<number | string>;
|
height?: ResponsiveStyleValue<number | string>;
|
||||||
gap?: ResponsiveStyleValue<number | string>;
|
gap?: ResponsiveStyleValue<number | string>;
|
||||||
|
showStats?: boolean;
|
||||||
|
statsPosition?: "left" | "right";
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ResponseTimeStats {
|
||||||
|
max: number;
|
||||||
|
avg: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_HEIGHT = 50;
|
const DEFAULT_HEIGHT = 50;
|
||||||
|
|
||||||
|
const calculateResponseTimeStats = (checks: CheckSnapshot[]): ResponseTimeStats => {
|
||||||
|
if (!Array.isArray(checks) || checks.length === 0) {
|
||||||
|
return { max: 0, avg: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
const validChecks = checks.filter(
|
||||||
|
(check) =>
|
||||||
|
(check as any).status !== "placeholder" &&
|
||||||
|
(check.originalResponseTime != null || check.responseTime != null)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (validChecks.length === 0) {
|
||||||
|
return { max: 0, avg: 0 };
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseTimes = validChecks.map(
|
||||||
|
(check) => check.originalResponseTime ?? check.responseTime ?? 0
|
||||||
|
);
|
||||||
|
|
||||||
|
const max = Math.max(...responseTimes);
|
||||||
|
const avg = Math.round(
|
||||||
|
responseTimes.reduce((sum, time) => sum + time, 0) / responseTimes.length
|
||||||
|
);
|
||||||
|
|
||||||
|
return { max, avg };
|
||||||
|
};
|
||||||
|
|
||||||
export const HistogramResponseTime = ({
|
export const HistogramResponseTime = ({
|
||||||
checks,
|
checks,
|
||||||
height = DEFAULT_HEIGHT,
|
height = DEFAULT_HEIGHT,
|
||||||
gap,
|
gap,
|
||||||
|
showStats = true,
|
||||||
|
statsPosition = "right",
|
||||||
}: HistogramResponseTimeProps) => {
|
}: HistogramResponseTimeProps) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const stats = useMemo(() => calculateResponseTimeStats(checks), [checks]);
|
||||||
|
|
||||||
if (!Array.isArray(checks) || checks.length === 0) return null;
|
if (!Array.isArray(checks) || checks.length === 0) return null;
|
||||||
|
|
||||||
let data = Array<any>();
|
const data =
|
||||||
|
checks.length !== 25
|
||||||
|
? [...checks, ...Array(25 - checks.length).fill({ status: "placeholder" })]
|
||||||
|
: checks;
|
||||||
|
|
||||||
if (!checks || checks.length === 0) {
|
|
||||||
data = [];
|
|
||||||
}
|
|
||||||
if (checks.length !== 25) {
|
|
||||||
const placeholders = Array(25 - checks.length).fill({
|
|
||||||
status: "placeholder",
|
|
||||||
});
|
|
||||||
data = [...checks, ...placeholders];
|
|
||||||
} else {
|
|
||||||
data = checks;
|
|
||||||
}
|
|
||||||
const chartHeight = typeof height === "number" ? `${height}px` : height;
|
const chartHeight = typeof height === "number" ? `${height}px` : height;
|
||||||
const gridGap = gap ?? theme.spacing(0.5);
|
const gridGap = gap ?? theme.spacing(0.5);
|
||||||
|
|
||||||
|
const statsContent = showStats && (stats.max > 0 || stats.avg > 0) && (
|
||||||
|
<Stack
|
||||||
|
justifyContent="center"
|
||||||
|
alignItems={statsPosition === "left" ? "flex-end" : "flex-start"}
|
||||||
|
sx={{
|
||||||
|
minWidth: 70,
|
||||||
|
pr: statsPosition === "left" ? theme.spacing(2) : 0,
|
||||||
|
pl: statsPosition === "right" ? theme.spacing(2) : 0,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
variant="caption"
|
||||||
|
sx={{
|
||||||
|
color: theme.palette.text.secondary,
|
||||||
|
fontSize: "0.65rem",
|
||||||
|
fontWeight: 500,
|
||||||
|
lineHeight: 1.4,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("common.charts.histogram.avg", { value: stats.avg })}
|
||||||
|
</Typography>
|
||||||
|
<Typography
|
||||||
|
variant="caption"
|
||||||
|
sx={{
|
||||||
|
color: theme.palette.text.secondary,
|
||||||
|
fontSize: "0.65rem",
|
||||||
|
fontWeight: 500,
|
||||||
|
lineHeight: 1.4,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{t("common.charts.histogram.max", { value: stats.max })}
|
||||||
|
</Typography>
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{ width: "100%" }}>
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
alignItems="center"
|
||||||
|
sx={{ width: "100%" }}
|
||||||
|
>
|
||||||
|
{statsPosition === "left" && statsContent}
|
||||||
|
<Box sx={{ flex: 1 }}>
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
@@ -98,5 +175,7 @@ export const HistogramResponseTime = ({
|
|||||||
})}
|
})}
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
{statsPosition === "right" && statsContent}
|
||||||
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -231,6 +231,10 @@
|
|||||||
"high": "High",
|
"high": "High",
|
||||||
"low": "Low",
|
"low": "Low",
|
||||||
"uptime": "Uptime"
|
"uptime": "Uptime"
|
||||||
|
},
|
||||||
|
"histogram": {
|
||||||
|
"avg": "Avg: {{value}} ms",
|
||||||
|
"max": "Max: {{value}} ms"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dialogs": {
|
"dialogs": {
|
||||||
|
|||||||
Reference in new issue
Block a user