ProxmoxAAS-Dashboard/scripts/account.js

81 lines
2.2 KiB
JavaScript
Raw Normal View History

import { requestAPI, goToPage, getCookie, setTitleAndHeader } from "./utils.js";
window.addEventListener("DOMContentLoaded", init);
let prefixes = {
1024: [
"",
"Ki",
"Mi",
"Gi",
"Ti"
],
1000: [
"",
"K",
"M",
"G",
"T"
]
}
2023-04-19 03:10:51 +00:00
async function init() {
setTitleAndHeader();
2023-04-19 05:58:30 +00:00
let cookie = document.cookie;
if (cookie === "") {
goToPage("login.html");
}
let resources = await requestAPI("/user/resources");
let instances = await requestAPI("/user/config/cluster");
let nodes = await requestAPI("/user/config/nodes");
document.querySelector("#username").innerText = `Username: ${getCookie("username")}`;
document.querySelector("#pool").innerText = `Pool: ${instances.pool}`;
document.querySelector("#vmid").innerText = `VMID Range: ${instances.vmid.min} - ${instances.vmid.max}`;
document.querySelector("#nodes").innerText = `Nodes: ${nodes.toString()}`;
buildResourceTable(resources, "#resource-table");
}
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");
Object.keys(resources.resources).forEach((element) => {
if (resources.resources[element].display) {
2023-06-23 03:34:44 +00:00
if (resources.resources[element].type === "list") {
}
else {
let row = tbody.insertRow();
let key = row.insertCell();
key.innerText = `${element}`;
let used = row.insertCell();
used.innerText = `${parseNumber(resources.used[element], resources.resources[element])}`;
let val = row.insertCell();
val.innerText = `${parseNumber(resources.avail[element], resources.resources[element])}`;
let total = row.insertCell();
total.innerText = `${parseNumber(resources.max[element], resources.resources[element])}`;
}
}
});
}
2023-04-19 03:10:51 +00:00
}
function parseNumber(value, unitData) {
let compact = unitData.compact;
let multiplier = unitData.multiplier;
let base = unitData.base;
2023-04-19 03:10:51 +00:00
let unit = unitData.unit;
value = multiplier * value;
if (value <= 0) {
return `0 ${unit}`;
}
else if (compact) {
let exponent = Math.floor(Math.log(value) / Math.log(base));
value = value / base ** exponent;
let unitPrefix = prefixes[base][exponent];
2023-04-19 03:10:51 +00:00
return `${value} ${unitPrefix}${unit}`
}
else {
2023-04-19 03:12:50 +00:00
return `${value} ${unit}`;
2023-04-19 03:10:51 +00:00
}
}