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

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

View File

@ -442,17 +442,17 @@ function populateNetworks () {
});
let ordered_keys = getOrdered(networks);
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 icon = document.createElement("img");
icon.src = "images/resources/network.svg";
icon.alt = netID;
icon.dataset.network = netID;
icon.dataset.network = `${prefix}${netID}`;
icon.dataset.netvals = netDetails;
field.appendChild(icon);