-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inaccurate memory usage chart for various data formats (#81)
Co-authored-by: pawel.wesolowski <[email protected]>
- Loading branch information
Showing
5 changed files
with
41 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,29 @@ | ||
import MemoryUnit from "../../types/MemoryUnit"; | ||
|
||
const round = (value: number): string => value.toFixed(2); | ||
|
||
const convert = (value: number): string => { | ||
if (value > 1000000) { | ||
return `${round(value / 1000000)} GB`; | ||
const convertMiB = (value: number): string => { | ||
const valueInMB = value * 1.048576; | ||
const valueInGB = value * 0.001048576; | ||
|
||
if (valueInGB > 1) { | ||
return `${round(valueInGB)} GB`; | ||
} | ||
return `${round(valueInMB)} MB`; | ||
}; | ||
|
||
const convertKiB = (value: number): string => { | ||
const valueInMB = value * 0.001024; | ||
const valueInGB = valueInMB / 1024; | ||
|
||
if (valueInGB > 1) { | ||
return `${round(valueInGB)} GB`; | ||
} | ||
return `${round(value / 1000)} MB`; | ||
return `${round(valueInMB)} MB`; | ||
}; | ||
|
||
// perform a "best effort" conversion to GBs | ||
export default function | ||
labelFormatter(value: string | number | Array<string | number>): string { | ||
return convert(value as number); | ||
export default function labelFormatter(value: string | number | Array<string | number>, unit: MemoryUnit): string { | ||
const numericValue = Number(value); | ||
return unit === MemoryUnit.MiB ? convertMiB(numericValue) : convertKiB(numericValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
enum MemoryUnit { | ||
MiB = 'MiB', | ||
KiB = 'KiB' | ||
} | ||
|
||
export default MemoryUnit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters