mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix parsing memory metrics expecting wrong unit suffix (#5585)
This commit is contained in:
parent
71473af796
commit
27adab8d1c
@ -10,6 +10,50 @@ describe("unitsToBytes", () => {
|
|||||||
expect(unitsToBytes("1234")).toBe(1234);
|
expect(unitsToBytes("1234")).toBe(1234);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should parse with B suffix", () => {
|
||||||
|
expect(unitsToBytes("1234B")).toBe(1234);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Ki suffix", () => {
|
||||||
|
expect(unitsToBytes("1234Ki")).toBe(1234 * (1024));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Ki suffix as the same as KiB", () => {
|
||||||
|
expect(unitsToBytes("1234Ki")).toBe(unitsToBytes("1234KiB"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Mi suffix", () => {
|
||||||
|
expect(unitsToBytes("1234Mi")).toBe(1234 * (1024 ** 2));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Mi suffix as the same as MiB", () => {
|
||||||
|
expect(unitsToBytes("1234Mi")).toBe(unitsToBytes("1234MiB"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Gi suffix", () => {
|
||||||
|
expect(unitsToBytes("1234Gi")).toBe(1234 * (1024 ** 3));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Gi suffix as the same as GiB", () => {
|
||||||
|
expect(unitsToBytes("1234Gi")).toBe(unitsToBytes("1234GiB"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Ti suffix", () => {
|
||||||
|
expect(unitsToBytes("1234Ti")).toBe(1234 * (1024 ** 4));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Ti suffix as the same as TiB", () => {
|
||||||
|
expect(unitsToBytes("1234Ti")).toBe(unitsToBytes("1234TiB"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Pi suffix", () => {
|
||||||
|
expect(unitsToBytes("1234Pi")).toBe(1234 * (1024 ** 5));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should parse with Pi suffix as the same as PiB", () => {
|
||||||
|
expect(unitsToBytes("1234Pi")).toBe(unitsToBytes("1234PiB"));
|
||||||
|
});
|
||||||
|
|
||||||
it("given unrelated data, return NaN", () => {
|
it("given unrelated data, return NaN", () => {
|
||||||
expect(unitsToBytes("I am not a number")).toBeNaN();
|
expect(unitsToBytes("I am not a number")).toBeNaN();
|
||||||
});
|
});
|
||||||
@ -64,14 +108,6 @@ describe("bytesToUnits", () => {
|
|||||||
expect(bytesToUnits(1900 * 1024 ** 4)).toBe("1.9PiB");
|
expect(bytesToUnits(1900 * 1024 ** 4)).toBe("1.9PiB");
|
||||||
expect(bytesToUnits(50*(1024 ** 5) + 1)).toBe("50.0PiB");
|
expect(bytesToUnits(50*(1024 ** 5) + 1)).toBe("50.0PiB");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("given a number within the magnitude of 1024^6.., format with EiB", () => {
|
|
||||||
expect(bytesToUnits(1024**6)).toBe("1.0EiB");
|
|
||||||
expect(bytesToUnits(2048**6)).toBe("64.0EiB");
|
|
||||||
expect(bytesToUnits(1900 * 1024 ** 5)).toBe("1.9EiB");
|
|
||||||
expect(bytesToUnits(50*(1024 ** 6) + 1)).toBe("50.0EiB");
|
|
||||||
expect(bytesToUnits(1024**8)).toBe("1048576.0EiB");
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("bytesToUnits -> unitsToBytes", () => {
|
describe("bytesToUnits -> unitsToBytes", () => {
|
||||||
|
|||||||
@ -9,17 +9,18 @@ import * as iter from "./iter";
|
|||||||
// Helper to convert memory from units Ki, Mi, Gi, Ti, Pi to bytes and vise versa
|
// Helper to convert memory from units Ki, Mi, Gi, Ti, Pi to bytes and vise versa
|
||||||
|
|
||||||
const baseMagnitude = 1024;
|
const baseMagnitude = 1024;
|
||||||
const maxMagnitude = ["EiB", baseMagnitude ** 6] as const;
|
const maxMagnitude = ["PiB", baseMagnitude ** 5] as const;
|
||||||
const magnitudes = new Map([
|
const magnitudes = new Map([
|
||||||
["B", 1] as const,
|
["B", 1] as const,
|
||||||
["KiB", baseMagnitude ** 1] as const,
|
["KiB", baseMagnitude ** 1] as const,
|
||||||
["MiB", baseMagnitude ** 2] as const,
|
["MiB", baseMagnitude ** 2] as const,
|
||||||
["GiB", baseMagnitude ** 3] as const,
|
["GiB", baseMagnitude ** 3] as const,
|
||||||
["TiB", baseMagnitude ** 4] as const,
|
["TiB", baseMagnitude ** 4] as const,
|
||||||
["PiB", baseMagnitude ** 5] as const,
|
|
||||||
maxMagnitude,
|
maxMagnitude,
|
||||||
]);
|
]);
|
||||||
const unitRegex = /(?<value>[0-9]+(\.[0-9]*)?)(?<suffix>(B|[KMGTPE]iB))?/;
|
const unitRegex = /(?<value>[0-9]+(\.[0-9]*)?)(?<suffix>(B|[KMGTP]iB?))?/;
|
||||||
|
|
||||||
|
type BinaryUnit = typeof magnitudes extends Map<infer Key, any> ? Key : never;
|
||||||
|
|
||||||
export function unitsToBytes(value: string): number {
|
export function unitsToBytes(value: string): number {
|
||||||
const unitsMatch = value.match(unitRegex);
|
const unitsMatch = value.match(unitRegex);
|
||||||
@ -34,7 +35,8 @@ export function unitsToBytes(value: string): number {
|
|||||||
return parsedValue;
|
return parsedValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const magnitude = magnitudes.get(unitsMatch.groups.suffix as never);
|
const magnitude = magnitudes.get(unitsMatch.groups.suffix as BinaryUnit)
|
||||||
|
?? magnitudes.get(`${unitsMatch.groups.suffix}B` as BinaryUnit);
|
||||||
|
|
||||||
assert(magnitude, "UnitRegex is wrong some how");
|
assert(magnitude, "UnitRegex is wrong some how");
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user