Skip to content

Commit 7b08859

Browse files
committed
chore(FR-831): update ResourcePresetSettingModal to display exact memory value
1 parent dcdd5ed commit 7b08859

25 files changed

Lines changed: 154 additions & 32 deletions

react/src/components/ResourceNumber.tsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { convertBinarySizeUnit } from '../helper';
1+
import {
2+
convertBinarySizeUnit,
3+
SizeUnit,
4+
sizeUnitToBinarySizeUnit,
5+
} from '../helper';
26
import {
37
BaseResourceSlotName,
48
KnownAcceleratorResourceSlotName,
@@ -22,6 +26,7 @@ interface ResourceNumberProps {
2226
value: string;
2327
hideTooltip?: boolean;
2428
max?: string;
29+
showDetailValue?: boolean;
2530
}
2631

2732
type ResourceTypeInfo<V> = {
@@ -36,6 +41,7 @@ const ResourceNumber: React.FC<ResourceNumberProps> = ({
3641
opts,
3742
hideTooltip = false,
3843
max,
44+
showDetailValue,
3945
}) => {
4046
const { token } = theme.useToken();
4147
const currentGroup = useCurrentResourceGroupValue();
@@ -46,13 +52,16 @@ const ResourceNumber: React.FC<ResourceNumberProps> = ({
4652
const formatAmount = (amount: string) => {
4753
return mergedResourceSlots?.[type]?.number_format.binary
4854
? Number(
49-
convertBinarySizeUnit(amount, 'g', 3, true)?.numberFixed,
55+
convertBinarySizeUnit(amount, 'g', 2, true)?.numberFixed,
5056
).toString()
5157
: (mergedResourceSlots?.[type]?.number_format.round_length || 0) > 0
5258
? parseFloat(amount).toFixed(2)
5359
: amount;
5460
};
5561

62+
const convertedAmount = formatAmount(amount);
63+
const convertedAmountByAuto = convertBinarySizeUnit(amount, 'auto', 2, true);
64+
5665
return (
5766
<Flex direction="row" gap="xxs">
5867
{mergedResourceSlots?.[type] ? (
@@ -62,16 +71,33 @@ const ResourceNumber: React.FC<ResourceNumberProps> = ({
6271
)}
6372

6473
<Typography.Text>
65-
{formatAmount(amount)}
74+
{convertedAmount}
6675
{_.isUndefined(max)
6776
? null
6877
: max === 'Infinity'
6978
? '~∞'
7079
: `~${formatAmount(max)}`}
7180
</Typography.Text>
72-
<Typography.Text type="secondary">
73-
{mergedResourceSlots?.[type]?.display_unit || ''}
74-
</Typography.Text>
81+
<div>
82+
<Typography.Text type="secondary">
83+
{mergedResourceSlots?.[type]?.display_unit || ''}
84+
</Typography.Text>
85+
{showDetailValue &&
86+
mergedResourceSlots?.[type]?.number_format.binary &&
87+
convertedAmount === '0' &&
88+
amount !== '0' ? (
89+
<Typography.Text>
90+
&#40;{Number(convertedAmountByAuto?.numberFixed).toString()}&nbsp;
91+
<Typography.Text type="secondary">
92+
{sizeUnitToBinarySizeUnit(
93+
convertedAmountByAuto?.unit as SizeUnit,
94+
)}
95+
</Typography.Text>
96+
&#41;
97+
</Typography.Text>
98+
) : null}
99+
</div>
100+
75101
{type === 'mem' && opts?.shmem && opts?.shmem > 0 ? (
76102
<Typography.Text
77103
type="secondary"

react/src/components/ResourcePresetList.tsx

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {
33
convertBinarySizeUnit,
44
localeCompare,
55
filterEmptyItem,
6+
sizeUnitToBinarySizeUnit,
7+
SizeUnit,
68
} from '../helper';
79
import { useSuspendedBackendaiClient, useUpdatableState } from '../hooks';
810
import Flex from './Flex';
@@ -93,7 +95,12 @@ const ResourcePresetList: React.FC<ResourcePresetListProps> = () => {
9395
<Flex gap="xxs">
9496
{!_.isEmpty(text)
9597
? _.map(JSON.parse(text), (value, key) => (
96-
<ResourceNumber key={key} type={key} value={value} />
98+
<ResourceNumber
99+
key={key}
100+
type={key}
101+
value={value}
102+
showDetailValue
103+
/>
97104
))
98105
: '-'}
99106
</Flex>
@@ -102,8 +109,43 @@ const ResourcePresetList: React.FC<ResourcePresetListProps> = () => {
102109
{
103110
title: t('resourcePreset.SharedMemory'),
104111
dataIndex: 'shared_memory',
105-
render: (text) =>
106-
text ? convertBinarySizeUnit(text + '', 'g')?.numberFixed : '-',
112+
render: (text) => {
113+
if (!text) {
114+
return '-';
115+
}
116+
const convertedValue = convertBinarySizeUnit(text + 'b', 'g', 2, true);
117+
const convertedValueByAuto = convertBinarySizeUnit(
118+
text + 'b',
119+
'auto',
120+
2,
121+
true,
122+
);
123+
return (
124+
<Flex gap="xxs">
125+
<Typography.Text>
126+
{Number(convertedValue?.numberFixed).toString()}
127+
</Typography.Text>
128+
<Flex>
129+
<Typography.Text type="secondary">
130+
{sizeUnitToBinarySizeUnit(convertedValue?.unit as SizeUnit)}
131+
</Typography.Text>
132+
{Number(convertedValue?.numberFixed).toString() === '0' &&
133+
text !== '0' ? (
134+
<Typography.Text>
135+
&#40;{Number(convertedValueByAuto?.numberFixed).toString()}
136+
&nbsp;
137+
<Typography.Text type="secondary">
138+
{sizeUnitToBinarySizeUnit(
139+
convertedValueByAuto?.unit as SizeUnit,
140+
)}
141+
</Typography.Text>
142+
&#41;
143+
</Typography.Text>
144+
) : null}
145+
</Flex>
146+
</Flex>
147+
);
148+
},
107149
},
108150
baiClient?.supports('resource-presets-per-resource-group') && {
109151
title: t('general.ResourceGroup'),

react/src/components/ResourcePresetSettingModal.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,16 @@ const ResourcePresetSettingModal: React.FC<ResourcePresetSettingModalProps> = ({
236236
JSON.parse(resourcePreset?.resource_slots || '{}'),
237237
(value, key) =>
238238
_.includes(key, 'mem')
239-
? convertBinarySizeUnit(value + 'b', 'g')?.numberUnit
239+
? convertBinarySizeUnit(
240+
value + 'b',
241+
value === '0' ? 'g' : 'auto',
242+
)?.numberUnit
240243
: value,
241244
) || {},
242245
shared_memory: resourcePreset?.shared_memory
243246
? convertBinarySizeUnit(
244247
resourcePreset?.shared_memory + 'b',
245-
'g',
248+
resourcePreset?.shared_memory === '0' ? 'g' : 'auto',
246249
)?.numberUnit
247250
: null,
248251
}

react/src/helper/index.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,57 @@ export type SizeUnit =
157157
| 'p'
158158
| 'e';
159159

160+
export type BinarySizeUnit =
161+
| 'Bytes'
162+
| 'KiB'
163+
| 'MiB'
164+
| 'GiB'
165+
| 'TiB'
166+
| 'PiB'
167+
| 'EiB';
168+
169+
export type DecimalSizeUnit = 'Bytes' | 'KB' | 'MB' | 'GB' | 'TB' | 'PB' | 'EB';
170+
171+
export function sizeUnitToDecimalSizeUnit(unit: SizeUnit): DecimalSizeUnit {
172+
const unitMap: Record<SizeUnit, DecimalSizeUnit> = {
173+
B: 'Bytes',
174+
K: 'KB',
175+
M: 'MB',
176+
G: 'GB',
177+
T: 'TB',
178+
P: 'PB',
179+
E: 'EB',
180+
b: 'Bytes',
181+
k: 'KB',
182+
m: 'MB',
183+
g: 'GB',
184+
t: 'TB',
185+
p: 'PB',
186+
e: 'EB',
187+
};
188+
return unitMap[unit] || unit;
189+
}
190+
191+
export function sizeUnitToBinarySizeUnit(unit: SizeUnit): BinarySizeUnit {
192+
const unitMap: Record<SizeUnit, BinarySizeUnit> = {
193+
B: 'Bytes',
194+
K: 'KiB',
195+
M: 'MiB',
196+
G: 'GiB',
197+
T: 'TiB',
198+
P: 'PiB',
199+
E: 'EiB',
200+
b: 'Bytes',
201+
k: 'KiB',
202+
m: 'MiB',
203+
g: 'GiB',
204+
t: 'TiB',
205+
p: 'PiB',
206+
e: 'EiB',
207+
};
208+
return unitMap[unit] || unit;
209+
}
210+
160211
function convertSizeUnit(
161212
sizeWithUnit: string | undefined,
162213
targetSizeUnit?: SizeUnit | 'auto',

resources/i18n/de.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@
11211121
"ResourcePresets": "Ressourcenvoreinstellungen",
11221122
"Resources": "Ressourcen",
11231123
"SHMEMShouldBeSmallerThanMemory": "Gemeinsamer Speicher muss kleiner sein als der Speicher",
1124-
"SharedMemory": "Gemeinsamer Speicher (GB)",
1124+
"SharedMemory": "Gemeinsamer Speicher",
11251125
"Updated": "Ressourcenvoreinstellung aktualisiert"
11261126
},
11271127
"session": {

resources/i18n/el.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@
11171117
"ResourcePresets": "Προεπιλογές πόρων",
11181118
"Resources": "Πόροι",
11191119
"SHMEMShouldBeSmallerThanMemory": "Η κοινόχρηστη μνήμη πρέπει να είναι μικρότερη από τη μνήμη",
1120-
"SharedMemory": "Κοινή μνήμη (GB)",
1120+
"SharedMemory": "Κοινόχρηστη μνήμη",
11211121
"Updated": "Η προεπιλογή πόρων ενημερώθηκε"
11221122
},
11231123
"session": {

resources/i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@
11271127
"ResourcePresets": "Resource Presets",
11281128
"Resources": "Resources",
11291129
"SHMEMShouldBeSmallerThanMemory": "Shared memory must be smaller than memory",
1130-
"SharedMemory": "Shared memory (GB)",
1130+
"SharedMemory": "Shared memory",
11311131
"Updated": "Resource preset updated"
11321132
},
11331133
"session": {

resources/i18n/es.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@
11211121
"ResourcePresets": "Recursos preestablecidos",
11221122
"Resources": "Recursos",
11231123
"SHMEMShouldBeSmallerThanMemory": "La memoria compartida debe ser menor que la memoria",
1124-
"SharedMemory": "Memoria compartida (GB)",
1124+
"SharedMemory": "Memoria compartida",
11251125
"Updated": "Preajuste de recursos actualizado"
11261126
},
11271127
"session": {

resources/i18n/fi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@
11191119
"ResourcePresets": "Resurssien esiasetukset",
11201120
"Resources": "Resurssit",
11211121
"SHMEMShouldBeSmallerThanMemory": "Jaetun muistin on oltava pienempi kuin muistin",
1122-
"SharedMemory": "Jaettu muisti (GB)",
1122+
"SharedMemory": "Jaettu muisti",
11231123
"Updated": "Resurssien esiasetus päivitetty"
11241124
},
11251125
"session": {

resources/i18n/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@
11211121
"ResourcePresets": "Préréglages de ressources",
11221122
"Resources": "Ressources",
11231123
"SHMEMShouldBeSmallerThanMemory": "La mémoire partagée doit être plus petite que la mémoire",
1124-
"SharedMemory": "Mémoire partagée (Go)",
1124+
"SharedMemory": "Mémoire partagée",
11251125
"Updated": "Préréglage de ressource mis à jour"
11261126
},
11271127
"session": {

0 commit comments

Comments
 (0)