Skip to content

Commit 8316737

Browse files
authored
Merge pull request pterodactyl#2105 from pterodactyl/fix/2071
Fix improper byte conversions
2 parents 45a8593 + a9bb692 commit 8316737

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

resources/scripts/components/dashboard/ServerRow.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { Link } from 'react-router-dom';
55
import { Server } from '@/api/server/getServer';
66
import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
77
import getServerResourceUsage, { ServerStats } from '@/api/server/getServerResourceUsage';
8-
import { bytesToHuman } from '@/helpers';
8+
import { bytesToHuman, megabytesToHuman } from '@/helpers';
99
import tw from 'twin.macro';
1010
import GreyRowBox from '@/components/elements/GreyRowBox';
1111

12+
1213
// Determines if the current value is in an alarm threshold so we can show it in red rather
1314
// than the more faded default style.
1415
const isAlarmState = (current: number, limit: number): boolean => {
15-
const limitInBytes = limit * 1000 * 1000;
16+
const limitInBytes = limit * 1024 * 1024;
1617

1718
return current / limitInBytes >= 0.90;
1819
};
@@ -49,8 +50,9 @@ export default ({ server }: { server: Server }) => {
4950
alarms.memory = isAlarmState(stats.memoryUsageInBytes, server.limits.memory);
5051
alarms.disk = server.limits.disk === 0 ? false : isAlarmState(stats.diskUsageInBytes, server.limits.disk);
5152
}
52-
const disklimit = server.limits.disk !== 0 ? bytesToHuman(server.limits.disk * 1000 * 1000) : 'Unlimited';
53-
const memorylimit = server.limits.memory !== 0 ? bytesToHuman(server.limits.memory * 1000 * 1000) : 'Unlimited';
53+
54+
const disklimit = server.limits.disk !== 0 ? megabytesToHuman(server.limits.disk) : "Unlimited";
55+
const memorylimit = server.limits.memory !== 0 ? megabytesToHuman(server.limits.memory) : "Unlimited";
5456

5557
return (
5658
<GreyRowBox as={Link} to={`/server/${server.id}`}>
@@ -128,6 +130,7 @@ export default ({ server }: { server: Server }) => {
128130
{bytesToHuman(stats.memoryUsageInBytes)}
129131
</p>
130132
</div>
133+
131134
<p css={tw`text-xs text-neutral-600 text-center mt-1`}>of {memorylimit}</p>
132135
</div>
133136
<div css={tw`flex-1 ml-4`}>
@@ -149,6 +152,7 @@ export default ({ server }: { server: Server }) => {
149152
{bytesToHuman(stats.diskUsageInBytes)}
150153
</p>
151154
</div>
155+
152156
<p css={tw`text-xs text-neutral-600 text-center mt-1`}>of {disklimit}</p>
153157
</div>
154158
</React.Fragment>

resources/scripts/components/server/ServerConsole.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { lazy, useEffect, useState } from 'react';
22
import { ServerContext } from '@/state/server';
33
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
44
import { faCircle, faHdd, faMemory, faMicrochip, faServer } from '@fortawesome/free-solid-svg-icons';
5-
import { bytesToHuman } from '@/helpers';
5+
import { bytesToHuman, megabytesToHuman } from '@/helpers';
66
import SuspenseSpinner from '@/components/elements/SuspenseSpinner';
77
import TitledGreyBox from '@/components/elements/TitledGreyBox';
88
import Can from '@/components/elements/Can';
@@ -56,8 +56,8 @@ export default () => {
5656
};
5757
}, [ instance, connected ]);
5858

59-
const disklimit = server.limits.disk ? bytesToHuman(server.limits.disk * 1000 * 1000) : 'Unlimited';
60-
const memorylimit = server.limits.memory ? bytesToHuman(server.limits.memory * 1000 * 1000) : 'Unlimited';
59+
const disklimit = server.limits.disk ? megabytesToHuman(server.limits.disk) : 'Unlimited';
60+
const memorylimit = server.limits.memory ? megabytesToHuman(server.limits.memory) : 'Unlimited';
6161

6262
return (
6363
<PageContentBlock css={tw`flex`}>

resources/scripts/global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare function tw (a: TemplateStringsArray | string): any;

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)