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 0f149ba5c7
commit 2bb73ce828
2 changed files with 26 additions and 16 deletions

View File

@ -1,16 +1,25 @@
import {requestAPI, goToPage, getCookie, setTitleAndHeader} from "./utils.js"; 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();
let cookie = document.cookie; let cookie = document.cookie;
if (cookie === "") { if (cookie === "") {
@ -26,7 +35,7 @@ async function init () {
buildResourceTable(resources, "#resource-table"); buildResourceTable(resources, "#resource-table");
} }
function buildResourceTable (resources, tableid) { function buildResourceTable(resources, tableid) {
if (resources instanceof Object) { if (resources instanceof Object) {
let table = document.querySelector(tableid); let table = document.querySelector(tableid);
@ -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);