From 29a85d367be2656fd3124cef5c187be869c9bd84 Mon Sep 17 00:00:00 2001 From: Martin Krulis Date: Wed, 29 Nov 2017 18:55:24 +0100 Subject: [PATCH] Fixing issue with inadequate bytes pretty printer. --- package.json | 1 - .../AdditionalFilesTableRow.js | 4 +- .../SupplementaryFilesTableRow.js | 4 +- .../ReferenceSolutionsEvaluationsResults.js | 19 ++++--- .../TestResultsTable/TestResultsTable.js | 4 +- .../Submissions/UploadsTable/UploadsTable.js | 55 ++++++++++++------- .../forms/Fields/KiloBytesTextField.js | 9 ++- src/components/helpers/stringFormatters.js | 21 +++++++ .../SourceCodeInfoBox/SourceCodeInfoBox.js | 9 ++- yarn.lock | 6 -- 10 files changed, 82 insertions(+), 50 deletions(-) create mode 100644 src/components/helpers/stringFormatters.js diff --git a/package.json b/package.json index dc719941e..5f23e9812 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,6 @@ "js-yaml": "^3.10.0", "jwt-decode": "^2.0.1", "moment": "^2.18.1", - "pretty-bytes": "^3.0.1", "pretty-ms": "^2.1.0", "prop-types": "^15.5.8", "react": "^15.5.4", diff --git a/src/components/Exercises/AttachedFilesTable/AdditionalFilesTableRow.js b/src/components/Exercises/AttachedFilesTable/AdditionalFilesTableRow.js index c21e5632b..165d7cc2e 100644 --- a/src/components/Exercises/AttachedFilesTable/AdditionalFilesTableRow.js +++ b/src/components/Exercises/AttachedFilesTable/AdditionalFilesTableRow.js @@ -1,6 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; import { FormattedDate, FormattedTime, FormattedMessage } from 'react-intl'; import withLinks from '../../../hoc/withLinks'; import { Button } from 'react-bootstrap'; @@ -25,7 +25,7 @@ const AdditionalFilesTableRow = ({ - {prettyBytes(size)} + {prettyPrintBytes(size)} diff --git a/src/components/Exercises/AttachedFilesTable/SupplementaryFilesTableRow.js b/src/components/Exercises/AttachedFilesTable/SupplementaryFilesTableRow.js index 8035f17ca..3626f472e 100644 --- a/src/components/Exercises/AttachedFilesTable/SupplementaryFilesTableRow.js +++ b/src/components/Exercises/AttachedFilesTable/SupplementaryFilesTableRow.js @@ -1,6 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; import { FormattedDate, FormattedTime, FormattedMessage } from 'react-intl'; import { Button } from 'react-bootstrap'; import Confirm from '../../../components/forms/Confirm'; @@ -26,7 +26,7 @@ const SupplementaryFilesTableRow = ({ } - {prettyBytes(size)} + {prettyPrintBytes(size)} diff --git a/src/components/ReferenceSolutions/ReferenceSolutionsEvaluationsResults/ReferenceSolutionsEvaluationsResults.js b/src/components/ReferenceSolutions/ReferenceSolutionsEvaluationsResults/ReferenceSolutionsEvaluationsResults.js index 1c6bc8502..298557736 100644 --- a/src/components/ReferenceSolutions/ReferenceSolutionsEvaluationsResults/ReferenceSolutionsEvaluationsResults.js +++ b/src/components/ReferenceSolutions/ReferenceSolutionsEvaluationsResults/ReferenceSolutionsEvaluationsResults.js @@ -1,12 +1,12 @@ import React from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage, FormattedDate } from 'react-intl'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; import prettyMs from 'pretty-ms'; import { Table } from 'react-bootstrap'; import Box from '../../widgets/Box'; -const ReferenceSolutionsEvaluationsResults = ({ results, testId, taskId }) => ( +const ReferenceSolutionsEvaluationsResults = ({ results, testId, taskId }) => ( return ( - {result.referenceSolution.description} + + {result.referenceSolution.description} + - {prettyBytes(taskStats.usedMemory)} - {prettyMs(taskStats.usedTime * 1000)} + + {prettyPrintBytes(taskStats.usedMemory)} + + + {prettyMs(taskStats.usedTime * 1000)} + ); })} - -); + ; ReferenceSolutionsEvaluationsResults.propTypes = { results: PropTypes.array.isRequired, diff --git a/src/components/Submissions/TestResultsTable/TestResultsTable.js b/src/components/Submissions/TestResultsTable/TestResultsTable.js index ca240cfc9..5da3cdfb0 100644 --- a/src/components/Submissions/TestResultsTable/TestResultsTable.js +++ b/src/components/Submissions/TestResultsTable/TestResultsTable.js @@ -5,7 +5,7 @@ import { FormattedMessage, FormattedNumber } from 'react-intl'; import { Table, OverlayTrigger, Tooltip } from 'react-bootstrap'; import Icon from 'react-fontawesome'; import prettyMs from 'pretty-ms'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; import exitCodeMapping from '../../helpers/exitCodeMapping'; @@ -184,7 +184,7 @@ const TestResultsTable = ({ results, runtimeEnvironmentId }) => memoryExceeded === false, memoryRatio, memory, - prettyBytes + prettyPrintBytes )} {tickOrCrossAndRatioOrValue( timeExceeded === false, diff --git a/src/components/Submissions/UploadsTable/UploadsTable.js b/src/components/Submissions/UploadsTable/UploadsTable.js index 8c4bba0e1..184ceda84 100644 --- a/src/components/Submissions/UploadsTable/UploadsTable.js +++ b/src/components/Submissions/UploadsTable/UploadsTable.js @@ -1,6 +1,6 @@ import React from 'react'; import PropTypes from 'prop-types'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; import Icon from 'react-fontawesome'; import { Table, Button, ButtonGroup } from 'react-bootstrap'; @@ -13,7 +13,7 @@ const UploadsTable = ({ returnFile, removeFailedFile, retryUploadFile -}) => ( +}) => @@ -24,13 +24,17 @@ const UploadsTable = ({ - {attachedFiles.map(payload => ( + {attachedFiles.map(payload => - - + + - ))} + )} - {uploadingFiles.map(payload => ( + {uploadingFiles.map(payload => - - + + - ))} + )} - {failedFiles.map(payload => ( + {failedFiles.map(payload => - - + + - ))} + )} - {removedFiles.map(payload => ( + {removedFiles.map(payload => - - + + - ))} + )} -
{payload.name}{prettyBytes(payload.file.size)} + {payload.name} + + {prettyPrintBytes(payload.file.size)} +
{payload.name}{prettyBytes(payload.file.size)} + {payload.name} + + {prettyPrintBytes(payload.file.size)} +
{payload.name}{prettyBytes(payload.file.size)} + {payload.name} + + {prettyPrintBytes(payload.file.size)} +
{payload.name}{prettyBytes(payload.file.size)} + {payload.name} + + {prettyPrintBytes(payload.file.size)} +
-); + ; UploadsTable.propTypes = { uploadingFiles: PropTypes.array.isRequired, diff --git a/src/components/forms/Fields/KiloBytesTextField.js b/src/components/forms/Fields/KiloBytesTextField.js index e3a7bb9e9..8cc0c8eeb 100644 --- a/src/components/forms/Fields/KiloBytesTextField.js +++ b/src/components/forms/Fields/KiloBytesTextField.js @@ -1,11 +1,11 @@ import React from 'react'; import PropTypes from 'prop-types'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; import TextField from './TextField'; import { HelpBlock } from 'react-bootstrap'; import { FormattedMessage } from 'react-intl'; -const KiloBytesTextField = ({ input, ...props }) => ( +const KiloBytesTextField = ({ input, ...props }) =>
@@ -13,10 +13,9 @@ const KiloBytesTextField = ({ input, ...props }) => ( id="app.bytesTextField.humanReadable" defaultMessage="Human readable variant:" />{' '} - {prettyBytes(Number(input.value) * 1000)} + {prettyPrintBytes(Number(input.value) * 1024)} -
-); + ; KiloBytesTextField.propTypes = { input: PropTypes.shape({ diff --git a/src/components/helpers/stringFormatters.js b/src/components/helpers/stringFormatters.js new file mode 100644 index 000000000..7b38328de --- /dev/null +++ b/src/components/helpers/stringFormatters.js @@ -0,0 +1,21 @@ +const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + +export function parseBytes(value) { + const bytes = Number(value); + let absValue = Math.abs(bytes); + if (!Number.isFinite(absValue)) return 'infinity'; + + const base = 1024; + let unit = 0; + while (absValue >= base && unit < units.length) { + absValue /= base; + ++unit; + } + const rounded = Math.round(absValue * 1000) / 1000; + return { value: rounded.toString(), unit: units[unit] }; +} + +export function prettyPrintBytes(input) { + const { value, unit } = parseBytes(input); + return `${value} ${unit}`; +} diff --git a/src/components/widgets/SourceCodeInfoBox/SourceCodeInfoBox.js b/src/components/widgets/SourceCodeInfoBox/SourceCodeInfoBox.js index c209f8b47..803ca5f96 100644 --- a/src/components/widgets/SourceCodeInfoBox/SourceCodeInfoBox.js +++ b/src/components/widgets/SourceCodeInfoBox/SourceCodeInfoBox.js @@ -1,15 +1,14 @@ import React from 'react'; import PropTypes from 'prop-types'; import { SimpleInfoBox } from '../InfoBox'; -import prettyBytes from 'pretty-bytes'; +import { prettyPrintBytes } from '../../helpers/stringFormatters'; -const SourceCodeInfoBox = ({ id, name, size, uploadedAt }) => ( +const SourceCodeInfoBox = ({ id, name, size, uploadedAt }) => -); + />; SourceCodeInfoBox.propTypes = { id: PropTypes.string.isRequired, diff --git a/yarn.lock b/yarn.lock index ddb63b352..23bfcdb54 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5814,12 +5814,6 @@ prettier@^1.3.1: version "1.5.3" resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.5.3.tgz#59dadc683345ec6b88f88b94ed4ae7e1da394bfe" -pretty-bytes@^3.0.1: - version "3.0.1" - resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-3.0.1.tgz#27d0008d778063a0b4811bb35c79f1bd5d5fbccf" - dependencies: - number-is-nan "^1.0.0" - pretty-ms@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/pretty-ms/-/pretty-ms-2.1.0.tgz#4257c256df3fb0b451d6affaab021884126981dc"