From e1a23d47c35f0b3d1d17fc5d04841b116ed5a82f Mon Sep 17 00:00:00 2001
From: XK4MiLX <62837435+XK4MiLX@users.noreply.github.com>
Date: Thu, 10 Oct 2024 22:34:58 +0200
Subject: [PATCH] [FIX] Volume size in Volume Browser
---
HomeUI/src/views/apps/Management.vue | 65 +++++++++++++++++++++++-----
1 file changed, 55 insertions(+), 10 deletions(-)
diff --git a/HomeUI/src/views/apps/Management.vue b/HomeUI/src/views/apps/Management.vue
index a3a7afc50..24c89457a 100644
--- a/HomeUI/src/views/apps/Management.vue
+++ b/HomeUI/src/views/apps/Management.vue
@@ -3039,7 +3039,7 @@
:style="getIconColorStyle(storage.used, storage.total)"
:icon="getIconName(storage.used, storage.total)"
scale="1.4"
- /> {{ `${storage.used.toFixed(2)} / ${storage.total.toFixed(2)}` }} GB
+ /> {{ `${convertVolumeSize(storage.used, 'GB', 1, true)} / ${convertVolumeSize(storage.total, 'GB', 1, true)}` }} GB
this.formatDataSize(value, { base: 10, round: 0 }) } },
+ y: { title: { display: true, text: '' }, beginAtZero: true, ticks: { callback: (value) => this.formatDataSize(value, { base: 2, round: 0 }) } },
},
plugins: {
tooltip: {
@@ -7596,10 +7599,10 @@ export default {
label: (tooltipItem) => {
const datasetLabel = tooltipItem.dataset.label;
const dataValue = tooltipItem.raw;
- return `${datasetLabel}: ${this.formatDataSize(dataValue, { base: 10, round: 1 })}`;
+ return `${datasetLabel}: ${this.formatDataSize(dataValue, { base: 2, round: 1 })}`;
},
footer: () => [
- `Available Bind Size: ${this.formatDataSize(this.diskBindLimit, { base: 10, round: 1 })}`,
+ `Available Bind Size: ${this.formatDataSize(this.diskBindLimit, { base: 2, round: 1 })}`,
`Bind Usage (%): ${this.diskUsagePercentage.toFixed(2)}%`,
],
},
@@ -7646,7 +7649,7 @@ export default {
responsive: true,
scales: {
x: { title: { display: true, text: '' } },
- y: { title: { display: true, text: '' }, beginAtZero: true, ticks: { callback: (value) => this.formatDataSize(value, { base: 10, round: 0 }) } },
+ y: { title: { display: true, text: '' }, beginAtZero: true, ticks: { callback: (value) => this.formatDataSize(value, { base: 2, round: 0 }) } },
},
plugins: {
tooltip: {
@@ -7656,7 +7659,7 @@ export default {
label: (tooltipItem) => {
const datasetLabel = tooltipItem.dataset.label;
const dataValue = tooltipItem.raw;
- return `${datasetLabel}: ${this.formatDataSize(dataValue, { base: 10, round: 1 })}`;
+ return `${datasetLabel}: ${this.formatDataSize(dataValue, { base: 2, round: 1 })}`;
},
},
},
@@ -8230,10 +8233,10 @@ export default {
},
async storageStats() {
try {
- this.volumeInfo = await this.executeLocalCommand(`/backup/getvolumedataofcomponent/${this.appName}/${this.selectedAppVolume}/${'GB'}/${2}/${'used,size'}`);
+ this.volumeInfo = await this.executeLocalCommand(`/backup/getvolumedataofcomponent/${this.appName}/${this.selectedAppVolume}/${'B'}/${2}/${'used,size'}`);
this.volumePath = this.volumeInfo.data?.data;
if (this.volumeInfo.data.status === 'success') {
- this.storage.total = this.volumeInfo.data.data.size;
+ this.storage.total = this.getHddByName(this.appSpecification, this.selectedAppVolume) * 1024 * 1024 * 1024;
this.storage.used = this.volumeInfo.data.data.used;
} else {
this.showToast('danger', this.volumeInfo.data.data.message || this.volumeInfo.data.data);
@@ -8480,6 +8483,48 @@ export default {
}
return `https://${ip.replace(/\./g, '-')}-${port}.node.api.runonflux.io/ioutils/fileupload/backup/${this.appName}/${this.restoreRemoteFile}/null/${filename}`;
},
+ convertVolumeSize(size, targetUnit = 'auto', decimal = 0, returnWithoutUnit = true) {
+ const multiplierMap = {
+ B: 1,
+ KB: 1024,
+ MB: 1024 * 1024,
+ GB: 1024 * 1024 * 1024,
+ };
+ // eslint-disable-next-line no-shadow
+ const getSizeWithMultiplier = (size, multiplier) => size / multiplierMap[multiplier.toUpperCase()];
+ const formatResult = (result, unit) => {
+ const formattedResult = unit === 'B' ? result.toFixed(0) : result.toFixed(decimal);
+ return returnWithoutUnit ? formattedResult : `${formattedResult} ${unit}`;
+ };
+
+ const sizeInBytes = +size;
+ // Validate input size
+ if (Number.isNaN(sizeInBytes)) {
+ console.error('Invalid size parameter');
+ return 'N/A';
+ }
+
+ // Auto-select best unit if 'auto' is chosen
+ if (targetUnit === 'auto') {
+ let bestMatchUnit;
+ let bestMatchResult = sizeInBytes;
+ Object.keys(multiplierMap).forEach((unit) => {
+ const result = getSizeWithMultiplier(sizeInBytes, unit);
+ if (result >= 1 && (bestMatchResult === undefined || result < bestMatchResult)) {
+ bestMatchResult = result;
+ bestMatchUnit = unit;
+ }
+ });
+
+ bestMatchUnit = bestMatchUnit || 'B';
+ return formatResult(bestMatchResult, bestMatchUnit);
+ // eslint-disable-next-line no-else-return
+ } else {
+ // Convert to specified target unit
+ const result = getSizeWithMultiplier(sizeInBytes, targetUnit);
+ return formatResult(result, targetUnit);
+ }
+ },
addAndConvertFileSizes(sizes, targetUnit = 'auto', decimal = 2) {
const multiplierMap = {
B: 1,