20251: Fix flaky collection file browser by using race-free state update callback
[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, WorkflowInputsData } from 'models/workflow';
21 import { getIOParamDisplayValue, ProcessIOParameter } from "views/process-panel/process-io-card";
22 import { OutputDetails, NodeInstanceType, NodeInfo } 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<WorkflowInputsData | 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     SET_NODE_INFO: ofType<NodeInfo>(),
36 });
37
38 export type ProcessPanelAction = UnionOf<typeof processPanelActions>;
39
40 export const toggleProcessPanelFilter = processPanelActions.TOGGLE_PROCESS_PANEL_FILTER;
41
42 export const loadProcessPanel = (uuid: string) =>
43     async (dispatch: Dispatch) => {
44         dispatch(processPanelActions.RESET_PROCESS_PANEL());
45         dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
46         dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
47         await dispatch<any>(loadProcess(uuid));
48         dispatch(initProcessPanelFilters);
49         dispatch<any>(initProcessLogsPanel(uuid));
50         dispatch<any>(loadSubprocessPanel());
51     };
52
53 export const navigateToOutput = (uuid: string) =>
54     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
55         try {
56             await services.collectionService.get(uuid);
57             dispatch<any>(navigateTo(uuid));
58         } catch {
59             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
60         }
61     };
62
63 export const loadInputs = (containerRequest: ContainerRequestResource) =>
64     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
65         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_RAW(getRawInputs(containerRequest)));
66         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_PARAMS(formatInputData(getInputs(containerRequest), getState().auth)));
67     };
68
69 export const loadOutputs = (containerRequest: ContainerRequestResource) =>
70     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
71         const noOutputs = { rawOutputs: {} };
72         if (!containerRequest.outputUuid) {
73             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
74             return;
75         };
76         try {
77             const propsOutputs = getRawOutputs(containerRequest);
78             const filesPromise = services.collectionService.files(containerRequest.outputUuid);
79             const collectionPromise = services.collectionService.get(containerRequest.outputUuid);
80             const [files, collection] = await Promise.all([filesPromise, collectionPromise]);
81
82             // If has propsOutput, skip fetching cwl.output.json
83             if (propsOutputs !== undefined) {
84                 dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
85                     rawOutputs: propsOutputs,
86                     pdh: collection.portableDataHash
87                 }));
88             } else {
89                 // Fetch outputs from keep
90                 const outputFile = files.find((file) => file.name === 'cwl.output.json') as CollectionFile | undefined;
91                 let outputData = outputFile ? await services.collectionService.getFileContents(outputFile) : undefined;
92                 if (outputData && (outputData = JSON.parse(outputData)) && collection.portableDataHash) {
93                     dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
94                         rawOutputs: outputData,
95                         pdh: collection.portableDataHash,
96                     }));
97                 } else {
98                     dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
99                 }
100             }
101         } catch {
102             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
103         }
104     };
105
106
107 export const loadNodeJson = (containerRequest: ContainerRequestResource) =>
108     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
109         const noLog = { nodeInfo: null };
110         if (!containerRequest.logUuid) {
111             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
112             return;
113         };
114         try {
115             const filesPromise = services.collectionService.files(containerRequest.logUuid);
116             const collectionPromise = services.collectionService.get(containerRequest.logUuid);
117             const [files] = await Promise.all([filesPromise, collectionPromise]);
118
119             // Fetch node.json from keep
120             const nodeFile = files.find((file) => file.name === 'node.json') as CollectionFile | undefined;
121             let nodeData = nodeFile ? await services.collectionService.getFileContents(nodeFile) : undefined;
122             if (nodeData && (nodeData = JSON.parse(nodeData))) {
123                 dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO({
124                     nodeInfo: nodeData as NodeInstanceType
125                 }));
126             } else {
127                 dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
128             }
129         } catch {
130             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
131         }
132     };
133
134 export const loadOutputDefinitions = (containerRequest: ContainerRequestResource) =>
135     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
136         if (containerRequest && containerRequest.mounts) {
137             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_DEFINITIONS(getOutputParameters(containerRequest)));
138         }
139     };
140
141 export const updateOutputParams = () =>
142     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
143         const outputDefinitions = getState().processPanel.outputDefinitions;
144         const outputRaw = getState().processPanel.outputRaw;
145
146         if (outputRaw !== null && outputRaw.rawOutputs) {
147             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth)));
148         }
149     };
150
151 export const openWorkflow = (uuid: string) =>
152     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
153         dispatch<any>(navigateToWorkflows);
154         dispatch<any>(showWorkflowDetails(uuid));
155     };
156
157 export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FILTERS([
158     ProcessStatus.QUEUED,
159     ProcessStatus.COMPLETED,
160     ProcessStatus.FAILED,
161     ProcessStatus.RUNNING,
162     ProcessStatus.ONHOLD,
163     ProcessStatus.FAILING,
164     ProcessStatus.WARNING,
165     ProcessStatus.CANCELLED
166 ]);
167
168 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
169     return inputs.map(input => {
170         return {
171             id: getIOParamId(input),
172             label: input.label || "",
173             value: getIOParamDisplayValue(auth, input)
174         };
175     });
176 };
177
178 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
179     return definitions.map(output => {
180         return {
181             id: getIOParamId(output),
182             label: output.label || "",
183             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
184         };
185     });
186 };