Skip to content

Commit

Permalink
Merge branch 'staging' of github.com:FNNDSC/ChRIS_ui into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Sep 30, 2024
2 parents 684d06a + bc76357 commit 9de4e08
Show file tree
Hide file tree
Showing 11 changed files with 627 additions and 423 deletions.
6 changes: 3 additions & 3 deletions src/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ export async function fetchComputeInfo(
}

export function catchError(errorRequest: any) {
if (errorRequest.response) {
return { error_message: errorRequest.response.data as string };
if (errorRequest?.response?.data) {
return { error_message: errorRequest.response.data };
}

if (errorRequest.message) {
if (errorRequest?.message) {
return { error_message: errorRequest.message as string };
}
return {
Expand Down
59 changes: 22 additions & 37 deletions src/components/AddNode/AddNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { getNodeOperations } from "../../store/plugin/pluginSlice";
import { addNode } from "../../store/pluginInstance/pluginInstanceSlice";
import type { ApplicationState } from "../../store/root/applicationState";
import { Alert } from "../Antd";
import { getRequiredObject } from "../CreateFeed/createFeedHelper";
import {
getParameterInput,
sanitizeAdvancedConfig,
} from "../CreateFeed/createFeedHelper";
import BasicConfiguration from "./BasicConfiguration";
import GuidedConfig from "./GuidedConfig";
import "./add-node.css";
Expand All @@ -30,6 +33,7 @@ const AddNode: React.FC = () => {
);
const params = useAppSelector((state) => state.plugin.parameters);
const { state, dispatch: nodeDispatch } = useContext(AddNodeContext);

const {
pluginMeta,
selectedPluginFromMeta: plugin,
Expand All @@ -56,51 +60,33 @@ const AddNode: React.FC = () => {
);

const handleSave = useCallback(async () => {
if (!plugin || !selectedPlugin || !pluginInstances) {
return;
}

const advancedConfigErrors: Record<string, string> = {};
const sanitizedInput: Record<string, string> = {};
if (!plugin || !selectedPlugin || !pluginInstances) return;

for (const key in advancedConfig) {
const inputValue = +advancedConfig[key];

if (Number.isNaN(inputValue)) {
advancedConfigErrors[key] = "A valid integer is required";
} else {
if (key === "cpu_limit") {
sanitizedInput[key] = `${inputValue * 1000}m`;
}
if (key === "memory_limit") {
sanitizedInput[key] = `${inputValue}${memoryLimit}`;
}
}
}
const { advancedConfigErrors, sanitizedInput } = sanitizeAdvancedConfig(
advancedConfig,
memoryLimit,
);

if (Object.keys(advancedConfigErrors).length > 0) {
errorCallback(advancedConfigErrors);
return;
}

const { data: nodes } = pluginInstances;
let parameterInput = await getRequiredObject(
dropdownInput,
requiredInput,
plugin,
selectedPlugin,
);

parameterInput = {
...parameterInput,
compute_resource_name: selectedComputeEnv,
...sanitizedInput,
};

try {
const parameterInput = await getParameterInput(
dropdownInput,
requiredInput,
plugin,
selectedComputeEnv,
sanitizedInput,
selectedPlugin,
);

const { data: nodes } = pluginInstances;
const pluginInstance = await plugin.getPluginInstances();
await pluginInstance.post(parameterInput);
const nodeList = pluginInstance.getItems();

if (nodeList) {
dispatch(addNode({ pluginItem: nodeList[0], nodes }));
toggleOpen();
Expand All @@ -115,7 +101,6 @@ const AddNode: React.FC = () => {
pluginInstances,
dropdownInput,
requiredInput,
plugin,
selectedComputeEnv,
advancedConfig,
memoryLimit,
Expand Down Expand Up @@ -163,7 +148,7 @@ const AddNode: React.FC = () => {
<WizardStep
id="2"
name="Plugin Form"
footer={{ nextButtonText: "Add Node", isNextDisabled: !!isDisabled }}
footer={{ nextButtonText: "Add Node", isNextDisabled: isDisabled }}
>
<GuidedConfig />
</WizardStep>
Expand Down
14 changes: 7 additions & 7 deletions src/components/CreateFeed/CreateFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export default function CreateFeed() {
useContext(PipelineContext);

const user = useAppSelector((state) => state.user);
const { pluginMeta, selectedPluginFromMeta, dropdownInput, requiredInput } =
addNodeState;
const {
pluginMeta,

dropdownInput,
requiredInput,
} = addNodeState;
const { wizardOpen, data, selectedConfig } = state;

const getUploadFileCount = (value: number) => {
Expand Down Expand Up @@ -106,11 +110,7 @@ export default function CreateFeed() {
}

if (selectedConfig.includes("fs_plugin")) {
feed = await createFeedInstanceWithFS(
dropdownInput,
requiredInput,
selectedPluginFromMeta,
);
feed = await createFeedInstanceWithFS(addNodeState);
}

if (feed) {
Expand Down
102 changes: 89 additions & 13 deletions src/components/CreateFeed/createFeedHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { InputType } from "../AddNode/types";
import { unpackParametersIntoObject } from "../AddNode/utils";
import type { PipelineState } from "../PipelinesCopy/context";
import type { CreateFeedData } from "./types/feed";
import type { AddNodeState } from "../AddNode/types";

export const createFeed = async (
data: CreateFeedData,
Expand Down Expand Up @@ -155,28 +156,42 @@ export const getPlugin = async (
}
};

export const createFeedInstanceWithFS = async (
dropdownInput: InputType,
requiredInput: InputType,
selectedPlugin: Plugin | undefined,
) => {
export const createFeedInstanceWithFS = async (state: AddNodeState) => {
const {
dropdownInput,
requiredInput,
advancedConfig,
selectedComputeEnv,
selectedPluginFromMeta: selectedPlugin,
memoryLimit,
} = state;

try {
if (selectedPlugin) {
const data = await getRequiredObject(
const { advancedConfigErrors, sanitizedInput } = sanitizeAdvancedConfig(
advancedConfig,
memoryLimit,
);

if (Object.keys(advancedConfigErrors).length > 0) {
throw new Error("Advanced config errors");
}

const parameterInput = await getParameterInput(
dropdownInput,
requiredInput,
selectedPlugin,
selectedComputeEnv,
sanitizedInput,
);
const pluginId = selectedPlugin.data.id;
const client = ChrisAPIClient.getClient();
const fsPluginInstance = await client.createPluginInstance(
pluginId,
//@ts-ignore
data,

const { feed } = await createPluginInstance(
selectedPlugin.data.id,
parameterInput,
);
const feed = await fsPluginInstance.getFeed();
return feed;
}

return undefined;
} catch (e: any) {
// biome-ignore lint/complexity/noUselessCatch: <explanation>
Expand Down Expand Up @@ -271,3 +286,64 @@ export const getRequiredObject = async (
throw error;
}
};

export const sanitizeAdvancedConfig = (
advancedConfig: Record<string, string>,
memoryLimit: string,
) => {
const advancedConfigErrors: Record<string, string> = {};
const sanitizedInput: Record<string, string> = {};

for (const key in advancedConfig) {
const inputValue = +advancedConfig[key];

if (Number.isNaN(inputValue)) {
advancedConfigErrors[key] = "A valid integer is required";
} else {
if (key === "cpu_limit") {
sanitizedInput[key] = `${inputValue * 1000}m`;
}
if (key === "memory_limit") {
sanitizedInput[key] = `${inputValue}${memoryLimit}`;
}
}
}

return { advancedConfigErrors, sanitizedInput };
};

export const createPluginInstance = async (
pluginId: number,
parameterInput: Record<string, any>,
) => {
const client = ChrisAPIClient.getClient();
const pluginInstance = await client.createPluginInstance(
pluginId,
//@ts-ignore
parameterInput,
);
const feed = await pluginInstance.getFeed();
return { pluginInstance, feed };
};

export const getParameterInput = async (
dropdownInput: Record<string, any>,
requiredInput: Record<string, any>,
plugin: any,
selectedComputeEnv: string,
sanitizedInput: Record<string, any>,
selectedPlugin?: PluginInstance,
) => {
let parameterInput = await getRequiredObject(
dropdownInput,
requiredInput,
plugin,
selectedPlugin,
);
parameterInput = {
...parameterInput,
compute_resource_name: selectedComputeEnv,
...sanitizedInput,
};
return parameterInput;
};
Loading

0 comments on commit 9de4e08

Please sign in to comment.