Skip to content

Commit 458f93b

Browse files
author
Jacob Wejendorp
committed
feat: add support for more quantity units
Adds support for missing decimal units: u, k, M, G, T, P and E. Fixes multiplier for nano from 1e-8 to 1e-9.
1 parent 89d785a commit 458f93b

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/top_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const mockedPodMetrics: PodMetricsList = {
2929
},
3030
timestamp: '2021-09-26T11:57:21Z',
3131
window: '30s',
32-
containers: [{ name: 'nginx', usage: { cpu: '5000000n', memory: '3912Ki' } }],
32+
containers: [{ name: 'nginx', usage: { cpu: '50000000n', memory: '3912Ki' } }],
3333
},
3434
{
3535
metadata: {
@@ -42,7 +42,7 @@ const mockedPodMetrics: PodMetricsList = {
4242
window: '30s',
4343
containers: [
4444
{ name: 'nginx', usage: { cpu: '0', memory: '4012Ki' } },
45-
{ name: 'sidecar', usage: { cpu: '140000000n', memory: '3012Ki' } },
45+
{ name: 'sidecar', usage: { cpu: '1400000000n', memory: '3012Ki' } },
4646
],
4747
},
4848
],

src/util.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,33 @@ export function quantityToScalar(quantity: string): number | bigint {
2828
}
2929
switch (suffix) {
3030
case 'n':
31-
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 100_000_000.0;
31+
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0;
32+
case 'u':
33+
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0;
3234
case 'm':
3335
return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0;
36+
case 'k':
37+
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000);
38+
case 'M':
39+
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000);
40+
case 'G':
41+
return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000);
42+
case 'T':
43+
return (
44+
BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000)
45+
);
46+
case 'P':
47+
return (
48+
BigInt(quantity.substr(0, quantity.length - 1)) *
49+
BigInt(1000 * 1000 * 1000) *
50+
BigInt(1000 * 1000)
51+
);
52+
case 'E':
53+
return (
54+
BigInt(quantity.substr(0, quantity.length - 1)) *
55+
BigInt(1000 * 1000 * 1000) *
56+
BigInt(1000 * 1000 * 1000)
57+
);
3458
case 'Ki':
3559
return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024);
3660
case 'Mi':

src/util_test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,21 @@ describe('Utils', () => {
5454
it('should parse quantities', () => {
5555
expect(quantityToScalar('')).to.equal(0);
5656

57+
expect(quantityToScalar('2n')).to.equal(2 / 1_000_000_000);
58+
expect(quantityToScalar('3u')).to.equal(3 / 1_000_000);
5759
expect(quantityToScalar('100m')).to.equal(0.1);
60+
expect(quantityToScalar('3k')).to.equal(BigInt(3000));
61+
expect(quantityToScalar('3M')).to.equal(BigInt(3 * 1000 * 1000));
62+
expect(quantityToScalar('3G')).to.equal(BigInt(3 * 1000 * 1000 * 1000));
63+
expect(quantityToScalar('5T')).to.equal(BigInt(5 * 1000 * 1000 * 1000) * BigInt(1000));
64+
expect(quantityToScalar('3P')).to.equal(BigInt(3 * 1000 * 1000 * 1000) * BigInt(1000 * 1000));
65+
expect(quantityToScalar('14E')).to.equal(
66+
BigInt(14 * 1000 * 1000 * 1000) * BigInt(1000 * 1000 * 1000),
67+
);
68+
5869
expect(quantityToScalar('0.2')).to.equal(0.2);
5970
expect(quantityToScalar('1976m')).to.equal(1.976);
6071

61-
expect(quantityToScalar('1024')).to.equal(1024);
6272
expect(quantityToScalar('1024')).to.equal(1024);
6373
expect(quantityToScalar('10e3')).to.equal(10000);
6474

0 commit comments

Comments
 (0)