Handle decimals in k8s resource usage calculation (#20514)
When one of the numerator/denominator is a number and other is bigint this was failing. decimals cannot be converted to bigints Signed-off-by: Tomasz Szuba <tszuba@box.com>
This commit is contained in:
@@ -24,18 +24,18 @@ import { SubvalueCell } from '@backstage/core-components';
|
||||
|
||||
describe('pod', () => {
|
||||
describe('currentToDeclaredResourceToPerc', () => {
|
||||
it('10%', () => {
|
||||
const tests: (number | string)[][] = [
|
||||
[10, 100],
|
||||
[10, '100'],
|
||||
['10', 100],
|
||||
['10', '100'],
|
||||
];
|
||||
tests.forEach(([a, b]) => {
|
||||
const result = currentToDeclaredResourceToPerc(a, b);
|
||||
expect(result).toBe('10%');
|
||||
});
|
||||
});
|
||||
it.each([
|
||||
[10, 100],
|
||||
[10, '100'],
|
||||
['10', 100],
|
||||
['10', '100'],
|
||||
['10', 100.0],
|
||||
[10.0, '100'],
|
||||
[10.1, '100'],
|
||||
['10', 100.1],
|
||||
])('%p out of %p gives 10%%', (current, resource) =>
|
||||
expect(currentToDeclaredResourceToPerc(current, resource)).toBe('10%'),
|
||||
);
|
||||
});
|
||||
describe('podStatusToCpuUtil', () => {
|
||||
it('does use correct units', () => {
|
||||
|
||||
@@ -126,8 +126,12 @@ export const currentToDeclaredResourceToPerc = (
|
||||
return `${Math.round((current / resource) * 100)}%`;
|
||||
}
|
||||
|
||||
const numerator: bigint = BigInt(current);
|
||||
const denominator: bigint = BigInt(resource);
|
||||
const numerator: bigint = BigInt(
|
||||
typeof current === 'number' ? Math.round(current) : current,
|
||||
);
|
||||
const denominator: bigint = BigInt(
|
||||
typeof resource === 'number' ? Math.round(resource) : resource,
|
||||
);
|
||||
|
||||
return `${(numerator * BigInt(100)) / denominator}%`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user