Merge branch '20990-name-btree' refs #20990
[arvados.git] / services / workbench2 / 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, subprocessPanelActions } 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, getState: () => RootState) => {
44         // Reset subprocess data explorer if navigating to new process
45         //  Avoids resetting pagination when refreshing same process
46         if (getState().processPanel.containerRequestUuid !== uuid) {
47             dispatch(subprocessPanelActions.CLEAR());
48         }
49         dispatch(processPanelActions.RESET_PROCESS_PANEL());
50         dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
51         dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
52         await dispatch<any>(loadProcess(uuid));
53         dispatch(initProcessPanelFilters);
54         dispatch<any>(initProcessLogsPanel(uuid));
55         dispatch<any>(loadSubprocessPanel());
56     };
57
58 export const navigateToOutput = (uuid: string) =>
59     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
60         try {
61             await services.collectionService.get(uuid);
62             dispatch<any>(navigateTo(uuid));
63         } catch {
64             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
65         }
66     };
67
68 export const loadInputs = (containerRequest: ContainerRequestResource) =>
69     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
70         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_RAW(getRawInputs(containerRequest)));
71         dispatch<ProcessPanelAction>(processPanelActions.SET_INPUT_PARAMS(formatInputData(getInputs(containerRequest), getState().auth)));
72     };
73
74 export const loadOutputs = (containerRequest: ContainerRequestResource) =>
75     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
76         const noOutputs = { rawOutputs: {} };
77         if (!containerRequest.outputUuid) {
78             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
79             return;
80         };
81         try {
82             const propsOutputs = getRawOutputs(containerRequest);
83             const filesPromise = services.collectionService.files(containerRequest.outputUuid);
84             const collectionPromise = services.collectionService.get(containerRequest.outputUuid);
85             const [files, collection] = await Promise.all([filesPromise, collectionPromise]);
86
87             // If has propsOutput, skip fetching cwl.output.json
88             if (propsOutputs !== undefined) {
89                 dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW({
90                     rawOutputs: propsOutputs,
91                     pdh: collection.portableDataHash
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>(processPanelActions.SET_OUTPUT_RAW({
99                         rawOutputs: outputData,
100                         pdh: collection.portableDataHash,
101                     }));
102                 } else {
103                     dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
104                 }
105             }
106         } catch {
107             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_RAW(noOutputs));
108         }
109     };
110
111
112 export const loadNodeJson = (containerRequest: ContainerRequestResource) =>
113     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
114         const noLog = { nodeInfo: null };
115         if (!containerRequest.logUuid) {
116             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
117             return;
118         };
119         try {
120             const filesPromise = services.collectionService.files(containerRequest.logUuid);
121             const collectionPromise = services.collectionService.get(containerRequest.logUuid);
122             const [files] = await Promise.all([filesPromise, collectionPromise]);
123
124             // Fetch node.json from keep
125             const nodeFile = files.find((file) => file.name === 'node.json') as CollectionFile | undefined;
126             let nodeData = nodeFile ? await services.collectionService.getFileContents(nodeFile) : undefined;
127             if (nodeData && (nodeData = JSON.parse(nodeData))) {
128                 dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO({
129                     nodeInfo: nodeData as NodeInstanceType
130                 }));
131             } else {
132                 dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
133             }
134         } catch {
135             dispatch<ProcessPanelAction>(processPanelActions.SET_NODE_INFO(noLog));
136         }
137     };
138
139 export const loadOutputDefinitions = (containerRequest: ContainerRequestResource) =>
140     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
141         if (containerRequest && containerRequest.mounts) {
142             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_DEFINITIONS(getOutputParameters(containerRequest)));
143         }
144     };
145
146 export const updateOutputParams = () =>
147     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
148         const outputDefinitions = getState().processPanel.outputDefinitions;
149         const outputRaw = getState().processPanel.outputRaw;
150
151         if (outputRaw !== null && outputRaw.rawOutputs) {
152             dispatch<ProcessPanelAction>(processPanelActions.SET_OUTPUT_PARAMS(formatOutputData(outputDefinitions, outputRaw.rawOutputs, outputRaw.pdh, getState().auth)));
153         }
154     };
155
156 export const openWorkflow = (uuid: string) =>
157     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
158         dispatch<any>(navigateToWorkflows);
159         dispatch<any>(showWorkflowDetails(uuid));
160     };
161
162 export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FILTERS([
163     ProcessStatus.QUEUED,
164     ProcessStatus.COMPLETED,
165     ProcessStatus.FAILED,
166     ProcessStatus.RUNNING,
167     ProcessStatus.ONHOLD,
168     ProcessStatus.FAILING,
169     ProcessStatus.WARNING,
170     ProcessStatus.CANCELLED
171 ]);
172
173 export const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
174     return inputs.map(input => {
175         return {
176             id: getIOParamId(input),
177             label: input.label || "",
178             value: getIOParamDisplayValue(auth, input)
179         };
180     });
181 };
182
183 export const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
184     return definitions.map(output => {
185         return {
186             id: getIOParamId(output),
187             label: output.label || "",
188             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
189         };
190     });
191 };