Merge branch '18128-multi-panel-view' into main. Closes #18128.
[arvados-workbench2.git] / src / store / processes / processes-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { updateResources } from 'store/resources/resources-actions';
9 import { Process } from './process';
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { projectPanelActions } from 'store/project-panel/project-panel-action';
13 import { navigateToRunProcess } from 'store/navigation/navigation-action';
14 import { goToStep, runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
15 import { getResource } from 'store/resources/resources';
16 import { initialize } from "redux-form";
17 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from "views/run-process-panel/run-process-basic-form";
18 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from "views/run-process-panel/run-process-advanced-form";
19 import { MOUNT_PATH_CWL_WORKFLOW, MOUNT_PATH_CWL_INPUT } from 'models/process';
20 import { getWorkflow, getWorkflowInputs } from "models/workflow";
21
22 export const loadProcess = (containerRequestUuid: string) =>
23     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process> => {
24         const containerRequest = await services.containerRequestService.get(containerRequestUuid);
25         dispatch<any>(updateResources([containerRequest]));
26
27         if (containerRequest.containerUuid) {
28             const container = await services.containerService.get(containerRequest.containerUuid);
29             dispatch<any>(updateResources([container]));
30             return { containerRequest, container };
31         }
32         return { containerRequest };
33     };
34
35 export const loadContainers = (filters: string) =>
36     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
37         const { items } = await services.containerService.list({ filters });
38         dispatch<any>(updateResources(items));
39         return items;
40     };
41
42 export const cancelRunningWorkflow = (uuid: string) =>
43     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         try {
45             const process = await services.containerRequestService.update(uuid, { priority: 0 });
46             return process;
47         } catch (e) {
48             throw new Error('Could not cancel the process.');
49         }
50     };
51
52 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
53     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54         const process = getResource<any>(processUuid)(getState().resources);
55         const workflows = getState().runProcessPanel.searchWorkflows;
56         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
57         if (workflow && process) {
58             const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
59             if (mainWf) { mainWf.inputs = getInputs(process); }
60             const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
61             const newWorkflow = { ...workflow, definition: stringifiedDefinition };
62
63             const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description };
64             dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
65
66             const advancedInitialData: RunProcessAdvancedFormData = {
67                 output: process.outputName,
68                 runtime: process.schedulingParameters.max_run_time,
69                 ram: process.runtimeConstraints.ram,
70                 vcpus: process.runtimeConstraints.vcpus,
71                 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
72                 acr_container_image: process.containerImage
73             };
74             dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
75
76             dispatch<any>(navigateToRunProcess);
77             dispatch<any>(goToStep(1));
78             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
79             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
80         } else {
81             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
82         }
83     };
84
85 const getInputs = (data: any) => {
86     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
87     const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
88     return inputs ? inputs.map(
89         (it: any) => (
90             {
91                 type: it.type,
92                 id: it.id,
93                 label: it.label,
94                 default: data.mounts[MOUNT_PATH_CWL_INPUT].content[it.id],
95                 doc: it.doc
96             }
97         )
98     ) : [];
99 };
100
101 export const openRemoveProcessDialog = (uuid: string) =>
102     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
103         dispatch(dialogActions.OPEN_DIALOG({
104             id: REMOVE_PROCESS_DIALOG,
105             data: {
106                 title: 'Remove process permanently',
107                 text: 'Are you sure you want to remove this process?',
108                 confirmButtonLabel: 'Remove',
109                 uuid
110             }
111         }));
112     };
113
114 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
115
116 export const removeProcessPermanently = (uuid: string) =>
117     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
118         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
119         await services.containerRequestService.delete(uuid);
120         dispatch(projectPanelActions.REQUEST_ITEMS());
121         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
122     };