21893: Implement virtual list for process io card input collections tab
[arvados.git] / services / workbench2 / src / views / process-panel / process-details-attributes.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6 import { Grid, StyleRulesCallback, withStyles, Typography } from "@material-ui/core";
7 import { Dispatch } from 'redux';
8 import { formatCost, formatDate } from "common/formatters";
9 import { resourceLabel } from "common/labels";
10 import { DetailsAttribute } from "components/details-attribute/details-attribute";
11 import { ResourceKind } from "models/resource";
12 import { CollectionName, ContainerRunTime, ResourceWithName } from "views-components/data-explorer/renderers";
13 import { Process, getProcess, getProcessStatus, ProcessProperties } from "store/processes/process";
14 import { RootState } from "store/store";
15 import { connect } from "react-redux";
16 import { ProcessResource, MOUNT_PATH_CWL_WORKFLOW } from "models/process";
17 import { ContainerResource, ContainerState } from "models/container";
18 import { navigateToOutput, openWorkflow, loadContainerStatus } from "store/process-panel/process-panel-actions";
19 import { ArvadosTheme } from "common/custom-theme";
20 import { ProcessRuntimeStatus } from "views-components/process-runtime-status/process-runtime-status";
21 import { getPropertyChip } from "views-components/resource-properties-form/property-chip";
22 import { ContainerRequestResource, ContainerRequestState } from "models/container-request";
23 import { filterResources } from "store/resources/resources";
24 import { JSONMount } from 'models/mount-types';
25 import { getCollectionUrl } from 'models/collection';
26 import { useAsyncInterval } from 'common/use-async-interval';
27 import { Link } from "react-router-dom";
28 import { getResourceUrl } from "routes/routes";
29 import WarningIcon from '@material-ui/icons/Warning';
30
31 type CssRules = 'link' | 'propertyTag';
32
33 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
34     link: {
35         fontSize: '0.875rem',
36         color: theme.palette.primary.main,
37         '&:hover': {
38             cursor: 'pointer'
39         }
40     },
41     propertyTag: {
42         marginRight: theme.spacing.unit / 2,
43         marginBottom: theme.spacing.unit / 2
44     },
45 });
46
47 const mapStateToProps = (state: RootState, props: { request: ProcessResource }) => {
48     const process = getProcess(props.request.uuid)(state.resources);
49
50     let workflowCollection = "";
51     let workflowPath = "";
52     let schedulingStatus = "";
53     if (process?.containerRequest?.mounts && process.containerRequest.mounts[MOUNT_PATH_CWL_WORKFLOW]) {
54         const wf = process.containerRequest.mounts[MOUNT_PATH_CWL_WORKFLOW] as JSONMount;
55
56         if (process?.container &&
57             state.processPanel.containerStatus?.uuid === process?.container?.uuid)
58         {
59             schedulingStatus = state.processPanel.containerStatus.schedulingStatus;
60         }
61
62         if (wf.content["$graph"] &&
63             wf.content["$graph"].length > 0 &&
64             wf.content["$graph"][0] &&
65             wf.content["$graph"][0]["steps"] &&
66             wf.content["$graph"][0]["steps"][0]) {
67
68             const REGEX = /keep:([0-9a-f]{32}\+\d+)\/(.*)/;
69             const pdh = wf.content["$graph"][0]["steps"][0].run.match(REGEX);
70             if (pdh) {
71                 workflowCollection = pdh[1];
72                 workflowPath = pdh[2];
73             }
74         }
75     }
76
77     return {
78         container: process?.container,
79         workflowCollection,
80         workflowPath,
81         schedulingStatus,
82         subprocesses: filterResources((resource: ContainerRequestResource) =>
83             (resource.kind === ResourceKind.CONTAINER_REQUEST &&
84              resource.requestingContainerUuid === process?.containerRequest.containerUuid)
85         )(state.resources),
86     };
87 };
88
89 interface ProcessDetailsAttributesActionProps {
90     navigateToOutput: (resource: ContainerRequestResource) => void;
91     openWorkflow: (uuid: string) => void;
92     pollSchedulingStatus: (uuid: string) => void;
93 }
94
95 const mapDispatchToProps = (dispatch: Dispatch): ProcessDetailsAttributesActionProps => ({
96     navigateToOutput: (resource) => dispatch<any>(navigateToOutput(resource)),
97     openWorkflow: (uuid) => dispatch<any>(openWorkflow(uuid)),
98     pollSchedulingStatus: (uuid) => dispatch<any>(loadContainerStatus(uuid)),
99 });
100
101 const isProcessScheduling = (container?: ContainerResource): boolean => (
102     container?.state === ContainerState.QUEUED || container?.state === ContainerState.LOCKED
103 );
104
105 export const ProcessDetailsAttributes = withStyles(styles, { withTheme: true })(
106     connect(mapStateToProps, mapDispatchToProps)(
107         (props: {
108             request: ProcessResource,
109             container?: ContainerResource,
110             subprocesses: ContainerRequestResource[],
111             workflowCollection,
112             workflowPath,
113             schedulingStatus,
114             twoCol?: boolean,
115             hideProcessPanelRedundantFields?: boolean,
116             pollSchedulingStatus: (processUuid: string) => Promise<void>,
117             classes: Record<CssRules, string>
118         } & ProcessDetailsAttributesActionProps) => {
119             const containerRequest = props.request;
120             const container = props.container;
121             const subprocesses = props.subprocesses;
122             const classes = props.classes;
123             const mdSize = props.twoCol ? 6 : 12;
124             const workflowCollection = props.workflowCollection;
125             const workflowPath = props.workflowPath;
126             const filteredPropertyKeys = Object.keys(containerRequest.properties)
127                                                .filter(k => (typeof containerRequest.properties[k] !== 'object'));
128             const hasTotalCost = containerRequest && containerRequest.cumulativeCost > 0;
129             const totalCostNotReady = container && container.cost > 0 && container.state === "Running" && containerRequest && containerRequest.cumulativeCost === 0 && subprocesses.length > 0;
130             let schedulingStatus = props.schedulingStatus;
131             const resubmittedUrl = containerRequest && getResourceUrl(containerRequest.properties[ProcessProperties.FAILED_CONTAINER_RESUBMITTED]);
132
133             useAsyncInterval(() => (
134                 props.pollSchedulingStatus(containerRequest.uuid)
135             ), isProcessScheduling(container) ? 5000 : null);
136
137             if (containerRequest.state == ContainerRequestState.UNCOMMITTED) {
138                 schedulingStatus = "In draft state, not ready to run";
139             }
140
141             return <Grid container>
142             <Grid item xs={12}>
143                 <ProcessRuntimeStatus runtimeStatus={container?.runtimeStatus} containerCount={containerRequest.containerCount} schedulingStatus={schedulingStatus} />
144             </Grid>
145             {!props.hideProcessPanelRedundantFields && <Grid item xs={12} md={mdSize}>
146                 <DetailsAttribute label='Type' value={resourceLabel(ResourceKind.PROCESS)} />
147             </Grid>}
148             {resubmittedUrl && <Grid item xs={12}>
149                 <Typography>
150                     <WarningIcon />
151                     This process failed but was automatically resubmitted.  <Link to={resubmittedUrl}> Click here to go to the resubmitted process.</Link>
152                 </Typography>
153             </Grid>}
154             <Grid item xs={12} md={mdSize}>
155                 <DetailsAttribute label='Container request UUID' linkToUuid={containerRequest.uuid} value={containerRequest.uuid} />
156             </Grid>
157             <Grid item xs={12} md={mdSize}>
158                 <DetailsAttribute label='Docker image locator'
159                                   linkToUuid={containerRequest.containerImage} value={containerRequest.containerImage} />
160             </Grid>
161             <Grid item xs={12} md={mdSize}>
162                 <DetailsAttribute
163                     label='Owner' linkToUuid={containerRequest.ownerUuid}
164                     uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
165             </Grid>
166             <Grid item xs={12} md={mdSize}>
167                 <DetailsAttribute label='Container UUID' value={containerRequest.containerUuid} />
168             </Grid>
169             {!props.hideProcessPanelRedundantFields && <Grid item xs={12} md={mdSize}>
170                 <DetailsAttribute label='Status' value={getProcessStatus({ containerRequest, container })} />
171             </Grid>}
172             <Grid item xs={12} md={mdSize}>
173                 <DetailsAttribute label='Created at' value={formatDate(containerRequest.createdAt)} />
174             </Grid>
175             <Grid item xs={12} md={mdSize}>
176                 <DetailsAttribute label='Started at' value={container ? formatDate(container.startedAt) : "(none)"} />
177             </Grid>
178             <Grid item xs={12} md={mdSize}>
179                 <DetailsAttribute label='Finished at' value={container ? formatDate(container.finishedAt) : "(none)"} />
180             </Grid>
181             <Grid item xs={12} md={mdSize}>
182                 <DetailsAttribute label='Container run time'>
183                     <ContainerRunTime uuid={containerRequest.uuid} />
184                 </DetailsAttribute>
185             </Grid>
186             {(containerRequest && containerRequest.modifiedByUserUuid) && <Grid item xs={12} md={mdSize} data-cy="process-details-attributes-modifiedby-user">
187                 <DetailsAttribute
188                     label='Submitted by' linkToUuid={containerRequest.modifiedByUserUuid}
189                     uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
190             </Grid>}
191             {(container && container.runtimeUserUuid && container.runtimeUserUuid !== containerRequest.modifiedByUserUuid) && <Grid item xs={12} md={mdSize} data-cy="process-details-attributes-runtime-user">
192                 <DetailsAttribute
193                     label='Run as' linkToUuid={container.runtimeUserUuid}
194                     uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
195             </Grid>}
196             <Grid item xs={12} md={mdSize}>
197                 <DetailsAttribute label='Requesting container UUID' value={containerRequest.requestingContainerUuid || "(none)"} />
198             </Grid>
199             <Grid item xs={6}>
200                 <DetailsAttribute label='Output collection' />
201                 {containerRequest.outputUuid && <span onClick={() => props.navigateToOutput(containerRequest!)}>
202                     <CollectionName className={classes.link} uuid={containerRequest.outputUuid} />
203                 </span>}
204             </Grid>
205             {container && <Grid item xs={12} md={mdSize}>
206                 <DetailsAttribute label='Cost' value={
207                 `${hasTotalCost ? formatCost(containerRequest.cumulativeCost) + ' total, ' : (totalCostNotReady ? 'total pending completion, ' : '')}${container.cost > 0 ? formatCost(container.cost) : 'not available'} for this container`
208                 } />
209             </Grid>}
210             {container && workflowCollection && <Grid item xs={12} md={mdSize}>
211                 <DetailsAttribute label='Workflow code' link={getCollectionUrl(workflowCollection)} value={workflowPath} />
212             </Grid>}
213             {containerRequest.properties.template_uuid &&
214              <Grid item xs={12} md={mdSize}>
215                  <span onClick={() => props.openWorkflow(containerRequest.properties.template_uuid)}>
216                      <DetailsAttribute classValue={classes.link}
217                                        label='Workflow' value={containerRequest.properties.workflowName} />
218                  </span>
219              </Grid>}
220             <Grid item xs={12} md={mdSize}>
221                 <DetailsAttribute label='Priority' value={containerRequest.priority} />
222             </Grid>
223             {/*
224                 NOTE: The property list should be kept at the bottom, because it spans
225                 the entire available width, without regards of the twoCol prop.
226               */}
227             <Grid item xs={12} md={12}>
228                 <DetailsAttribute label='Properties' />
229                 {filteredPropertyKeys.length > 0
230                                              ? filteredPropertyKeys.map(k =>
231                                                  Array.isArray(containerRequest.properties[k])
232                                                  ? containerRequest.properties[k].map((v: string) =>
233                                                      getPropertyChip(k, v, undefined, classes.propertyTag))
234                                                  : getPropertyChip(k, containerRequest.properties[k], undefined, classes.propertyTag))
235                                              : <div>No properties</div>}
236             </Grid>
237             </Grid>;
238         }
239     )
240 );