fix network resource usage calculation

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-05-16 16:50:24 +00:00
parent ce901e71d3
commit f27ea02f10
2 changed files with 13 additions and 10 deletions

View File

@ -44,7 +44,7 @@
}, },
"network": { "network": {
"type": "network", "type": "network",
"multiplier": 1, "multiplier": 1000000,
"compact": true, "compact": true,
"unit": "MB/s" "unit": "MB/s"
} }

19
pve.js
View File

@ -1,7 +1,7 @@
import axios from 'axios'; import axios from 'axios';
import { pveAPI, pveAPIToken } from "./vars.js"; import { pveAPI, pveAPIToken } from "./vars.js";
export async function checkAuth (cookies, res, vmpath = null) { export async function checkAuth(cookies, res, vmpath = null) {
let auth = false; let auth = false;
if (vmpath) { if (vmpath) {
let result = await requestPVE(`/${vmpath}/config`, "GET", cookies); let result = await requestPVE(`/${vmpath}/config`, "GET", cookies);
@ -12,13 +12,13 @@ export async function checkAuth (cookies, res, vmpath = null) {
auth = result.status === 200; auth = result.status === 200;
} }
if (!auth) { if (!auth) {
res.status(401).send({auth: auth}); res.status(401).send({ auth: auth });
res.end(); res.end();
} }
return auth; return auth;
} }
export async function requestPVE (path, method, cookies, body = null, token = null) { export async function requestPVE(path, method, cookies, body = null, token = null) {
let url = `${pveAPI}${path}`; let url = `${pveAPI}${path}`;
let content = { let content = {
method: method, method: method,
@ -50,7 +50,7 @@ export async function requestPVE (path, method, cookies, body = null, token = nu
} }
} }
export async function handleResponse (node, result, res) { export async function handleResponse(node, result, res) {
const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay)); const waitFor = delay => new Promise(resolve => setTimeout(resolve, delay));
if (result.data.data) { if (result.data.data) {
let upid = result.data.data; let upid = result.data.data;
@ -78,7 +78,7 @@ export async function handleResponse (node, result, res) {
} }
} }
export async function getUsedResources (req, resourceMeta) { export async function getUsedResources(req, resourceMeta) {
let response = await requestPVE("/cluster/resources", "GET", req.cookies); let response = await requestPVE("/cluster/resources", "GET", req.cookies);
let used = {}; let used = {};
let diskprefixes = []; let diskprefixes = [];
@ -96,11 +96,14 @@ export async function getUsedResources (req, resourceMeta) {
config = config.data.data; config = config.data.data;
for (let key of Object.keys(config)) { for (let key of Object.keys(config)) {
if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") { if (Object.keys(used).includes(key) && resourceMeta[key].type === "numeric") {
used[key] += config[key]; used[key] += Number(config[key]);
} }
else if (diskprefixes.some(prefix => key.startsWith(prefix))) { else if (diskprefixes.some(prefix => key.startsWith(prefix))) {
let diskInfo = await getDiskInfo(instance.node, instance.type, instance.vmid, key); let diskInfo = await getDiskInfo(instance.node, instance.type, instance.vmid, key);
used[diskInfo.storage] += diskInfo.size; used[diskInfo.storage] += Number(diskInfo.size);
}
else if (key.startsWith("net")) {
used.network += Number(config[key].split("rate=")[1].split(",")[0]);
} }
} }
} }
@ -108,7 +111,7 @@ export async function getUsedResources (req, resourceMeta) {
return used; return used;
} }
export async function getDiskInfo (node, type, vmid, disk) { export async function getDiskInfo(node, type, vmid, disk) {
let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken); let config = await requestPVE(`/nodes/${node}/${type}/${vmid}/config`, "GET", null, null, pveAPIToken);
let storageID = config.data.data[disk].split(":")[0]; let storageID = config.data.data[disk].split(":")[0];
let volID = config.data.data[disk].split(",")[0]; let volID = config.data.data[disk].split(",")[0];