Format Size

This commit is contained in:
Sidharth Vinod 2024-01-24 21:59:58 +05:30
parent 6d4b27aacb
commit bea76aa682
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
2 changed files with 26 additions and 5 deletions

View File

@ -108,8 +108,6 @@ jobs:
run: |
pnpm run build:viz
mv stats cypress/snapshots/stats/head
ls -l cypress/snapshots/stats/base
ls -l cypress/snapshots/stats/head
{
echo 'size_diff<<EOF'
npx tsx scripts/size.ts

View File

@ -21,6 +21,25 @@ const readStats = async (path: string): Promise<Record<string, number>> => {
return Object.fromEntries(sizes);
};
const formatBytes = (bytes: number): string => {
if (bytes == 0) {
return '0 Bytes';
}
const base = 1024;
const decimals = 2;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(base));
return parseFloat((bytes / Math.pow(base, i)).toFixed(decimals)) + ' ' + sizes[i];
};
const formatSize = (bytes: number): string => {
const formatted = formatBytes(bytes);
if (formatted.includes('Bytes')) {
return formatted;
}
return `${formatBytes(bytes)} (${bytes} Bytes)`;
};
const percentageDifference = (oldValue: number, newValue: number): string => {
const difference = Math.abs(newValue - oldValue);
const avg = (newValue + oldValue) / 2;
@ -40,9 +59,13 @@ const main = async () => {
.map(([key, value]) => {
const oldValue = oldStats[key];
const delta = value - oldValue;
return [key, oldValue, value, delta, percentageDifference(oldValue, value)].map((v) =>
v.toString()
);
return [
key,
formatSize(oldValue),
formatSize(value),
formatSize(delta),
percentageDifference(oldValue, value),
].map((v) => v.toString());
})
.filter(([, , , delta]) => delta !== '0');
if (diff.length === 0) {