Files
Checkmate/client/src/pages/infrastructure/InfraDetailsNetGraphs.tsx
T

130 lines
3.7 KiB
TypeScript
Raw Normal View History

2025-11-13 13:47:26 -08:00
import Stack from "@mui/material/Stack";
import MenuItem from "@mui/material/MenuItem";
import Grid from "@mui/material/Grid";
import { HistogramInfrastructure } from "@/components/monitors";
import { Select } from "@/components/inputs";
2026-01-05 21:12:28 +00:00
import type { AggregateCheck } from "@/types/check";
2025-11-13 13:47:26 -08:00
import { useMemo, useState, useEffect } from "react";
import { useTheme } from "@mui/material/styles";
import useMediaQuery from "@mui/material/useMediaQuery";
const getChartConfigs = (
theme: any,
2026-01-05 21:12:28 +00:00
checks: AggregateCheck[],
2025-11-13 13:47:26 -08:00
selectedInterface: string
) => {
2026-01-05 21:12:28 +00:00
const idx =
checks?.[0]?.net?.findIndex((i) => i.name === selectedInterface) ?? -1;
2025-11-13 13:47:26 -08:00
if (idx === -1) return [];
return [
{
type: "Bytes received",
dataKeys: [`net[${idx}].bytes_recv`],
2025-12-08 19:55:29 +00:00
strokeColor: theme.palette.primary.main,
gradientStartColor: theme.palette.primary.main,
2025-11-13 13:47:26 -08:00
yDomain: [0, 1],
idx: null,
},
{
type: "Bytes sent",
dataKeys: [`net[${idx}].bytes_sent`],
strokeColor: theme.palette.success.main,
gradientStartColor: theme.palette.success.main,
yDomain: [0, 1],
idx: null,
},
{
type: "Packets received",
dataKeys: [`net[${idx}].packets_recv`],
strokeColor: theme.palette.warning.main,
gradientStartColor: theme.palette.warning.main,
yDomain: [0, 1],
idx: null,
},
{
type: "Packets sent",
dataKeys: [`net[${idx}].packets_sent`],
strokeColor: theme.palette.error.main,
gradientStartColor: theme.palette.error.main,
yDomain: [0, 1],
idx: null,
},
];
};
export const InfraDetailsNetGraphs = ({
checks,
range,
}: {
2026-01-05 21:12:28 +00:00
checks: AggregateCheck[];
2025-11-13 13:47:26 -08:00
range: string;
}) => {
const theme = useTheme();
const isSmall = useMediaQuery(theme.breakpoints.down("md"));
const [selectedInterface, setSelectedInterface] = useState<string>("");
const chartConfigs = useMemo(
() => getChartConfigs(theme, checks, selectedInterface),
[theme, checks, selectedInterface]
);
useEffect(() => {
2026-01-05 21:12:28 +00:00
if (checks?.[0]?.net && checks[0].net.length > 0) {
2025-11-13 13:47:26 -08:00
const firstNonLoopback = checks[0].net.find(
(iface) => iface.name !== "lo"
);
2026-01-05 21:12:28 +00:00
setSelectedInterface(
firstNonLoopback?.name || checks[0].net[0]?.name || ""
);
} else {
setSelectedInterface("");
2025-11-13 13:47:26 -08:00
}
}, [checks]);
return (
<Stack gap={theme.spacing(8)}>
<Grid container spacing={theme.spacing(8)}>
<Grid size={4}>
<Select
fullWidth
value={selectedInterface}
onChange={(e) => setSelectedInterface(e.target.value)}
>
2026-01-05 21:12:28 +00:00
{checks?.[0]?.net?.map((iface) => {
2025-11-13 13:47:26 -08:00
return (
<MenuItem key={iface.name} value={iface.name}>
{iface.name}
</MenuItem>
);
})}
</Select>
</Grid>
<Grid size={8} />
{chartConfigs.map((config) => {
return (
<Grid
size={isSmall ? 12 : 6}
key={`${config.type}-${config.idx ?? ""}`}
>
<HistogramInfrastructure
range={range}
title={config.type}
2025-12-15 22:19:00 +00:00
type={"net"}
2025-11-13 13:47:26 -08:00
idx={config.idx}
key={`${config.type}-${config.idx ?? ""}`}
checks={checks}
2026-01-05 21:12:28 +00:00
xKey="bucketDate"
2025-11-13 13:47:26 -08:00
yDomain={config.yDomain}
dataKeys={config.dataKeys}
gradient={true}
gradientStartColor={config.gradientStartColor}
gradientEndColor="#ffffff"
strokeColor={config.strokeColor}
/>
</Grid>
);
})}
</Grid>
</Stack>
);
};