a08d8aecec0e925ae860f3ba03fe43adcf1ef46f
[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
26 type CssRules = 'root';
27
28 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
29     root: {
30         width: '100%',
31     },
32 });
33
34 export interface ProcessPanelRootDataProps {
35     process?: Process;
36     subprocesses: Array<Process>;
37     filters: Array<SubprocessFilterDataProps>;
38     processLogsPanel: ProcessLogsPanel;
39     auth: AuthState;
40 }
41
42 export interface ProcessPanelRootActionProps {
43     onContextMenu: (event: React.MouseEvent<HTMLElement>, process: Process) => void;
44     onToggle: (status: string) => void;
45     cancelProcess: (uuid: string) => void;
46     onLogFilterChange: (filter: FilterOption) => void;
47     navigateToLog: (uuid: string) => void;
48     onCopyToClipboard: (uuid: string) => void;
49     fetchOutputs: (uuid: string, fetchOutputs) => void;
50 }
51
52 export type ProcessPanelRootProps = ProcessPanelRootDataProps & ProcessPanelRootActionProps & WithStyles<CssRules>;
53
54 type OutputDetails = {
55     rawOutputs?: any;
56     pdh?: string;
57 }
58
59 const panelsData: MPVPanelState[] = [
60     {name: "Details"},
61     {name: "Command"},
62     {name: "Logs", visible: true},
63     {name: "Inputs"},
64     {name: "Outputs"},
65     {name: "Subprocesses"},
66 ];
67
68 export const ProcessPanelRoot = withStyles(styles)(
69     ({ process, auth, processLogsPanel, fetchOutputs, ...props }: ProcessPanelRootProps) => {
70
71     const [outputDetails, setOutputs] = useState<OutputDetails | undefined>(undefined);
72     const [rawInputs, setInputs] = useState<CommandInputParameter[] | undefined>(undefined);
73
74     const [processedOutputs, setProcessedOutputs] = useState<ProcessIOParameter[] | undefined>(undefined);
75     const [processedInputs, setProcessedInputs] = useState<ProcessIOParameter[] | undefined>(undefined);
76
77     const outputUuid = process?.containerRequest.outputUuid;
78     const requestUuid = process?.containerRequest.uuid;
79
80     const inputMounts = getInputCollectionMounts(process?.containerRequest);
81
82     // Resets state when changing processes
83     React.useEffect(() => {
84         setOutputs(undefined);
85         setInputs(undefined);
86         setProcessedOutputs(undefined);
87         setProcessedInputs(undefined);
88     }, [requestUuid]);
89
90     React.useEffect(() => {
91         if (outputUuid) {
92             fetchOutputs(outputUuid, setOutputs);
93         }
94     }, [outputUuid, fetchOutputs]);
95
96     React.useEffect(() => {
97         if (outputDetails !== undefined && outputDetails.rawOutputs && process) {
98             const outputDefinitions = getOutputParameters(process.containerRequest);
99             setProcessedOutputs(formatOutputData(outputDefinitions, outputDetails.rawOutputs, outputDetails.pdh, auth));
100         }
101     }, [outputDetails, auth, process]);
102
103     React.useEffect(() => {
104         if (process) {
105             const rawInputs = getRawInputs(process.containerRequest);
106             setInputs(rawInputs);
107
108             const inputs = getInputs(process.containerRequest);
109             setProcessedInputs(formatInputData(inputs, auth));
110         }
111     }, [requestUuid, auth, process]);
112
113     return process
114         ? <MPVContainer className={props.classes.root} spacing={8} panelStates={panelsData}  justify-content="flex-start" direction="column" wrap="nowrap">
115             <MPVPanelContent forwardProps xs="auto" data-cy="process-details">
116                 <ProcessDetailsCard
117                     process={process}
118                     onContextMenu={event => props.onContextMenu(event, process)}
119                     cancelProcess={props.cancelProcess}
120                 />
121             </MPVPanelContent>
122             <MPVPanelContent forwardProps xs="auto" data-cy="process-cmd">
123                 <ProcessCmdCard
124                     onCopy={props.onCopyToClipboard}
125                     process={process} />
126             </MPVPanelContent>
127             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-logs">
128                 <ProcessLogsCard
129                     onCopy={props.onCopyToClipboard}
130                     process={process}
131                     lines={getProcessPanelLogs(processLogsPanel)}
132                     selectedFilter={{
133                         label: processLogsPanel.selectedFilter,
134                         value: processLogsPanel.selectedFilter
135                     }}
136                     filters={processLogsPanel.filters.map(
137                         filter => ({ label: filter, value: filter })
138                     )}
139                     onLogFilterChange={props.onLogFilterChange}
140                     navigateToLog={props.navigateToLog}
141                 />
142             </MPVPanelContent>
143             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-inputs">
144                 <ProcessIOCard
145                     label={ProcessIOCardType.INPUT}
146                     process={process}
147                     params={processedInputs}
148                     raw={rawInputs}
149                     mounts={inputMounts}
150                  />
151             </MPVPanelContent>
152             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-outputs">
153                 <ProcessIOCard
154                     label={ProcessIOCardType.OUTPUT}
155                     process={process}
156                     params={processedOutputs}
157                     raw={outputDetails?.rawOutputs}
158                     outputUuid={outputUuid || ""}
159                  />
160             </MPVPanelContent>
161             <MPVPanelContent forwardProps xs maxHeight='50%' data-cy="process-children">
162                 <SubprocessPanel />
163             </MPVPanelContent>
164         </MPVContainer>
165         : <Grid container
166             alignItems='center'
167             justify='center'
168             style={{ minHeight: '100%' }}>
169             <DefaultView
170                 icon={ProcessIcon}
171                 messages={['Process not found']} />
172         </Grid>;
173     }
174 );
175
176 const formatInputData = (inputs: CommandInputParameter[], auth: AuthState): ProcessIOParameter[] => {
177     return inputs.map(input => {
178         return {
179             id: getIOParamId(input),
180             label: input.label || "",
181             value: getIOParamDisplayValue(auth, input)
182         };
183     });
184 };
185
186 const formatOutputData = (definitions: CommandOutputParameter[], values: any, pdh: string | undefined, auth: AuthState): ProcessIOParameter[] => {
187     return definitions.map(output => {
188         return {
189             id: getIOParamId(output),
190             label: output.label || "",
191             value: getIOParamDisplayValue(auth, Object.assign(output, { value: values[getIOParamId(output)] || [] }), pdh)
192         };
193     });
194 };