16073: Add process IO panels with image preview and raw view
[arvados.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 } from "models/workflow";
21 import { ProjectResource } from "models/project";
22 import { UserResource } from "models/user";
23
24 export const loadProcess = (containerRequestUuid: string) =>
25     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process> => {
26         const containerRequest = await services.containerRequestService.get(containerRequestUuid);
27         dispatch<any>(updateResources([containerRequest]));
28
29         if (containerRequest.containerUuid) {
30             const container = await services.containerService.get(containerRequest.containerUuid);
31             dispatch<any>(updateResources([container]));
32             return { containerRequest, container };
33         }
34         return { containerRequest };
35     };
36
37 export const loadContainers = (filters: string) =>
38     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39         const { items } = await services.containerService.list({ filters });
40         dispatch<any>(updateResources(items));
41         return items;
42     };
43
44 export const cancelRunningWorkflow = (uuid: string) =>
45     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46         try {
47             const process = await services.containerRequestService.update(uuid, { priority: 0 });
48             return process;
49         } catch (e) {
50             throw new Error('Could not cancel the process.');
51         }
52     };
53
54 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
55     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56         const process = getResource<any>(processUuid)(getState().resources);
57         const workflows = getState().runProcessPanel.searchWorkflows;
58         const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
59         if (workflow && process) {
60             const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
61             if (mainWf) { mainWf.inputs = getInputs(process); }
62             const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
63             const newWorkflow = { ...workflow, definition: stringifiedDefinition };
64
65             const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
66             const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
67             dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
68
69             const advancedInitialData: RunProcessAdvancedFormData = {
70                 output: process.outputName,
71                 runtime: process.schedulingParameters.max_run_time,
72                 ram: process.runtimeConstraints.ram,
73                 vcpus: process.runtimeConstraints.vcpus,
74                 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
75                 acr_container_image: process.containerImage
76             };
77             dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
78
79             dispatch<any>(navigateToRunProcess);
80             dispatch<any>(goToStep(1));
81             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
82             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
83         } else {
84             dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
85         }
86     };
87
88 export const getInputs = (data: any): CommandInputParameter[] => {
89     if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
90     const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
91     return inputs ? inputs.map(
92         (it: any) => (
93             {
94                 type: it.type,
95                 id: it.id,
96                 label: it.label,
97                 default: data.mounts[MOUNT_PATH_CWL_INPUT].content[it.id],
98                 value: data.mounts[MOUNT_PATH_CWL_INPUT].content[it.id.split('/').pop()] || [],
99                 doc: it.doc
100             }
101         )
102     ) : [];
103 };
104
105 export const openRemoveProcessDialog = (uuid: string) =>
106     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
107         dispatch(dialogActions.OPEN_DIALOG({
108             id: REMOVE_PROCESS_DIALOG,
109             data: {
110                 title: 'Remove process permanently',
111                 text: 'Are you sure you want to remove this process?',
112                 confirmButtonLabel: 'Remove',
113                 uuid
114             }
115         }));
116     };
117
118 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
119
120 export const removeProcessPermanently = (uuid: string) =>
121     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
122         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
123         await services.containerRequestService.delete(uuid);
124         dispatch(projectPanelActions.REQUEST_ITEMS());
125         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
126     };