Merge branch '21901-file-log-throttling'
[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 { 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 } from "models/container";
18 import { navigateToOutput, openWorkflow } 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 } 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 { Link } from "react-router-dom";
27 import { getResourceUrl } from "routes/routes";
28 import WarningIcon from '@material-ui/icons/Warning';
29
30 type CssRules = 'link' | 'propertyTag';
31
32 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
33     link: {
34         fontSize: '0.875rem',
35         color: theme.palette.primary.main,
36         '&:hover': {
37             cursor: 'pointer'
38         }
39     },
40     propertyTag: {
41         marginRight: theme.spacing.unit / 2,
42         marginBottom: theme.spacing.unit / 2
43     },
44 });
45
46 const mapStateToProps = (state: RootState, props: { request: ProcessResource }) => {
47     const process = getProcess(props.request.uuid)(state.resources);
48
49     let workflowCollection = "";
50     let workflowPath = "";
51     if (process?.containerRequest?.mounts && process.containerRequest.mounts[MOUNT_PATH_CWL_WORKFLOW]) {
52         const wf = process.containerRequest.mounts[MOUNT_PATH_CWL_WORKFLOW] as JSONMount;
53
54         if (wf.content["$graph"] &&
55             wf.content["$graph"].length > 0 &&
56             wf.content["$graph"][0] &&
57             wf.content["$graph"][0]["steps"] &&
58             wf.content["$graph"][0]["steps"][0]) {
59
60             const REGEX = /keep:([0-9a-f]{32}\+\d+)\/(.*)/;
61             const pdh = wf.content["$graph"][0]["steps"][0].run.match(REGEX);
62             if (pdh) {
63                 workflowCollection = pdh[1];
64                 workflowPath = pdh[2];
65             }
66         }
67     }
68
69     return {
70         container: process?.container,
71         workflowCollection,
72         workflowPath,
73         subprocesses: filterResources((resource: ContainerRequestResource) =>
74             resource.kind === ResourceKind.CONTAINER_REQUEST &&
75             resource.requestingContainerUuid === process?.containerRequest.containerUuid
76         )(state.resources),
77     };
78 };
79
80 interface ProcessDetailsAttributesActionProps {
81     navigateToOutput: (resource: ContainerRequestResource) => void;
82     openWorkflow: (uuid: string) => void;
83 }
84
85 const mapDispatchToProps = (dispatch: Dispatch): ProcessDetailsAttributesActionProps => ({
86     navigateToOutput: (resource) => dispatch<any>(navigateToOutput(resource)),
87     openWorkflow: (uuid) => dispatch<any>(openWorkflow(uuid)),
88 });
89
90 export const ProcessDetailsAttributes = withStyles(styles, { withTheme: true })(
91     connect(mapStateToProps, mapDispatchToProps)(
92         (props: {
93             request: ProcessResource, container?: ContainerResource, subprocesses: ContainerRequestResource[],
94             workflowCollection, workflowPath,
95             twoCol?: boolean, hideProcessPanelRedundantFields?: boolean, classes: Record<CssRules, string>
96         } & ProcessDetailsAttributesActionProps) => {
97             const containerRequest = props.request;
98             const container = props.container;
99             const subprocesses = props.subprocesses;
100             const classes = props.classes;
101             const mdSize = props.twoCol ? 6 : 12;
102             const workflowCollection = props.workflowCollection;
103             const workflowPath = props.workflowPath;
104             const filteredPropertyKeys = Object.keys(containerRequest.properties)
105                 .filter(k => (typeof containerRequest.properties[k] !== 'object'));
106             const hasTotalCost = containerRequest && containerRequest.cumulativeCost > 0;
107             const totalCostNotReady = container && container.cost > 0 && container.state === "Running" && containerRequest && containerRequest.cumulativeCost === 0 && subprocesses.length > 0;
108             const resubmittedUrl = containerRequest && getResourceUrl(containerRequest.properties[ProcessProperties.FAILED_CONTAINER_RESUBMITTED]);
109
110             return <Grid container>
111             <Grid item xs={12}>
112                 <ProcessRuntimeStatus runtimeStatus={container?.runtimeStatus} containerCount={containerRequest.containerCount} />
113             </Grid>
114             {!props.hideProcessPanelRedundantFields && <Grid item xs={12} md={mdSize}>
115                 <DetailsAttribute label='Type' value={resourceLabel(ResourceKind.PROCESS)} />
116             </Grid>}
117             {resubmittedUrl && <Grid item xs={12}>
118                 <Typography>
119                     <WarningIcon />
120                     This process failed but was automatically resubmitted.  <Link to={resubmittedUrl}> Click here to go to the resubmitted process.</Link>
121                 </Typography>
122             </Grid>}
123             <Grid item xs={12} md={mdSize}>
124                 <DetailsAttribute label='Container request UUID' linkToUuid={containerRequest.uuid} value={containerRequest.uuid} />
125             </Grid>
126             <Grid item xs={12} md={mdSize}>
127                 <DetailsAttribute label='Docker image locator'
128                                   linkToUuid={containerRequest.containerImage} value={containerRequest.containerImage} />
129             </Grid>
130             <Grid item xs={12} md={mdSize}>
131                 <DetailsAttribute
132                     label='Owner' linkToUuid={containerRequest.ownerUuid}
133                     uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
134             </Grid>
135             <Grid item xs={12} md={mdSize}>
136                 <DetailsAttribute label='Container UUID' value={containerRequest.containerUuid} />
137             </Grid>
138             {!props.hideProcessPanelRedundantFields && <Grid item xs={12} md={mdSize}>
139                 <DetailsAttribute label='Status' value={getProcessStatus({ containerRequest, container })} />
140             </Grid>}
141                 <Grid item xs={12} md={mdSize}>
142                     <DetailsAttribute label='Created at' value={formatDate(containerRequest.createdAt)} />
143                 </Grid>
144                 <Grid item xs={12} md={mdSize}>
145                     <DetailsAttribute label='Started at' value={container ? formatDate(container.startedAt) : "(none)"} />
146                 </Grid>
147                 <Grid item xs={12} md={mdSize}>
148                     <DetailsAttribute label='Finished at' value={container ? formatDate(container.finishedAt) : "(none)"} />
149                 </Grid>
150                 <Grid item xs={12} md={mdSize}>
151                     <DetailsAttribute label='Container run time'>
152                         <ContainerRunTime uuid={containerRequest.uuid} />
153                     </DetailsAttribute>
154                 </Grid>
155                 {(containerRequest && containerRequest.modifiedByUserUuid) && <Grid item xs={12} md={mdSize} data-cy="process-details-attributes-modifiedby-user">
156                     <DetailsAttribute
157                         label='Submitted by' linkToUuid={containerRequest.modifiedByUserUuid}
158                         uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
159                 </Grid>}
160                 {(container && container.runtimeUserUuid && container.runtimeUserUuid !== containerRequest.modifiedByUserUuid) && <Grid item xs={12} md={mdSize} data-cy="process-details-attributes-runtime-user">
161                     <DetailsAttribute
162                         label='Run as' linkToUuid={container.runtimeUserUuid}
163                         uuidEnhancer={(uuid: string) => <ResourceWithName uuid={uuid} />} />
164                 </Grid>}
165                 <Grid item xs={12} md={mdSize}>
166                     <DetailsAttribute label='Requesting container UUID' value={containerRequest.requestingContainerUuid || "(none)"} />
167                 </Grid>
168                 <Grid item xs={6}>
169                     <DetailsAttribute label='Output collection' />
170                     {containerRequest.outputUuid && <span onClick={() => props.navigateToOutput(containerRequest!)}>
171                         <CollectionName className={classes.link} uuid={containerRequest.outputUuid} />
172                     </span>}
173                 </Grid>
174                 {container && <Grid item xs={12} md={mdSize}>
175                     <DetailsAttribute label='Cost' value={
176                         `${hasTotalCost ? formatCost(containerRequest.cumulativeCost) + ' total, ' : (totalCostNotReady ? 'total pending completion, ' : '')}${container.cost > 0 ? formatCost(container.cost) : 'not available'} for this container`
177                     } />
178
179                     {container && workflowCollection && <Grid item xs={12} md={mdSize}>
180                         <DetailsAttribute label='Workflow code' link={getCollectionUrl(workflowCollection)} value={workflowPath} />
181                     </Grid>}
182                 </Grid>}
183                 {containerRequest.properties.template_uuid &&
184                     <Grid item xs={12} md={mdSize}>
185                         <span onClick={() => props.openWorkflow(containerRequest.properties.template_uuid)}>
186                             <DetailsAttribute classValue={classes.link}
187                                 label='Workflow' value={containerRequest.properties.workflowName} />
188                         </span>
189                     </Grid>}
190                 <Grid item xs={12} md={mdSize}>
191                     <DetailsAttribute label='Priority' value={containerRequest.priority} />
192                 </Grid>
193                 {/*
194                         NOTE: The property list should be kept at the bottom, because it spans
195                         the entire available width, without regards of the twoCol prop.
196                         */}
197                 <Grid item xs={12} md={12}>
198                     <DetailsAttribute label='Properties' />
199                     {filteredPropertyKeys.length > 0
200                         ? filteredPropertyKeys.map(k =>
201                             Array.isArray(containerRequest.properties[k])
202                                 ? containerRequest.properties[k].map((v: string) =>
203                                     getPropertyChip(k, v, undefined, classes.propertyTag))
204                                 : getPropertyChip(k, containerRequest.properties[k], undefined, classes.propertyTag))
205                         : <div>No properties</div>}
206                 </Grid>
207             </Grid>;
208         }
209     )
210 );