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