Merge branch '16583-intermediate-collections' refs #16583
[arvados-workbench2.git] / src / store / run-process-panel / run-process-panel-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 { unionize, ofType, UnionOf } from "common/unionize";
7 import { ServiceRepository } from "services/services";
8 import { RootState } from 'store/store';
9 import { getUserUuid } from "common/getuser";
10 import { WorkflowResource, WorkflowRunnerResources, getWorkflow, getWorkflowInputs, parseWorkflowDefinition } from 'models/workflow';
11 import { getFormValues, initialize } from 'redux-form';
12 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from 'views/run-process-panel/run-process-basic-form';
13 import { RUN_PROCESS_INPUTS_FORM } from 'views/run-process-panel/run-process-inputs-form';
14 import { WorkflowInputsData } from 'models/workflow';
15 import { createWorkflowMounts } from 'models/process';
16 import { ContainerRequestState } from 'models/container-request';
17 import { navigateTo } from '../navigation/navigation-action';
18 import {
19     RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM, VCPUS_FIELD,
20     KEEP_CACHE_RAM_FIELD, RAM_FIELD, RUNTIME_FIELD, OUTPUT_FIELD, RUNNER_IMAGE_FIELD
21 } from 'views/run-process-panel/run-process-advanced-form';
22 import { dialogActions } from 'store/dialog/dialog-actions';
23 import { setBreadcrumbs } from 'store/breadcrumbs/breadcrumbs-actions';
24
25 export const runProcessPanelActions = unionize({
26     SET_PROCESS_PATHNAME: ofType<string>(),
27     SET_PROCESS_OWNER_UUID: ofType<string>(),
28     SET_CURRENT_STEP: ofType<number>(),
29     SET_STEP_CHANGED: ofType<boolean>(),
30     SET_WORKFLOWS: ofType<WorkflowResource[]>(),
31     SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
32     SET_WORKFLOW_PRESETS: ofType<WorkflowResource[]>(),
33     SELECT_WORKFLOW_PRESET: ofType<WorkflowResource>(),
34     SEARCH_WORKFLOWS: ofType<string>(),
35     RESET_RUN_PROCESS_PANEL: ofType<{}>(),
36 });
37
38 export interface RunProcessSecondStepDataFormProps {
39     name: string;
40     description: string;
41 }
42
43 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
44 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
45
46 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
47
48 export const loadRunProcessPanel = () =>
49     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
50         try {
51             dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
52             const response = await services.workflowService.list();
53             dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
54         } catch (e) {
55             return;
56         }
57     };
58
59 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
60     (dispatch: Dispatch, getState: () => RootState) => {
61         const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
62         const isStepChanged = getState().runProcessPanel.isStepChanged;
63         if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
64             dispatch(dialogActions.OPEN_DIALOG({
65                 id: SET_WORKFLOW_DIALOG,
66                 data: {
67                     title: 'Form will be cleared',
68                     text: 'Changing a workflow will clean all input fields in next step.',
69                     confirmButtonLabel: 'Change Workflow',
70                     workflow
71                 }
72             }));
73         } else {
74             dispatch<any>(setWorkflow(workflow, false));
75         }
76     };
77
78 export const getWorkflowRunnerSettings = (workflow: WorkflowResource) => {
79     const advancedFormValues = {};
80     Object.assign(advancedFormValues, DEFAULT_ADVANCED_FORM_VALUES);
81
82     const wf = getWorkflow(parseWorkflowDefinition(workflow));
83     const hints = wf ? wf.hints : undefined;
84     if (hints) {
85         const resc = hints.find(item => item.class === 'http://arvados.org/cwl#WorkflowRunnerResources') as WorkflowRunnerResources | undefined;
86         if (resc) {
87             if (resc.ramMin) { advancedFormValues[RAM_FIELD] = resc.ramMin * (1024 * 1024); }
88             if (resc.coresMin) { advancedFormValues[VCPUS_FIELD] = resc.coresMin; }
89             if (resc.keep_cache) { advancedFormValues[KEEP_CACHE_RAM_FIELD] = resc.keep_cache * (1024 * 1024); }
90             if (resc.acrContainerImage) { advancedFormValues[RUNNER_IMAGE_FIELD] = resc.acrContainerImage; }
91         }
92     }
93     return advancedFormValues;
94 };
95
96 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
97     (dispatch: Dispatch<any>, getState: () => RootState) => {
98         const isStepChanged = getState().runProcessPanel.isStepChanged;
99
100         const advancedFormValues = getWorkflowRunnerSettings(workflow);
101
102         if (isStepChanged && isWorkflowChanged) {
103             dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
104             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
105             dispatch<any>(loadPresets(workflow.uuid));
106             dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
107         }
108         if (!isWorkflowChanged) {
109             dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
110             dispatch<any>(loadPresets(workflow.uuid));
111             dispatch(initialize(RUN_PROCESS_ADVANCED_FORM, advancedFormValues));
112         }
113     };
114
115 export const loadPresets = (workflowUuid: string) =>
116     async (dispatch: Dispatch<any>, _: () => RootState, { workflowService }: ServiceRepository) => {
117         const { items } = await workflowService.presets(workflowUuid);
118         dispatch(runProcessPanelActions.SET_WORKFLOW_PRESETS(items));
119     };
120
121 export const selectPreset = (preset: WorkflowResource) =>
122     (dispatch: Dispatch<any>) => {
123         dispatch(runProcessPanelActions.SELECT_WORKFLOW_PRESET(preset));
124         const inputs = getWorkflowInputs(parseWorkflowDefinition(preset)) || [];
125         const values = inputs.reduce((values, input) => ({
126             ...values,
127             [input.id]: input.default,
128         }), {});
129         dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
130     };
131
132 export const goToStep = (step: number) =>
133     (dispatch: Dispatch) => {
134         if (step === 1) {
135             dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
136         }
137         dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
138     };
139
140 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
141     const state = getState();
142     const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
143     const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
144     const userUuid = getUserUuid(getState());
145     if (!userUuid) { return; }
146     const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
147     const ownerUUid = processOwnerUuid ? processOwnerUuid : userUuid;
148     if (selectedWorkflow) {
149         const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData || getWorkflowRunnerSettings(selectedWorkflow);
150         const newProcessData = {
151             ownerUuid: ownerUUid,
152             name: basicForm.name,
153             description: basicForm.description,
154             state: ContainerRequestState.COMMITTED,
155             mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
156             runtimeConstraints: {
157                 API: true,
158                 vcpus: advancedForm[VCPUS_FIELD],
159                 ram: (advancedForm[KEEP_CACHE_RAM_FIELD] + advancedForm[RAM_FIELD]),
160             },
161             schedulingParameters: {
162                 max_run_time: advancedForm[RUNTIME_FIELD]
163             },
164             containerImage: advancedForm[RUNNER_IMAGE_FIELD],
165             cwd: '/var/spool/cwl',
166             command: [
167                 'arvados-cwl-runner',
168                 '--api=containers',
169                 '--local',
170                 `--project-uuid=${ownerUUid}`,
171                 '/var/lib/cwl/workflow.json#main',
172                 '/var/lib/cwl/cwl.input.json'
173             ],
174             outputPath: '/var/spool/cwl',
175             priority: 1,
176             outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : undefined,
177             properties: {
178                 workflowUuid: selectedWorkflow.uuid,
179                 workflowName: selectedWorkflow.name
180             },
181             useExisting: false
182         };
183         const newProcess = await services.containerRequestService.create(newProcessData);
184         dispatch(navigateTo(newProcess.uuid));
185     }
186 };
187
188 const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
189     [VCPUS_FIELD]: 1,
190     [RAM_FIELD]: 1073741824,
191     [KEEP_CACHE_RAM_FIELD]: 268435456,
192     [RUNNER_IMAGE_FIELD]: "arvados/jobs"
193 };
194
195 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
196     Object.keys(inputs).reduce((normalizedInputs, key) => ({
197         ...normalizedInputs,
198         [key.split('/').slice(1).join('/')]: inputs[key],
199     }), {});
200 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);