Skip to content

Commit 4490933

Browse files
authored
Use IEC byte units on action page (#10574)
All of the platform properties are parsed as IEC bytes (powers of 1024), so render the resource usage using IEC bytes as well, instead of SI (powers of 1000). For example if an action requests `EstimatedMemory=1.5GB` in platform props, then we currently render "Requested memory: 1.6GB" which is confusing.
1 parent 8ac86bc commit 4490933

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

app/format/format.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -169,23 +169,25 @@ function truncateDecimalZeroes(numString: string): string {
169169
}
170170

171171
export function bytes(bytes: number | Long) {
172-
bytes = +bytes;
173-
if (bytes < 1e3) {
174-
return bytes + "B";
175-
}
176-
if (bytes < 1e6) {
177-
return truncateDecimalZeroes((bytes / 1e3).toPrecision(4)) + "KB";
178-
}
179-
if (bytes < 1e9) {
180-
return truncateDecimalZeroes((bytes / 1e6).toPrecision(4)) + "MB";
181-
}
182-
if (bytes < 1e12) {
183-
return truncateDecimalZeroes((bytes / 1e9).toPrecision(4)) + "GB";
172+
bytes = Number(bytes);
173+
const units = ["B", "KB", "MB", "GB", "TB", "PB"];
174+
for (const [i, unit] of units.entries()) {
175+
if (bytes < Math.pow(1000, i + 1) || i === units.length - 1) {
176+
return truncateDecimalZeroes((bytes / Math.pow(1000, i)).toPrecision(4)) + unit;
177+
}
184178
}
185-
if (bytes < 1e15) {
186-
return truncateDecimalZeroes((bytes / 1e12).toPrecision(4)) + "TB";
179+
throw new Error("unreachable code");
180+
}
181+
182+
export function bytesIEC(bytes: number | Long) {
183+
bytes = Number(bytes);
184+
const units = ["B", "KB", "MB", "GB", "TB", "PB"];
185+
for (const [i, unit] of units.entries()) {
186+
if (bytes < Math.pow(1024, i + 1) || i === units.length - 1) {
187+
return truncateDecimalZeroes((bytes / Math.pow(1024, i)).toPrecision(4)) + unit;
188+
}
187189
}
188-
return truncateDecimalZeroes((bytes / 1e15).toPrecision(4)) + "PB";
190+
throw new Error("unreachable code");
189191
}
190192

191193
export function bitsPerSecond(bitsPerSecond: number | Long) {
@@ -420,6 +422,7 @@ export default {
420422
sentenceCase,
421423
percent,
422424
bytes,
425+
bytesIEC,
423426
bitsPerSecond,
424427
count,
425428
truncateList,

app/format/format_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ describe("bytes", () => {
124124
});
125125
});
126126

127+
describe("bytesIEC", () => {
128+
it("should abbreviate large numbers", () => {
129+
expect(format.bytesIEC(0)).toEqual("0B");
130+
expect(format.bytesIEC(1)).toEqual("1B");
131+
expect(format.bytesIEC(1023)).toEqual("1023B");
132+
expect(format.bytesIEC(1024)).toEqual("1KB");
133+
expect(format.bytesIEC(1024 + 512)).toEqual("1.5KB");
134+
});
135+
});
136+
127137
describe("count", () => {
128138
it("should abbreviate large numbers", () => {
129139
expect(format.count(0)).toEqual("0");

app/invocation/invocation_action_card.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -936,26 +936,27 @@ export default class InvocationActionCardComponent extends React.Component<Props
936936
<>
937937
<div className="metadata-title">Resource usage</div>
938938
<div>
939-
<div>Peak memory: {format.bytes(usageStats.peakMemoryBytes)}</div>
939+
<div>Peak memory: {format.bytesIEC(usageStats.peakMemoryBytes)}</div>
940940
<div>MilliCPU: {computeMilliCpu(this.state.actionResult!)}</div>
941941
{usageStats.peakFileSystemUsage?.map((fs) => (
942942
<div>
943-
Peak disk usage: {fs.target} ({fs.fstype}): {format.bytes(fs.usedBytes)} of {format.bytes(fs.totalBytes)}
943+
Peak disk usage: {fs.target} ({fs.fstype}): {format.bytesIEC(fs.usedBytes)} of{" "}
944+
{format.bytesIEC(fs.totalBytes)}
944945
</div>
945946
))}
946947
{usageStats.cgroupIoStats && (
947948
<>
948-
<div>Disk bytes read: {format.bytes(usageStats.cgroupIoStats.rbytes)}</div>
949+
<div>Disk bytes read: {format.bytesIEC(usageStats.cgroupIoStats.rbytes)}</div>
949950
<div>Disk read operations: {format.count(usageStats.cgroupIoStats.rios)}</div>
950-
<div>Disk bytes written: {format.bytes(usageStats.cgroupIoStats.wbytes)}</div>
951+
<div>Disk bytes written: {format.bytesIEC(usageStats.cgroupIoStats.wbytes)}</div>
951952
<div>Disk write operations: {format.count(usageStats.cgroupIoStats.wios)}</div>
952953
</>
953954
)}
954955
{usageStats.networkStats && (
955956
<>
956-
<div>Network bytes received: {format.bytes(usageStats.networkStats.bytesReceived)}</div>
957+
<div>Network bytes received: {format.bytesIEC(usageStats.networkStats.bytesReceived)}</div>
957958
<div>Network packets received: {format.count(usageStats.networkStats.packetsReceived)}</div>
958-
<div>Network bytes sent: {format.bytes(usageStats.networkStats.bytesSent)}</div>
959+
<div>Network bytes sent: {format.bytesIEC(usageStats.networkStats.bytesSent)}</div>
959960
<div>Network packets sent: {format.count(usageStats.networkStats.packetsSent)}</div>
960961
</>
961962
)}
@@ -1334,7 +1335,7 @@ export default class InvocationActionCardComponent extends React.Component<Props
13341335
<div>
13351336
<div>
13361337
Peak memory:{" "}
1337-
{format.bytes(
1338+
{format.bytesIEC(
13381339
this.state.actionResult.executionMetadata.estimatedTaskSize.estimatedMemoryBytes
13391340
)}
13401341
</div>

0 commit comments

Comments
 (0)