-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathformatBytes.ts
58 lines (43 loc) · 1.62 KB
/
formatBytes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
export const formatBytes = (a: number, b = 1) => {
if (!+a) return '0 Bytes';
const c = b < 0 ? 0 : b,
d = Math.floor(Math.log(a) / Math.log(1024));
return `${parseFloat((a / Math.pow(1024, d)).toFixed(c))} ${
['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'][d]
}`;
};
export const convertGibToBytes = (value: number) => {
if (typeof value !== 'number') return 0;
return value * Math.pow(1024, 3);
};
export const HumanizeNumber = (val: number) => {
// Thousands, millions, billions etc..
const s = ['', ' K', ' M', ' B', ' T'];
// Dividing the value by 3.
const sNum = Math.floor(('' + val).length / 3);
// Calculating the precised value.
const sVal = parseFloat((sNum != 0 ? val / Math.pow(1000, sNum) : val).toPrecision(4));
if (sVal % 1 != 0) {
return sVal.toFixed(1) + s[sNum];
}
// Appending the letter to precised val.
return sVal + s[sNum];
};
export const sanitizeEventsCount = (val: any) => {
return typeof val === 'number' ? HumanizeNumber(val) : '0';
};
export const bytesStringToInteger = (str: string) => {
if (!str || typeof str !== 'string') return null;
const strChuncks = str?.split(' ');
return Array.isArray(strChuncks) && !isNaN(Number(strChuncks[0])) ? parseInt(strChuncks[0]) : null;
};
export const sanitizeBytes = (str: any) => {
const size = bytesStringToInteger(str);
return size ? formatBytes(size) : '0 bytes';
};
export const calcCompressionRate = (storageSize: number, ingestionSize: number): string => {
if (ingestionSize === 0) return '0%';
const rate = 100 - (storageSize / ingestionSize) * 100;
if (rate <= 0) return '0%';
return `${rate.toFixed(2)}%`;
};