Files
Checkmate/Client/src/Pages/Infrastructure/details/index.jsx
T

282 lines
7.3 KiB
React
Raw Normal View History

2024-11-19 11:40:45 +08:00
import { monitorData } from "./data";
import { useParams } from "react-router-dom";
import Breadcrumbs from "../../../Components/Breadcrumbs";
import { Stack, Box, Typography } from "@mui/material";
import { useTheme } from "@emotion/react";
2024-11-19 12:02:37 +08:00
import CustomGauge from "../../../Components/Charts/CustomGauge";
2024-11-19 13:35:00 +08:00
import AreaChart from "../../../Components/Charts/AreaChart";
import {
TzTick,
PercentTick,
InfrastructureTooltip,
} from "../../../Components/Charts/Utils/chartUtils";
2024-11-19 11:40:45 +08:00
const bytesToGB = (bytes) => {
if (typeof bytes !== "number") return 0;
if (bytes === 0) return 0;
const GB = bytes / (1024 * 1024 * 1024);
return Number(GB.toFixed(0));
};
const BaseBox = ({ children }) => {
const theme = useTheme();
return (
<Box
sx={{
height: "100%",
2024-11-19 11:40:45 +08:00
padding: `${theme.spacing(4)} ${theme.spacing(8)}`,
minWidth: 200,
width: 225,
backgroundColor: theme.palette.background.main,
border: 1,
borderStyle: "solid",
borderColor: theme.palette.border.light,
}}
>
{children}
</Box>
);
};
const StatBox = ({ heading, subHeading }) => {
return (
<BaseBox>
<Typography component="h2">{heading}</Typography>
<Typography>{subHeading}</Typography>
</BaseBox>
);
};
const GaugeBox = ({ value, heading, metricOne, valueOne, metricTwo, valueTwo }) => {
const theme = useTheme();
return (
<BaseBox>
2024-11-19 12:02:37 +08:00
<Stack
direction="column"
gap={theme.spacing(2)}
alignItems="center"
>
<CustomGauge
progress={value}
radius={100}
color={theme.palette.primary.main}
/>
<Typography component="h2">{heading}</Typography>
<Box
sx={{
width: "100%",
borderTop: `1px solid ${theme.palette.border.light}`,
}}
>
<Stack
justifyContent={"space-between"}
direction="row"
gap={theme.spacing(2)}
>
<Typography>{metricOne}</Typography>
<Typography>{valueOne}</Typography>
</Stack>
<Stack
justifyContent={"space-between"}
direction="row"
gap={theme.spacing(2)}
>
<Typography>{metricTwo}</Typography>
<Typography>{valueTwo}</Typography>
</Stack>
</Box>
</Stack>
2024-11-19 11:40:45 +08:00
</BaseBox>
);
};
const InfrastructureDetails = () => {
const theme = useTheme();
const { monitorId } = useParams();
const testData = monitorData;
const latestCheck = testData[testData.length - 1];
const navList = [
{ name: "infrastructure monitors", path: "/infrastructure" },
{ name: "details", path: `/infrastructure/${monitorId}` },
];
return (
<Box>
<Breadcrumbs list={navList} />
<Stack
direction="column"
gap={theme.spacing(10)}
mt={theme.spacing(10)}
>
<Stack
direction="row"
gap={theme.spacing(8)}
>
<StatBox
heading={"CPU"}
subHeading={`${latestCheck.cpu.physical_core} cores`}
/>
<StatBox
heading={"Memory"}
subHeading={`${bytesToGB(latestCheck.memory.total_bytes)}GB`}
/>
<StatBox
heading={"Disk"}
subHeading={`${bytesToGB(latestCheck.disk[0].total_bytes)}GB`}
/>
<StatBox
heading={"Uptime"}
subHeading={"100%"}
/>
<StatBox
heading={"Status"}
subHeading={latestCheck.status === true ? "Active" : "Inactive"}
2024-11-19 11:40:45 +08:00
/>
</Stack>
2024-11-19 12:02:37 +08:00
<Stack
direction="row"
gap={theme.spacing(8)}
>
<GaugeBox
value={latestCheck.cpu.usage_percent * 100}
2024-11-19 12:02:37 +08:00
heading={"Memory Usage"}
metricOne={"Used"}
valueOne={`${bytesToGB(latestCheck.memory.used_bytes)}GB`}
metricTwo={"Total"}
valueTwo={`${bytesToGB(latestCheck.memory.total_bytes)}GB`}
/>
<GaugeBox
value={latestCheck.cpu.usage_percent * 100}
2024-11-19 12:02:37 +08:00
heading={"CPU Usage"}
metricOne={"Cores"}
valueOne={latestCheck.cpu.physical_core}
metricTwo={"Frequency"}
valueTwo={`${latestCheck.cpu.frequency} Ghz`}
/>
{latestCheck.disk.map((disk, idx) => {
return (
<GaugeBox
key={disk._id}
value={disk.usage_percent * 100}
2024-11-19 12:02:37 +08:00
heading={`Disk${idx} usage`}
metricOne={"Used"}
valueOne={`${bytesToGB(disk.total_bytes - disk.free_bytes)}GB`}
metricTwo={"Total"}
valueTwo={`${bytesToGB(disk.total_bytes)}GB`}
/>
);
})}
</Stack>
2024-11-19 13:35:00 +08:00
<Stack
direction={"row"}
height={300} // FE team HELP!
gap={theme.spacing(8)} // FE team HELP!
flexWrap="wrap" // //FE team HELP! Better way to do this?
2024-11-19 13:35:00 +08:00
sx={{
"& > *": {
flexBasis: `calc(50% - ${theme.spacing(8)})`,
maxWidth: `calc(50% - ${theme.spacing(8)})`,
2024-11-19 13:35:00 +08:00
},
}}
>
<BaseBox>
<Typography
component="h2"
padding={theme.spacing(8)}
>
Memory usage
</Typography>
<AreaChart
data={testData}
dataKey="memory.usage_percent"
xKey="createdAt"
yKey="memory.usage_percent"
customTooltip={({ active, payload, label }) => (
<InfrastructureTooltip
label={label}
yKey="memory.usage_percent"
yLabel="Memory Usage"
active={active}
payload={payload}
/>
)}
xTick={<TzTick />}
yTick={<PercentTick />}
strokeColor={theme.palette.primary.main}
gradient={true}
gradientStartColor={theme.palette.primary.main} //FE team HELP! Not sure what colors to use
gradientEndColor="#ffffff" // FE team HELP!
/>
</BaseBox>
<BaseBox>
<Typography
component="h2"
padding={theme.spacing(8)}
>
CPU usage
</Typography>
<AreaChart
data={testData}
dataKey="cpu.usage_percent"
xKey="createdAt"
yKey="cpu.usage_percent"
customTooltip={({ active, payload, label }) => (
<InfrastructureTooltip
label={label}
yKey="cpu.usage_percent"
yLabel="CPU Usage"
active={active}
payload={payload}
/>
)}
xTick={<TzTick />}
yTick={<PercentTick />}
strokeColor={theme.palette.success.main} // FE team HELP!
gradient={true}
fill={theme.palette.success.main} // FE team HELP!
gradientStartColor={theme.palette.success.main}
gradientEndColor="#ffffff"
/>
</BaseBox>
2024-11-19 13:35:00 +08:00
{latestCheck.disk.map((disk, idx) => {
// disk is an array of disks, so we need to map over it
2024-11-19 13:35:00 +08:00
return (
<BaseBox key={disk._id}>
<Typography
component="h2"
padding={theme.spacing(8)}
>
{`Disk${idx} usage`}
</Typography>
<AreaChart
data={testData}
dataKey={`disk[${idx}].usage_percent`}
xKey="createdAt"
yKey={`disk[${idx}].usage_percent`} // We are looking for the usage_percent of the current disk in the array
customTooltip={({ active, payload, label }) => (
<InfrastructureTooltip
label={label} // label must be a string
yKey={`disk.usage_percent`}
yLabel="Disk Usage"
yIdx={idx}
active={active}
payload={payload}
/>
)}
xTick={<TzTick />}
yTick={<PercentTick />}
strokeColor={theme.palette.warning.main}
gradient={true}
gradientStartColor={theme.palette.warning.main}
gradientEndColor="#ffffff"
/>
</BaseBox>
2024-11-19 13:35:00 +08:00
);
})}
</Stack>
2024-11-19 11:40:45 +08:00
</Stack>
</Box>
);
};
export default InfrastructureDetails;