Skip to content

Commit e903d4c

Browse files
committed
Use 1024 instead of 1000 for byte conversions, rename gloabl.d.ts to global.d.ts
1 parent 050fe30 commit e903d4c

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

resources/scripts/components/dashboard/ServerRow.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Link } from 'react-router-dom';
99
import { Server } from '@/api/server/getServer';
1010
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
1111
import getServerResourceUsage, { ServerStats } from '@/api/server/getServerResourceUsage';
12-
import { bytesToHuman } from '@/helpers';
12+
import { bytesToHuman, megabytesToHuman } from '@/helpers';
1313
import classNames from 'classnames';
1414

1515
// Determines if the current value is in an alarm threshold so we can show it in red rather
@@ -127,7 +127,7 @@ export default ({ server, className }: { server: Server; className: string | und
127127
{bytesToHuman(stats.memoryUsageInBytes)}
128128
</p>
129129
</div>
130-
<p className={'text-xs text-neutral-600 text-center mt-1'}>of {bytesToHuman(server.limits.memory * 1000 * 1000)}</p>
130+
<p className={'text-xs text-neutral-600 text-center mt-1'}>of {megabytesToHuman(server.limits.memory)}</p>
131131
</div>
132132
<div className={'flex-1 ml-4'}>
133133
<div className={'flex justify-center'}>
@@ -148,7 +148,7 @@ export default ({ server, className }: { server: Server; className: string | und
148148
</p>
149149
</div>
150150
<p className={'text-xs text-neutral-600 text-center mt-1'}>
151-
of {bytesToHuman(server.limits.disk * 1000 * 1000)}
151+
of {megabytesToHuman(server.limits.disk)}
152152
</p>
153153
</div>
154154
</React.Fragment>

resources/scripts/components/server/ServerConsole.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import classNames from 'classnames';
77
import { faMemory } from '@fortawesome/free-solid-svg-icons/faMemory';
88
import { faMicrochip } from '@fortawesome/free-solid-svg-icons/faMicrochip';
99
import { faHdd } from '@fortawesome/free-solid-svg-icons/faHdd';
10-
import { bytesToHuman } from '@/helpers';
10+
import { bytesToHuman, megabytesToHuman } from '@/helpers';
1111
import SuspenseSpinner from '@/components/elements/SuspenseSpinner';
1212
import TitledGreyBox from '@/components/elements/TitledGreyBox';
1313
import Can from '@/components/elements/Can';
@@ -112,7 +112,7 @@ export default () => {
112112
className={'mr-1'}
113113
/>
114114
&nbsp;{bytesToHuman(memory)}
115-
<span className={'text-neutral-500'}> / {bytesToHuman(server.limits.memory * 1000 * 1000)}</span>
115+
<span className={'text-neutral-500'}> / {megabytesToHuman(server.limits.memory)}</span>
116116
</p>
117117
<p className={'text-xs mt-2'}>
118118
<FontAwesomeIcon
@@ -121,7 +121,7 @@ export default () => {
121121
className={'mr-1'}
122122
/>
123123
&nbsp;{bytesToHuman(disk)}
124-
<span className={'text-neutral-500'}> / {bytesToHuman(server.limits.disk * 1000 * 1000)}</span>
124+
<span className={'text-neutral-500'}> / {megabytesToHuman(server.limits.disk)}</span>
125125
</p>
126126
</TitledGreyBox>
127127
{!server.isInstalling ?

resources/scripts/helpers.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1+
export const bytesToMegabytes = (bytes: number) => Math.floor(bytes / 1024 / 1024);
2+
3+
export const megabytesToBytes = (mb: number) => Math.floor(mb * 1024 * 1024);
4+
15
export function bytesToHuman (bytes: number): string {
26
if (bytes === 0) {
37
return '0 kB';
48
}
59

6-
const i = Math.floor(Math.log(bytes) / Math.log(1000));
7-
// @ts-ignore
8-
return `${(bytes / Math.pow(1000, i)).toFixed(2) * 1} ${[ 'Bytes', 'kB', 'MB', 'GB', 'TB' ][i]}`;
10+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
11+
return `${Number((bytes / Math.pow(1024, i)).toFixed(2))} ${[ 'Bytes', 'kB', 'MB', 'GB', 'TB' ][i]}`;
912
}
1013

11-
export const bytesToMegabytes = (bytes: number) => Math.floor(bytes / 1000 / 1000);
14+
export function megabytesToHuman (mb: number): string {
15+
return bytesToHuman(megabytesToBytes(mb));
16+
}
1217

1318
export const randomInt = (low: number, high: number) => Math.floor(Math.random() * (high - low) + low);
1419

0 commit comments

Comments
 (0)