1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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<{}>(),
35 export interface RunProcessSecondStepDataFormProps {
40 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
41 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
43 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
45 export const loadRunProcessPanel = () =>
46 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
48 dispatch(setBreadcrumbs([{ label: 'Run Process' }]));
49 const response = await services.workflowService.list();
50 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
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,
64 title: 'Form will be cleared',
65 text: 'Changing a workflow will clean all input fields in next step.',
66 confirmButtonLabel: 'Change Workflow',
71 dispatch<any>(setWorkflow(workflow, false));
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));
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));
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));
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) => ({
103 [input.id]: input.default,
105 dispatch(initialize(RUN_PROCESS_INPUTS_FORM, values));
108 export const goToStep = (step: number) =>
109 (dispatch: Dispatch) => {
111 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
113 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
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: {
133 vcpus: advancedForm[VCPUS_FIELD],
134 ram: advancedForm[RAM_FIELD],
135 api: advancedForm[API_FIELD],
137 schedulingParameters: {
138 max_run_time: advancedForm[RUNTIME_FIELD]
140 containerImage: 'arvados/jobs',
141 cwd: '/var/spool/cwl',
143 'arvados-cwl-runner',
146 '/var/lib/cwl/workflow.json#main',
147 '/var/lib/cwl/cwl.input.json'
149 outputPath: '/var/spool/cwl',
151 outputName: advancedForm[OUTPUT_FIELD] ? advancedForm[OUTPUT_FIELD] : undefined,
153 workflowUuid: selectedWorkflow.uuid,
154 workflowName: selectedWorkflow.name
157 const newProcess = await services.containerRequestService.create(newProcessData);
158 dispatch(navigateTo(newProcess.uuid));
162 export const DEFAULT_ADVANCED_FORM_VALUES: Partial<RunProcessAdvancedFormData> = {
164 [RAM_FIELD]: 1073741824,
168 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
169 Object.keys(inputs).reduce((normalizedInputs, key) => ({
171 [key.split('/').slice(1).join('/')]: inputs[key],
173 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);