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

98 lines
2.4 KiB
React
Raw Normal View History

2025-08-05 20:46:42 +05:30
import PropTypes from "prop-types";
2025-08-03 00:30:04 +05:30
import NetworkStatBoxes from "./NetworkStatBoxes";
import NetworkCharts from "./NetworkCharts";
import MonitorTimeFrameHeader from "../../../../../Components/MonitorTimeFrameHeader";
const Network = ({ net, checks, isLoading, dateRange, setDateRange }) => {
2025-08-03 01:26:13 +05:30
const eth0Data = getEth0TimeSeries(checks);
2025-08-03 00:30:04 +05:30
2025-08-03 01:26:13 +05:30
return (
<>
<NetworkStatBoxes
shouldRender={!isLoading}
net={net}
/>
<MonitorTimeFrameHeader
isLoading={isLoading}
dateRange={dateRange}
setDateRange={setDateRange}
/>
<NetworkCharts
2025-08-05 20:46:42 +05:30
eth0Data={eth0Data}
dateRange={dateRange}
2025-08-03 01:26:13 +05:30
/>
</>
);
2025-08-03 00:30:04 +05:30
};
2025-08-05 20:46:42 +05:30
Network.propTypes = {
net: PropTypes.array,
checks: PropTypes.array,
isLoading: PropTypes.bool.isRequired,
dateRange: PropTypes.string.isRequired,
setDateRange: PropTypes.func.isRequired,
};
2025-08-03 00:30:04 +05:30
export default Network;
/* ---------- Helper functions ---------- */
function getEth0TimeSeries(checks) {
2025-08-03 01:26:13 +05:30
const sorted = [...(checks || [])].sort((a, b) => new Date(a._id) - new Date(b._id));
const series = [];
let prev = null;
2025-08-03 00:30:04 +05:30
2025-08-03 01:26:13 +05:30
for (const check of sorted) {
const eth = (check.net || []).find((iface) => iface.name === "en0");
if (!eth) {
prev = check;
continue;
}
2025-08-05 20:46:42 +05:30
2025-08-03 01:26:13 +05:30
if (prev) {
const prevEth = (prev.net || []).find((iface) => iface.name === "en0");
const t1 = new Date(check._id);
const t0 = new Date(prev._id);
2025-08-05 20:46:42 +05:30
2025-08-03 01:26:13 +05:30
if (!prevEth || isNaN(t1) || isNaN(t0)) {
prev = check;
continue;
}
2025-08-05 20:46:42 +05:30
2025-08-03 01:26:13 +05:30
const dt = (t1 - t0) / 1000;
2025-08-05 20:46:42 +05:30
2025-08-03 01:26:13 +05:30
if (dt > 0) {
2025-08-05 20:46:42 +05:30
const bytesField = eth.avgBytesSent;
const prevBytesField = prevEth.avgBytesSent;
if (bytesField !== undefined && prevBytesField !== undefined) {
const dataPoint = {
_id: check._id,
2025-08-05 20:46:42 +05:30
bytesPerSec: (bytesField - prevBytesField) / dt,
packetsPerSec: (eth.avgPacketsSent - prevEth.avgPacketsSent) / dt,
errors: (eth.avgErrIn ?? 0) + (eth.avgErrOut ?? 0),
drops: 0,
2025-08-05 20:46:42 +05:30
};
series.push(dataPoint);
}
2025-08-03 01:26:13 +05:30
}
}
prev = check;
}
2025-08-03 00:30:04 +05:30
2025-08-05 20:46:42 +05:30
// If we only have one check, create a single data point with absolute values
if (series.length === 0 && sorted.length === 1) {
const check = sorted[0];
const eth = (check.net || []).find((iface) => iface.name === "en0");
if (eth) {
series.push({
_id: check._id,
bytesPerSec: eth.avgBytesSent || 0,
packetsPerSec: eth.avgPacketsSent || 0,
2025-08-05 20:46:42 +05:30
errors: (eth.avgErrIn ?? 0) + (eth.avgErrOut ?? 0),
drops: 0,
});
}
}
return series;
2025-08-03 00:30:04 +05:30
}