add units to resource allocation

This commit is contained in:
Arthur Lu 2023-04-19 03:10:51 +00:00
parent 94ea16d4f5
commit 472952d298

View File

@ -2,6 +2,14 @@ import {requestPVE, requestAPI} from "./utils.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
let SIPrefix = [
"",
"Ki",
"Mi",
"Gi",
"Ti"
]
async function init () { async function init () {
let resources = await requestAPI("/user/resources"); let resources = await requestAPI("/user/resources");
buildResourceTable(resources, "#resource-table"); buildResourceTable(resources, "#resource-table");
@ -10,7 +18,6 @@ async function init () {
function buildResourceTable (object, tableid) { function buildResourceTable (object, tableid) {
if (object instanceof Object) { if (object instanceof Object) {
let table = document.querySelector(tableid); let table = document.querySelector(tableid);
let tbody = table.querySelector("tbody"); let tbody = table.querySelector("tbody");
Object.keys(object.avail).forEach((element) => { Object.keys(object.avail).forEach((element) => {
@ -18,11 +25,27 @@ function buildResourceTable (object, tableid) {
let key = row.insertCell(); let key = row.insertCell();
key.innerText = `${element}`; key.innerText = `${element}`;
let used = row.insertCell(); let used = row.insertCell();
used.innerText = `${object.used[element]}`; used.innerText = `${parseNumber(object.used[element], object.units[element])}`;
let val = row.insertCell(); let val = row.insertCell();
val.innerText = `${object.avail[element]}`; val.innerText = `${parseNumber(object.avail[element], object.units[element])}`;
let total = row.insertCell(); let total = row.insertCell();
total.innerText = `${object.max[element]}`; total.innerText = `${parseNumber(object.max[element], object.units[element])}`;
}); });
} }
}
function parseNumber(value, unitData) {
let compact = unitData.compact;
let multiplier = unitData.multiplier;
let unit = unitData.unit;
value = multiplier * value;
if (compact) {
let exponent = Math.floor(Math.log2(value) / 10);
value = value / 1024 ** exponent;
let unitPrefix = SIPrefix[exponent];
return `${value} ${unitPrefix}${unit}`
}
else {
return value;
}
} }