1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

resolve comments, add unit tests

Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
Sebastian Malton 2020-08-04 11:17:46 -04:00
parent 55489c0356
commit 0f84cf1016
6 changed files with 35 additions and 23 deletions

View File

@ -4,7 +4,7 @@ import kebabCase from "lodash/kebabCase";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { Trans } from "@lingui/macro"; import { Trans } from "@lingui/macro";
import { DrawerItem, DrawerTitle } from "../drawer"; import { DrawerItem, DrawerTitle } from "../drawer";
import { cpuUnitsToNumber, cssNames, unitsToBytes, compactedUnitsToNumber } from "../../utils"; import { cpuUnitsToNumber, cssNames, unitsToBytes, metricUnitsToNumber } from "../../utils";
import { KubeObjectDetailsProps } from "../kube-object"; import { KubeObjectDetailsProps } from "../kube-object";
import { ResourceQuota, resourceQuotaApi } from "../../api/endpoints/resource-quota.api"; import { ResourceQuota, resourceQuotaApi } from "../../api/endpoints/resource-quota.api";
import { LineProgress } from "../line-progress"; import { LineProgress } from "../line-progress";
@ -21,21 +21,17 @@ function transformUnit(name: string, value: string): number {
if (name.includes("memory") || name.includes("storage")) { if (name.includes("memory") || name.includes("storage")) {
return unitsToBytes(value) return unitsToBytes(value)
} }
if (name.includes("cpu")) { if (name.includes("cpu")) {
return cpuUnitsToNumber(value) return cpuUnitsToNumber(value)
} }
if (onlyNumbers.test(value)) { return metricUnitsToNumber(value);
return parseInt(value);
}
return compactedUnitsToNumber(value);
} }
function renderQuotas(quota: ResourceQuota): JSX.Element[] { function renderQuotas(quota: ResourceQuota): JSX.Element[] {
const { hard = {}, used = {} } = quota.status const { hard = {}, used = {} } = quota.status
return Object.entries(hard) return Object.entries(hard)
.filter(([name]) => used[name]) .filter(([name]) => used[name])
.map(([name, value]) => { .map(([name, value]) => {

View File

@ -1,10 +0,0 @@
const base = 1000;
const suffixes = ["k", "m", "g", "t", "q"];
export function compactedUnitsToNumber(value: string): number {
const justLetters = value.toLowerCase().replace(/[0-9]\./g, '')[0];
const index = suffixes.indexOf(justLetters);
return parseInt(
(parseFloat(value) * Math.pow(base, index + 1)).toFixed(1)
)
}

View File

@ -8,9 +8,8 @@ export function unitsToBytes(value: string) {
return parseFloat(value) return parseFloat(value)
} }
const index = suffixes.findIndex(suffix => const suffix = value.replace(/[0-9]|i|\./g, '');
suffix == value.replace(/[0-9]|i|\./g, '') const index = suffixes.indexOf(suffix);
)
return parseInt( return parseInt(
(parseFloat(value) * Math.pow(base, index + 1)).toFixed(1) (parseFloat(value) * Math.pow(base, index + 1)).toFixed(1)
) )
@ -22,8 +21,10 @@ export function bytesToUnits(bytes: number, precision = 1) {
if (!bytes) { if (!bytes) {
return "N/A" return "N/A"
} }
if (index === 0) { if (index === 0) {
return `${bytes}${sizes[index]}` return `${bytes}${sizes[index]}`
} }
return `${(bytes / (1024 ** index)).toFixed(precision)}${sizes[index]}i` return `${(bytes / (1024 ** index)).toFixed(precision)}${sizes[index]}i`
} }

View File

@ -20,4 +20,4 @@ export * from './formatDuration'
export * from './isReactNode' export * from './isReactNode'
export * from './convertMemory' export * from './convertMemory'
export * from './convertCpu' export * from './convertCpu'
export * from './compactedUnitsToNumber' export * from './metricUnitsToNumber'

View File

@ -0,0 +1,10 @@
const base = 1000;
const suffixes = ["k", "m", "g", "t", "q"];
export function metricUnitsToNumber(value: string): number {
const suffix = value.toLowerCase().slice(-1);
const index = suffixes.indexOf(suffix);
return parseInt(
(parseFloat(value) * Math.pow(base, index + 1)).toFixed(1)
)
}

View File

@ -0,0 +1,15 @@
import { metricUnitsToNumber } from "./metricUnitsToNumber";
describe("metricUnitsToNumber tests", () => {
test("plain number", () => {
expect(metricUnitsToNumber("124")).toStrictEqual(124);
});
test("with k suffix", () => {
expect(metricUnitsToNumber("124k")).toStrictEqual(124000);
});
test("with m suffix", () => {
expect(metricUnitsToNumber("124m")).toStrictEqual(124000000);
});
});