]> git.proxmox.com Git - proxmox-widget-toolkit.git/commitdiff
format/render size: allow one to specifiy if base 2 or 10 is desired
authorThomas Lamprecht <t.lamprecht@proxmox.com>
Tue, 4 May 2021 08:33:28 +0000 (10:33 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Fri, 7 May 2021 15:56:04 +0000 (17:56 +0200)
Storage capacity and usage seems to prefer the base ten (SI unit) use
(makes the capacity a bigger number) while memory (RAM) is normally
preferred to use base 2.

So allow both, mostly to allow consistent displaying of metrics.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/Utils.js

index 52f1314cf6ce91d3e506c2fbdefaff550040aae2..bc602dea70e4b2a8b2c256c2dd2264f5d56670f3 100644 (file)
@@ -608,14 +608,22 @@ utilities: {
        return text;
     },
 
-    format_size: function(size) {
-       let units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
-       let num = 0;
-       while (size >= 1024 && num++ <= units.length) {
-           size = size / 1024;
+    format_size: function(size, useSI) {
+       let units = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
+       let order = 0;
+       const baseValue = useSI ? 1000 : 1024;
+       while (size >= baseValue && order < units.length) {
+           size = size / baseValue;
+           order++;
        }
 
-       return size.toFixed(num > 0?2:0) + " " + units[num] + "B";
+       let unit = units[order], commaDigits = 2;
+       if (order === 0) {
+           commaDigits = 0;
+       } else if (!useSI) {
+           unit += 'i';
+       }
+       return `${size.toFixed(commaDigits)} ${unit}B`;
     },
 
     render_upid: function(value, metaData, record) {
@@ -865,11 +873,11 @@ utilities: {
        );
     },
 
-    render_size_usage: function(val, max) {
+    render_size_usage: function(val, max, useSI) {
        if (max === 0) {
            return gettext('N/A');
        }
-       let fmt = v => Proxmox.Utils.format_size(v);
+       let fmt = v => Proxmox.Utils.format_size(v, useSI);
        let ratio = (val * 100 / max).toFixed(2);
        return ratio + '% (' + Ext.String.format(gettext('{0} of {1}'), fmt(val), fmt(max)) + ')';
     },