mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Fix ResourceQuotaDetails quotas display bugs (#5909)
This commit is contained in:
parent
08cc9101d9
commit
d9785ad4dc
@ -3,18 +3,38 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* 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
|
// Helper to convert CPU K8S units to numbers
|
||||||
|
|
||||||
const thousand = 1000;
|
const unitConverters = new Map([
|
||||||
const million = thousand * thousand;
|
["n", 1000 ** -3],
|
||||||
const shortBillion = thousand * million;
|
["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 cpuUnitsRegex = TypedRegEx("^(?<digits>[+-]?[0-9.]+(e[-+]?[0-9]+)?)(?<unit>[EinumkKMGTP]*)$");
|
||||||
const cpuNum = parseInt(cpu);
|
|
||||||
|
|
||||||
if (cpu.includes("m")) return cpuNum / thousand;
|
export function cpuUnitsToNumber(value: string) {
|
||||||
if (cpu.includes("u")) return cpuNum / million;
|
const match = cpuUnitsRegex.captures(value);
|
||||||
if (cpu.includes("n")) return cpuNum / shortBillion;
|
|
||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,7 +19,7 @@ import logger from "../../../common/logger";
|
|||||||
export interface ResourceQuotaDetailsProps extends KubeObjectDetailsProps<ResourceQuota> {
|
export interface ResourceQuotaDetailsProps extends KubeObjectDetailsProps<ResourceQuota> {
|
||||||
}
|
}
|
||||||
|
|
||||||
function transformUnit(name: string, value: string): number {
|
function transformUnit(name: string, value: string): number | undefined {
|
||||||
if (name.includes("memory") || name.includes("storage")) {
|
if (name.includes("memory") || name.includes("storage")) {
|
||||||
return unitsToBytes(value);
|
return unitsToBytes(value);
|
||||||
}
|
}
|
||||||
@ -36,23 +36,38 @@ function renderQuotas(quota: ResourceQuota): JSX.Element[] {
|
|||||||
|
|
||||||
return object.entries(hard)
|
return object.entries(hard)
|
||||||
.filter(hasDefinedTupleValue)
|
.filter(hasDefinedTupleValue)
|
||||||
.map(([name, value]) => {
|
.map(([name, rawMax]) => {
|
||||||
const current = transformUnit(name, value);
|
const rawCurrent = used[name] ?? "0";
|
||||||
const max = transformUnit(name, value);
|
const current = transformUnit(name, rawCurrent);
|
||||||
const usage = max === 0 ? 100 : Math.ceil(current / max * 100); // special case 0 max as always 100% usage
|
const max = transformUnit(name, rawMax);
|
||||||
|
|
||||||
|
if (current === undefined || max === undefined) {
|
||||||
|
return (
|
||||||
|
<div key={name} className={cssNames("param", kebabCase(name))}>
|
||||||
|
<span className="title">{name}</span>
|
||||||
|
<span className="value">
|
||||||
|
{`${rawCurrent} / ${rawMax}`}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const usage = max === 0
|
||||||
|
? 100 // special case 0 max as always 100% usage
|
||||||
|
: current / max * 100;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div key={name} className={cssNames("param", kebabCase(name))}>
|
<div key={name} className={cssNames("param", kebabCase(name))}>
|
||||||
<span className="title">{name}</span>
|
<span className="title">{name}</span>
|
||||||
<span className="value">
|
<span className="value">
|
||||||
{`${used[name]} / ${value}`}
|
{`${rawCurrent} / ${rawMax}`}
|
||||||
</span>
|
</span>
|
||||||
<LineProgress
|
<LineProgress
|
||||||
max={max}
|
max={max}
|
||||||
value={current}
|
value={current}
|
||||||
tooltip={(
|
tooltip={(
|
||||||
<p>
|
<p>
|
||||||
{`Set: ${value}. Usage: ${usage}%`}
|
{`Set: ${rawMax}. Usage: ${+usage.toFixed(2)}%`}
|
||||||
</p>
|
</p>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -82,7 +82,7 @@ export class PodStore extends KubeObjectStore<Pod, PodApi> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cpu: total.cpu + cpuUnitsToNumber(cpu),
|
cpu: total.cpu + (cpuUnitsToNumber(cpu) ?? 0),
|
||||||
memory: total.memory + unitsToBytes(memory),
|
memory: total.memory + unitsToBytes(memory),
|
||||||
};
|
};
|
||||||
}, empty);
|
}, empty);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user