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