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-06-29 22:20:15 +00:00
|
|
|
const prefixes = {
|
2023-05-16 16:51:39 +00:00
|
|
|
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 () {
|
2023-05-16 15:18:36 +00:00
|
|
|
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");
|
|
|
|
}
|
2023-06-29 22:20:15 +00:00
|
|
|
const resources = await requestAPI("/user/resources");
|
|
|
|
const instances = await requestAPI("/user/config/cluster");
|
|
|
|
const nodes = await requestAPI("/user/config/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-06-14 22:34:48 +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-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");
|
2023-06-14 04:56:49 +00:00
|
|
|
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 {
|
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-06-14 04:56:49 +00:00
|
|
|
}
|
2023-04-03 21:57:03 +00:00
|
|
|
});
|
|
|
|
}
|
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;
|
2023-04-25 15:20:59 +00:00
|
|
|
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));
|
2023-05-16 16:51:39 +00:00
|
|
|
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
|
|
|
}
|