16073: Refactor process io loading into actions and reducers to eliminate infinite...
[arvados-workbench2.git] / src / store / process-panel / process-panel-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { unionize, ofType, UnionOf } from "common/unionize";
6 import { getInputs, getOutputParameters, getRawInputs, getRawOutputs, loadProcess } from 'store/processes/processes-actions';
7 import { Dispatch } from 'redux';
8 import { ProcessStatus } from 'store/processes/process';
9 import { RootState } from 'store/store';
10 import { ServiceRepository } from "services/services";
11 import { navigateTo, navigateToWorkflows } from 'store/navigation/navigation-action';
12 import { snackbarActions } from 'store/snackbar/snackbar-actions';
13 import { SnackbarKind } from '../snackbar/snackbar-actions';
14 import { showWorkflowDetails } from 'store/workflow-panel/workflow-panel-actions';
15 import { loadSubprocessPanel } from "../subprocess-panel/subprocess-panel-actions";
16 import { initProcessLogsPanel, processLogsPanelActions } from "store/process-logs-panel/process-logs-panel-actions";
17 import { CollectionFile } from "models/collection-file";
18 import { ContainerRequestResource } from "models/container-request";
19 import { CommandOutputParameter } from 'cwlts/mappings/v1.0/CommandOutputParameter';
20 import { CommandInputParameter, getIOParamId } from 'models/workflow';
21 import { getIOParamDisplayValue, ProcessIOParameter } from "views/process-panel/process-io-card";
22 import { OutputDetails } from "./process-panel";
23 import { AuthState } from "store/auth/auth-reducer";
24
25 export const processPanelActions = unionize({
26     RESET_PROCESS_PANEL: ofType<{}>(),
27     SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID: ofType<string>(),
28     SET_PROCESS_PANEL_FILTERS: ofType<string[]>(),
29     TOGGLE_PROCESS_PANEL_FILTER: ofType<string>(),
30     SET_INPUT_RAW: ofType<CommandInputParameter[] | null>(),
31     SET_INPUT_PARAMS: ofType<ProcessIOParameter[] | null>(),
32     SET_OUTPUT_RAW: ofType<OutputDetails | null>(),
33     SET_OUTPUT_DEFINITIONS: ofType<CommandOutputParameter[]>(),
34     SET_OUTPUT_PARAMS: ofType<ProcessIOParameter[] | null>(),
35 });
36
37 export type ProcessPanelAction = UnionOf<typeof processPanelActions>;
38
39 export const toggleProcessPanelFilter = processPanelActions.TOGGLE_PROCESS_PANEL_FILTER;
40
41 export const loadProcessPanel = (uuid: string) =>
42     async (dispatch: Dispatch) => {
43         dispatch(processPanelActions.RESET_PROCESS_PANEL());
44         dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
45         dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
46         await dispatch<any>(loadProcess(uuid));
47         dispatch(initProcessPanelFilters);
48         dispatch<any>(initProcessLogsPanel(uuid));
49         dispatch<any>(loadSubprocessPanel());
50     };
51
52 export const navigateToOutput = (uuid: string) =>
53     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
54         try {
55             await services.collectionService.get(uuid);
56             dispatch<any>(navigateTo(uuid));
57         } catch {
58             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
59         }
60     };
61
62 export const loadInputs = (containerRequest: ContainerRequestResource) =>
63     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
64         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_RAW(getRawInputs(containerRequest)));
65         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_PARAMS(formatInputData(getInputs(containerRequest), getState().auth)));
66     };
67
68 export const loadOutputs = (containerRequest: ContainerRequestResource) =>
69     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
70         const noOutputs = {rawOutputs: {}};
71         if (!containerRequest.outputUuid) {
72             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
73             return;
74         };
75         try {
76             const propsOutputs = getRawOutputs(containerRequest);
77             const filesPromise = services.collectionService.files(containerRequest.outputUuid);
78             const collectionPromise = services.collectionService.get(containerRequest.outputUuid);
79             const [files, collection] = await Promise.all([filesPromise, collectionPromise]);
80
81             // If has propsOutput, skip fetching cwl.output.json
82             if (propsOutputs !== undefined) {
83                 dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
84                     rawOutputs: propsOutputs,
85                     pdh: collection.portableDataHash
86                 }));
87             } else {
88                 // Fetch outputs from keep
89                 const outputFile = files.find((file) => file.name === 'cwl.output.json') as CollectionFile | undefined;
90                 let outputData = outputFile ? await services.collectionService.getFileContents(outputFile) : undefined;
91                 if (outputData && (outputData = JSON.parse(outputData)) && collection.portableDataHash) {
92                     dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
93                         rawOutputs: outputData,
94                         pdh: collection.portableDataHash,
95                     }));
96                 } else {
97                     dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
98                 }
99             }
100         } catch {
101             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
102         }
103     };
104
105 export const loadOutputDefinitions = (containerRequest: ContainerRequestResource) =>
106     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
107         if (containerRequest && containerRequest.mounts) {
108             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_DEFINITIONS(getOutputParameters(containerRequest)));
109         }
110     };
111
112 export const updateOutputParams = () =>
113     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
114         const outputDefinitions = getState().processPanel.outputDefinitions;
115         const outputRaw = getState().processPanel.outputRaw;
116
117         if (outputRaw !== null && outputRaw.rawOutputs) {
118             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth)));
119         }
120     };
121
122 export const openWorkflow = (uuid: string) =>
123     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124         dispatch<any>(navigateToWorkflows);
125         dispatch<any>(showWorkflowDetails(uuid));
126     };
127
128 export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FILTERS([
129     ProcessStatus.QUEUED,
130     ProcessStatus.COMPLETED,
131     ProcessStatus.FAILED,
132     ProcessStatus.RUNNING,
133     ProcessStatus.ONHOLD,
134     ProcessStatus.FAILING,
135     ProcessStatus.WARNING,
136     ProcessStatus.CANCELLED
137 ]);
138
139 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
140     return inputs.map(input => {
141         return {
142             id: getIOParamId(input),
143             label: input.label || "",
144             value: getIOParamDisplayValue(auth, input)
145         };
146     });
147 };
148
149 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
150     return definitions.map(output => {
151         return {
152             id: getIOParamId(output),
153             label: output.label || "",
154             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
155         };
156     });
157 };