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 } from '~/models/workflow';
10 import { getFormValues } 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 { navigateToProcess } from '../navigation/navigation-action';
17 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from '~/views/run-process-panel/run-process-advanced-form';
18 import { isItemNotInProject, isProjectOrRunProcessRoute } from '~/store/projects/project-create-actions';
19 import { dialogActions } from '~/store/dialog/dialog-actions';
21 export const runProcessPanelActions = unionize({
22 SET_PROCESS_OWNER_UUID: ofType<string>(),
23 SET_CURRENT_STEP: ofType<number>(),
24 SET_STEP_CHANGED: ofType<boolean>(),
25 SET_WORKFLOWS: ofType<WorkflowResource[]>(),
26 SET_SELECTED_WORKFLOW: ofType<WorkflowResource>(),
27 SEARCH_WORKFLOWS: ofType<string>(),
28 RESET_RUN_PROCESS_PANEL: ofType<{}>(),
31 export interface RunProcessSecondStepDataFormProps {
36 export const SET_WORKFLOW_DIALOG = 'setWorkflowDialog';
37 export const RUN_PROCESS_SECOND_STEP_FORM_NAME = 'runProcessSecondStepFormName';
39 export type RunProcessPanelAction = UnionOf<typeof runProcessPanelActions>;
41 export const loadRunProcessPanel = () =>
42 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
44 dispatch(runProcessPanelActions.RESET_RUN_PROCESS_PANEL());
45 const response = await services.workflowService.list();
46 dispatch(runProcessPanelActions.SET_WORKFLOWS(response.items));
52 export const openSetWorkflowDialog = (workflow: WorkflowResource) =>
53 (dispatch: Dispatch, getState: () => RootState) => {
54 const selectedWorkflow = getState().runProcessPanel.selectedWorkflow;
55 const isStepChanged = getState().runProcessPanel.isStepChanged;
56 if (isStepChanged && selectedWorkflow && selectedWorkflow.uuid !== workflow.uuid) {
57 dispatch(dialogActions.OPEN_DIALOG({
58 id: SET_WORKFLOW_DIALOG,
60 title: 'Data loss warning',
61 text: 'Changing a workflow will clean all input fields in next step.',
62 confirmButtonLabel: 'Change Workflow',
67 dispatch<any>(setWorkflow(workflow, false));
71 export const setWorkflow = (workflow: WorkflowResource, isWorkflowChanged = true) =>
72 (dispatch: Dispatch<any>, getState: () => RootState) => {
73 const isStepChanged = getState().runProcessPanel.isStepChanged;
74 if (isStepChanged && isWorkflowChanged) {
75 dispatch(runProcessPanelActions.SET_STEP_CHANGED(false));
76 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
78 if (!isWorkflowChanged) {
79 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(workflow));
83 export const goToStep = (step: number) =>
84 (dispatch: Dispatch, getState: () => RootState) => {
86 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
88 dispatch(runProcessPanelActions.SET_CURRENT_STEP(step));
91 export const runProcess = async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
92 const state = getState();
93 const basicForm = getFormValues(RUN_PROCESS_BASIC_FORM)(state) as RunProcessBasicFormData;
94 const inputsForm = getFormValues(RUN_PROCESS_INPUTS_FORM)(state) as WorkflowInputsData;
95 const advancedForm = getFormValues(RUN_PROCESS_ADVANCED_FORM)(state) as RunProcessAdvancedFormData;
96 const userUuid = getState().auth.user!.uuid;
97 const router = getState();
98 const properties = getState().properties;
99 const { processOwnerUuid, selectedWorkflow } = state.runProcessPanel;
100 if (selectedWorkflow) {
101 const newProcessData = {
102 ownerUuid: isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router) ? userUuid : processOwnerUuid,
103 name: basicForm.name,
104 description: basicForm.description,
105 state: ContainerRequestState.COMMITTED,
106 mounts: createWorkflowMounts(selectedWorkflow, normalizeInputKeys(inputsForm)),
107 runtimeConstraints: {
112 containerImage: 'arvados/jobs',
113 cwd: '/var/spool/cwl',
115 'arvados-cwl-runner',
118 `--project-uuid=${processOwnerUuid}`,
119 '/var/lib/cwl/workflow.json#main',
120 '/var/lib/cwl/cwl.input.json'
122 outputPath: '/var/spool/cwl',
124 outputName: advancedForm && advancedForm.output ? advancedForm.output : undefined,
126 const newProcess = await services.containerRequestService.create(newProcessData);
127 dispatch(navigateToProcess(newProcess.uuid));
131 const normalizeInputKeys = (inputs: WorkflowInputsData): WorkflowInputsData =>
132 Object.keys(inputs).reduce((normalizedInputs, key) => ({
134 [key.split('/').slice(1).join('/')]: inputs[key],
136 export const searchWorkflows = (term: string) => runProcessPanelActions.SEARCH_WORKFLOWS(term);