16073: Display process io params from props, hide preview when lacking workflow mount
[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 { CommandInputParameter, getWorkflow, getWorkflowInputs, getWorkflowOutputs } from "models/workflow";
21 import { ProjectResource } from "models/project";
22 import { UserResource } from "models/user";
23 import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
24
25 export const loadProcess = (containerRequestUuid: string) =>
26     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process> => {
27         const containerRequest = await services.containerRequestService.get(containerRequestUuid);
28         dispatch<any>(updateResources([containerRequest]));
29
30         if (containerRequest.outputUuid) {
31             const collection = await services.collectionService.get(containerRequest.outputUuid);
32             dispatch<any>(updateResources([collection]));
33         }
34
35         if (containerRequest.containerUuid) {
36             const container = await services.containerService.get(containerRequest.containerUuid);
37             dispatch<any>(updateResources([container]));
38             return { containerRequest, container };
39         }
40         return { containerRequest };
41     };
42
43 export const loadContainers = (filters: string, loadMounts: boolean = true) =>
44     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45         let args: any = { filters };
46         if (!loadMounts) {
47             args.select = containerFieldsNoMounts;
48         }
49         const { items } = await services.containerService.list(args);
50         dispatch<any>(updateResources(items));
51         return items;
52     };
53
54 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
55 const containerFieldsNoMounts = [
56     "auth_uuid",
57     "command",
58     "container_image",
59     "created_at",
60     "cwd",
61     "environment",
62     "etag",
63     "exit_code",
64     "finished_at",
65     "gateway_address",
66     "href",
67     "interactive_session_started",
68     "kind",
69     "lock_count",
70     "locked_by_uuid",
71     "log",
72     "modified_at",
73     "modified_by_client_uuid",
74     "modified_by_user_uuid",
75     "output_path",
76     "output_properties",
77     "output_storage_classes",
78     "output",
79     "owner_uuid",
80     "priority",
81     "progress",
82     "runtime_auth_scopes",
83     "runtime_constraints",
84     "runtime_status",
85     "runtime_user_uuid",
86     "scheduling_parameters",
87     "started_at",
88     "state",
89     "uuid",
90 ]
91
92 export const cancelRunningWorkflow = (uuid: string) =>
93     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
94         try {
95             const process = await services.containerRequestService.update(uuid, { priority: 0 });
96             return process;
97         } catch (e) {
98             throw new Error('Could not cancel the process.');
99         }
100     };
101
102 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
103     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
104         const process = getResource<any>(processUuid)(getState().resources);
105         const workflows = getState().runProcessPanel.searchWorkflows;
106         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
107         if (workflow && process) {
108             const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
109             if (mainWf) { mainWf.inputs = getInputs(process); }
110             const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
111             const newWorkflow = { ...workflow, definition: stringifiedDefinition };
112
113             const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
114             const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
115             dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
116
117             const advancedInitialData: RunProcessAdvancedFormData = {
118                 output: process.outputName,
119                 runtime: process.schedulingParameters.max_run_time,
120                 ram: process.runtimeConstraints.ram,
121                 vcpus: process.runtimeConstraints.vcpus,
122                 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
123                 acr_container_image: process.containerImage
124             };
125             dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
126
127             dispatch<any>(navigateToRunProcess);
128             dispatch<any>(goToStep(1));
129             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
130             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
131         } else {
132             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
133         }
134     };
135
136 /*
137  * Fetches raw inputs from containerRequest mounts with fallback to properties
138  */
139 export const getRawInputs = (data: any): CommandInputParameter[] | undefined => {
140     if (!data) { return undefined; }
141     const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
142     const propsInput = data.properties?.cwl_input;
143     if (!mountInput && !propsInput) { return undefined; }
144     return (mountInput || propsInput);
145 }
146
147 export const getInputs = (data: any): CommandInputParameter[] => {
148     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
149     const content  = getRawInputs(data) as any;
150     if (!content) { return []; }
151
152     const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
153     return inputs ? inputs.map(
154         (it: any) => (
155             {
156                 type: it.type,
157                 id: it.id,
158                 label: it.label,
159                 default: content[it.id],
160                 value: content[it.id.split('/').pop()] || [],
161                 doc: it.doc
162             }
163         )
164     ) : [];
165 };
166
167 /*
168  * Fetches raw outputs from containerRequest properties
169  */
170 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
171     if (!data || !data.properties || !data.properties.cwl_output) { return undefined; }
172     return (data.properties.cwl_output);
173 }
174
175 export type InputCollectionMount = {
176     path: string;
177     pdh: string;
178 }
179
180 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
181     if (!data || !data.mounts) { return []; }
182     return Object.keys(data.mounts)
183         .map(key => ({
184             ...data.mounts[key],
185             path: key,
186         }))
187         .filter(mount => mount.kind === 'collection' &&
188                 mount.portable_data_hash &&
189                 mount.path)
190         .map(mount => ({
191             path: mount.path,
192             pdh: mount.portable_data_hash,
193         }));
194 };
195
196 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
197     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
198     const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
199     return outputs ? outputs.map(
200         (it: any) => (
201             {
202                 type: it.type,
203                 id: it.id,
204                 label: it.label,
205                 doc: it.doc
206             }
207         )
208     ) : [];
209 };
210
211 export const openRemoveProcessDialog = (uuid: string) =>
212     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
213         dispatch(dialogActions.OPEN_DIALOG({
214             id: REMOVE_PROCESS_DIALOG,
215             data: {
216                 title: 'Remove process permanently',
217                 text: 'Are you sure you want to remove this process?',
218                 confirmButtonLabel: 'Remove',
219                 uuid
220             }
221         }));
222     };
223
224 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
225
226 export const removeProcessPermanently = (uuid: string) =>
227     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
228         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
229         await services.containerRequestService.delete(uuid);
230         dispatch(projectPanelActions.REQUEST_ITEMS());
231         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
232     };