Skip to content

Commit

Permalink
Merge pull request #757 from wejendorp/quantityToScalar-units-653
Browse files Browse the repository at this point in the history
Add support for more quantity units
  • Loading branch information
k8s-ci-robot committed Dec 29, 2021
2 parents 89d785a + 25bda5f commit 39cd5a7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/top_test.ts
Expand Up @@ -29,7 +29,7 @@ const mockedPodMetrics: PodMetricsList = {
},
timestamp: '2021-09-26T11:57:21Z',
window: '30s',
containers: [{ name: 'nginx', usage: { cpu: '5000000n', memory: '3912Ki' } }],
containers: [{ name: 'nginx', usage: { cpu: '50000000n', memory: '3912Ki' } }],
},
{
metadata: {
Expand All @@ -42,7 +42,7 @@ const mockedPodMetrics: PodMetricsList = {
window: '30s',
containers: [
{ name: 'nginx', usage: { cpu: '0', memory: '4012Ki' } },
{ name: 'sidecar', usage: { cpu: '140000000n', memory: '3012Ki' } },
{ name: 'sidecar', usage: { cpu: '1400000000n', memory: '3012Ki' } },
],
},
],
Expand Down
26 changes: 25 additions & 1 deletion src/util.ts
Expand Up @@ -28,9 +28,33 @@ export function quantityToScalar(quantity: string): number | bigint {
}
switch (suffix) {
case 'n':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 100_000_000.0;
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0;
case 'u':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0;
case 'm':
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0;
case 'k':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000);
case 'M':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000);
case 'G':
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000);
case 'T':
return (
BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000)
);
case 'P':
return (
BigInt(quantity.substr(0, quantity.length - 1)) *
BigInt(1000 * 1000 * 1000) *
BigInt(1000 * 1000)
);
case 'E':
return (
BigInt(quantity.substr(0, quantity.length - 1)) *
BigInt(1000 * 1000 * 1000) *
BigInt(1000 * 1000 * 1000)
);
case 'Ki':
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024);
case 'Mi':
Expand Down
12 changes: 11 additions & 1 deletion src/util_test.ts
Expand Up @@ -54,11 +54,21 @@ describe('Utils', () => {
it('should parse quantities', () => {
expect(quantityToScalar('')).to.equal(0);

expect(quantityToScalar('2n')).to.equal(2 / 1_000_000_000);
expect(quantityToScalar('3u')).to.equal(3 / 1_000_000);
expect(quantityToScalar('100m')).to.equal(0.1);
expect(quantityToScalar('3k')).to.equal(BigInt(3000));
expect(quantityToScalar('3M')).to.equal(BigInt(3 * 1000 * 1000));
expect(quantityToScalar('3G')).to.equal(BigInt(3 * 1000 * 1000 * 1000));
expect(quantityToScalar('5T')).to.equal(BigInt(5 * 1000 * 1000 * 1000) * BigInt(1000));
expect(quantityToScalar('3P')).to.equal(BigInt(3 * 1000 * 1000 * 1000) * BigInt(1000 * 1000));
expect(quantityToScalar('14E')).to.equal(
BigInt(14 * 1000 * 1000 * 1000) * BigInt(1000 * 1000 * 1000),
);

expect(quantityToScalar('0.2')).to.equal(0.2);
expect(quantityToScalar('1976m')).to.equal(1.976);

expect(quantityToScalar('1024')).to.equal(1024);
expect(quantityToScalar('1024')).to.equal(1024);
expect(quantityToScalar('10e3')).to.equal(10000);

Expand Down

0 comments on commit 39cd5a7

Please sign in to comment.