c2267ec02a28ed5ed6e15020147ff2c25db86fcd
[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     // Fetch outputDefinitons from mounts whenever containerRequest is updated
103     React.useEffect(() => {
104         if (containerRequest && containerRequest.mounts) {
105             const newOutputDefinitions = getOutputParameters(containerRequest);
106             // Avoid setting output definitions to [] when mounts briefly go missing
107             if (newOutputDefinitions.length) {
108                 setOutputDefinitions(newOutputDefinitions);
109             }
110         }
111     }, [containerRequest]);
112
113     // Format raw output into ProcessIOParameter[] when it changes
114     React.useEffect(() => {
115         if (outputDetails !== undefined && outputDetails.rawOutputs) {
116             // Update processed outputs as long as outputDetails is loaded (or failed to load with {} rawOutputs)
117             setProcessedOutputs(formatOutputData(outputDefinitions, outputDetails.rawOutputs, outputDetails.pdh, auth));
118         }
119     }, [outputDetails, auth, outputDefinitions]);
120
121     // Fetch raw inputs and format into ProcessIOParameter[]
122     //   Can be sync because inputs are either already in containerRequest mounts or props
123     React.useEffect(() => {
124         if (containerRequest) {
125             // Since mounts can disappear and reappear, only set inputs if raw / processed inputs is undefined or new inputs has content
126             const newRawInputs = getRawInputs(containerRequest);
127             if (rawInputs === undefined || (newRawInputs && newRawInputs.length)) {
128                 setInputs(newRawInputs);
129             }
130             const newInputs = getInputs(containerRequest);
131             if (processedInputs === undefined || (newInputs && newInputs.length)) {
132                 setProcessedInputs(formatInputData(newInputs, auth));
133             }
134         }
135     }, [requestUuid, auth, containerRequest, processedInputs, rawInputs]);
136
137     return process
138         ? <MPVContainer className={props.classes.root} spacing={8} panelStates={panelsData}  justify-content="flex-start" direction="column" wrap="nowrap">
139             <MPVPanelContent forwardProps xs="auto" data-cy="process-details">
140                 <ProcessDetailsCard
141                     process={process}
142                     onContextMenu={event => props.onContextMenu(event, process)}
143                     cancelProcess={props.cancelProcess}
144                 />
145             </MPVPanelContent>
146             <MPVPanelContent forwardProps xs="auto" data-cy="process-cmd">
147                 <ProcessCmdCard
148                     onCopy={props.onCopyToClipboard}
149                     process={process} />
150             </MPVPanelContent>
151             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-logs">
152                 <ProcessLogsCard
153                     onCopy={props.onCopyToClipboard}
154                     process={process}
155                     lines={getProcessPanelLogs(processLogsPanel)}
156                     selectedFilter={{
157                         label: processLogsPanel.selectedFilter,
158                         value: processLogsPanel.selectedFilter
159                     }}
160                     filters={processLogsPanel.filters.map(
161                         filter => ({ label: filter, value: filter })
162                     )}
163                     onLogFilterChange={props.onLogFilterChange}
164                     navigateToLog={props.navigateToLog}
165                 />
166             </MPVPanelContent>
167             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-inputs">
168                 <ProcessIOCard
169                     label={ProcessIOCardType.INPUT}
170                     process={process}
171                     params={processedInputs}
172                     raw={rawInputs}
173                     mounts={inputMounts}
174                  />
175             </MPVPanelContent>
176             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-outputs">
177                 <ProcessIOCard
178                     label={ProcessIOCardType.OUTPUT}
179                     process={process}
180                     params={processedOutputs}
181                     raw={outputDetails?.rawOutputs}
182                     outputUuid={outputUuid || ""}
183                  />
184             </MPVPanelContent>
185             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-children">
186                 <SubprocessPanel />
187             </MPVPanelContent>
188         </MPVContainer>
189         : <Grid container
190             alignItems='center'
191             justify='center'
192             style={{ minHeight: '100%' }}>
193             <DefaultView
194                 icon={ProcessIcon}
195                 messages={['Process not found']} />
196         </Grid>;
197     }
198 );
199
200 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
201     return inputs.map(input => {
202         return {
203             id: getIOParamId(input),
204             label: input.label || "",
205             value: getIOParamDisplayValue(auth, input)
206         };
207     });
208 };
209
210 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
211     return definitions.map(output => {
212         return {
213             id: getIOParamId(output),
214             label: output.label || "",
215             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
216         };
217     });
218 };