Files
Checkmate/client/src/Pages/Infrastructure/Details/Components/GaugeBoxes/index.jsx
T

89 lines
2.4 KiB
React
Raw Normal View History

2025-01-31 13:43:57 -08:00
// Components
import { Stack } from "@mui/material";
import Gauge from "./Gauge";
import SkeletonLayout from "./skeleton";
2025-07-23 15:33:09 +05:30
import PropTypes from "prop-types";
2025-01-31 13:43:57 -08:00
// Utils
import { useHardwareUtils } from "../../Hooks/useHardwareUtils";
import { useTheme } from "@emotion/react";
2025-03-12 00:56:28 +05:30
import { useTranslation } from "react-i18next";
2025-01-31 13:43:57 -08:00
2025-07-23 15:33:09 +05:30
const Gauges = ({ isLoading = false, monitor }) => {
2025-01-31 13:43:57 -08:00
const { decimalToPercentage, formatBytes } = useHardwareUtils();
const theme = useTheme();
2025-03-12 00:56:28 +05:30
const { t } = useTranslation();
2025-01-31 13:43:57 -08:00
2025-07-23 15:33:09 +05:30
if (isLoading) {
2025-01-31 13:43:57 -08:00
return <SkeletonLayout />;
}
const { stats } = monitor ?? {};
let latestCheck = stats?.aggregateData?.latestCheck;
const memoryUsagePercent = latestCheck?.memory?.usage_percent ?? 0;
const memoryUsedBytes = latestCheck?.memory?.used_bytes ?? 0;
const memoryTotalBytes = latestCheck?.memory?.total_bytes ?? 0;
const cpuUsagePercent = latestCheck?.cpu?.usage_percent ?? 0;
const cpuPhysicalCores = latestCheck?.cpu?.physical_core ?? 0;
const cpuFrequency = latestCheck?.cpu?.frequency ?? 0;
const gauges = [
{
type: "memory",
value: decimalToPercentage(memoryUsagePercent),
2025-03-12 00:56:28 +05:30
heading: t("memoryUsage"),
metricOne: t("used"),
2025-01-31 13:43:57 -08:00
valueOne: formatBytes(memoryUsedBytes, true),
2025-03-12 00:56:28 +05:30
metricTwo: t("total"),
2025-01-31 13:43:57 -08:00
valueTwo: formatBytes(memoryTotalBytes, true),
},
{
type: "cpu",
value: decimalToPercentage(cpuUsagePercent),
2025-03-12 00:56:28 +05:30
heading: t("cpuUsage"),
metricOne: t("cores"),
2025-01-31 13:43:57 -08:00
valueOne: cpuPhysicalCores ?? 0,
2025-03-12 00:56:28 +05:30
metricTwo: t("frequency"),
2025-01-31 13:43:57 -08:00
valueTwo: `${(cpuFrequency / 1000).toFixed(2)} Ghz`,
},
...(latestCheck?.disk ?? []).map((disk, idx) => ({
type: "disk",
diskIndex: idx,
value: decimalToPercentage(disk.usage_percent),
heading: `Disk${idx} usage`,
2025-03-12 00:56:28 +05:30
metricOne: t("used"),
2025-01-31 13:43:57 -08:00
valueOne: formatBytes(disk.total_bytes - disk.free_bytes, true),
2025-03-12 00:56:28 +05:30
metricTwo: t("total"),
2025-01-31 13:43:57 -08:00
valueTwo: formatBytes(disk.total_bytes, true),
})),
];
return (
<Stack
direction="row"
2025-07-23 15:33:09 +05:30
flexWrap="wrap"
2025-01-31 13:43:57 -08:00
gap={theme.spacing(8)}
>
{gauges.map((gauge) => {
return (
<Gauge
key={`${gauge.type}-${gauge.diskIndex ?? ""}`}
value={gauge.value}
heading={gauge.heading}
metricOne={gauge.metricOne}
valueOne={gauge.valueOne}
metricTwo={gauge.metricTwo}
valueTwo={gauge.valueTwo}
/>
);
})}
</Stack>
);
};
2025-07-23 15:33:09 +05:30
Gauges.propTypes = {
isLoading: PropTypes.bool,
monitor: PropTypes.object,
};
2025-01-31 13:43:57 -08:00
export default Gauges;