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