Skip to content

Commit 868bd00

Browse files
authored
Merge pull request #3827 from RedisInsight/feature/fe/overview-dynamic-units
Dynamically show multiple byte formats
2 parents d830122 + 5c103bc commit 868bd00

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

redisinsight/ui/src/components/database-overview/components/OverviewMetrics/OverviewMetrics.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,21 @@ export const getOverviewMetrics = ({ theme, items, db = 0 }: Props): Array<IMetr
119119
},
120120
}
121121

122-
const networkInKbpsItem = {
122+
let [networkIn, networkInUnit] = formatBytes(networkInKbps * 1000, 3, true, 1000)
123+
networkInUnit = networkInUnit ? `${networkInUnit.toLowerCase()}/s` : ''
124+
let [networkOut, networkOutUnit] = formatBytes(networkOutKbps * 1000, 3, true, 1000)
125+
networkOutUnit = networkOutUnit ? `${networkOutUnit.toLowerCase()}/s` : ''
126+
127+
const networkInItem = {
123128
id: 'network-input',
124129
groupId: opsPerSecItem.id,
125130
title: 'Network Input',
126131
icon: theme === Theme.Dark ? InputDarkIcon : InputLightIcon,
127-
value: networkInKbps,
132+
value: networkIn,
128133
content: (
129134
<>
130-
<b>{networkInKbps}</b>
131-
&nbsp;kb/s
135+
<b>{networkIn}</b>
136+
&nbsp;{networkInUnit}
132137
</>
133138
),
134139
unavailableText: 'Network Input is not available',
@@ -137,23 +142,23 @@ export const getOverviewMetrics = ({ theme, items, db = 0 }: Props): Array<IMetr
137142
icon: theme === Theme.Dark ? InputDarkIcon : InputLightIcon,
138143
content: (
139144
<>
140-
<b>{networkInKbps}</b>
141-
&nbsp;kb/s
145+
<b>{networkIn}</b>
146+
&nbsp;{networkInUnit}
142147
</>
143148
),
144149
},
145150
}
146151

147-
const networkOutKbpsItem = {
152+
const networkOutItem = {
148153
id: 'network-output-tip',
149154
groupId: opsPerSecItem.id,
150155
title: 'Network Output',
151156
icon: theme === Theme.Dark ? OutputDarkIcon : OutputLightIcon,
152-
value: networkOutKbps,
157+
value: networkOut,
153158
content: (
154159
<>
155-
<b>{networkOutKbps}</b>
156-
&nbsp;kb/s
160+
<b>{networkOut}</b>
161+
&nbsp;{networkOutUnit}
157162
</>
158163
),
159164
unavailableText: 'Network Output is not available',
@@ -162,8 +167,8 @@ export const getOverviewMetrics = ({ theme, items, db = 0 }: Props): Array<IMetr
162167
icon: theme === Theme.Dark ? OutputDarkIcon : OutputLightIcon,
163168
content: (
164169
<>
165-
<b>{networkOutKbps}</b>
166-
&nbsp;kb/s
170+
<b>{networkOut}</b>
171+
&nbsp;{networkOutUnit}
167172
</>
168173
),
169174
},
@@ -179,8 +184,8 @@ export const getOverviewMetrics = ({ theme, items, db = 0 }: Props): Array<IMetr
179184
content: opsPerSecond,
180185
unavailableText: 'Commands/s are not available',
181186
},
182-
networkInKbpsItem,
183-
networkOutKbpsItem
187+
networkInItem,
188+
networkOutItem
184189
]
185190
}
186191

redisinsight/ui/src/utils/tests/transformers/formatBytes.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ describe('formatBytes', () => {
4646
it('should return proper array with splitResults', () => {
4747
expect(formatBytes(1572864, 0, true)).toEqual([2, 'MB'])
4848
expect(formatBytes(1347545989, 3, true)).toEqual([1.255, 'GB'])
49+
expect(formatBytes(0, 3, true)).toEqual([0, 'B'])
50+
})
51+
52+
it('should properly set the baseK', () => {
53+
expect(formatBytes(1347545989, 3, true)).toEqual([1.255, 'GB']) // default uses 1024
54+
expect(formatBytes(1347545989, 3, true, 1000)).toEqual([1.348, 'GB'])
4955
})
5056
})
5157

redisinsight/ui/src/utils/transformers/formatBytes.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ const SIZES = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
33
export const formatBytes = (
44
input: number,
55
decimals: number = 3,
6-
splitResult: boolean = false
6+
splitResult: boolean = false,
7+
baseK = 1024,
78
): string | [number, string] => {
89
try {
910
const bytes = parseFloat(String(input))
10-
const k = 1024
11+
const k = baseK
1112
const dm = decimals < 0 ? 0 : decimals
1213
if (Number.isNaN(bytes) || bytes < 0) return '-'
13-
if (bytes === 0) return `0 ${SIZES[0]}`
14+
if (bytes === 0) return splitResult ? [0, SIZES[0]] : `0 ${SIZES[0]}`
1415

1516
const i = Math.floor(Math.log(bytes) / Math.log(k))
1617
const sizeIndex = Math.min(i, SIZES.length - 1)

0 commit comments

Comments
 (0)