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