ProxmoxAAS-Dashboard/scripts/account.js

101 lines
2.9 KiB
JavaScript
Raw Normal View History

import { requestAPI, goToPage, getCookie, setTitleAndHeader } from "./utils.js";
window.addEventListener("DOMContentLoaded", init);
2023-06-29 22:20:15 +00:00
const prefixes = {
1024: [
"",
"Ki",
"Mi",
"Gi",
"Ti"
],
1000: [
"",
"K",
"M",
"G",
"T"
]
2023-06-29 22:20:15 +00:00
};
2023-04-19 03:10:51 +00:00
2023-06-29 22:20:15 +00:00
async function init () {
setTitleAndHeader();
2023-06-29 22:20:15 +00:00
const cookie = document.cookie;
2023-04-19 05:58:30 +00:00
if (cookie === "") {
goToPage("login.html");
}
const resources = await requestAPI("/user/dynamic/resources");
2023-06-29 22:20:15 +00:00
const instances = await requestAPI("/user/config/cluster");
const 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");
}
2023-06-29 22:20:15 +00:00
function buildResourceTable (resources, tableid) {
2023-04-21 22:58:15 +00:00
if (resources instanceof Object) {
2023-06-29 22:20:15 +00:00
const table = document.querySelector(tableid);
const 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") {
2023-07-05 23:29:14 +00:00
const row = tbody.insertRow();
const key = row.insertCell();
key.innerHTML = `${element}`;
const used = row.insertCell();
parseList(used, resources.used[element]);
const avail = row.insertCell();
parseList(avail, resources.avail[element]);
const total = row.insertCell();
parseList(total, resources.max[element]);
2023-06-23 03:34:44 +00:00
}
else {
2023-06-29 22:20:15 +00:00
const row = tbody.insertRow();
const key = row.insertCell();
2023-06-23 03:34:44 +00:00
key.innerText = `${element}`;
2023-06-29 22:20:15 +00:00
const used = row.insertCell();
2023-06-23 03:34:44 +00:00
used.innerText = `${parseNumber(resources.used[element], resources.resources[element])}`;
2023-06-29 22:20:15 +00:00
const val = row.insertCell();
2023-06-23 03:34:44 +00:00
val.innerText = `${parseNumber(resources.avail[element], resources.resources[element])}`;
2023-06-29 22:20:15 +00:00
const total = row.insertCell();
2023-06-23 03:34:44 +00:00
total.innerText = `${parseNumber(resources.max[element], resources.resources[element])}`;
}
}
});
}
2023-04-19 03:10:51 +00:00
}
2023-06-29 22:20:15 +00:00
function parseNumber (value, unitData) {
const compact = unitData.compact;
const multiplier = unitData.multiplier;
const base = unitData.base;
const unit = unitData.unit;
2023-04-19 03:10:51 +00:00
value = multiplier * value;
if (value <= 0) {
return `0 ${unit}`;
}
else if (compact) {
2023-06-29 22:20:15 +00:00
const exponent = Math.floor(Math.log(value) / Math.log(base));
value = value / base ** exponent;
2023-06-29 22:20:15 +00:00
const unitPrefix = prefixes[base][exponent];
return `${value} ${unitPrefix}${unit}`;
2023-04-19 03:10:51 +00:00
}
else {
2023-04-19 03:12:50 +00:00
return `${value} ${unit}`;
2023-04-19 03:10:51 +00:00
}
2023-06-29 22:20:15 +00:00
}
2023-07-05 23:29:14 +00:00
function parseList (cell, list) {
const listElem = document.createElement("ul");
listElem.style = "list-style-type: none; padding: 0; margin: 0;";
for (const item of list) {
const itemElem = document.createElement("li");
itemElem.innerText = item;
listElem.append(itemElem);
}
cell.append(listElem);
}