Fix ResourceQuotaDetails quotas display bugs (#5909)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2022-08-05 14:02:35 -04:00
1 parent 08cc9101d9
commit d9785ad4dc
3 files changed
+52 -17

No files matched your search

+29 -9
View File
@@ -3,18 +3,38 @@
* 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([
["n", 1000 ** -3],
["u", 1000 ** -2],
["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("^(?<digits>[+-]?[0-9.]+(e[-+]?[0-9]+)?)(?<unit>[EinumkKMGTP]*)$");
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 conversion = unitConverters.get(unit);
if (conversion === undefined) {
return undefined;
}
return parseFloat(digits) * conversion;
}