16073: Fix compiler warnings
[arvados-workbench2.git] / src / views / process-panel / process-panel-root.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React, { useState } from 'react';
6 import { Grid, StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core';
7 import { DefaultView } from 'components/default-view/default-view';
8 import { ProcessIcon } from 'components/icon/icon';
9 import { Process } from 'store/processes/process';
10 import { SubprocessPanel } from 'views/subprocess-panel/subprocess-panel';
11 import { SubprocessFilterDataProps } from 'components/subprocess-filter/subprocess-filter';
12 import { MPVContainer, MPVPanelContent, MPVPanelState } from 'components/multi-panel-view/multi-panel-view';
13 import { ArvadosTheme } from 'common/custom-theme';
14 import { ProcessDetailsCard } from './process-details-card';
15 import { getIOParamDisplayValue, ProcessIOCard, ProcessIOCardType, ProcessIOParameter } from './process-io-card';
16
17 import { getProcessPanelLogs, ProcessLogsPanel } from 'store/process-logs-panel/process-logs-panel';
18 import { ProcessLogsCard } from './process-log-card';
19 import { FilterOption } from 'views/process-panel/process-log-form';
20 import { getInputs, getInputCollectionMounts, getOutputParameters, getRawInputs } from 'store/processes/processes-actions';
21 import { CommandInputParameter, getIOParamId } from 'models/workflow';
22 import { CommandOutputParameter } from 'cwlts/mappings/v1.0/CommandOutputParameter';
23 import { AuthState } from 'store/auth/auth-reducer';
24 import { ProcessCmdCard } from './process-cmd-card';
25 import { ContainerRequestResource } from 'models/container-request';
26
27 type CssRules = 'root';
28
29 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30     root: {
31         width: '100%',
32     },
33 });
34
35 export interface ProcessPanelRootDataProps {
36     process?: Process;
37     subprocesses: Array<Process>;
38     filters: Array<SubprocessFilterDataProps>;
39     processLogsPanel: ProcessLogsPanel;
40     auth: AuthState;
41 }
42
43 export interface ProcessPanelRootActionProps {
44     onContextMenu: (event: React.MouseEvent<HTMLElement>, process: Process) => void;
45     onToggle: (status: string) => void;
46     cancelProcess: (uuid: string) => void;
47     onLogFilterChange: (filter: FilterOption) => void;
48     navigateToLog: (uuid: string) => void;
49     onCopyToClipboard: (uuid: string) => void;
50     fetchOutputs: (containerRequest: ContainerRequestResource, fetchOutputs) => void;
51 }
52
53 export type ProcessPanelRootProps = ProcessPanelRootDataProps & ProcessPanelRootActionProps & WithStyles<CssRules>;
54
55 type OutputDetails = {
56     rawOutputs?: any;
57     pdh?: string;
58 }
59
60 const panelsData: MPVPanelState[] = [
61     {name: "Details"},
62     {name: "Command"},
63     {name: "Logs", visible: true},
64     {name: "Inputs"},
65     {name: "Outputs"},
66     {name: "Subprocesses"},
67 ];
68
69 export const ProcessPanelRoot = withStyles(styles)(
70     ({ process, auth, processLogsPanel, fetchOutputs, ...props }: ProcessPanelRootProps) => {
71
72     const [outputDetails, setOutputs] = useState<OutputDetails | undefined>(undefined);
73     const [outputDefinitions, setOutputDefinitions] = useState<CommandOutputParameter[]>([]);
74     const [rawInputs, setInputs] = useState<CommandInputParameter[] | undefined>(undefined);
75
76     const [processedOutputs, setProcessedOutputs] = useState<ProcessIOParameter[] | undefined>(undefined);
77     const [processedInputs, setProcessedInputs] = useState<ProcessIOParameter[] | undefined>(undefined);
78
79     const outputUuid = process?.containerRequest.outputUuid;
80     const requestUuid = process?.containerRequest.uuid;
81
82     const containerRequest = process?.containerRequest;
83
84     const inputMounts = getInputCollectionMounts(process?.containerRequest);
85
86     // Resets state when changing processes
87     React.useEffect(() => {
88         setOutputs(undefined);
89         setOutputDefinitions([]);
90         setInputs(undefined);
91         setProcessedOutputs(undefined);
92         setProcessedInputs(undefined);
93     }, [requestUuid]);
94
95     // Fetch raw output (async for fetching from keep)
96     React.useEffect(() => {
97         if (containerRequest) {
98             fetchOutputs(containerRequest, setOutputs);
99         }
100     }, [containerRequest, fetchOutputs]);
101
102     // Format raw output into ProcessIOParameter[] when it changes
103     React.useEffect(() => {
104         if (outputDetails !== undefined && outputDetails.rawOutputs && containerRequest) {
105             const newOutputDefinitions = getOutputParameters(containerRequest);
106             // Avoid setting output definitions back to [] when mounts briefly go missing
107             if (newOutputDefinitions.length) {
108                 setOutputDefinitions(newOutputDefinitions);
109             }
110             setProcessedOutputs(formatOutputData(outputDefinitions, outputDetails.rawOutputs, outputDetails.pdh, auth));
111         }
112     }, [outputDetails, auth, containerRequest, outputDefinitions]);
113
114     // Fetch raw inputs and format into ProcessIOParameter[]
115     //   Can be sync because inputs are either already in containerRequest mounts or props
116     React.useEffect(() => {
117         if (containerRequest) {
118             // Since mounts can disappear and reappear, only set inputs if raw / processed inputs is undefined or new inputs has content
119             const newRawInputs = getRawInputs(containerRequest);
120             if (rawInputs === undefined || (newRawInputs && newRawInputs.length)) {
121                 setInputs(newRawInputs);
122             }
123             const newInputs = getInputs(containerRequest);
124             if (processedInputs === undefined || (newInputs && newInputs.length)) {
125                 setProcessedInputs(formatInputData(newInputs, auth));
126             }
127         }
128     }, [requestUuid, auth, containerRequest, processedInputs, rawInputs]);
129
130     return process
131         ? <MPVContainer className={props.classes.root} spacing={8} panelStates={panelsData}  justify-content="flex-start" direction="column" wrap="nowrap">
132             <MPVPanelContent forwardProps xs="auto" data-cy="process-details">
133                 <ProcessDetailsCard
134                     process={process}
135                     onContextMenu={event => props.onContextMenu(event, process)}
136                     cancelProcess={props.cancelProcess}
137                 />
138             </MPVPanelContent>
139             <MPVPanelContent forwardProps xs="auto" data-cy="process-cmd">
140                 <ProcessCmdCard
141                     onCopy={props.onCopyToClipboard}
142                     process={process} />
143             </MPVPanelContent>
144             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-logs">
145                 <ProcessLogsCard
146                     onCopy={props.onCopyToClipboard}
147                     process={process}
148                     lines={getProcessPanelLogs(processLogsPanel)}
149                     selectedFilter={{
150                         label: processLogsPanel.selectedFilter,
151                         value: processLogsPanel.selectedFilter
152                     }}
153                     filters={processLogsPanel.filters.map(
154                         filter => ({ label: filter, value: filter })
155                     )}
156                     onLogFilterChange={props.onLogFilterChange}
157                     navigateToLog={props.navigateToLog}
158                 />
159             </MPVPanelContent>
160             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-inputs">
161                 <ProcessIOCard
162                     label={ProcessIOCardType.INPUT}
163                     process={process}
164                     params={processedInputs}
165                     raw={rawInputs}
166                     mounts={inputMounts}
167                  />
168             </MPVPanelContent>
169             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-outputs">
170                 <ProcessIOCard
171                     label={ProcessIOCardType.OUTPUT}
172                     process={process}
173                     params={processedOutputs}
174                     raw={outputDetails?.rawOutputs}
175                     outputUuid={outputUuid || ""}
176                  />
177             </MPVPanelContent>
178             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-children">
179                 <SubprocessPanel />
180             </MPVPanelContent>
181         </MPVContainer>
182         : <Grid container
183             alignItems='center'
184             justify='center'
185             style={{ minHeight: '100%' }}>
186             <DefaultView
187                 icon={ProcessIcon}
188                 messages={['Process not found']} />
189         </Grid>;
190     }
191 );
192
193 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
194     return inputs.map(input => {
195         return {
196             id: getIOParamId(input),
197             label: input.label || "",
198             value: getIOParamDisplayValue(auth, input)
199         };
200     });
201 };
202
203 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
204     return definitions.map(output => {
205         return {
206             id: getIOParamId(output),
207             label: output.label || "",
208             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
209         };
210     });
211 };