ProxmoxAAS-Dashboard/scripts/account.js

64 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-04-21 22:58:15 +00:00
import {requestAPI, goToPage, getCookie} from "./utils.js";
window.addEventListener("DOMContentLoaded", init);
2023-04-19 03:10:51 +00:00
let SIPrefix = [
"",
"Ki",
"Mi",
"Gi",
"Ti"
]
async function init () {
2023-04-19 05:58:30 +00:00
let cookie = document.cookie;
if (cookie === "") {
goToPage("login.html");
}
let user = await requestAPI("/user");
let resources = user.resources;
let instances = user.instances;
2023-04-21 22:58:15 +00:00
document.querySelector("#username").innerHTML += getCookie("username");
document.querySelector("#pool").innerHTML += instances.pool;
document.querySelector("#vmid").innerHTML += `[${instances.vmid.min},${instances.vmid.max}]`;
buildResourceTable(resources, "#resource-table");
}
2023-04-21 22:58:15 +00:00
function buildResourceTable (resources, tableid) {
2023-04-21 22:58:15 +00:00
if (resources instanceof Object) {
let table = document.querySelector(tableid);
let tbody = table.querySelector("tbody");
2023-04-21 22:58:15 +00:00
Object.keys(resources.avail).forEach((element) => {
let row = tbody.insertRow();
let key = row.insertCell();
key.innerText = `${element}`;
let used = row.insertCell();
2023-04-21 22:58:15 +00:00
used.innerText = `${parseNumber(resources.used[element], resources.units[element])}`;
let val = row.insertCell();
2023-04-21 22:58:15 +00:00
val.innerText = `${parseNumber(resources.avail[element], resources.units[element])}`;
let total = row.insertCell();
2023-04-21 22:58:15 +00:00
total.innerText = `${parseNumber(resources.max[element], resources.units[element])}`;
});
}
2023-04-19 03:10:51 +00:00
}
function parseNumber(value, unitData) {
let compact = unitData.compact;
let multiplier = unitData.multiplier;
let unit = unitData.unit;
value = multiplier * value;
if (value <= 0) {
return `0 ${unit}`;
}
else if (compact) {
2023-04-19 03:10:51 +00:00
let exponent = Math.floor(Math.log2(value) / 10);
value = value / 1024 ** exponent;
let unitPrefix = SIPrefix[exponent];
return `${value} ${unitPrefix}${unit}`
}
else {
2023-04-19 03:12:50 +00:00
return `${value} ${unit}`;
2023-04-19 03:10:51 +00:00
}
}