From a4fad0e37b02f4128d209a9360dfa9fd470f5443 Mon Sep 17 00:00:00 2001 From: frohikey Date: Sun, 7 Apr 2019 18:29:10 +0200 Subject: [PATCH 1/2] Fix parsing disk space / ram --- client/src/utils/unitHelpers.js | 59 ++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/client/src/utils/unitHelpers.js b/client/src/utils/unitHelpers.js index 22565ed..dbb7fd6 100644 --- a/client/src/utils/unitHelpers.js +++ b/client/src/utils/unitHelpers.js @@ -1,6 +1,7 @@ import _ from 'lodash'; const RAM_TYPES = ['Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei']; +const UNITS = ['B', 'K', 'M', 'G', 'T', 'P', 'E']; export const TO_GB = 1024 * 1024 * 1024; export const TO_ONE_M_CPU = 1000000; @@ -9,29 +10,55 @@ export const TO_ONE_CPU = 1000000000; export function parseDiskSpace(value) { if (!value) return 0; - const number = parseInt(value, 10); - if (value.endsWith('Ki')) return number * 1024; - if (value.endsWith('Mi')) return number * 1024 * 1024; - if (value.endsWith('Gi')) return number * 1024 * 1024 * 1024; - if (value.endsWith('Ti')) return number * 1024 * 1024 * 1024 * 1024; - if (value.endsWith('Pi')) return number * 1024 * 1024 * 1024 * 1024 * 1024; - if (value.endsWith('Ei')) return number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024; + const groups = value.match(/(\d+)([BKMGTPEe])?(i)?(\d+)?/); + const number = parseInt(groups[1], 10); - return number; + // number ex. 1000 + if (groups[2] === undefined) { + return number; + } + + // number with exponent ex. 1e3 + if (groups[4] !== undefined) { + return number * (10 ** parseInt(groups[4], 10)); + } + + const unitIndex = _.indexOf(UNITS, groups[2]); + + // Unit + i ex. 1Ki + if (groups[3] !== undefined) { + return number * (1024 ** unitIndex); + } + + // Unit ex. 1K + return number * (1000 ** unitIndex); } export function parseRam(value) { if (!value) return 0; - const number = parseInt(value, 10); - if (value.endsWith('Ki')) return number * 1024; - if (value.endsWith('Mi')) return number * 1024 * 1024; - if (value.endsWith('Gi')) return number * 1024 * 1024 * 1024; - if (value.endsWith('Ti')) return number * 1024 * 1024 * 1024 * 1024; - if (value.endsWith('Pi')) return number * 1024 * 1024 * 1024 * 1024 * 1024; - if (value.endsWith('Ei')) return number * 1024 * 1024 * 1024 * 1024 * 1024 * 1024; + const groups = value.match(/(\d+)([BKMGTPEe])?(i)?(\d+)?/); + const number = parseInt(groups[1], 10); - return number; + // number ex. 1000 + if (groups[2] === undefined) { + return number; + } + + // number with exponent ex. 1e3 + if (groups[4] !== undefined) { + return number * (10 ** parseInt(groups[4], 10)); + } + + const unitIndex = _.indexOf(UNITS, groups[2]); + + // Unit + i ex. 1Ki + if (groups[3] !== undefined) { + return number * (1024 ** unitIndex); + } + + // Unit ex. 1K + return number * (1000 ** unitIndex); } export function unparseRam(value) { From 7c88f48f99b7ab3a06d89b961724f3c4980b66e7 Mon Sep 17 00:00:00 2001 From: frohikey Date: Sun, 7 Apr 2019 21:52:54 +0200 Subject: [PATCH 2/2] Helper function parseUnitsOfBytes --- client/src/utils/unitHelpers.js | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/client/src/utils/unitHelpers.js b/client/src/utils/unitHelpers.js index dbb7fd6..73d0e30 100644 --- a/client/src/utils/unitHelpers.js +++ b/client/src/utils/unitHelpers.js @@ -8,33 +8,14 @@ export const TO_ONE_M_CPU = 1000000; export const TO_ONE_CPU = 1000000000; export function parseDiskSpace(value) { - if (!value) return 0; - - const groups = value.match(/(\d+)([BKMGTPEe])?(i)?(\d+)?/); - const number = parseInt(groups[1], 10); - - // number ex. 1000 - if (groups[2] === undefined) { - return number; - } - - // number with exponent ex. 1e3 - if (groups[4] !== undefined) { - return number * (10 ** parseInt(groups[4], 10)); - } - - const unitIndex = _.indexOf(UNITS, groups[2]); - - // Unit + i ex. 1Ki - if (groups[3] !== undefined) { - return number * (1024 ** unitIndex); - } - - // Unit ex. 1K - return number * (1000 ** unitIndex); + return parseUnitsOfBytes(value); } export function parseRam(value) { + return parseUnitsOfBytes(value); +} + +function parseUnitsOfBytes(value) { if (!value) return 0; const groups = value.match(/(\d+)([BKMGTPEe])?(i)?(\d+)?/);