Format all bytes using helpers, even the ones that are already MiBs

This commit is contained in:
Michael Lange 2021-03-26 17:02:54 -07:00
parent 2a614b18a2
commit bf6fb48ca6
14 changed files with 42 additions and 69 deletions

View File

@ -1,7 +1,7 @@
import Component from '@ember/component';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import { formatScheduledBytes } from 'nomad-ui/utils/units';
import { formatScheduledBytes, formatBytes } from 'nomad-ui/utils/units';
import { tagName } from '@ember-decorators/component';
import classic from 'ember-classic-decorator';
@ -35,13 +35,13 @@ export default class AllocationStat extends Component {
@computed('metric', 'stat.used')
get formattedStat() {
if (!this.stat) return undefined;
if (this.metric === 'memory') return formatScheduledBytes(this.stat.used);
if (this.metric === 'memory') return formatBytes(this.stat.used);
return this.stat.used;
}
@computed('metric', 'statsTracker.{reservedMemory,reservedCPU}')
get formattedReserved() {
if (this.metric === 'memory') return `${this.statsTracker.reservedMemory} MiB`;
if (this.metric === 'memory') return formatBytes(this.statsTracker.reservedMemory, 'MiB');
if (this.metric === 'cpu') return `${this.statsTracker.reservedCPU} MHz`;
return undefined;
}

View File

@ -34,7 +34,7 @@
{{#if (eq this.metric "cpu")}}
<strong>{{this.data.lastObject.used}} MHz</strong> / {{this.reservedAmount}} MHz Total
{{else if (eq this.metric "memory")}}
<strong>{{format-scheduled-bytes this.data.lastObject.used}}</strong> / {{this.reservedAmount}} MiB Total
<strong>{{format-scheduled-bytes this.data.lastObject.used}}</strong> / {{format-scheduled-bytes this.reservedAmount start="MiB"}} Total
{{else}}
<strong>{{this.data.lastObject.used}}</strong> / {{this.reservedAmount}} Total
{{/if}}

View File

@ -20,7 +20,7 @@
{{#if (eq this.metric "cpu")}}
<strong>{{this.data.lastObject.used}} MHz</strong> / {{this.reservedAmount}} MHz Total
{{else if (eq this.metric "memory")}}
<strong>{{format-scheduled-bytes this.data.lastObject.used}}</strong> / {{this.reservedAmount}} MiB Total
<strong>{{format-scheduled-bytes this.data.lastObject.used}}</strong> / {{format-scheduled-bytes this.reservedAmount start="MiB"}} Total
{{else}}
<strong>{{this.data.lastObject.used}}</strong> / {{this.reservedAmount}} Total
{{/if}}

View File

@ -14,7 +14,7 @@
{{#if (eq this.metric "cpu")}}
<strong>{{this.data.lastObject.used}} MHz</strong> / {{this.reservedAmount}} MHz Total
{{else if (eq this.metric "memory")}}
<strong>{{format-scheduled-bytes this.data.lastObject.used}}</strong> / {{this.reservedAmount}} MiB Total
<strong>{{format-scheduled-bytes this.data.lastObject.used}}</strong> / {{format-scheduled-bytes this.reservedAmount start="MiB"}} Total
{{else}}
<strong>{{this.data.lastObject.used}}</strong> / {{this.reservedAmount}} Total
{{/if}}

View File

@ -4,13 +4,13 @@ import { formatBytes } from 'nomad-ui/utils/units';
/**
* Bytes Formatter
*
* Usage: {{format-bytes bytes}}
* Usage: {{format-bytes bytes start="KiB"}}
*
* Outputs the bytes reduced to the largest supported unit size for which
* bytes is larger than one.
*/
function formatBytesHelper([bytes]) {
return formatBytes(bytes);
function formatBytesHelper([bytes], { start }) {
return formatBytes(bytes, start);
}
export default Helper.helper(formatBytesHelper);

View File

@ -4,13 +4,13 @@ import { formatScheduledBytes } from 'nomad-ui/utils/units';
/**
* Scheduled Bytes Formatter
*
* Usage: {{format-scheduled-bytes bytes}}
* Usage: {{format-scheduled-bytes bytes start="KiB"}}
*
* Outputs the bytes reduced to the resolution the scheduler
* and job spec operate at.
*/
function formatScheduledBytesHelper([bytes]) {
return formatScheduledBytes(bytes);
function formatScheduledBytesHelper([bytes], { start }) {
return formatScheduledBytes(bytes, start);
}
export default Helper.helper(formatScheduledBytesHelper);

View File

@ -2,7 +2,7 @@ import { test } from 'qunit';
import { currentURL, visit } from '@ember/test-helpers';
import { filesForPath } from 'nomad-ui/mirage/config';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
import { formatBytes } from 'nomad-ui/utils/units';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import Response from 'ember-cli-mirage/response';
@ -130,7 +130,7 @@ export default function browseFilesystem({
const fileRecord = sortedFiles[2];
assert.equal(file.name, fileRecord.name);
assert.ok(file.isFile);
assert.equal(file.size, formatBytes([fileRecord.size]));
assert.equal(file.size, formatBytes(fileRecord.size));
assert.equal(file.lastModified, moment(fileRecord.modTime).fromNow());
});

View File

@ -4,7 +4,7 @@ import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
import { formatBytes } from 'nomad-ui/utils/units';
import moment from 'moment';
import ClientDetail from 'nomad-ui/tests/pages/clients/detail';
import Clients from 'nomad-ui/tests/pages/clients/list';
@ -175,7 +175,10 @@ module('Acceptance | client detail', function(hooks) {
);
assert.equal(
allocationRow.memTooltip,
`${formatBytes([allocStats.resourceUsage.MemoryStats.RSS])} / ${memoryUsed} MiB`,
`${formatBytes(allocStats.resourceUsage.MemoryStats.RSS)} / ${formatBytes(
memoryUsed,
'MiB'
)}`,
'Detailed memory information is in a tooltip'
);
});

View File

@ -4,7 +4,7 @@ import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import moment from 'moment';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
import { formatBytes } from 'nomad-ui/utils/units';
import PluginDetail from 'nomad-ui/tests/pages/storage/plugins/detail';
import Layout from 'nomad-ui/tests/pages/layout';
@ -137,7 +137,10 @@ module('Acceptance | plugin detail', function(hooks) {
);
assert.equal(
allocationRow.memTooltip,
`${formatBytes([allocStats.resourceUsage.MemoryStats.RSS])} / ${memoryUsed} MiB`,
`${formatBytes(allocStats.resourceUsage.MemoryStats.RSS)} / ${formatBytes(
memoryUsed,
'MiB'
)}`,
'Detailed memory information is in a tooltip'
);
});

View File

@ -3,7 +3,7 @@ import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
import { formatBytes } from 'nomad-ui/utils/units';
import TaskGroup from 'nomad-ui/tests/pages/jobs/job/task-group';
import Layout from 'nomad-ui/tests/pages/layout';
import pageSizeSelect from './behaviors/page-size-select';
@ -223,7 +223,10 @@ module('Acceptance | task group detail', function(hooks) {
assert.equal(
allocationRow.memTooltip,
`${formatBytes([allocStats.resourceUsage.MemoryStats.RSS])} / ${memoryUsed} MiB`,
`${formatBytes(allocStats.resourceUsage.MemoryStats.RSS)} / ${formatBytes(
memoryUsed,
'MiB'
)}`,
'Detailed memory information is in a tooltip'
);
});

View File

@ -5,7 +5,7 @@ import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import Topology from 'nomad-ui/tests/pages/topology';
import { reduceToLargestUnit } from 'nomad-ui/helpers/format-bytes';
import { reduceBytes, formatBytes, formatScheduledBytes } from 'nomad-ui/utils/units';
import queryString from 'query-string';
const sumResources = (list, dimension) =>
@ -60,12 +60,9 @@ module('Acceptance | topology', function(hooks) {
assert.equal(Topology.clusterInfoPanel.memoryProgressValue, reservedMem / totalMem);
assert.equal(Topology.clusterInfoPanel.cpuProgressValue, reservedCPU / totalCPU);
const [rNum, rUnit] = reduceToLargestUnit(reservedMem * 1024 * 1024);
const [tNum, tUnit] = reduceToLargestUnit(totalMem * 1024 * 1024);
assert.equal(
Topology.clusterInfoPanel.memoryAbsoluteValue,
`${Math.floor(rNum)} ${rUnit} / ${Math.floor(tNum)} ${tUnit} reserved`
`${formatBytes(reservedMem, 'MiB')} / ${formatBytes(totalMem, 'MiB')} reserved`
);
assert.equal(
@ -199,12 +196,12 @@ module('Acceptance | topology', function(hooks) {
assert.equal(Topology.nodeInfoPanel.memoryProgressValue, reservedMem / totalMem);
assert.equal(Topology.nodeInfoPanel.cpuProgressValue, reservedCPU / totalCPU);
const [rNum, rUnit] = reduceToLargestUnit(reservedMem * 1024 * 1024);
const [tNum, tUnit] = reduceToLargestUnit(totalMem * 1024 * 1024);
assert.equal(
Topology.nodeInfoPanel.memoryAbsoluteValue,
`${Math.floor(rNum)} ${rUnit} / ${Math.floor(tNum)} ${tUnit} reserved`
`${formatScheduledBytes(reservedMem * 1024 * 1024)} / ${formatScheduledBytes(
totalMem,
'MiB'
)} reserved`
);
assert.equal(

View File

@ -4,7 +4,7 @@ import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import a11yAudit from 'nomad-ui/tests/helpers/a11y-audit';
import moment from 'moment';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
import { formatBytes } from 'nomad-ui/utils/units';
import VolumeDetail from 'nomad-ui/tests/pages/storage/volumes/detail';
import Layout from 'nomad-ui/tests/pages/layout';
@ -152,7 +152,10 @@ module('Acceptance | volume detail', function(hooks) {
);
assert.equal(
allocationRow.memTooltip,
`${formatBytes([allocStats.resourceUsage.MemoryStats.RSS])} / ${memoryUsed} MiB`,
`${formatBytes(allocStats.resourceUsage.MemoryStats.RSS)} / ${formatBytes(
memoryUsed,
'MiB'
)}`,
'Detailed memory information is in a tooltip'
);
});

View File

@ -5,7 +5,7 @@ import hbs from 'htmlbars-inline-precompile';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
import sinon from 'sinon';
import RSVP from 'rsvp';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
import { formatBytes } from 'nomad-ui/utils/units';
module('Integration | Component | image file', function(hooks) {
setupRenderingTest(hooks);
@ -81,7 +81,7 @@ module('Integration | Component | image file', function(hooks) {
'Width and height are formatted correctly'
);
assert.ok(
statsEl.textContent.trim().endsWith(formatBytes([commonProperties.size]) + ')'),
statsEl.textContent.trim().endsWith(formatBytes(commonProperties.size) + ')'),
'Human-formatted size is included'
);
});

View File

@ -1,36 +0,0 @@
import { module, test } from 'qunit';
import { formatBytes } from 'nomad-ui/helpers/format-bytes';
module('Unit | Helper | format-bytes', function() {
test('formats null/undefined as 0 bytes', function(assert) {
assert.equal(formatBytes([undefined]), '0 Bytes');
assert.equal(formatBytes([null]), '0 Bytes');
});
test('formats x < 1024 as bytes', function(assert) {
assert.equal(formatBytes([0]), '0 Bytes');
assert.equal(formatBytes([100]), '100 Bytes');
assert.equal(formatBytes([1023]), '1023 Bytes');
});
test('formats 1024 <= x < 1024 * 1024 as KiB', function(assert) {
assert.equal(formatBytes([1024]), '1 KiB');
assert.equal(formatBytes([125952]), '123 KiB');
assert.equal(formatBytes([1024 * 1024 - 1]), '1023 KiB');
});
test('formats 1024 * 1024 <= x < 1024 * 1024 * 1024 as MiB', function(assert) {
assert.equal(formatBytes([1024 * 1024]), '1 MiB');
assert.equal(formatBytes([128974848]), '123 MiB');
});
test('formats 1024 * 1024 * 1024 <= x < 1024 * 1024 * 1024 * 1024 as GiB', function(assert) {
assert.equal(formatBytes([1024 * 1024 * 1024]), '1 GiB');
assert.equal(formatBytes([1024 * 1024 * 1024 * 4]), '4 GiB');
});
test('formats x > 1024 * 1024 * 1024 * 1024 as GiB, since it is the highest allowed unit', function(assert) {
assert.equal(formatBytes([1024 * 1024 * 1024 * 1024]), '1024 GiB');
assert.equal(formatBytes([1024 * 1024 * 1024 * 1024 * 4]), '4096 GiB');
});
});