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:
Alexander Holliday authored and GitHub committed 2026-02-17 09:31:25 -08:00
commit a2cf2afc09
2 files changed
+124 -67

No files matched your search

@@ -1,102 +1,155 @@
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);
return (
<Box sx={{ width: "100%" }}> const statsContent = showStats &&
<Box (typeof stats.max === "number" || typeof stats.avg === "number") && (
sx={{ <Stack
width: "100%", justifyContent="center"
display: "grid", alignItems={statsPosition === "left" ? "flex-end" : "flex-start"}
gridTemplateColumns: "repeat(25, 1fr)", minWidth={70}
gap: gridGap, pr={statsPosition === "left" ? theme.spacing(8) : 0}
alignItems: "end", pl={statsPosition === "right" ? theme.spacing(8) : 0}
height: chartHeight,
}}
> >
{data.map((check, index) => { <Typography variant="body2">
const isPlaceholder = (check as any).status === "placeholder"; {t("common.charts.histogram.avg", { value: stats.avg })}
const heightPct = `${Math.max(0, Math.min(100, (check as any).responseTime ?? 0))}%`; </Typography>
const barColor = <Typography variant="body2">
check.status === true {t("common.charts.histogram.max", { value: stats.max })}
? theme.palette.success.light </Typography>
: theme.palette.error.light; </Stack>
const bar = ( );
<Box
sx={{ return (
position: "relative", <Stack
width: "100%", direction={statsPosition === "left" ? "row-reverse" : "row"}
height: "100%", alignItems="center"
borderRadius: theme.spacing(1), sx={{ width: "100%" }}
bgcolor: theme.palette.action.hover, >
overflow: "hidden", <Box sx={{ flex: 1 }}>
}} <Box
> sx={{
width: "100%",
display: "grid",
gridTemplateColumns: "repeat(25, 1fr)",
gap: gridGap,
alignItems: "end",
height: chartHeight,
}}
>
{data.map((check, index) => {
const isPlaceholder = (check as any).status === "placeholder";
const heightPct = `${Math.max(0, Math.min(100, (check as any).responseTime ?? 0))}%`;
const barColor =
check.status === true
? theme.palette.success.light
: theme.palette.error.light;
const bar = (
<Box <Box
sx={{ sx={{
position: "absolute", position: "relative",
bottom: 0,
left: 0,
width: "100%", width: "100%",
height: isPlaceholder ? 0 : heightPct, height: "100%",
bgcolor: barColor, borderRadius: theme.spacing(1),
transition: "height 500ms cubic-bezier(0.4, 0, 0.2, 1)", bgcolor: theme.palette.action.hover,
overflow: "hidden",
}} }}
/> >
</Box> <Box
); sx={{
position: "absolute",
bottom: 0,
left: 0,
width: "100%",
height: isPlaceholder ? 0 : heightPct,
bgcolor: barColor,
transition: "height 500ms cubic-bezier(0.4, 0, 0.2, 1)",
}}
/>
</Box>
);
return isPlaceholder ? ( return isPlaceholder ? (
<Box <Box
key={index} key={index}
sx={{ height: "100%" }} sx={{ height: "100%" }}
> >
{bar} {bar}
</Box> </Box>
) : ( ) : (
<HeatmapResponseTimeTooltip <HeatmapResponseTimeTooltip
key={index} key={index}
check={check} check={check}
> >
{bar} {bar}
</HeatmapResponseTimeTooltip> </HeatmapResponseTimeTooltip>
); );
})} })}
</Box>
</Box> </Box>
</Box> {statsContent}
</Stack>
); );
}; };
+4
View File
@@ -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": {