fix account network usage

Signed-off-by: Arthur Lu <learthurgo@gmail.com>
This commit is contained in:
Arthur Lu 2023-05-16 16:51:39 +00:00
parent d10aeedcf2
commit 6a00a22dc2
2 changed files with 26 additions and 16 deletions

View File

@ -2,13 +2,22 @@ import {requestAPI, goToPage, getCookie, setTitleAndHeader} from "./utils.js";
window.addEventListener("DOMContentLoaded", init); window.addEventListener("DOMContentLoaded", init);
let SIPrefix = [ let prefixes = {
1024: [
"", "",
"Ki", "Ki",
"Mi", "Mi",
"Gi", "Gi",
"Ti" "Ti"
],
1000: [
"",
"K",
"M",
"G",
"T"
] ]
}
async function init() { async function init() {
setTitleAndHeader(); setTitleAndHeader();
@ -48,15 +57,16 @@ function buildResourceTable (resources, tableid) {
function parseNumber(value, unitData) { function parseNumber(value, unitData) {
let compact = unitData.compact; let compact = unitData.compact;
let multiplier = unitData.multiplier; let multiplier = unitData.multiplier;
let base = unitData.base;
let unit = unitData.unit; let unit = unitData.unit;
value = multiplier * value; value = multiplier * value;
if (value <= 0) { if (value <= 0) {
return `0 ${unit}`; return `0 ${unit}`;
} }
else if (compact) { else if (compact) {
let exponent = Math.floor(Math.log2(value) / 10); let exponent = Math.floor(Math.log(value) / Math.log(base));
value = value / 1024 ** exponent; value = value / base ** exponent;
let unitPrefix = SIPrefix[exponent]; let unitPrefix = prefixes[base][exponent];
return `${value} ${unitPrefix}${unit}` return `${value} ${unitPrefix}${unit}`
} }
else { else {

View File

@ -442,17 +442,17 @@ function populateNetworks () {
}); });
let ordered_keys = getOrdered(networks); let ordered_keys = getOrdered(networks);
ordered_keys.forEach(element => { ordered_keys.forEach(element => {
addNetworkLine("networks", `${prefix}${element}`, networks[element]); addNetworkLine("networks", prefix, element, networks[element]);
}); });
} }
function addNetworkLine (fieldset, netID, netDetails) { function addNetworkLine (fieldset, prefix, netID, netDetails) {
let field = document.querySelector(`#${fieldset}`); let field = document.querySelector(`#${fieldset}`);
let icon = document.createElement("img"); let icon = document.createElement("img");
icon.src = "images/resources/network.svg"; icon.src = "images/resources/network.svg";
icon.alt = netID; icon.alt = netID;
icon.dataset.network = netID; icon.dataset.network = `${prefix}${netID}`;
icon.dataset.netvals = netDetails; icon.dataset.netvals = netDetails;
field.appendChild(icon); field.appendChild(icon);