2023-05-16 16:51:39 +00:00
|
|
|
import { requestAPI, goToPage, getCookie, setTitleAndHeader } from "./utils.js";
|
2023-04-03 21:57:03 +00:00
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
2023-05-16 16:51:39 +00:00
|
|
|
let prefixes = {
|
|
|
|
1024: [
|
|
|
|
"",
|
|
|
|
"Ki",
|
|
|
|
"Mi",
|
|
|
|
"Gi",
|
|
|
|
"Ti"
|
|
|
|
],
|
|
|
|
1000: [
|
|
|
|
"",
|
|
|
|
"K",
|
|
|
|
"M",
|
|
|
|
"G",
|
|
|
|
"T"
|
|
|
|
]
|
|
|
|
}
|
2023-04-19 03:10:51 +00:00
|
|
|
|
2023-05-16 16:51:39 +00:00
|
|
|
async function init() {
|
2023-05-16 15:18:36 +00:00
|
|
|
setTitleAndHeader();
|
2023-04-19 05:58:30 +00:00
|
|
|
let cookie = document.cookie;
|
|
|
|
if (cookie === "") {
|
|
|
|
goToPage("login.html");
|
|
|
|
}
|
2023-05-12 21:14:49 +00:00
|
|
|
let resources = await requestAPI("/user/resources");
|
|
|
|
let instances = await requestAPI("/user/instances");
|
|
|
|
let nodes = await requestAPI("/user/nodes");
|
2023-05-03 21:06:27 +00:00
|
|
|
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}`;
|
2023-05-12 21:14:49 +00:00
|
|
|
document.querySelector("#nodes").innerText = `Nodes: ${nodes.toString()}`;
|
2023-04-18 23:31:54 +00:00
|
|
|
buildResourceTable(resources, "#resource-table");
|
2023-04-03 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 16:51:39 +00:00
|
|
|
function buildResourceTable(resources, tableid) {
|
2023-04-03 21:57:03 +00:00
|
|
|
|
2023-04-21 22:58:15 +00:00
|
|
|
if (resources instanceof Object) {
|
2023-04-05 22:29:15 +00:00
|
|
|
let table = document.querySelector(tableid);
|
2023-04-05 22:48:36 +00:00
|
|
|
let tbody = table.querySelector("tbody");
|
2023-04-21 22:58:15 +00:00
|
|
|
Object.keys(resources.avail).forEach((element) => {
|
2023-04-05 22:48:36 +00:00
|
|
|
let row = tbody.insertRow();
|
2023-04-05 22:29:15 +00:00
|
|
|
let key = row.insertCell();
|
|
|
|
key.innerText = `${element}`;
|
2023-04-18 23:31:54 +00:00
|
|
|
let used = row.insertCell();
|
2023-04-21 22:58:15 +00:00
|
|
|
used.innerText = `${parseNumber(resources.used[element], resources.units[element])}`;
|
2023-04-05 22:29:15 +00:00
|
|
|
let val = row.insertCell();
|
2023-04-21 22:58:15 +00:00
|
|
|
val.innerText = `${parseNumber(resources.avail[element], resources.units[element])}`;
|
2023-04-18 23:31:54 +00:00
|
|
|
let total = row.insertCell();
|
2023-04-21 22:58:15 +00:00
|
|
|
total.innerText = `${parseNumber(resources.max[element], resources.units[element])}`;
|
2023-04-03 21:57:03 +00:00
|
|
|
});
|
|
|
|
}
|
2023-04-19 03:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseNumber(value, unitData) {
|
|
|
|
let compact = unitData.compact;
|
|
|
|
let multiplier = unitData.multiplier;
|
2023-05-16 16:51:39 +00:00
|
|
|
let base = unitData.base;
|
2023-04-19 03:10:51 +00:00
|
|
|
let unit = unitData.unit;
|
|
|
|
value = multiplier * value;
|
2023-04-25 15:20:59 +00:00
|
|
|
if (value <= 0) {
|
|
|
|
return `0 ${unit}`;
|
|
|
|
}
|
|
|
|
else if (compact) {
|
2023-05-16 16:51:39 +00:00
|
|
|
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
|
|
|
}
|
2023-04-03 21:57:03 +00:00
|
|
|
}
|