From 71e7e034bbc317e1be45ef60eb6208344a3dfee1 Mon Sep 17 00:00:00 2001 From: Kylan Hurt Date: Fri, 13 Oct 2023 18:54:28 -0300 Subject: [PATCH] Implement logic for showing gb vs mb vs kb for account resources Add account resource units for gb Clean up determineUnits work --- src/components/AccountCard.vue | 75 ++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/src/components/AccountCard.vue b/src/components/AccountCard.vue index bf0c8082..d1ffaefd 100644 --- a/src/components/AccountCard.vue +++ b/src/components/AccountCard.vue @@ -50,6 +50,10 @@ export default defineComponent({ const none = ref(UInt64.from(0)); const MICRO_UNIT = ref(Math.pow(10, -6)); const KILO_UNIT = ref(Math.pow(10, 3)); + const MEGA_UNIT = ref(Math.pow(10, 6)); + const GIGA_UNIT = ref(Math.pow(10, 9)); + const netUnit = ref('kb'); + const ramUnit = ref('kb'); const resources = ref(0); const delegatedByOthers = ref(0.0); const delegatedToOthers = computed( @@ -152,14 +156,48 @@ export default defineComponent({ } }; - const loadResources = () => { - ram_used.value = fixDec( - Number(accountData.value.ram_usage) / KILO_UNIT.value, - ); + const determineUnit = (size: number) => { + if (size > GIGA_UNIT.value) { + return { + unit: 'gb', + denominator: GIGA_UNIT.value, + }; + } + if (size > MEGA_UNIT.value) { + return { + unit: 'mb', + denominator: MEGA_UNIT.value, + }; + } + return { + unit: 'kb', + denominator: KILO_UNIT.value, + }; + }; + const loadResources = () => { + let ramDenominator; if (props.account !== system_account.value) { + // display max resource unit value for readability + const ramMaxNumber = Number(accountData.value.ram_quota); + const ramUnitResult = determineUnit(ramMaxNumber); + ramDenominator = ramUnitResult.denominator; + ramUnit.value = ramUnitResult.unit; + // get displayed number value ram_max.value = fixDec( - Number(accountData.value.ram_quota) / KILO_UNIT.value, + ramMaxNumber / ramDenominator, + ); + // get units + const netUsedNumber = Number(accountData.value.net_limit.used); + const netMaxNumber = Number(accountData.value.net_limit.max); + const netUnitResult = determineUnit(netMaxNumber); + const netDenominator = netUnitResult.denominator; + netUnit.value = netUnitResult.unit; + net_used.value = fixDec( + netUsedNumber / netDenominator, + ); + net_max.value = fixDec( + netMaxNumber / netDenominator, ); cpu_used.value = fixDec( Number(accountData.value.cpu_limit.used) * MICRO_UNIT.value, @@ -167,13 +205,6 @@ export default defineComponent({ cpu_max.value = fixDec( Number(accountData.value.cpu_limit.max) * MICRO_UNIT.value, ); - net_used.value = fixDec( - Number(accountData.value.net_limit.used) / KILO_UNIT.value, - ); - net_max.value = fixDec( - Number(accountData.value.net_limit.max) / KILO_UNIT.value, - ); - stakedResources.value = Number(accountData.value.total_resources.cpu_weight.value) + Number(accountData.value.total_resources.net_weight.value); @@ -190,6 +221,16 @@ export default defineComponent({ stakedResources.value - stakedNET.value - stakedCPU.value, ); } + const ramUsedNumber = Number(accountData.value.ram_usage); + // only change denominator and units if wasn't already set + if (!ramDenominator) { + const ramUnitResult = determineUnit(ramUsedNumber); + ramDenominator = ramUnitResult.denominator; + ramUnit.value = ramUnitResult.unit; + } + ram_used.value = fixDec( + ramUsedNumber / ramDenominator, + ); }; const setTotalBalance = () => { @@ -373,8 +414,8 @@ export default defineComponent({ return { accountPageSettings, - MICRO_UNIT, - KILO_UNIT, + netUnit, + ramUnit, stakedCPU, stakedNET, cpu_used, @@ -484,7 +525,7 @@ export default defineComponent({ :fraction="net_used" :total="net_max" label="NET" - unit="kb" + :unit="netUnit" />
-
RAM USED: {{ ram_used }} kb
+
RAM USED: {{ ram_used }} {{ ramUnit }}