Enhancement: resources network widget (#4327)

This commit is contained in:
shamoon 2024-11-24 22:43:13 -08:00
parent a06964dd17
commit 897309a47c
No known key found for this signature in database
6 changed files with 86 additions and 4 deletions

View file

@ -7,7 +7,7 @@ const logger = createLogger("resources");
const si = require("systeminformation");
export default async function handler(req, res) {
const { type, target } = req.query;
const { type, target, interfaceName = "default" } = req.query;
if (type === "cpu") {
const load = await si.currentLoad();
@ -57,6 +57,32 @@ export default async function handler(req, res) {
});
}
if (type === "network") {
let networkData = await si.networkStats();
let interfaceDefault;
logger.debug("networkData:", JSON.stringify(networkData));
if (interfaceName && interfaceName !== "default") {
networkData = networkData.filter((network) => network.iface === interfaceName).at(0);
if (!networkData) {
return res.status(404).json({
error: "Interface not found",
});
}
} else {
interfaceDefault = await si.networkInterfaceDefault();
networkData = networkData.filter((network) => network.iface === interfaceDefault).at(0);
if (!networkData) {
return res.status(404).json({
error: "Default interface not found",
});
}
}
return res.status(200).json({
network: networkData,
interface: interfaceName !== "default" ? interfaceName : interfaceDefault,
});
}
return res.status(400).json({
error: "invalid type",
});