1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { updateResources } from 'store/resources/resources-actions';
9 import { Process } from './process';
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { projectPanelActions } from 'store/project-panel/project-panel-action';
13 import { navigateToRunProcess } from 'store/navigation/navigation-action';
14 import { goToStep, runProcessPanelActions } from 'store/run-process-panel/run-process-panel-actions';
15 import { getResource } from 'store/resources/resources';
16 import { initialize } from "redux-form";
17 import { RUN_PROCESS_BASIC_FORM, RunProcessBasicFormData } from "views/run-process-panel/run-process-basic-form";
18 import { RunProcessAdvancedFormData, RUN_PROCESS_ADVANCED_FORM } from "views/run-process-panel/run-process-advanced-form";
19 import { MOUNT_PATH_CWL_WORKFLOW, MOUNT_PATH_CWL_INPUT } from 'models/process';
20 import { CommandInputParameter, getWorkflow, getWorkflowInputs, getWorkflowOutputs } from "models/workflow";
21 import { ProjectResource } from "models/project";
22 import { UserResource } from "models/user";
23 import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
25 export const loadProcess = (containerRequestUuid: string) =>
26 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository): Promise<Process> => {
27 const containerRequest = await services.containerRequestService.get(containerRequestUuid);
28 dispatch<any>(updateResources([containerRequest]));
30 if (containerRequest.outputUuid) {
31 const collection = await services.collectionService.get(containerRequest.outputUuid);
32 dispatch<any>(updateResources([collection]));
35 if (containerRequest.containerUuid) {
36 const container = await services.containerService.get(containerRequest.containerUuid);
37 dispatch<any>(updateResources([container]));
38 return { containerRequest, container };
40 return { containerRequest };
43 export const loadContainers = (filters: string, loadMounts: boolean = true) =>
44 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45 let args: any = { filters };
47 args.select = containerFieldsNoMounts;
49 const { items } = await services.containerService.list(args);
50 dispatch<any>(updateResources(items));
54 // Until the api supports unselecting fields, we need a list of all other fields to omit mounts
55 const containerFieldsNoMounts = [
67 "interactive_session_started",
73 "modified_by_client_uuid",
74 "modified_by_user_uuid",
77 "output_storage_classes",
82 "runtime_auth_scopes",
83 "runtime_constraints",
86 "scheduling_parameters",
92 export const cancelRunningWorkflow = (uuid: string) =>
93 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
95 const process = await services.containerRequestService.update(uuid, { priority: 0 });
98 throw new Error('Could not cancel the process.');
102 export const reRunProcess = (processUuid: string, workflowUuid: string) =>
103 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
104 const process = getResource<any>(processUuid)(getState().resources);
105 const workflows = getState().runProcessPanel.searchWorkflows;
106 const workflow = workflows.find(workflow => workflow.uuid === workflowUuid);
107 if (workflow && process) {
108 const mainWf = getWorkflow(process.mounts[MOUNT_PATH_CWL_WORKFLOW]);
109 if (mainWf) { mainWf.inputs = getInputs(process); }
110 const stringifiedDefinition = JSON.stringify(process.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
111 const newWorkflow = { ...workflow, definition: stringifiedDefinition };
113 const owner = getResource<ProjectResource | UserResource>(workflow.ownerUuid)(getState().resources);
114 const basicInitialData: RunProcessBasicFormData = { name: `Copy of: ${process.name}`, description: process.description, owner };
115 dispatch<any>(initialize(RUN_PROCESS_BASIC_FORM, basicInitialData));
117 const advancedInitialData: RunProcessAdvancedFormData = {
118 output: process.outputName,
119 runtime: process.schedulingParameters.max_run_time,
120 ram: process.runtimeConstraints.ram,
121 vcpus: process.runtimeConstraints.vcpus,
122 keep_cache_ram: process.runtimeConstraints.keep_cache_ram,
123 acr_container_image: process.containerImage
125 dispatch<any>(initialize(RUN_PROCESS_ADVANCED_FORM, advancedInitialData));
127 dispatch<any>(navigateToRunProcess);
128 dispatch<any>(goToStep(1));
129 dispatch(runProcessPanelActions.SET_STEP_CHANGED(true));
130 dispatch(runProcessPanelActions.SET_SELECTED_WORKFLOW(newWorkflow));
132 dispatch<any>(snackbarActions.OPEN_SNACKBAR({ message: `You can't re-run this process`, kind: SnackbarKind.ERROR }));
137 * Fetches raw inputs from containerRequest mounts with fallback to properties
139 export const getRawInputs = (data: any): CommandInputParameter[] | undefined => {
140 if (!data) { return undefined; }
141 const mountInput = data.mounts?.[MOUNT_PATH_CWL_INPUT]?.content;
142 const propsInput = data.properties?.cwl_input;
143 if (!mountInput && !propsInput) { return undefined; }
144 return (mountInput || propsInput);
147 export const getInputs = (data: any): CommandInputParameter[] => {
148 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
149 const content = getRawInputs(data) as any;
150 if (!content) { return []; }
152 const inputs = getWorkflowInputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
153 return inputs ? inputs.map(
159 default: content[it.id],
160 value: content[it.id.split('/').pop()] || [],
168 * Fetches raw outputs from containerRequest properties
170 export const getRawOutputs = (data: any): CommandInputParameter[] | undefined => {
171 if (!data || !data.properties || !data.properties.cwl_output) { return undefined; }
172 return (data.properties.cwl_output);
175 export type InputCollectionMount = {
180 export const getInputCollectionMounts = (data: any): InputCollectionMount[] => {
181 if (!data || !data.mounts) { return []; }
182 return Object.keys(data.mounts)
187 .filter(mount => mount.kind === 'collection' &&
188 mount.portable_data_hash &&
192 pdh: mount.portable_data_hash,
196 export const getOutputParameters = (data: any): CommandOutputParameter[] => {
197 if (!data || !data.mounts || !data.mounts[MOUNT_PATH_CWL_WORKFLOW]) { return []; }
198 const outputs = getWorkflowOutputs(data.mounts[MOUNT_PATH_CWL_WORKFLOW].content);
199 return outputs ? outputs.map(
211 export const openRemoveProcessDialog = (uuid: string) =>
212 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
213 dispatch(dialogActions.OPEN_DIALOG({
214 id: REMOVE_PROCESS_DIALOG,
216 title: 'Remove process permanently',
217 text: 'Are you sure you want to remove this process?',
218 confirmButtonLabel: 'Remove',
224 export const REMOVE_PROCESS_DIALOG = 'removeProcessDialog';
226 export const removeProcessPermanently = (uuid: string) =>
227 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
228 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
229 await services.containerRequestService.delete(uuid);
230 dispatch(projectPanelActions.REQUEST_ITEMS());
231 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));