16073: Display process io params from props, hide preview when lacking workflow mount
[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 [rawInputs, setInputs] = useState<CommandInputParameter[] | undefined>(undefined);
74
75     const [processedOutputs, setProcessedOutputs] = useState<ProcessIOParameter[] | undefined>(undefined);
76     const [processedInputs, setProcessedInputs] = useState<ProcessIOParameter[] | undefined>(undefined);
77
78     const outputUuid = process?.containerRequest.outputUuid;
79     const requestUuid = process?.containerRequest.uuid;
80
81     const containerRequest = process?.containerRequest;
82
83     const inputMounts = getInputCollectionMounts(process?.containerRequest);
84
85     // Resets state when changing processes
86     React.useEffect(() => {
87         setOutputs(undefined);
88         setInputs(undefined);
89         setProcessedOutputs(undefined);
90         setProcessedInputs(undefined);
91     }, [requestUuid]);
92
93     // Fetch raw output (async for fetching from keep)
94     React.useEffect(() => {
95         if (containerRequest) {
96             fetchOutputs(containerRequest, setOutputs);
97         }
98     }, [containerRequest, fetchOutputs]);
99
100     // Format raw output into ProcessIOParameter[] when it changes
101     React.useEffect(() => {
102         if (outputDetails !== undefined && outputDetails.rawOutputs && containerRequest) {
103             const outputDefinitions = getOutputParameters(containerRequest);
104             setProcessedOutputs(formatOutputData(outputDefinitions, outputDetails.rawOutputs, outputDetails.pdh, auth));
105         }
106     }, [outputDetails, auth, containerRequest]);
107
108     // Fetch raw inputs and format into ProcessIOParameter[]
109     //   Can be sync because inputs are either already in containerRequest mounts or props
110     React.useEffect(() => {
111         if (containerRequest) {
112             const rawInputs = getRawInputs(containerRequest);
113             setInputs(rawInputs);
114
115             const inputs = getInputs(containerRequest);
116             setProcessedInputs(formatInputData(inputs, auth));
117         }
118     }, [requestUuid, auth, containerRequest]);
119
120     return process
121         ? <MPVContainer className={props.classes.root} spacing={8} panelStates={panelsData}  justify-content="flex-start" direction="column" wrap="nowrap">
122             <MPVPanelContent forwardProps xs="auto" data-cy="process-details">
123                 <ProcessDetailsCard
124                     process={process}
125                     onContextMenu={event => props.onContextMenu(event, process)}
126                     cancelProcess={props.cancelProcess}
127                 />
128             </MPVPanelContent>
129             <MPVPanelContent forwardProps xs="auto" data-cy="process-cmd">
130                 <ProcessCmdCard
131                     onCopy={props.onCopyToClipboard}
132                     process={process} />
133             </MPVPanelContent>
134             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-logs">
135                 <ProcessLogsCard
136                     onCopy={props.onCopyToClipboard}
137                     process={process}
138                     lines={getProcessPanelLogs(processLogsPanel)}
139                     selectedFilter={{
140                         label: processLogsPanel.selectedFilter,
141                         value: processLogsPanel.selectedFilter
142                     }}
143                     filters={processLogsPanel.filters.map(
144                         filter => ({ label: filter, value: filter })
145                     )}
146                     onLogFilterChange={props.onLogFilterChange}
147                     navigateToLog={props.navigateToLog}
148                 />
149             </MPVPanelContent>
150             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-inputs">
151                 <ProcessIOCard
152                     label={ProcessIOCardType.INPUT}
153                     process={process}
154                     params={processedInputs}
155                     raw={rawInputs}
156                     mounts={inputMounts}
157                  />
158             </MPVPanelContent>
159             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-outputs">
160                 <ProcessIOCard
161                     label={ProcessIOCardType.OUTPUT}
162                     process={process}
163                     params={processedOutputs}
164                     raw={outputDetails?.rawOutputs}
165                     outputUuid={outputUuid || ""}
166                  />
167             </MPVPanelContent>
168             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-children">
169                 <SubprocessPanel />
170             </MPVPanelContent>
171         </MPVContainer>
172         : <Grid container
173             alignItems='center'
174             justify='center'
175             style={{ minHeight: '100%' }}>
176             <DefaultView
177                 icon={ProcessIcon}
178                 messages={['Process not found']} />
179         </Grid>;
180     }
181 );
182
183 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
184     return inputs.map(input => {
185         return {
186             id: getIOParamId(input),
187             label: input.label || "",
188             value: getIOParamDisplayValue(auth, input)
189         };
190     });
191 };
192
193 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
194     return definitions.map(output => {
195         return {
196             id: getIOParamId(output),
197             label: output.label || "",
198             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
199         };
200     });
201 };