2023-04-21 22:58:15 +00:00
|
|
|
import {requestAPI, goToPage, getCookie} from "./utils.js";
|
2023-04-03 21:57:03 +00:00
|
|
|
|
|
|
|
window.addEventListener("DOMContentLoaded", init);
|
|
|
|
|
2023-04-19 03:10:51 +00:00
|
|
|
let SIPrefix = [
|
|
|
|
"",
|
|
|
|
"Ki",
|
|
|
|
"Mi",
|
|
|
|
"Gi",
|
|
|
|
"Ti"
|
|
|
|
]
|
|
|
|
|
2023-04-03 21:57:03 +00:00
|
|
|
async function init () {
|
2023-04-19 05:58:30 +00:00
|
|
|
let cookie = document.cookie;
|
|
|
|
if (cookie === "") {
|
|
|
|
goToPage("login.html");
|
|
|
|
}
|
|
|
|
|
2023-04-24 21:47:03 +00:00
|
|
|
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}]`;
|
2023-04-18 23:31:54 +00:00
|
|
|
buildResourceTable(resources, "#resource-table");
|
2023-04-03 21:57:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-21 22:58:15 +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;
|
|
|
|
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-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
|
|
|
}
|
2023-04-03 21:57:03 +00:00
|
|
|
}
|