fix(striker-ui): combine functions for filter anvils and get max values

main
Tsu-ba-me 3 years ago
parent 3489e97d6d
commit 740f6c27b8
  1. 260
      striker-ui/components/ProvisionServerDialog.tsx

@ -479,72 +479,6 @@ const organizeFiles = (
[], [],
); );
const getMaxAvailableValues = (
organizedAnvils: OrganizedAnvilDetailMetadataForProvisionServer[],
{
includeAnvilUUIDs,
includeStorageGroupUUIDs,
}: {
includeAnvilUUIDs?: string[];
includeStorageGroupUUIDs?: string[];
} = {},
) => {
let testIncludeAnvil: (uuid: string) => boolean = () => true;
let testIncludeStorageGroup: (uuid: string) => boolean = () => true;
if (includeAnvilUUIDs && includeAnvilUUIDs.length > 0) {
testIncludeAnvil = (uuid: string) => includeAnvilUUIDs.includes(uuid);
}
if (includeStorageGroupUUIDs && includeStorageGroupUUIDs.length > 0) {
testIncludeStorageGroup = (uuid: string) =>
includeStorageGroupUUIDs.includes(uuid);
}
return organizedAnvils.reduce<{
maxCPUCores: number;
maxMemory: bigint;
maxVirtualDiskSize: bigint;
}>(
(
reducedValues,
{
anvilUUID,
anvilTotalCPUCores,
anvilTotalAvailableMemory,
storageGroups,
},
) => {
if (testIncludeAnvil(anvilUUID)) {
reducedValues.maxCPUCores = Math.max(
anvilTotalCPUCores,
reducedValues.maxCPUCores,
);
if (anvilTotalAvailableMemory > reducedValues.maxMemory) {
reducedValues.maxMemory = anvilTotalAvailableMemory;
}
storageGroups.forEach(({ storageGroupUUID, storageGroupFree }) => {
if (
testIncludeStorageGroup(storageGroupUUID) &&
storageGroupFree > reducedValues.maxVirtualDiskSize
) {
reducedValues.maxVirtualDiskSize = storageGroupFree;
}
});
}
return reducedValues;
},
{
maxCPUCores: 0,
maxMemory: BIGINT_ZERO,
maxVirtualDiskSize: BIGINT_ZERO,
},
);
};
const dSize = ( const dSize = (
valueToFormat: FormatDataSizeInputValue, valueToFormat: FormatDataSizeInputValue,
{ {
@ -602,21 +536,75 @@ const filterAnvils = (
organizedAnvils: OrganizedAnvilDetailMetadataForProvisionServer[], organizedAnvils: OrganizedAnvilDetailMetadataForProvisionServer[],
cpuCores: number, cpuCores: number,
memory: bigint, memory: bigint,
selectedStorageGroupUUID: string | undefined, {
) => includeAnvilUUIDs = [],
organizedAnvils.filter( includeStorageGroupUUIDs = [],
({ anvilTotalCPUCores, anvilTotalAvailableMemory, storageGroups }) => { }: {
includeAnvilUUIDs?: string[];
includeStorageGroupUUIDs?: string[];
} = {},
) => {
let testIncludeAnvil: (uuid: string) => boolean = () => true;
let testIncludeStorageGroup: (uuid: string) => boolean = () => true;
if (includeAnvilUUIDs.length > 0) {
testIncludeAnvil = (uuid: string) => includeAnvilUUIDs.includes(uuid);
}
if (includeStorageGroupUUIDs.length > 0) {
testIncludeStorageGroup = (uuid: string) =>
includeStorageGroupUUIDs.includes(uuid);
}
return organizedAnvils.reduce<{
anvils: OrganizedAnvilDetailMetadataForProvisionServer[];
anvilUUIDs: string[];
maxCPUCores: number;
maxMemory: bigint;
maxVirtualDiskSize: bigint;
}>(
(result, organizedAnvil) => {
const { anvilUUID } = organizedAnvil;
if (testIncludeAnvil(anvilUUID)) {
const { anvilTotalCPUCores, anvilTotalAvailableMemory, storageGroups } =
organizedAnvil;
const isEnoughCPUCores = cpuCores <= anvilTotalCPUCores; const isEnoughCPUCores = cpuCores <= anvilTotalCPUCores;
const isEnoughMemory = memory <= anvilTotalAvailableMemory; const isEnoughMemory = memory <= anvilTotalAvailableMemory;
const hasSelectedStorageGroup =
storageGroups.find(
({ storageGroupUUID }) =>
storageGroupUUID === selectedStorageGroupUUID,
) !== undefined;
return isEnoughCPUCores && isEnoughMemory && hasSelectedStorageGroup; if (isEnoughCPUCores && isEnoughMemory) {
result.anvils.push(organizedAnvil);
result.anvilUUIDs.push(anvilUUID);
result.maxCPUCores = Math.max(anvilTotalCPUCores, result.maxCPUCores);
if (anvilTotalAvailableMemory > result.maxMemory) {
result.maxMemory = anvilTotalAvailableMemory;
}
storageGroups.forEach(({ storageGroupUUID, storageGroupFree }) => {
if (
testIncludeStorageGroup(storageGroupUUID) &&
storageGroupFree > result.maxVirtualDiskSize
) {
result.maxVirtualDiskSize = storageGroupFree;
}
});
}
}
return result;
},
{
anvils: [],
anvilUUIDs: [],
maxCPUCores: 0,
maxMemory: BIGINT_ZERO,
maxVirtualDiskSize: BIGINT_ZERO,
}, },
); );
};
const filterStorageGroups = ( const filterStorageGroups = (
organizedStorageGroups: OrganizedStorageGroupMetadataForProvisionServer[], organizedStorageGroups: OrganizedStorageGroupMetadataForProvisionServer[],
@ -672,9 +660,7 @@ const ProvisionServerDialog = ({
const [excludedFileUUIDs, setExcludedFileUUIDs] = useState<string[]>([]); const [excludedFileUUIDs, setExcludedFileUUIDs] = useState<string[]>([]);
const [anvilValue, setAnvilValue] = useState<string[]>([]); const [anvilValue, setAnvilValue] = useState<string[]>([]);
const [selectedAnvilUUID, setSelectedAnvilUUID] = useState< const [includeAnvilUUIDs, setIncludeAnvilUUIDs] = useState<string[]>([]);
string | undefined
>();
const data = MOCK_DATA; const data = MOCK_DATA;
@ -682,8 +668,11 @@ const ProvisionServerDialog = ({
const organizedStorageGroups = organizeStorageGroups(organizedAnvils); const organizedStorageGroups = organizeStorageGroups(organizedAnvils);
const organizedFiles = organizeFiles(organizedAnvils); const organizedFiles = organizeFiles(organizedAnvils);
const { maxCPUCores, maxMemory, maxVirtualDiskSize } = const { maxCPUCores, maxMemory, maxVirtualDiskSize } = filterAnvils(
getMaxAvailableValues(organizedAnvils); organizedAnvils,
0,
BIGINT_ZERO,
);
const selectFiles = organizedFiles.map(({ fileUUID, fileName }) => ({ const selectFiles = organizedFiles.map(({ fileUUID, fileName }) => ({
displayValue: fileName, displayValue: fileName,
@ -734,7 +723,21 @@ const ProvisionServerDialog = ({
{createOutlinedSlider('ps-cpu-cores', 'CPU cores', cpuCoresValue, { {createOutlinedSlider('ps-cpu-cores', 'CPU cores', cpuCoresValue, {
sliderProps: { sliderProps: {
onChange: (value) => { onChange: (value) => {
setCPUCoresValue(value as number); const setValue = value as number;
setCPUCoresValue(setValue);
const {
anvilUUIDs,
maxCPUCores: localMaxCPUCores,
maxMemory: localMaxMemory,
maxVirtualDiskSize: localMaxVDSize,
} = filterAnvils(organizedAnvils, setValue, memoryValue);
setIncludeAnvilUUIDs(anvilUUIDs);
setInputCPUCoresMax(localMaxCPUCores);
setInputMemoryMax(localMaxMemory);
setInputVirtualDiskSizeMax(localMaxVDSize);
}, },
max: inputCPUCoresMax, max: inputCPUCoresMax,
min: 1, min: 1,
@ -749,7 +752,25 @@ const ProvisionServerDialog = ({
onChange: ({ target: { value } }) => { onChange: ({ target: { value } }) => {
setInputMemoryValue(value); setInputMemoryValue(value);
dSizeToBytes(value, inputMemoryUnit, setMemoryValue); dSizeToBytes(value, inputMemoryUnit, (convertedMemoryValue) => {
setMemoryValue(convertedMemoryValue);
const {
anvilUUIDs,
maxCPUCores: localMaxCPUCores,
maxMemory: localMaxMemory,
maxVirtualDiskSize: localMaxVDSize,
} = filterAnvils(
organizedAnvils,
cpuCoresValue,
convertedMemoryValue,
);
setIncludeAnvilUUIDs(anvilUUIDs);
setInputCPUCoresMax(localMaxCPUCores);
setInputMemoryMax(localMaxMemory);
setInputVirtualDiskSizeMax(localMaxVDSize);
});
}, },
value: inputMemoryValue, value: inputMemoryValue,
}, },
@ -759,7 +780,29 @@ const ProvisionServerDialog = ({
setInputMemoryUnit(selectedUnit); setInputMemoryUnit(selectedUnit);
dSizeToBytes(inputMemoryValue, selectedUnit, setMemoryValue); dSizeToBytes(
inputMemoryValue,
selectedUnit,
(convertedMemoryValue) => {
setMemoryValue(convertedMemoryValue);
const {
anvilUUIDs,
maxCPUCores: localMaxCPUCores,
maxMemory: localMaxMemory,
maxVirtualDiskSize: localMaxVDSize,
} = filterAnvils(
organizedAnvils,
cpuCoresValue,
convertedMemoryValue,
);
setIncludeAnvilUUIDs(anvilUUIDs);
setInputCPUCoresMax(localMaxCPUCores);
setInputMemoryMax(localMaxMemory);
setInputVirtualDiskSizeMax(localMaxVDSize);
},
);
}, },
value: inputMemoryUnit, value: inputMemoryUnit,
}, },
@ -834,12 +877,12 @@ const ProvisionServerDialog = ({
)[0]?.storageGroupUUID, )[0]?.storageGroupUUID,
); );
setInputVirtualDiskSizeMax( // setInputVirtualDiskSizeMax(
getMaxAvailableValues(organizedAnvils, { // getMaxAvailableValues(organizedAnvils, {
includeAnvilUUIDs: anvilValue, // includeAnvilUUIDs: anvilValue,
includeStorageGroupUUIDs: subsetStorageGroupsUUID, // includeStorageGroupUUIDs: subsetStorageGroupsUUID,
}).maxVirtualDiskSize, // }).maxVirtualDiskSize,
); // );
}, },
value: storageGroupValue, value: storageGroupValue,
}, },
@ -863,7 +906,6 @@ const ProvisionServerDialog = ({
value: driverISOFileUUID, value: driverISOFileUUID,
}, },
})} })}
<BodyText text={`Selected anvil UUID: ${selectedAnvilUUID}`} />
{createOutlinedSelect( {createOutlinedSelect(
'ps-anvil', 'ps-anvil',
'Anvil', 'Anvil',
@ -873,6 +915,7 @@ const ProvisionServerDialog = ({
})), })),
{ {
checkItem: (value) => anvilValue.includes(value), checkItem: (value) => anvilValue.includes(value),
hideItem: (value) => !includeAnvilUUIDs.includes(value),
selectProps: { selectProps: {
multiple: true, multiple: true,
onChange: ({ target: { value } }) => { onChange: ({ target: { value } }) => {
@ -941,29 +984,18 @@ const ProvisionServerDialog = ({
} }
}); });
const { // const {
maxCPUCores: localMaxCPUCores, // maxCPUCores: localMaxCPUCores,
maxMemory: localMaxMemory, // maxMemory: localMaxMemory,
maxVirtualDiskSize: localMaxVDSize, // maxVirtualDiskSize: localMaxVDSize,
} = getMaxAvailableValues(organizedAnvils, { // } = getMaxAvailableValues(organizedAnvils, {
includeAnvilUUIDs: subsetAnvilUUIDs, // includeAnvilUUIDs: subsetAnvilUUIDs,
includeStorageGroupUUIDs: newStorageGroupValue, // includeStorageGroupUUIDs: newStorageGroupValue,
}); // });
setInputCPUCoresMax(localMaxCPUCores); // setInputCPUCoresMax(localMaxCPUCores);
setInputMemoryMax(localMaxMemory); // setInputMemoryMax(localMaxMemory);
setInputVirtualDiskSizeMax(localMaxVDSize); // setInputVirtualDiskSizeMax(localMaxVDSize);
setCPUCoresValue(Math.min(cpuCoresValue, localMaxCPUCores));
setSelectedAnvilUUID(
filterAnvils(
organizedAnvils,
cpuCoresValue,
memoryValue,
selectedStorageGroupUUID,
)[0]?.anvilUUID,
);
}, },
value: anvilValue, value: anvilValue,
}, },

Loading…
Cancel
Save