2026-04-23 22:49:50 -04:00
|
|
|
import { useMemo } from "react";
|
|
|
|
|
import Tooltip from "@mui/material/Tooltip";
|
|
|
|
|
import Box from "@mui/material/Box";
|
|
|
|
|
import Stack from "@mui/material/Stack";
|
|
|
|
|
import Typography from "@mui/material/Typography";
|
2026-04-27 18:21:31 -04:00
|
|
|
import type { SxProps, Theme } from "@mui/material/styles";
|
2026-04-23 22:49:50 -04:00
|
|
|
import { useTranslation } from "react-i18next";
|
2026-04-29 11:15:33 -07:00
|
|
|
import { useSelector } from "react-redux";
|
|
|
|
|
import type { RootState } from "@/store";
|
2026-04-23 22:49:50 -04:00
|
|
|
import type { CheckSnapshot } from "@/Types/Check";
|
2026-04-29 11:15:33 -07:00
|
|
|
import { formatDateWithTz } from "@/Utils/TimeUtils";
|
2026-04-23 22:49:50 -04:00
|
|
|
|
|
|
|
|
const CELLS = 25;
|
|
|
|
|
const MIN_HEIGHT_PCT = 6;
|
|
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
export type BarKind = "up" | "down" | "empty";
|
|
|
|
|
|
2026-04-23 22:49:50 -04:00
|
|
|
interface Props {
|
|
|
|
|
checks: CheckSnapshot[];
|
2026-04-27 18:39:42 -04:00
|
|
|
containerSx: SxProps<Theme>;
|
|
|
|
|
barSx: (kind: BarKind, heightPct: number) => SxProps<Theme>;
|
|
|
|
|
statsSx: SxProps<Theme>;
|
2026-04-23 22:49:50 -04:00
|
|
|
statsGap?: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
const tone = (check: CheckSnapshot): Exclude<BarKind, "empty"> =>
|
|
|
|
|
check.status ? "up" : "down";
|
2026-04-23 22:49:50 -04:00
|
|
|
|
2026-04-27 18:21:31 -04:00
|
|
|
export const ThemedHistogram = ({
|
|
|
|
|
checks,
|
|
|
|
|
containerSx,
|
|
|
|
|
barSx,
|
|
|
|
|
statsSx,
|
|
|
|
|
statsGap = 1,
|
|
|
|
|
}: Props) => {
|
2026-04-23 22:49:50 -04:00
|
|
|
const { t } = useTranslation();
|
2026-04-29 11:15:33 -07:00
|
|
|
const uiTimezone = useSelector((state: RootState) => state.ui.timezone);
|
2026-04-23 22:49:50 -04:00
|
|
|
|
|
|
|
|
const { padded, max, avg, peak } = useMemo(() => {
|
2026-04-29 12:03:23 -07:00
|
|
|
const source = checks.slice(-CELLS);
|
2026-04-23 22:49:50 -04:00
|
|
|
const out: (CheckSnapshot | null)[] = [
|
|
|
|
|
...source,
|
2026-04-29 12:03:23 -07:00
|
|
|
...Array.from({ length: Math.max(0, CELLS - source.length) }, () => null),
|
2026-04-23 22:49:50 -04:00
|
|
|
];
|
|
|
|
|
const valid = out.filter((c): c is CheckSnapshot => c !== null && c.responseTime > 0);
|
|
|
|
|
const maxRt = valid.length ? Math.max(...valid.map((c) => c.responseTime)) : 1;
|
|
|
|
|
const avgRt = valid.length
|
|
|
|
|
? Math.round(valid.reduce((s, c) => s + c.responseTime, 0) / valid.length)
|
|
|
|
|
: 0;
|
|
|
|
|
return { padded: out, max: maxRt, avg: avgRt, peak: valid.length ? maxRt : 0 };
|
|
|
|
|
}, [checks]);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack gap={statsGap}>
|
2026-04-27 18:39:42 -04:00
|
|
|
<Box sx={containerSx}>
|
2026-04-23 22:49:50 -04:00
|
|
|
{padded.map((check, i) => {
|
|
|
|
|
if (!check) {
|
2026-04-27 18:39:42 -04:00
|
|
|
return (
|
2026-04-27 18:21:31 -04:00
|
|
|
<Box
|
|
|
|
|
key={`empty-${i}`}
|
2026-04-27 18:39:42 -04:00
|
|
|
sx={barSx("empty", MIN_HEIGHT_PCT)}
|
2026-04-23 22:49:50 -04:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const height = Math.max(
|
|
|
|
|
MIN_HEIGHT_PCT,
|
|
|
|
|
Math.round((check.responseTime / max) * 100)
|
|
|
|
|
);
|
|
|
|
|
const tooltipContent = (
|
|
|
|
|
<Stack gap={0.25}>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
fontWeight={600}
|
|
|
|
|
>
|
|
|
|
|
{check.status
|
|
|
|
|
? `${check.responseTime} ms`
|
|
|
|
|
: t("pages.statusPages.monitorsList.chart.downTooltip")}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{ opacity: 0.8 }}
|
|
|
|
|
>
|
2026-04-29 11:15:33 -07:00
|
|
|
{formatDateWithTz(
|
|
|
|
|
check.createdAt,
|
|
|
|
|
"ddd, MMMM D, YYYY, HH:mm A",
|
|
|
|
|
uiTimezone
|
|
|
|
|
)}
|
2026-04-23 22:49:50 -04:00
|
|
|
</Typography>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
return (
|
|
|
|
|
<Tooltip
|
|
|
|
|
key={check.id ?? i}
|
|
|
|
|
title={tooltipContent}
|
|
|
|
|
arrow
|
|
|
|
|
placement="top"
|
|
|
|
|
>
|
2026-04-27 18:39:42 -04:00
|
|
|
<Box
|
|
|
|
|
sx={barSx(tone(check), height)}
|
|
|
|
|
aria-label={`${check.responseTime} ms`}
|
|
|
|
|
/>
|
2026-04-23 22:49:50 -04:00
|
|
|
</Tooltip>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Box>
|
|
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
justifyContent="space-between"
|
2026-04-27 18:39:42 -04:00
|
|
|
sx={statsSx}
|
2026-04-23 22:49:50 -04:00
|
|
|
>
|
|
|
|
|
<span>{t("pages.statusPages.monitorsList.chart.avg", { value: avg })}</span>
|
|
|
|
|
<span>{t("pages.statusPages.monitorsList.chart.max", { value: peak })}</span>
|
|
|
|
|
</Stack>
|
|
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|