Skip to content

Commit 30d9bb4

Browse files
chore: change gossip network bytes to bits
1 parent 5668d80 commit 30d9bb4

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

src/features/Gossip/PeerTable.tsx/index.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import { gossipPeersSizeAtom } from "../../../api/atoms";
1313
import usePeerTableQuery, { type SortOrder } from "./usePeerTableQuery";
1414
import { Box, Flex, Reset, Separator, Table, Text } from "@radix-ui/themes";
1515
import { lamportsPerSol } from "../../../consts";
16-
import byteSize from "byte-size";
1716
import { CaretDownIcon, CaretUpIcon } from "@radix-ui/react-icons";
1817
import { useKey } from "react-use";
19-
import { copyToClipboard } from "../../../utils";
18+
import { copyToClipboard, formatBytesAsBits } from "../../../utils";
2019
import styles from "./peerTable.module.css";
2120
import clsx from "clsx";
2221
import { headerGap } from "../consts";
@@ -31,7 +30,7 @@ interface ColSpec {
3130
}
3231
const byteFormat = (value: number | string) => {
3332
if (typeof value === "number") {
34-
const formatted = byteSize(value);
33+
const formatted = formatBytesAsBits(value);
3534
return `${formatted.value} ${formatted.unit}`;
3635
}
3736
return value;
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Table } from "@radix-ui/themes";
2-
import byteSize from "byte-size";
32
import { useEmaValue } from "../../hooks/useEma";
3+
import { formatBytesAsBits } from "../../utils";
44

55
interface RateTableCellProps {
66
inBytes?: boolean;
@@ -10,11 +10,15 @@ interface RateTableCellProps {
1010
export default function EmaTableCell({ inBytes, value }: RateTableCellProps) {
1111
const emaValue = useEmaValue(value) ?? 0;
1212

13-
const formattedValue =
14-
emaValue !== undefined
15-
? inBytes
16-
? byteSize(emaValue).toString()
17-
: Math.trunc(emaValue).toLocaleString()
18-
: "-";
13+
let formattedValue = "-";
14+
if (emaValue !== undefined) {
15+
if (inBytes) {
16+
const { value, unit } = formatBytesAsBits(emaValue);
17+
formattedValue = `${value.toLocaleString()} ${unit}`;
18+
} else {
19+
formattedValue = Math.trunc(emaValue).toLocaleString();
20+
}
21+
}
22+
1923
return <Table.Cell align="right">{formattedValue}</Table.Cell>;
2024
}

src/features/Gossip/TrafficTreeMap.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import { useAtomValue } from "jotai";
1616
import { peersAtom } from "../../atoms";
1717
import { formatNumberLamports } from "../Overview/ValidatorsCard/formatAmt";
1818
import { sum } from "lodash";
19-
import byteSize from "byte-size";
2019
import AutoSizer from "react-virtualized-auto-sizer";
2120
import { headerGap } from "./consts";
2221
import styles from "./trafficTreeMap.module.css";
22+
import { formatBytesAsBits } from "../../utils";
2323

2424
const colorsList = [
2525
"#00F0FF",
@@ -123,6 +123,10 @@ export function TrafficTreeMap({
123123

124124
if (!data) return;
125125

126+
const formattedThroughput = formatBytesAsBits(
127+
networkTraffic.total_throughput,
128+
);
129+
126130
return (
127131
<Flex
128132
direction="column"
@@ -137,7 +141,7 @@ export function TrafficTreeMap({
137141
<span>
138142
<Text className={styles.totalText}>Total</Text>
139143
<Text className={styles.throughputText}>
140-
&nbsp;{byteSize(networkTraffic.total_throughput).toString()}/s
144+
&nbsp;{`${formattedThroughput.value} ${formattedThroughput.unit}`}/s
141145
</Text>
142146
</span>
143147
</Flex>
@@ -348,12 +352,12 @@ function NodeText({ leaf, total, getPeerValues }: NodeTextProps) {
348352
const firstBaselineY = textY; // firstLineTop + idFontSize;
349353
// const iconY = textY; // firstLineTop + Math.max(0, (idFontSize - iconSize) / 2);
350354

351-
const byteValue = byteSize(leaf.value ?? 0);
352-
let throughputLine = `${byteValue ? `${byteValue.toString()} ` : ""}(${pct.toFixed(1)}%)`;
355+
const formatted = formatBytesAsBits(leaf.value ?? 0);
356+
let throughputLine = `${formatted ? `${formatted.value} ${formatted.unit} ` : ""}(${pct.toFixed(1)}%)`;
353357

354358
const showPct = measureTextWidth(throughputLine, idFont) < availableWidth;
355359
if (!showPct) {
356-
throughputLine = `${byteValue.toString()}`;
360+
throughputLine = `${formatted.value} ${formatted.unit}`;
357361
}
358362

359363
if (rectWidth < minLabelWidth) return;

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,14 +289,14 @@ export function formatBytesAsBits(bytes: number): {
289289
unit: string;
290290
} {
291291
const bits = bytes * 8;
292-
if (bits < 1_000) return { value: bits, unit: "bit" };
292+
if (bits < 1_000) return { value: bits, unit: "b" };
293293
if (bits < 1_000_000)
294-
return { value: getRoundedBitsValue(bits / 1_000), unit: "Kbit" };
294+
return { value: getRoundedBitsValue(bits / 1_000), unit: "Kb" };
295295
if (bits < 1_000_000_000) {
296-
return { value: getRoundedBitsValue(bits / 1_000_000), unit: "Mbit" };
296+
return { value: getRoundedBitsValue(bits / 1_000_000), unit: "Mb" };
297297
}
298298

299-
return { value: getRoundedBitsValue(bits / 1_000_000_000), unit: "Gbit" };
299+
return { value: getRoundedBitsValue(bits / 1_000_000_000), unit: "Gb" };
300300
}
301301

302302
export interface FormattedBytes {

0 commit comments

Comments
 (0)