From 851ebc36cdaee5235be4764cc66c69982b85db4b Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Sat, 20 Nov 2021 21:36:01 +0100 Subject: [PATCH] utils: add size unit related helpers to parse/auto-scale/format Signed-off-by: Thomas Lamprecht --- src/Utils.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Utils.js b/src/Utils.js index b8ffd85..9ceea62 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -680,6 +680,37 @@ utilities: { 'PB': 1000*1000*1000*1000*1000, }, + parse_size_unit: function(val) { + //let m = val.match(/([.\d])+\s?([KMGTP]?)(i?)B?\s*$/i); + let m = val.match(/(\d+(?:\.\d+)?)\s?([KMGTP]?)(i?)B?\s*$/i); + let size = parseFloat(m[1]); + let scale = m[2].toUpperCase(); + let binary = m[3].toLowerCase(); + + let unit = `${scale}${binary}B`; + let factor = Proxmox.Utils.SizeUnits[unit]; + + return { size, factor, unit, binary }; // for convenience return all we got + }, + + size_unit_to_bytes: function(val) { + let { size, factor } = Proxmox.Utils.parse_size_unit(val); + return size * factor; + }, + + autoscale_size_unit: function(val) { + let { size, factor, binary } = Proxmox.Utils.parse_size_unit(val); + return Proxmox.Utils.format_size(size * factor, binary !== "i"); + }, + + size_unit_ratios: function(a, b) { + a = typeof a !== "undefined" ? a : 0; + b = typeof b !== "undefined" ? b : Infinity; + let aBytes = typeof a === "number" ? a : Proxmox.Utils.size_unit_to_bytes(a); + let bBytes = typeof b === "number" ? b : Proxmox.Utils.size_unit_to_bytes(b); + return aBytes / (bBytes || Infinity); // avoid division by zero + }, + render_upid: function(value, metaData, record) { let task = record.data; let type = task.type || task.worker_type;