Merge branch 'main' into 21067-process-panel-error
[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 } from "store/navigation/navigation-action";
12 import { snackbarActions } from "store/snackbar/snackbar-actions";
13 import { SnackbarKind } from "../snackbar/snackbar-actions";
14 import { loadSubprocessPanel, subprocessPanelActions } from "../subprocess-panel/subprocess-panel-actions";
15 import { initProcessLogsPanel, processLogsPanelActions } from "store/process-logs-panel/process-logs-panel-actions";
16 import { CollectionFile } from "models/collection-file";
17 import { ContainerRequestResource } from "models/container-request";
18 import { CommandOutputParameter } from "cwlts/mappings/v1.0/CommandOutputParameter";
19 import { CommandInputParameter, getIOParamId, WorkflowInputsData } from "models/workflow";
20 import { getIOParamDisplayValue, ProcessIOParameter } from "views/process-panel/process-io-card";
21 import { OutputDetails, NodeInstanceType, NodeInfo } from "./process-panel";
22 import { AuthState } from "store/auth/auth-reducer";
23
24 export const processPanelActions = unionize({
25     RESET_PROCESS_PANEL: ofType<{}>(),
26     SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID: ofType<string>(),
27     SET_PROCESS_PANEL_FILTERS: ofType<string[]>(),
28     TOGGLE_PROCESS_PANEL_FILTER: ofType<string>(),
29     SET_INPUT_RAW: ofType<WorkflowInputsData | null>(),
30     SET_INPUT_PARAMS: ofType<ProcessIOParameter[] | null>(),
31     SET_OUTPUT_RAW: ofType<OutputDetails | null>(),
32     SET_OUTPUT_DEFINITIONS: ofType<CommandOutputParameter[]>(),
33     SET_OUTPUT_PARAMS: ofType<ProcessIOParameter[] | null>(),
34     SET_NODE_INFO: ofType<NodeInfo>(),
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) => async (dispatch: Dispatch, getState: () => RootState) => {
42     // Reset subprocess data explorer if navigating to new process
43     //  Avoids resetting pagination when refreshing same process
44     if (getState().processPanel.containerRequestUuid !== uuid) {
45         dispatch(subprocessPanelActions.CLEAR());
46     }
47     dispatch(processPanelActions.RESET_PROCESS_PANEL());
48     dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
49     dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
50     await dispatch<any>(loadProcess(uuid));
51     dispatch(initProcessPanelFilters);
52     dispatch<any>(initProcessLogsPanel(uuid));
53     dispatch<any>(loadSubprocessPanel());
54 };
55
56 export const navigateToOutput = (uuid: string) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
57     try {
58         await services.collectionService.get(uuid);
59         dispatch<any>(navigateTo(uuid));
60     } catch {
61         dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Output collection was trashed or deleted.", hideDuration: 4000, kind: SnackbarKind.WARNING }));
62     }
63 };
64
65 export const loadInputs =
66     (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
67         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_RAW(getRawInputs(containerRequest)));
68         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_PARAMS(formatInputData(getInputs(containerRequest), getState().auth)));
69     };
70
71 export const loadOutputs =
72     (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
73         const noOutputs = { rawOutputs: {} };
74
75         if (!containerRequest.outputUuid) {
76             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({ uuid: containerRequest.uuid, outputRaw: noOutputs }));
77             return;
78         }
79         try {
80             const propsOutputs = getRawOutputs(containerRequest);
81             const filesPromise = services.collectionService.files(containerRequest.outputUuid);
82             const collectionPromise = services.collectionService.get(containerRequest.outputUuid);
83             const [files, collection] = await Promise.all([filesPromise, collectionPromise]);
84
85             // If has propsOutput, skip fetching cwl.output.json
86             if (propsOutputs !== undefined) {
87                 dispatch<ProcessPanelAction>(
88                     processPanelActions.SET_OUTPUT_RAW({
89                         rawOutputs: propsOutputs,
90                         pdh: collection.portableDataHash,
91                     })
92                 );
93             } else {
94                 // Fetch outputs from keep
95                 const outputFile = files.find(file => file.name === "cwl.output.json") as CollectionFile | undefined;
96                 let outputData = outputFile ? await services.collectionService.getFileContents(outputFile) : undefined;
97                 if (outputData && (outputData = JSON.parse(outputData)) && collection.portableDataHash) {
98                     dispatch<ProcessPanelAction>(
99                         processPanelActions.SET_OUTPUT_RAW({
100                             uuid: containerRequest.uuid,
101                             outputRaw: { rawOutputs: outputData, pdh: collection.portableDataHash },
102                         })
103                     );
104                 } else {
105                     dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({ uuid: containerRequest.uuid, outputRaw: noOutputs }));
106                 }
107             }
108         } catch {
109             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({ uuid: containerRequest.uuid, outputRaw: noOutputs }));
110         }
111     };
112
113 export const loadNodeJson =
114     (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
115         const noLog = { nodeInfo: null };
116         if (!containerRequest.logUuid) {
117             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
118             return;
119         }
120         try {
121             const filesPromise = services.collectionService.files(containerRequest.logUuid);
122             const collectionPromise = services.collectionService.get(containerRequest.logUuid);
123             const [files] = await Promise.all([filesPromise, collectionPromise]);
124
125             // Fetch node.json from keep
126             const nodeFile = files.find(file => file.name === "node.json") as CollectionFile | undefined;
127             let nodeData = nodeFile ? await services.collectionService.getFileContents(nodeFile) : undefined;
128             if (nodeData && (nodeData = JSON.parse(nodeData))) {
129                 dispatch<ProcessPanelAction>(
130                     processPanelActions.SET_NODE_INFO({
131                         nodeInfo: nodeData as NodeInstanceType,
132                     })
133                 );
134             } else {
135                 dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
136             }
137         } catch {
138             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
139         }
140     };
141
142 export const loadOutputDefinitions =
143     (containerRequest: ContainerRequestResource) => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
144         if (containerRequest && containerRequest.mounts) {
145             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_DEFINITIONS(getOutputParameters(containerRequest)));
146         }
147     };
148
149 export const updateOutputParams = () => async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
150     const outputDefinitions = getState().processPanel.outputDefinitions;
151     const outputRaw = getState().processPanel.outputRaw;
152
153     if (outputRaw && outputRaw.rawOutputs) {
154         dispatch<ProcessPanelAction>(
155             processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth))
156         );
157     }
158 };
159
160 export const openWorkflow = (uuid: string) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
161     dispatch<any>(navigateTo(uuid));
162 };
163
164 export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FILTERS([
165     ProcessStatus.QUEUED,
166     ProcessStatus.COMPLETED,
167     ProcessStatus.FAILED,
168     ProcessStatus.RUNNING,
169     ProcessStatus.ONHOLD,
170     ProcessStatus.FAILING,
171     ProcessStatus.WARNING,
172     ProcessStatus.CANCELLED,
173 ]);
174
175 export const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
176     return inputs.map(input => {
177         return {
178             id: getIOParamId(input),
179             label: input.label || "",
180             value: getIOParamDisplayValue(auth, input),
181         };
182     });
183 };
184
185 export const formatOutputData = (
186     definitions: CommandOutputParameter[],
187     values: any,
188     pdh: string | undefined,
189     auth: AuthState
190 ): ProcessIOParameter[] => {
191     return definitions.map(output => {
192         return {
193             id: getIOParamId(output),
194             label: output.label || "",
195             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh),
196         };
197     });
198 };