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