6b88e2618622c41409ad185a93732746649c497d
[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             // Since mounts can disappear and reappear, only set inputs if raw / processed inputs is undefined or new inputs has content
113             const newRawInputs = getRawInputs(containerRequest);
114             if (rawInputs === undefined || newRawInputs && newRawInputs.length) {
115                 setInputs(newRawInputs);
116             }
117             const newInputs = getInputs(containerRequest);
118             if (processedInputs === undefined || newInputs && newInputs.length) {
119                 setProcessedInputs(formatInputData(newInputs, auth));
120             }
121         }
122     }, [requestUuid, auth, containerRequest]);
123
124     return process
125         ? <MPVContainer className={props.classes.root} spacing={8} panelStates={panelsData}  justify-content="flex-start" direction="column" wrap="nowrap">
126             <MPVPanelContent forwardProps xs="auto" data-cy="process-details">
127                 <ProcessDetailsCard
128                     process={process}
129                     onContextMenu={event => props.onContextMenu(event, process)}
130                     cancelProcess={props.cancelProcess}
131                 />
132             </MPVPanelContent>
133             <MPVPanelContent forwardProps xs="auto" data-cy="process-cmd">
134                 <ProcessCmdCard
135                     onCopy={props.onCopyToClipboard}
136                     process={process} />
137             </MPVPanelContent>
138             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-logs">
139                 <ProcessLogsCard
140                     onCopy={props.onCopyToClipboard}
141                     process={process}
142                     lines={getProcessPanelLogs(processLogsPanel)}
143                     selectedFilter={{
144                         label: processLogsPanel.selectedFilter,
145                         value: processLogsPanel.selectedFilter
146                     }}
147                     filters={processLogsPanel.filters.map(
148                         filter => ({ label: filter, value: filter })
149                     )}
150                     onLogFilterChange={props.onLogFilterChange}
151                     navigateToLog={props.navigateToLog}
152                 />
153             </MPVPanelContent>
154             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-inputs">
155                 <ProcessIOCard
156                     label={ProcessIOCardType.INPUT}
157                     process={process}
158                     params={processedInputs}
159                     raw={rawInputs}
160                     mounts={inputMounts}
161                  />
162             </MPVPanelContent>
163             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-outputs">
164                 <ProcessIOCard
165                     label={ProcessIOCardType.OUTPUT}
166                     process={process}
167                     params={processedOutputs}
168                     raw={outputDetails?.rawOutputs}
169                     outputUuid={outputUuid || ""}
170                  />
171             </MPVPanelContent>
172             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-children">
173                 <SubprocessPanel />
174             </MPVPanelContent>
175         </MPVContainer>
176         : <Grid container
177             alignItems='center'
178             justify='center'
179             style={{ minHeight: '100%' }}>
180             <DefaultView
181                 icon={ProcessIcon}
182                 messages={['Process not found']} />
183         </Grid>;
184     }
185 );
186
187 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
188     return inputs.map(input => {
189         return {
190             id: getIOParamId(input),
191             label: input.label || "",
192             value: getIOParamDisplayValue(auth, input)
193         };
194     });
195 };
196
197 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
198     return definitions.map(output => {
199         return {
200             id: getIOParamId(output),
201             label: output.label || "",
202             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
203         };
204     });
205 };