Skip to content

Commit

Permalink
refactor: size unit convert functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yomybaby committed Nov 19, 2024
1 parent 39e7191 commit 3323020
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 120 deletions.
30 changes: 19 additions & 11 deletions react/src/components/AgentDetailModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { iSizeToSize } from '../helper';
import { convertBinarySizeUnit } from '../helper';
import { useResourceSlotsDetails } from '../hooks/backendai';
import BAIModal, { BAIModalProps } from './BAIModal';
import BAIProgressWithLabel from './BAIProgressWithLabel';
Expand Down Expand Up @@ -93,16 +93,18 @@ const AgentDetailModal: React.FC<AgentDetailModalProps> = ({
</Typography.Title>
<BAIProgressWithLabel
percent={
((iSizeToSize(_.toString(agent?.mem_cur_bytes), 'g')
?.number ?? 0) /
(iSizeToSize(parsedAvailableSlots?.mem, 'g')?.number ??
0)) *
((convertBinarySizeUnit(
_.toString(agent?.mem_cur_bytes),
'g',
)?.number ?? 0) /
(convertBinarySizeUnit(parsedAvailableSlots?.mem, 'g')
?.number ?? 0)) *
100 ?? 0
}
valueLabel={`${
iSizeToSize(_.toString(agent?.mem_cur_bytes), 'g')
convertBinarySizeUnit(_.toString(agent?.mem_cur_bytes), 'g')
?.numberUnit
}iB / ${iSizeToSize(parsedAvailableSlots?.mem, 'g')?.numberUnit}iB`}
}iB / ${convertBinarySizeUnit(parsedAvailableSlots?.mem, 'g')?.numberUnit}iB`}
/>
</Flex>
) : null}
Expand All @@ -115,8 +117,11 @@ const AgentDetailModal: React.FC<AgentDetailModalProps> = ({
<Typography.Text>TX:</Typography.Text>
<Typography.Text>
{
iSizeToSize(parsedLiveStat?.node?.net_tx?.current, 'm', 1)
?.numberUnit
convertBinarySizeUnit(
parsedLiveStat?.node?.net_tx?.current,
'm',
1,
)?.numberUnit
}
iB
</Typography.Text>
Expand All @@ -125,8 +130,11 @@ const AgentDetailModal: React.FC<AgentDetailModalProps> = ({
<Typography.Text>RX:</Typography.Text>
<Typography.Text>
{
iSizeToSize(parsedLiveStat?.node?.net_rx?.current, 'm', 1)
?.numberUnit
convertBinarySizeUnit(
parsedLiveStat?.node?.net_rx?.current,
'm',
1,
)?.numberUnit
}
iB
</Typography.Text>
Expand Down
32 changes: 21 additions & 11 deletions react/src/components/AgentList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
bytesToGB,
iSizeToSize,
convertBinarySizeUnit,
toFixedFloorWithoutTrailingZeros,
transformSorterToOrderString,
} from '../helper';
Expand Down Expand Up @@ -340,11 +340,17 @@ const AgentList: React.FC<AgentListProps> = ({
<Flex gap="xxs">
<ResourceTypeIcon type={'mem'} />
<Typography.Text>
{iSizeToSize(parsedOccupiedSlots.mem, 'g', 0)
?.numberFixed ?? 0}
{convertBinarySizeUnit(
parsedOccupiedSlots.mem,
'g',
0,
)?.numberFixed ?? 0}
/
{iSizeToSize(parsedAvailableSlots.mem, 'g', 0)
?.numberFixed ?? 0}
{convertBinarySizeUnit(
parsedAvailableSlots.mem,
'g',
0,
)?.numberFixed ?? 0}
</Typography.Text>
<Typography.Text
type="secondary"
Expand Down Expand Up @@ -501,11 +507,15 @@ const AgentList: React.FC<AgentListProps> = ({
percent={liveStat.mem_util.ratio}
width={120}
valueLabel={
iSizeToSize(_.toString(liveStat.mem_util.current), 'g')
?.numberFixed +
convertBinarySizeUnit(
_.toString(liveStat.mem_util.current),
'g',
)?.numberFixed +
'/' +
iSizeToSize(_.toString(liveStat.mem_util.capacity), 'g')
?.numberFixed +
convertBinarySizeUnit(
_.toString(liveStat.mem_util.capacity),
'g',
)?.numberFixed +
' GiB'
}
/>
Expand Down Expand Up @@ -568,15 +578,15 @@ const AgentList: React.FC<AgentListProps> = ({
100 || 0
}
valueLabel={
iSizeToSize(
convertBinarySizeUnit(
_.toString(
liveStat[statKey as keyof typeof liveStat]
.current,
),
'g',
)?.numberFixed +
'/' +
iSizeToSize(
convertBinarySizeUnit(
_.toString(
liveStat[statKey as keyof typeof liveStat]
.capacity,
Expand Down
10 changes: 6 additions & 4 deletions react/src/components/AvailableResourcesCard.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { iSizeToSize } from '../helper';
import { convertBinarySizeUnit } from '../helper';
import { useCurrentProjectValue } from '../hooks/useCurrentProject';
import { useResourceLimitAndRemaining } from '../hooks/useResourceLimitAndRemaining';
import BAIProgressWithLabel from './BAIProgressWithLabel';
Expand Down Expand Up @@ -58,12 +58,14 @@ const AvailableResourcesCard = () => {
<BAIProgressWithLabel
title="MEM"
percent={
((iSizeToSize(remaining.mem + '', 'm')?.number || 0) /
(iSizeToSize(resourceLimits.mem?.max + '', 'm')?.number || 1)) *
((convertBinarySizeUnit(remaining.mem + '', 'm')?.number || 0) /
(convertBinarySizeUnit(resourceLimits.mem?.max + '', 'm')
?.number || 1)) *
100
}
valueLabel={
iSizeToSize(remaining.mem + '', 'g', 2)?.numberFixed + ' GiB'
convertBinarySizeUnit(remaining.mem + '', 'g', 2)?.numberFixed +
' GiB'
}
/>
</Flex>
Expand Down
11 changes: 7 additions & 4 deletions react/src/components/DynamicUnitInputNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { iSizeToSize, parseUnit } from '../helper';
import { convertBinarySizeUnit, parseUnit, SizeUnit } from '../helper';
import useControllableState from '../hooks/useControllableState';
import { usePrevious } from 'ahooks';
import { InputNumber, InputNumberProps, Select, Typography } from 'antd';
Expand Down Expand Up @@ -99,13 +99,16 @@ const DynamicUnitInputNumber: React.FC<DynamicUnitInputNumberProps> = ({
}}
//TODO: When min and max have different units, they should be calculated and put in.
// 입력의 초소단위 확인 0.4g 가 되는지 확인
// @ts-ignore
max={maxUnit === unit ? maxNumValue : iSizeToSize(max, unit).number}
max={
maxUnit === unit
? maxNumValue
: convertBinarySizeUnit(max, unit as SizeUnit)?.number
}
min={
minUnit === unit
? minNumValue
: // @ts-ignore
iSizeToSize(min, unit).number
convertBinarySizeUnit(min, unit).number
}
addonAfter={
<Select
Expand Down
11 changes: 7 additions & 4 deletions react/src/components/DynamicUnitInputNumberWithSlider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { compareNumberWithUnits, iSizeToSize } from '../helper';
import { compareNumberWithUnits, convertBinarySizeUnit } from '../helper';
import { useUpdatableState } from '../hooks';
import useControllableState from '../hooks/useControllableState';
import DynamicUnitInputNumber, {
Expand Down Expand Up @@ -36,9 +36,12 @@ const DynamicUnitInputNumberWithSlider: React.FC<
},
);
const { token } = theme.useToken();
const minGiB = useMemo(() => iSizeToSize(min, 'g', 2), [min]);
const maxGiB = useMemo(() => iSizeToSize(max, 'g', 2), [max]);
const valueGiB = useMemo(() => iSizeToSize(value || '0g', 'g', 2), [value]);
const minGiB = useMemo(() => convertBinarySizeUnit(min, 'g', 2), [min]);
const maxGiB = useMemo(() => convertBinarySizeUnit(max, 'g', 2), [max]);
const valueGiB = useMemo(
() => convertBinarySizeUnit(value || '0g', 'g', 2),
[value],
);

// const warnPercent = useMemo(() => {
// return warn
Expand Down
10 changes: 5 additions & 5 deletions react/src/components/KeypairResourcePolicySettingModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { iSizeToSize } from '../helper';
import { convertBinarySizeUnit } from '../helper';
import {
UNLIMITED_MAX_CONCURRENT_SESSIONS,
UNLIMITED_MAX_CONTAINERS_PER_SESSIONS,
Expand Down Expand Up @@ -125,7 +125,7 @@ const KeypairResourcePolicySettingModal: React.FC<
);

if (parsedTotalResourceSlots?.mem) {
parsedTotalResourceSlots.mem = iSizeToSize(
parsedTotalResourceSlots.mem = convertBinarySizeUnit(
parsedTotalResourceSlots?.mem + 'b',
'g',
2,
Expand Down Expand Up @@ -167,7 +167,7 @@ const KeypairResourcePolicySettingModal: React.FC<
values?.parsedTotalResourceSlots,
(value, key) => {
if (_.includes(key, 'mem')) {
return iSizeToSize(value, 'b', 0)?.numberFixed;
return convertBinarySizeUnit(value, 'b', 0)?.numberFixed;
}
return value;
},
Expand Down Expand Up @@ -353,9 +353,9 @@ const KeypairResourcePolicySettingModal: React.FC<
_.includes(resourceSlotKey, 'mem') &&
value &&
// @ts-ignore
iSizeToSize(value, 'p').number >
convertBinarySizeUnit(value, 'p').number >
// @ts-ignore
iSizeToSize('300p', 'p').number
convertBinarySizeUnit('300p', 'p').number
) {
return Promise.reject(
new Error(
Expand Down
23 changes: 15 additions & 8 deletions react/src/components/ResourceAllocationFormItems.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
addNumberWithUnits,
compareNumberWithUnits,
iSizeToSize,
convertBinarySizeUnit,
} from '../helper';
import { useSuspendedBackendaiClient, useUpdatableState } from '../hooks';
import { useResourceSlotsDetails } from '../hooks/backendai';
Expand Down Expand Up @@ -239,9 +239,9 @@ const ResourceAllocationFormItems: React.FC<
const minimumResources: Partial<ResourceAllocationFormValue['resource']> = {
cpu: resourceLimits.cpu?.min,
mem:
iSizeToSize(
(iSizeToSize(resourceLimits.shmem?.min, 'm')?.number || 0) +
(iSizeToSize(resourceLimits.mem?.min, 'm')?.number || 0) +
convertBinarySizeUnit(
(convertBinarySizeUnit(resourceLimits.shmem?.min, 'm')?.number || 0) +
(convertBinarySizeUnit(resourceLimits.mem?.min, 'm')?.number || 0) +
'm',
'g',
)?.number + 'g', //to prevent loosing precision
Expand Down Expand Up @@ -345,7 +345,11 @@ const ResourceAllocationFormItems: React.FC<
(preset) => preset.name === name,
);
const slots = _.pick(preset?.resource_slots, _.keys(resourceSlots));
const mem = iSizeToSize((slots?.mem || 0) + 'b', 'g', 2)?.numberUnit;
const mem = convertBinarySizeUnit(
(slots?.mem || 0) + 'b',
'g',
2,
)?.numberUnit;
const acceleratorObj = _.omit(slots, ['cpu', 'mem', 'shmem']);

// Select the first matched AI accelerator type and value
Expand All @@ -372,7 +376,7 @@ const ResourceAllocationFormItems: React.FC<
...acceleratorSetting,
// transform to GB based on preset values
mem,
shmem: iSizeToSize((preset?.shared_memory || 0) + 'b', 'g', 2)
shmem: convertBinarySizeUnit((preset?.shared_memory || 0) + 'b', 'g', 2)
?.numberUnit,
cpu: parseInt(slots?.cpu || '0') || 0,
},
Expand Down Expand Up @@ -766,8 +770,11 @@ const ResourceAllocationFormItems: React.FC<
...(remaining.mem
? {
//@ts-ignore
[iSizeToSize(remaining.mem + 'b', 'g', 3)
?.numberFixed]: {
[convertBinarySizeUnit(
remaining.mem + 'b',
'g',
3,
)?.numberFixed]: {
label: <RemainingMark />,
},
}
Expand Down
9 changes: 6 additions & 3 deletions react/src/components/ResourceNumber.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { iSizeToSize } from '../helper';
import { convertBinarySizeUnit } from '../helper';
import {
BaseResourceSlotName,
KnownAcceleratorResourceSlotName,
Expand Down Expand Up @@ -45,7 +45,9 @@ const ResourceNumber: React.FC<ResourceNumberProps> = ({

const formatAmount = (amount: string) => {
return mergedResourceSlots?.[type]?.number_format.binary
? Number(iSizeToSize(amount, 'g', 3, true)?.numberFixed).toString()
? Number(
convertBinarySizeUnit(amount, 'g', 3, true)?.numberFixed,
).toString()
: (mergedResourceSlots?.[type]?.number_format.round_length || 0) > 0
? parseFloat(amount).toFixed(2)
: amount;
Expand Down Expand Up @@ -75,7 +77,8 @@ const ResourceNumber: React.FC<ResourceNumberProps> = ({
type="secondary"
style={{ fontSize: token.fontSizeSM }}
>
(SHM: {iSizeToSize(opts.shmem + 'b', 'g', 2, true)?.numberFixed}
(SHM:{' '}
{convertBinarySizeUnit(opts.shmem + 'b', 'g', 2, true)?.numberFixed}
GiB)
</Typography.Text>
) : null}
Expand Down
8 changes: 6 additions & 2 deletions react/src/components/ResourcePresetList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { filterNonNullItems, iSizeToSize, localeCompare } from '../helper';
import {
filterNonNullItems,
convertBinarySizeUnit,
localeCompare,
} from '../helper';
import { useUpdatableState } from '../hooks';
import Flex from './Flex';
import ResourceNumber from './ResourceNumber';
Expand Down Expand Up @@ -131,7 +135,7 @@ const ResourcePresetList: React.FC<ResourcePresetListProps> = () => {
title: t('resourcePreset.SharedMemory'),
dataIndex: 'shared_memory',
render: (text) =>
text ? iSizeToSize(text + '', 'g')?.number : '-',
text ? convertBinarySizeUnit(text + '', 'g')?.number : '-',
sorter: (a, b) => a.shared_memory - b.shared_memory,
},
{
Expand Down
Loading

0 comments on commit 3323020

Please sign in to comment.