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