Skip to content

Commit

Permalink
Add TiB support for display, fix unit to correct one (#15201)
Browse files Browse the repository at this point in the history
* Add TiB support for display, fix unit to correct one

Signed-off-by: Alexis <[email protected]>

* Fix unit test

Signed-off-by: Alexis <[email protected]>
  • Loading branch information
sixeela authored Jun 29, 2021
1 parent b789674 commit 573d97f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
8 changes: 5 additions & 3 deletions src/portal/src/app/shared/units/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ describe('functions in utils.ts should work', () => {
expect(getSizeNumber(10)).toEqual(10);
expect(getSizeNumber(456400)).toEqual('445.70');
expect(getSizeNumber(45640000)).toEqual('43.53');
expect(getSizeNumber(4564000000000)).toEqual('4.15');
});

it('function getSizeUnit() should work', () => {
expect(getSizeUnit).toBeTruthy();
expect(getSizeUnit(4564)).toEqual('KB');
expect(getSizeUnit(4564)).toEqual('KiB');
expect(getSizeUnit(10)).toEqual('Byte');
expect(getSizeUnit(4564000)).toEqual('MB');
expect(getSizeUnit(4564000000)).toEqual('GB');
expect(getSizeUnit(4564000)).toEqual('MiB');
expect(getSizeUnit(4564000000)).toEqual('GiB');
expect(getSizeUnit(4564000000000)).toEqual('TiB');
});
});
18 changes: 12 additions & 6 deletions src/portal/src/app/shared/units/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,13 @@ export const validateLimit = unitContrl => {
export function formatSize(tagSize: string): string {
let size: number = Number.parseInt(tagSize);
if (Math.pow(1024, 1) <= size && size < Math.pow(1024, 2)) {
return (size / Math.pow(1024, 1)).toFixed(2) + "KB";
return (size / Math.pow(1024, 1)).toFixed(2) + "KiB";
} else if (Math.pow(1024, 2) <= size && size < Math.pow(1024, 3)) {
return (size / Math.pow(1024, 2)).toFixed(2) + "MB";
return (size / Math.pow(1024, 2)).toFixed(2) + "MiB";
} else if (Math.pow(1024, 3) <= size && size < Math.pow(1024, 4)) {
return (size / Math.pow(1024, 3)).toFixed(2) + "GB";
return (size / Math.pow(1024, 3)).toFixed(2) + "GiB";
} else if (Math.pow(1024, 4) <= size && size < Math.pow(1024, 5)) {
return (size / Math.pow(1024, 4)).toFixed(2) + "TiB";
} else {
return size + "B";
}
Expand All @@ -590,6 +592,8 @@ export function getSizeNumber(size: number): string | number {
return (size / Math.pow(1024, 2)).toFixed(2);
} else if (Math.pow(1024, 3) <= size && size < Math.pow(1024, 4)) {
return (size / Math.pow(1024, 3)).toFixed(2);
} else if (Math.pow(1024, 4) <= size && size < Math.pow(1024, 5)) {
return (size / Math.pow(1024, 4)).toFixed(2);
} else {
return size;
}
Expand All @@ -601,11 +605,13 @@ export function getSizeNumber(size: number): string | number {
*/
export function getSizeUnit(size: number): string {
if (Math.pow(1024, 1) <= size && size < Math.pow(1024, 2)) {
return "KB";
return "KiB";
} else if (Math.pow(1024, 2) <= size && size < Math.pow(1024, 3)) {
return "MB";
return "MiB";
} else if (Math.pow(1024, 3) <= size && size < Math.pow(1024, 4)) {
return "GB";
return "GiB";
} else if (Math.pow(1024, 4) <= size && size < Math.pow(1024, 5)) {
return "TiB";
} else {
return "Byte";
}
Expand Down

0 comments on commit 573d97f

Please sign in to comment.