From ccb8e7a02e8d4c26b346b753ff09c9cb641d9cb2 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 29 Jul 2022 15:38:28 -0400 Subject: [PATCH] Fix cpu units not being correctly handled Signed-off-by: Sebastian Malton --- src/common/utils/convertCpu.ts | 36 ++++++++++++++----- .../resource-quota-details.tsx | 20 +++++++++-- .../components/+workloads-pods/store.ts | 2 +- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/common/utils/convertCpu.ts b/src/common/utils/convertCpu.ts index 5d537b34e7..3243838a31 100644 --- a/src/common/utils/convertCpu.ts +++ b/src/common/utils/convertCpu.ts @@ -3,18 +3,36 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ +import { TypedRegEx } from "typed-regex"; + // Helper to convert CPU K8S units to numbers -const thousand = 1000; -const million = thousand * thousand; -const shortBillion = thousand * million; +const unitConverters = new Map([ + ["m", 1000 ** -1], // milli + ["", 1000 ** 0], // no units + ["k", 1000 ** 1], + ["M", 1000 ** 2], + ["G", 1000 ** 3], + ["P", 1000 ** 4], + ["T", 1000 ** 5], + ["E", 1000 ** 6], +]); -export function cpuUnitsToNumber(cpu: string) { - const cpuNum = parseInt(cpu); +const cpuUnitsRegex = TypedRegEx("^(?\\d+)(?.*)$"); - if (cpu.includes("m")) return cpuNum / thousand; - if (cpu.includes("u")) return cpuNum / million; - if (cpu.includes("n")) return cpuNum / shortBillion; +export function cpuUnitsToNumber(value: string) { + const match = cpuUnitsRegex.captures(value); - return parseFloat(cpu); + if (!match) { + return undefined; + } + + const { digits, unit } = match; + const convertion = unitConverters.get(unit); + + if (convertion === undefined) { + return undefined; + } + + return parseInt(digits) * convertion; } diff --git a/src/renderer/components/+config-resource-quotas/resource-quota-details.tsx b/src/renderer/components/+config-resource-quotas/resource-quota-details.tsx index 12052b8344..30a7d3496c 100644 --- a/src/renderer/components/+config-resource-quotas/resource-quota-details.tsx +++ b/src/renderer/components/+config-resource-quotas/resource-quota-details.tsx @@ -19,7 +19,7 @@ import logger from "../../../common/logger"; export interface ResourceQuotaDetailsProps extends KubeObjectDetailsProps { } -function transformUnit(name: string, value: string): number { +function transformUnit(name: string, value: string): number | undefined { if (name.includes("memory") || name.includes("storage")) { return unitsToBytes(value); } @@ -40,7 +40,21 @@ function renderQuotas(quota: ResourceQuota): JSX.Element[] { const rawCurrent = used[name] ?? "0"; const current = transformUnit(name, rawCurrent); const max = transformUnit(name, rawMax); - const usage = max === 0 ? 100 : Math.ceil(current / max * 100); // special case 0 max as always 100% usage + + if (current === undefined || max === undefined) { + return ( +
+ {name} + + {`${rawCurrent} / ${rawMax}`} + +
+ ); + } + + const usage = max === 0 + ? 100 // special case 0 max as always 100% usage + : current / max * 100; return (
@@ -53,7 +67,7 @@ function renderQuotas(quota: ResourceQuota): JSX.Element[] { value={current} tooltip={(

- {`Set: ${rawMax}. Usage: ${usage}%`} + {`Set: ${rawMax}. Usage: ${+usage.toFixed(2)}%`}

)} /> diff --git a/src/renderer/components/+workloads-pods/store.ts b/src/renderer/components/+workloads-pods/store.ts index 54adec7837..146eb0b390 100644 --- a/src/renderer/components/+workloads-pods/store.ts +++ b/src/renderer/components/+workloads-pods/store.ts @@ -82,7 +82,7 @@ export class PodStore extends KubeObjectStore { } return { - cpu: total.cpu + cpuUnitsToNumber(cpu), + cpu: total.cpu + (cpuUnitsToNumber(cpu) ?? 0), memory: total.memory + unitsToBytes(memory), }; }, empty);