Merge pull request #3279 from bluewave-labs/enhancement/uptime-graph-avg-max-response
enhancement: add average and max response time to histogram
This commit is contained in:
2 files changed
+70
-13
No files matched your search
@@ -1,43 +1,94 @@
|
|||||||
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 | string;
|
||||||
|
avg: number | string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_HEIGHT = 50;
|
const DEFAULT_HEIGHT = 50;
|
||||||
|
|
||||||
|
const calculateResponseTimeStats = (checks: CheckSnapshot[]): ResponseTimeStats => {
|
||||||
|
if (!Array.isArray(checks) || checks.length === 0) {
|
||||||
|
return { max: "-", avg: "-" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const validChecks = checks.filter((check) => check.originalResponseTime != null);
|
||||||
|
if (validChecks.length === 0) {
|
||||||
|
return { max: "-", avg: "-" };
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseTimes = validChecks.map((check) => check.originalResponseTime);
|
||||||
|
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 &&
|
||||||
|
(typeof stats.max === "number" || typeof stats.avg === "number") && (
|
||||||
|
<Stack
|
||||||
|
justifyContent="center"
|
||||||
|
alignItems={statsPosition === "left" ? "flex-end" : "flex-start"}
|
||||||
|
minWidth={70}
|
||||||
|
pr={statsPosition === "left" ? theme.spacing(8) : 0}
|
||||||
|
pl={statsPosition === "right" ? theme.spacing(8) : 0}
|
||||||
|
>
|
||||||
|
<Typography variant="body2">
|
||||||
|
{t("common.charts.histogram.avg", { value: stats.avg })}
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2">
|
||||||
|
{t("common.charts.histogram.max", { value: stats.max })}
|
||||||
|
</Typography>
|
||||||
|
</Stack>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{ width: "100%" }}>
|
<Stack
|
||||||
|
direction={statsPosition === "left" ? "row-reverse" : "row"}
|
||||||
|
alignItems="center"
|
||||||
|
sx={{ width: "100%" }}
|
||||||
|
>
|
||||||
|
<Box sx={{ flex: 1 }}>
|
||||||
<Box
|
<Box
|
||||||
sx={{
|
sx={{
|
||||||
width: "100%",
|
width: "100%",
|
||||||
@@ -98,5 +149,7 @@ export const HistogramResponseTime = ({
|
|||||||
})}
|
})}
|
||||||
</Box>
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
|
{statsContent}
|
||||||
|
</Stack>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -47,6 +47,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