Added delta instead of avg, and formatted the code to re-use already existing code.

This commit is contained in:
Owaise committed 2025-08-12 23:48:47 +05:30
1 parent 355a445ead
commit e27cc8a12b
7 files changed
+201 -60

No files matched your search

@@ -101,16 +101,17 @@ const getFormattedPercentage = (value) => {
* @param {number} props.index - The index of the tick. * @param {number} props.index - The index of the tick.
* @returns {JSX.Element|null} The rendered tick component or null for the first tick. * @returns {JSX.Element|null} The rendered tick component or null for the first tick.
*/ */
export const NetworkTick = ({ x, y, payload, index }) => { export const NetworkTick = ({ x, y, payload, index, formatter}) => {
const theme = useTheme(); const theme = useTheme();
if (index === 0) return null; if (index === 0) return null;
const formatBytes = (bytes) => { if (formatter === undefined) {
if (bytes >= 1_000_000_000) return `${(bytes / 1_000_000_000).toFixed(1)} GB/s`; formatter = (value, space=false) => {
if (bytes >= 1_000_000) return `${(bytes / 1_000_000).toFixed(1)} MB/s`; if (typeof value !== "number") return value;
if (bytes >= 1_000) return `${(bytes / 1_000).toFixed(1)} KB/s`; // need to add space between value and unit
return `${bytes} B/s`; return `${(value / 1024).toFixed(2)}${space ? " " : ""}Kbps`;
}; };
}
return ( return (
<Text <Text
@@ -121,7 +122,7 @@ export const NetworkTick = ({ x, y, payload, index }) => {
fontSize={11} fontSize={11}
fontWeight={400} fontWeight={400}
> >
{formatBytes(payload?.value)} {formatter(payload?.value, true)}
</Text> </Text>
); );
}; };
@@ -131,6 +132,7 @@ NetworkTick.propTypes = {
y: PropTypes.number, y: PropTypes.number,
payload: PropTypes.object, payload: PropTypes.object,
index: PropTypes.number, index: PropTypes.number,
formatter: PropTypes.func,
}; };
/** /**
@@ -1,9 +1,7 @@
// NetworkCharts.jsx
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import { Stack } from "@mui/material"; import { Stack, Typography } from "@mui/material";
import InfraAreaChart from "../../../../../Pages/Infrastructure/Details/Components/AreaChartBoxes/InfraAreaChart"; import InfraAreaChart from "../../../../../Pages/Infrastructure/Details/Components/AreaChartBoxes/InfraAreaChart";
// Utils
import { import {
TzTick, TzTick,
InfrastructureTooltip, InfrastructureTooltip,
@@ -11,43 +9,42 @@ import {
} from "../../../../../Components/Charts/Utils/chartUtils"; } from "../../../../../Components/Charts/Utils/chartUtils";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useHardwareUtils } from "../../Hooks/useHardwareUtils";
const getFormattedNetworkMetric = (value) => { const NetworkCharts = ({ ethernetData, dateRange }) => {
if (typeof value !== "number" || isNaN(value)) return "0";
if (value >= 1024 ** 3) return `${(value / 1024 ** 3).toFixed(1)} GB/s`;
if (value >= 1024 ** 2) return `${(value / 1024 ** 2).toFixed(1)} MB/s`;
if (value >= 1024) return `${(value / 1024).toFixed(1)} KB/s`;
return `${Math.round(value)} B/s`;
};
const NetworkCharts = ({ eth0Data, dateRange }) => {
const theme = useTheme(); const theme = useTheme();
const { t } = useTranslation(); const { t } = useTranslation();
const {formatBytesString} = useHardwareUtils();
if (!ethernetData?.length) {
return <Typography>{t("noNetworkStatsAvailable")}</Typography>;
}
const configs = [ const configs = [
{ {
type: "network-bytes", type: "network-bytes",
data: eth0Data, data: ethernetData,
dataKeys: ["bytesPerSec"], dataKeys: ["bytesPerSec"],
heading: t("bytesPerSecond"), heading: t("bytesPerSecond"),
strokeColor: theme.palette.info.main, strokeColor: theme.palette.info.main,
gradientStartColor: theme.palette.info.main, gradientStartColor: theme.palette.info.main,
yLabel: t("bytesPerSecond"), yLabel: t("bytesPerSecond"),
xTick: <TzTick dateRange={dateRange} />, xTick: <TzTick dateRange={dateRange} />,
yTick: <NetworkTick />, yTick: <NetworkTick formatter={formatBytesString}/>,
toolTip: ( toolTip: (
<InfrastructureTooltip <InfrastructureTooltip
dotColor={theme.palette.info.main} dotColor={theme.palette.info.main}
yKey={"bytesPerSec"} yKey={"bytesPerSec"}
yLabel={t("bytesPerSecond")} yLabel={t("bytesPerSecond")}
dateRange={dateRange} dateRange={dateRange}
formatter={getFormattedNetworkMetric} formatter={formatBytesString}
/> />
), ),
}, },
{ {
type: "network-packets", type: "network-packets",
data: eth0Data, data: ethernetData,
dataKeys: ["packetsPerSec"], dataKeys: ["packetsPerSec"],
heading: t("packetsPerSecond"), heading: t("packetsPerSecond"),
strokeColor: theme.palette.success.main, strokeColor: theme.palette.success.main,
@@ -66,7 +63,7 @@ const NetworkCharts = ({ eth0Data, dateRange }) => {
}, },
{ {
type: "network-errors", type: "network-errors",
data: eth0Data, data: ethernetData,
dataKeys: ["errors"], dataKeys: ["errors"],
heading: t("errors"), heading: t("errors"),
strokeColor: theme.palette.error.main, strokeColor: theme.palette.error.main,
@@ -85,7 +82,7 @@ const NetworkCharts = ({ eth0Data, dateRange }) => {
}, },
{ {
type: "network-drops", type: "network-drops",
data: eth0Data, data: ethernetData,
dataKeys: ["drops"], dataKeys: ["drops"],
heading: t("drops"), heading: t("drops"),
strokeColor: theme.palette.warning.main, strokeColor: theme.palette.warning.main,
@@ -127,7 +124,7 @@ const NetworkCharts = ({ eth0Data, dateRange }) => {
}; };
NetworkCharts.propTypes = { NetworkCharts.propTypes = {
eth0Data: PropTypes.array.isRequired, ethernetData: PropTypes.array.isRequired,
dateRange: PropTypes.string.isRequired, dateRange: PropTypes.string.isRequired,
}; };
@@ -1,27 +1,19 @@
// NetworkStatBoxes.jsx
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import StatusBoxes from "../../../../../Components/StatusBoxes"; import StatusBoxes from "../../../../../Components/StatusBoxes";
import StatBox from "../../../../../Components/StatBox"; import StatBox from "../../../../../Components/StatBox";
import { Typography } from "@mui/material"; import { Typography } from "@mui/material";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useHardwareUtils } from "../../Hooks/useHardwareUtils";
function formatBytes(bytes) {
if (bytes === 0 || bytes == null) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB", "TB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
}
// Format numbers with commas
function formatNumber(num) { function formatNumber(num) {
return num != null ? num.toLocaleString() : "0"; return num != null ? num.toLocaleString() : "0";
} }
const NetworkStatBoxes = ({ shouldRender, net }) => { const NetworkStatBoxes = ({ shouldRender, net, ifaceName }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const filtered = const { formatBytes } = useHardwareUtils();
net?.filter((iface) => iface.name === "en0" || iface.name === "wlan0") || [];
const filtered = net?.filter((iface) => iface.name === ifaceName) || [];
if (!net?.length) { if (!net?.length) {
return <Typography>{t("noNetworkStatsAvailable")}</Typography>; return <Typography>{t("noNetworkStatsAvailable")}</Typography>;
@@ -83,6 +75,7 @@ NetworkStatBoxes.propTypes = {
err_out: PropTypes.number, err_out: PropTypes.number,
}) })
), ),
ifaceName: PropTypes.string.isRequired,
}; };
export default NetworkStatBoxes; export default NetworkStatBoxes;
@@ -3,37 +3,40 @@ import NetworkStatBoxes from "./NetworkStatBoxes";
import NetworkCharts from "./NetworkCharts"; import NetworkCharts from "./NetworkCharts";
import MonitorTimeFrameHeader from "../../../../../Components/MonitorTimeFrameHeader"; import MonitorTimeFrameHeader from "../../../../../Components/MonitorTimeFrameHeader";
const getNetworkInterfaceData = (checks) => { const getInterfaceName = (net) => {
const interfaceNames = ["eth0", "Ethernet", "en0"]; const interfaceNames = ["eth0", "Ethernet", "en0"];
const found = (net || []).find((iface) => interfaceNames.includes(iface.name));
return found ? found.name : null;
};
const getNetworkInterfaceData = (checks, ifaceName) => {
return (checks || []) return (checks || [])
.map((check) => { .map((check) => {
const networkInterface = (check.net || []).find((iface) => const networkInterface = (check.net || []).find(
interfaceNames.includes(iface.name) (iface) => iface.name === ifaceName
); );
if (!networkInterface) return null;
if (!networkInterface) {
return null;
}
return { return {
_id: check._id, _id: check._id,
bytesPerSec: networkInterface.avgBytesRecv, bytesPerSec: networkInterface.deltaBytesRecv,
packetsPerSec: networkInterface.avgPacketsRecv, packetsPerSec: networkInterface.deltaPacketsRecv,
errors: networkInterface.avgErrOut ?? 0, errors: networkInterface.deltaErrOut ?? 0,
drops: networkInterface.avgDropOut ?? 0, drops: networkInterface.deltaDropOut ?? 0,
}; };
}) })
.filter(Boolean); .filter(Boolean);
}; };
const Network = ({ net, checks, isLoading, dateRange, setDateRange }) => { const Network = ({ net, checks, isLoading, dateRange, setDateRange }) => {
const eth0Data = getNetworkInterfaceData(checks); const ifaceName = getInterfaceName(net);
const ethernetData = getNetworkInterfaceData(checks, ifaceName);
return ( return (
<> <>
<NetworkStatBoxes <NetworkStatBoxes
shouldRender={!isLoading} shouldRender={!isLoading}
net={net} net={net}
ifaceName={ifaceName}
/> />
<MonitorTimeFrameHeader <MonitorTimeFrameHeader
isLoading={isLoading} isLoading={isLoading}
@@ -41,7 +44,7 @@ const Network = ({ net, checks, isLoading, dateRange, setDateRange }) => {
setDateRange={setDateRange} setDateRange={setDateRange}
/> />
<NetworkCharts <NetworkCharts
eth0Data={eth0Data} ethernetData={ethernetData}
dateRange={dateRange} dateRange={dateRange}
/> />
</> </>
@@ -57,7 +57,7 @@ const useHardwareUtils = () => {
if (GB >= 1) { if (GB >= 1) {
return ( return (
<> <>
{Number(GB.toFixed(0))} {Number(GB.toFixed(2))}
{space ? " " : ""} {space ? " " : ""}
<Typography component="span">{t("gb")}</Typography> <Typography component="span">{t("gb")}</Typography>
</> </>
@@ -65,7 +65,7 @@ const useHardwareUtils = () => {
} else { } else {
return ( return (
<> <>
{Number(MB.toFixed(0))} {Number(MB.toFixed(2))}
{space ? " " : ""} {space ? " " : ""}
<Typography component="span">{t("mb")}</Typography> <Typography component="span">{t("mb")}</Typography>
</> </>
@@ -73,6 +73,26 @@ const useHardwareUtils = () => {
} }
}; };
const formatBytesString = (bytes, space = false) => {
if (
bytes === undefined ||
bytes === null ||
typeof bytes !== "number" ||
bytes === 0
) {
return `0${space ? " " : ""}${t("gb")}`;
}
const GB = bytes / (1024 * 1024 * 1024);
const MB = bytes / (1024 * 1024);
if (GB >= 1) {
return `${Number(GB.toFixed(2))}${space ? " " : ""}${t("gb")}`;
} else {
return `${Number(MB.toFixed(2))}${space ? " " : ""}${t("mb")}`;
}
};
/** /**
* Converts a decimal value to a percentage * Converts a decimal value to a percentage
* *
@@ -134,6 +154,7 @@ const useHardwareUtils = () => {
decimalToPercentage, decimalToPercentage,
buildTemps, buildTemps,
getDimensions, getDimensions,
formatBytesString,
}; };
}; };
@@ -343,6 +343,17 @@ class MonitorModule {
const stats = hardwareStats[0]; const stats = hardwareStats[0];
if (stats?.net?.length) {
const elapsedSeconds = (new Date(dates.end).getTime() - new Date(dates.start).getTime()) / 1000;
stats.net = stats.net.map((iface) => ({
...iface,
bytesSentPerSec: iface.deltaBytesSent / elapsedSeconds,
bytesRecvPerSec: iface.deltaBytesRecv / elapsedSeconds,
packetsSentPerSec: iface.deltaPacketsSent / elapsedSeconds,
packetsRecvPerSec: iface.deltaPacketsRecv / elapsedSeconds,
}));
}
return { return {
...monitor.toObject(), ...monitor.toObject(),
stats, stats,
@@ -395,7 +395,7 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
"$$netIndex", "$$netIndex",
], ],
}, },
avgBytesSent: { deltaBytesSent: {
$subtract: [ $subtract: [
{ {
$arrayElemAt: [ $arrayElemAt: [
@@ -417,7 +417,7 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
}, },
], ],
}, },
avgBytesRecv: { deltaBytesRecv: {
$subtract: [ $subtract: [
{ {
$arrayElemAt: [ $arrayElemAt: [
@@ -439,7 +439,7 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
}, },
], ],
}, },
avgPacketsSent: { deltaPacketsSent: {
$subtract: [ $subtract: [
{ {
$arrayElemAt: [ $arrayElemAt: [
@@ -461,7 +461,7 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
}, },
], ],
}, },
avgPacketsRecv: { deltaPacketsRecv: {
$subtract: [ $subtract: [
{ {
$arrayElemAt: [ $arrayElemAt: [
@@ -483,6 +483,120 @@ const buildHardwareDetailsPipeline = (monitor, dates, dateString) => {
}, },
], ],
}, },
deltaErrIn: {
$subtract: [
{
$arrayElemAt: [
{
$map: {
input: { $arrayElemAt: ["$net", { $subtract: [{ $size: "$net" }, 1] }] },
as: "iface",
in: "$$iface.err_in",
},
},
"$$netIndex",
],
},
{
$arrayElemAt: [{ $map: { input: { $arrayElemAt: ["$net", 0] }, as: "iface", in: "$$iface.err_in" } }, "$$netIndex"],
},
],
},
deltaErrOut: {
$subtract: [
{
$arrayElemAt: [
{
$map: {
input: { $arrayElemAt: ["$net", { $subtract: [{ $size: "$net" }, 1] }] },
as: "iface",
in: "$$iface.err_out",
},
},
"$$netIndex",
],
},
{
$arrayElemAt: [{ $map: { input: { $arrayElemAt: ["$net", 0] }, as: "iface", in: "$$iface.err_out" } }, "$$netIndex"],
},
],
},
deltaDropIn: {
$subtract: [
{
$arrayElemAt: [
{
$map: {
input: { $arrayElemAt: ["$net", { $subtract: [{ $size: "$net" }, 1] }] },
as: "iface",
in: "$$iface.drop_in",
},
},
"$$netIndex",
],
},
{
$arrayElemAt: [{ $map: { input: { $arrayElemAt: ["$net", 0] }, as: "iface", in: "$$iface.drop_in" } }, "$$netIndex"],
},
],
},
deltaDropOut: {
$subtract: [
{
$arrayElemAt: [
{
$map: {
input: { $arrayElemAt: ["$net", { $subtract: [{ $size: "$net" }, 1] }] },
as: "iface",
in: "$$iface.drop_out",
},
},
"$$netIndex",
],
},
{
$arrayElemAt: [{ $map: { input: { $arrayElemAt: ["$net", 0] }, as: "iface", in: "$$iface.drop_out" } }, "$$netIndex"],
},
],
},
deltaFifoIn: {
$subtract: [
{
$arrayElemAt: [
{
$map: {
input: { $arrayElemAt: ["$net", { $subtract: [{ $size: "$net" }, 1] }] },
as: "iface",
in: "$$iface.fifo_in",
},
},
"$$netIndex",
],
},
{
$arrayElemAt: [{ $map: { input: { $arrayElemAt: ["$net", 0] }, as: "iface", in: "$$iface.fifo_in" } }, "$$netIndex"],
},
],
},
deltaFifoOut: {
$subtract: [
{
$arrayElemAt: [
{
$map: {
input: { $arrayElemAt: ["$net", { $subtract: [{ $size: "$net" }, 1] }] },
as: "iface",
in: "$$iface.fifo_out",
},
},
"$$netIndex",
],
},
{
$arrayElemAt: [{ $map: { input: { $arrayElemAt: ["$net", 0] }, as: "iface", in: "$$iface.fifo_out" } }, "$$netIndex"],
},
],
},
}, },
}, },
}, },