From 332302044e0636a52648dd1974df46700c878d08 Mon Sep 17 00:00:00 2001 From: Jong Eun Lee Date: Tue, 19 Nov 2024 15:07:37 +0800 Subject: [PATCH] refactor: size unit convert functions --- react/src/components/AgentDetailModal.tsx | 30 ++- react/src/components/AgentList.tsx | 32 ++- .../src/components/AvailableResourcesCard.tsx | 10 +- .../src/components/DynamicUnitInputNumber.tsx | 11 +- .../DynamicUnitInputNumberWithSlider.tsx | 11 +- .../KeypairResourcePolicySettingModal.tsx | 10 +- .../ResourceAllocationFormItems.tsx | 23 +- react/src/components/ResourceNumber.tsx | 9 +- react/src/components/ResourcePresetList.tsx | 8 +- .../components/ResourcePresetSettingModal.tsx | 26 ++- .../components/ServiceLauncherPageContent.tsx | 6 +- react/src/helper/index.test.tsx | 212 ++++++++++++++++-- react/src/helper/index.tsx | 84 ++++--- .../hooks/useResourceLimitAndRemaining.tsx | 12 +- react/src/pages/SessionLauncherPage.tsx | 7 +- 15 files changed, 371 insertions(+), 120 deletions(-) diff --git a/react/src/components/AgentDetailModal.tsx b/react/src/components/AgentDetailModal.tsx index c0f80a9cfb..ce6b89c311 100644 --- a/react/src/components/AgentDetailModal.tsx +++ b/react/src/components/AgentDetailModal.tsx @@ -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'; @@ -93,16 +93,18 @@ const AgentDetailModal: React.FC = ({ ) : null} @@ -115,8 +117,11 @@ const AgentDetailModal: React.FC = ({ TX: { - iSizeToSize(parsedLiveStat?.node?.net_tx?.current, 'm', 1) - ?.numberUnit + convertBinarySizeUnit( + parsedLiveStat?.node?.net_tx?.current, + 'm', + 1, + )?.numberUnit } iB @@ -125,8 +130,11 @@ const AgentDetailModal: React.FC = ({ RX: { - iSizeToSize(parsedLiveStat?.node?.net_rx?.current, 'm', 1) - ?.numberUnit + convertBinarySizeUnit( + parsedLiveStat?.node?.net_rx?.current, + 'm', + 1, + )?.numberUnit } iB diff --git a/react/src/components/AgentList.tsx b/react/src/components/AgentList.tsx index a87665057a..4049b5126c 100644 --- a/react/src/components/AgentList.tsx +++ b/react/src/components/AgentList.tsx @@ -1,6 +1,6 @@ import { bytesToGB, - iSizeToSize, + convertBinarySizeUnit, toFixedFloorWithoutTrailingZeros, transformSorterToOrderString, } from '../helper'; @@ -340,11 +340,17 @@ const AgentList: React.FC = ({ - {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} = ({ 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' } /> @@ -568,7 +578,7 @@ const AgentList: React.FC = ({ 100 || 0 } valueLabel={ - iSizeToSize( + convertBinarySizeUnit( _.toString( liveStat[statKey as keyof typeof liveStat] .current, @@ -576,7 +586,7 @@ const AgentList: React.FC = ({ 'g', )?.numberFixed + '/' + - iSizeToSize( + convertBinarySizeUnit( _.toString( liveStat[statKey as keyof typeof liveStat] .capacity, diff --git a/react/src/components/AvailableResourcesCard.tsx b/react/src/components/AvailableResourcesCard.tsx index 69fe66c6c8..db214d22fc 100644 --- a/react/src/components/AvailableResourcesCard.tsx +++ b/react/src/components/AvailableResourcesCard.tsx @@ -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'; @@ -58,12 +58,14 @@ const AvailableResourcesCard = () => { diff --git a/react/src/components/DynamicUnitInputNumber.tsx b/react/src/components/DynamicUnitInputNumber.tsx index 16bbd16b81..36f8ecadf3 100644 --- a/react/src/components/DynamicUnitInputNumber.tsx +++ b/react/src/components/DynamicUnitInputNumber.tsx @@ -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'; @@ -99,13 +99,16 @@ const DynamicUnitInputNumber: React.FC = ({ }} //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={