c8c0bcc77d34c4d5a4ebcf32f6df7af1bf4e74a7
[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 { 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
19 export const processPanelActions = unionize({
20     SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID: ofType<string>(),
21     SET_PROCESS_PANEL_FILTERS: ofType<string[]>(),
22     TOGGLE_PROCESS_PANEL_FILTER: ofType<string>(),
23 });
24
25 export type ProcessPanelAction = UnionOf<typeof processPanelActions>;
26
27 export const toggleProcessPanelFilter = processPanelActions.TOGGLE_PROCESS_PANEL_FILTER;
28
29 export const loadProcessPanel = (uuid: string) =>
30     async (dispatch: Dispatch) => {
31         dispatch(processLogsPanelActions.RESET_PROCESS_LOGS_PANEL());
32         dispatch<ProcessPanelAction>(processPanelActions.SET_PROCESS_PANEL_CONTAINER_REQUEST_UUID(uuid));
33         await dispatch<any>(loadProcess(uuid));
34         dispatch(initProcessPanelFilters);
35         dispatch<any>(initProcessLogsPanel(uuid));
36         dispatch<any>(loadSubprocessPanel());
37     };
38
39 export const navigateToOutput = (uuid: string) =>
40     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
41         try {
42             await services.collectionService.get(uuid);
43             dispatch<any>(navigateTo(uuid));
44         } catch {
45             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This collection does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
46         }
47     };
48
49 export const loadOutputs = (uuid: string, setOutputs) =>
50     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
51         try {
52             const filesPromise = services.collectionService.files(uuid);
53             const collectionPromise = services.collectionService.get(uuid);
54             const [files, collection] = await Promise.all([filesPromise, collectionPromise]);
55
56             const outputFile = files.find((file) => file.name === 'cwl.output.json') as CollectionFile | undefined;
57             let outputData = outputFile ? await services.collectionService.getFileContents(outputFile) : undefined;
58             if ((outputData = JSON.parse(outputData)) && collection.portableDataHash) {
59                 setOutputs({
60                     rawOutputs: outputData,
61                     pdh: collection.portableDataHash,
62                 });
63             } else {
64                 setOutputs({});
65             }
66         } catch {
67             setOutputs({});
68         }
69     };
70
71 export const openWorkflow = (uuid: string) =>
72     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
73         dispatch<any>(navigateToWorkflows);
74         dispatch<any>(showWorkflowDetails(uuid));
75     };
76
77 export const initProcessPanelFilters = processPanelActions.SET_PROCESS_PANEL_FILTERS([
78     ProcessStatus.QUEUED,
79     ProcessStatus.COMPLETED,
80     ProcessStatus.FAILED,
81     ProcessStatus.RUNNING,
82     ProcessStatus.ONHOLD,
83     ProcessStatus.FAILING,
84     ProcessStatus.WARNING,
85     ProcessStatus.CANCELLED
86 ]);