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

360 lines
9.9 KiB
React
Raw Normal View History

2024-11-19 11:40:45 +08:00
import { useParams } from "react-router-dom";
2024-11-19 16:01:08 +08:00
import { useEffect, useState } from "react";
2024-11-19 11:40:45 +08:00
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";
2024-11-19 16:01:08 +08:00
import axios from "axios";
2024-11-19 13:35:00 +08:00
import {
TzTick,
PercentTick,
InfrastructureTooltip,
} from "../../../Components/Charts/Utils/chartUtils";
import PropTypes from "prop-types";
2024-11-19 11:40:45 +08:00
/**
* Converts bytes to gigabytes
* @param {number} bytes - Number of bytes to convert
* @returns {number} Converted value in gigabytes
*/
2024-11-19 16:01:08 +08:00
const formatBytes = (bytes) => {
if (typeof bytes !== "number") return "0 GB";
if (bytes === 0) return "0 GB";
2024-11-19 11:40:45 +08:00
const GB = bytes / (1024 * 1024 * 1024);
2024-11-19 16:01:08 +08:00
const MB = bytes / (1024 * 1024);
if (GB >= 1) {
return `${Number(GB.toFixed(0))} GB`;
} else {
return `${Number(MB.toFixed(0))} MB`;
}
2024-11-19 11:40:45 +08:00
};
/**
* Renders a base box with consistent styling
* @param {Object} props - Component properties
* @param {React.ReactNode} props.children - Child components to render inside the box
* @returns {React.ReactElement} Styled box component
*/
2024-11-19 11:40:45 +08:00
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>
);
};
BaseBox.propTypes = {
children: PropTypes.node.isRequired,
};
/**
* Renders a statistic box with a heading and subheading
* @param {Object} props - Component properties
* @param {string} props.heading - Primary heading text
* @param {string} props.subHeading - Secondary heading text
* @returns {React.ReactElement} Stat box component
*/
2024-11-19 11:40:45 +08:00
const StatBox = ({ heading, subHeading }) => {
return (
<BaseBox>
<Typography component="h2">{heading}</Typography>
<Typography>{subHeading}</Typography>
</BaseBox>
);
};
StatBox.propTypes = {
heading: PropTypes.string.isRequired,
subHeading: PropTypes.string.isRequired,
};
/**
* Renders a gauge box with usage visualization
* @param {Object} props - Component properties
* @param {number} props.value - Percentage value for gauge
* @param {string} props.heading - Box heading
* @param {string} props.metricOne - First metric label
* @param {string} props.valueOne - First metric value
* @param {string} props.metricTwo - Second metric label
* @param {string} props.valueTwo - Second metric value
* @returns {React.ReactElement} Gauge box component
*/
2024-11-19 11:40:45 +08:00
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>
);
};
GaugeBox.propTypes = {
2024-11-19 16:01:08 +08:00
value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
heading: PropTypes.string.isRequired,
metricOne: PropTypes.string.isRequired,
2024-11-19 16:01:08 +08:00
valueOne: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
metricTwo: PropTypes.string.isRequired,
2024-11-19 16:01:08 +08:00
valueTwo: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
};
/**
* Renders the infrastructure details page
* @returns {React.ReactElement} Infrastructure details page component
*/
2024-11-19 11:40:45 +08:00
const InfrastructureDetails = () => {
const theme = useTheme();
const { monitorId } = useParams();
const navList = [
{ name: "infrastructure monitors", path: "/infrastructure" },
{ name: "details", path: `/infrastructure/${monitorId}` },
];
2024-11-19 16:01:08 +08:00
const [monitor, setMonitor] = useState(null);
useEffect(() => {
const fetchData = async () => {
try {
const response = await axios.get("http://localhost:5000/api/v1/dummy-data", {
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
},
});
setMonitor(response.data.data);
} catch (error) {
console.error(error);
}
};
fetchData();
}, []);
2024-11-19 11:40:45 +08:00
return (
2024-11-19 16:01:08 +08:00
monitor && (
<Box>
<Breadcrumbs list={navList} />
2024-11-19 11:40:45 +08:00
<Stack
2024-11-19 16:01:08 +08:00
direction="column"
gap={theme.spacing(10)}
mt={theme.spacing(10)}
2024-11-19 11:40:45 +08:00
>
2024-11-19 16:01:08 +08:00
<Stack
direction="row"
gap={theme.spacing(8)}
>
<StatBox
heading={"CPU"}
subHeading={`${monitor.checks[0].cpu.physical_core} cores`}
/>
2024-11-19 16:01:08 +08:00
<StatBox
heading={"Memory"}
subHeading={formatBytes(monitor.checks[0].memory.total_bytes)}
/>
<StatBox
heading={"Disk"}
subHeading={formatBytes(monitor.checks[0].disk[0].total_bytes)}
/>
<StatBox
heading={"Uptime"}
subHeading={"100%"}
/>
<StatBox
heading={"Status"}
subHeading={monitor.status === true ? "Active" : "Inactive"}
/>
</Stack>
<Stack
direction="row"
gap={theme.spacing(8)}
>
<GaugeBox
value={monitor.checks[0].memory.usage_percent * 100}
heading={"Memory Usage"}
metricOne={"Used"}
valueOne={formatBytes(monitor.checks[0].memory.used_bytes)}
metricTwo={"Total"}
valueTwo={formatBytes(monitor.checks[0].memory.total_bytes)}
/>
2024-11-19 16:01:08 +08:00
<GaugeBox
value={monitor.checks[0].cpu.usage_percent * 100}
heading={"CPU Usage"}
metricOne={"Cores"}
valueOne={monitor.checks[0].cpu.physical_core}
metricTwo={"Frequency"}
valueTwo={`${(monitor.checks[0].cpu.frequency / 1000).toFixed(2)} Ghz`}
/>
{monitor.checks[0].disk.map((disk, idx) => {
return (
<GaugeBox
key={disk._id}
value={disk.usage_percent * 100}
heading={`Disk${idx} usage`}
metricOne={"Used"}
valueOne={formatBytes(disk.total_bytes - disk.free_bytes)}
metricTwo={"Total"}
valueTwo={formatBytes(disk.total_bytes)}
/>
2024-11-19 16:01:08 +08:00
);
})}
</Stack>
<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?
sx={{
"& > *": {
flexBasis: `calc(50% - ${theme.spacing(8)})`,
maxWidth: `calc(50% - ${theme.spacing(8)})`,
},
}}
>
<BaseBox>
<Typography
component="h2"
padding={theme.spacing(8)}
>
Memory usage
</Typography>
<AreaChart
data={monitor?.checks ?? []}
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={monitor?.checks ?? []}
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>
{monitor?.checks?.[0]?.disk?.map((disk, idx) => {
// disk is an array of disks, so we need to map over it
return (
<BaseBox key={disk._id}>
<Typography
component="h2"
padding={theme.spacing(8)}
>
{`Disk${idx} usage`}
</Typography>
<AreaChart
data={monitor?.checks ?? []}
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>
);
})}
</Stack>
2024-11-19 13:35:00 +08:00
</Stack>
2024-11-19 16:01:08 +08:00
</Box>
)
2024-11-19 11:40:45 +08:00
);
};
export default InfrastructureDetails;