Merge branch '21356-clean-imports'
[arvados.git] / services / workbench2 / src / views / process-panel / process-resource-card.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 {
7     StyleRulesCallback,
8     WithStyles,
9     withStyles,
10     Card,
11     CardHeader,
12     IconButton,
13     CardContent,
14     Tooltip,
15     Typography,
16     Grid,
17     Link,
18 } from '@material-ui/core';
19 import { ArvadosTheme } from 'common/custom-theme';
20 import {
21     CloseIcon,
22     MaximizeIcon,
23     ResourceIcon,
24     UnMaximizeIcon,
25     ShowChartIcon,
26 } from 'components/icon/icon';
27 import { MPVPanelProps } from 'components/multi-panel-view/multi-panel-view';
28 import { connect } from 'react-redux';
29 import { Process } from 'store/processes/process';
30 import { NodeInstanceType } from 'store/process-panel/process-panel';
31 import { DetailsAttribute } from "components/details-attribute/details-attribute";
32 import { formatFileSize } from "common/formatters";
33 import { MountKind } from 'models/mount-types';
34
35 interface ProcessResourceCardDataProps {
36     process: Process;
37     nodeInfo: NodeInstanceType | null;
38     usageReport: string | null;
39 }
40
41 type CssRules = "card" | "header" | "title" | "avatar" | "iconHeader" | "content" | "sectionH3" | "reportButton";
42
43 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
44     card: {
45         height: '100%'
46     },
47     header: {
48         paddingBottom: "0px"
49     },
50     title: {
51         paddingTop: theme.spacing.unit * 0.5
52     },
53     avatar: {
54         paddingTop: theme.spacing.unit * 0.5
55     },
56     iconHeader: {
57         fontSize: '1.875rem',
58         color: theme.customs.colors.greyL,
59     },
60     content: {
61         paddingTop: "0px",
62         maxHeight: `calc(100% - ${theme.spacing.unit * 7.5}px)`,
63         overflow: "auto"
64     },
65     sectionH3: {
66         margin: "0.5em",
67         color: theme.customs.colors.greyD,
68         fontSize: "0.8125rem",
69         textTransform: "uppercase",
70     },
71     reportButton: {
72     }
73 });
74
75 type ProcessResourceCardProps = ProcessResourceCardDataProps & WithStyles<CssRules> & MPVPanelProps;
76
77 export const ProcessResourceCard = withStyles(styles)(connect()(
78     ({ classes, nodeInfo, usageReport, doHidePanel, doMaximizePanel, doUnMaximizePanel, panelMaximized, panelName, process, }: ProcessResourceCardProps) => {
79         let diskRequest = 0;
80         if (process.container?.mounts) {
81             for (const mnt in process.container.mounts) {
82                 const mp = process.container.mounts[mnt];
83                 if (mp.kind === MountKind.TEMPORARY_DIRECTORY) {
84                     diskRequest += mp.capacity;
85                 }
86             }
87         }
88
89         return <Card className={classes.card} data-cy="process-resources-card">
90             <CardHeader
91                 className={classes.header}
92                 classes={{
93                     content: classes.title,
94                     avatar: classes.avatar,
95                 }}
96                 avatar={<ResourceIcon className={classes.iconHeader} />}
97                 title={
98                     <Typography noWrap variant='h6' color='inherit'>
99                         Resources
100                     </Typography>
101                 }
102                 action={
103                     <div>
104                         {usageReport && <Link href={usageReport} className={classes.reportButton} target="_blank"><ShowChartIcon /> Resource usage report</Link>}
105                         {doUnMaximizePanel && panelMaximized &&
106                             <Tooltip title={`Unmaximize ${panelName || 'panel'}`} disableFocusListener>
107                                 <IconButton onClick={doUnMaximizePanel}><UnMaximizeIcon /></IconButton>
108                             </Tooltip>}
109                         {doMaximizePanel && !panelMaximized &&
110                             <Tooltip title={`Maximize ${panelName || 'panel'}`} disableFocusListener>
111                                 <IconButton onClick={doMaximizePanel}><MaximizeIcon /></IconButton>
112                             </Tooltip>}
113                         {doHidePanel &&
114                             <Tooltip title={`Close ${panelName || 'panel'}`} disableFocusListener>
115                                 <IconButton disabled={panelMaximized} onClick={doHidePanel}><CloseIcon /></IconButton>
116                             </Tooltip>}
117                     </div>
118                 } />
119             <CardContent className={classes.content}>
120                 <Grid container>
121                     <Grid item xs={4}>
122                         <h3 className={classes.sectionH3}>Requested Resources</h3>
123                         <Grid container>
124                             <Grid item xs={12}>
125                                 <DetailsAttribute label="Cores" value={process.container?.runtimeConstraints.vcpus} />
126                             </Grid>
127                             <Grid item xs={12}>
128                                 <DetailsAttribute label="RAM*" value={formatFileSize(process.container?.runtimeConstraints.ram)} />
129                             </Grid>
130                             <Grid item xs={12}>
131                                 <DetailsAttribute label="Disk" value={formatFileSize(diskRequest)} />
132                             </Grid>
133
134                             {process.container?.runtimeConstraints.cuda &&
135                                 process.container?.runtimeConstraints.cuda.device_count > 0 ?
136                                 <>
137                                     <Grid item xs={12}>
138                                         <DetailsAttribute label="CUDA devices" value={process.container?.runtimeConstraints.cuda.device_count} />
139                                     </Grid>
140                                     <Grid item xs={12}>
141                                         <DetailsAttribute label="CUDA driver version" value={process.container?.runtimeConstraints.cuda.driver_version} />
142                                     </Grid>
143                                     <Grid item xs={12}>
144                                         <DetailsAttribute label="CUDA hardware capability" value={process.container?.runtimeConstraints.cuda.hardware_capability} />
145                                     </Grid>
146                                 </> : null}
147
148                             {process.container?.runtimeConstraints.keep_cache_ram &&
149                                 process.container?.runtimeConstraints.keep_cache_ram > 0 ?
150                                 <Grid item xs={12}>
151                                     <DetailsAttribute label="Keep cache (RAM)" value={formatFileSize(process.container?.runtimeConstraints.keep_cache_ram)} />
152                                 </Grid> : null}
153
154                             {process.container?.runtimeConstraints.keep_cache_disk &&
155                                 process.container?.runtimeConstraints.keep_cache_disk > 0 ?
156                                 <Grid item xs={12}>
157                                     <DetailsAttribute label="Keep cache (disk)" value={formatFileSize(process.container?.runtimeConstraints.keep_cache_disk)} />
158                                 </Grid> : null}
159
160                             {process.container?.runtimeConstraints.API ? <Grid item xs={12}>
161                                 <DetailsAttribute label="API access" value={process.container?.runtimeConstraints.API.toString()} />
162                             </Grid> : null}
163
164                         </Grid>
165                     </Grid>
166
167
168                     <Grid item xs={8}>
169                         <h3 className={classes.sectionH3}>Assigned Instance Type</h3>
170                         {nodeInfo === null ? <Grid item xs={8}>
171                             No instance type recorded
172                         </Grid>
173                             :
174                             <Grid container>
175                                 <Grid item xs={6}>
176                                     <DetailsAttribute label="Cores" value={nodeInfo.VCPUs} />
177                                 </Grid>
178
179                                 <Grid item xs={6}>
180                                     <DetailsAttribute label="Provider type" value={nodeInfo.ProviderType} />
181                                 </Grid>
182
183                                 <Grid item xs={6}>
184                                     <DetailsAttribute label="RAM" value={formatFileSize(nodeInfo.RAM)} />
185                                 </Grid>
186
187                                 <Grid item xs={6}>
188                                     <DetailsAttribute label="Price" value={"$" + nodeInfo.Price.toString()} />
189                                 </Grid>
190
191                                 <Grid item xs={6}>
192                                     <DetailsAttribute label="Disk" value={formatFileSize(nodeInfo.IncludedScratch + nodeInfo.AddedScratch)} />
193                                 </Grid>
194
195                                 <Grid item xs={6}>
196                                     <DetailsAttribute label="Preemptible" value={nodeInfo.Preemptible.toString()} />
197                                 </Grid>
198
199                                 {nodeInfo.CUDA && nodeInfo.CUDA.DeviceCount > 0 &&
200                                     <>
201                                         <Grid item xs={6}>
202                                             <DetailsAttribute label="CUDA devices" value={nodeInfo.CUDA.DeviceCount} />
203                                         </Grid>
204
205                                         <Grid item xs={6}>
206                                         </Grid>
207
208                                         <Grid item xs={6}>
209                                             <DetailsAttribute label="CUDA driver version" value={nodeInfo.CUDA.DriverVersion} />
210                                         </Grid>
211
212                                         <Grid item xs={6}>
213                                         </Grid>
214
215                                         <Grid item xs={6}>
216                                             <DetailsAttribute label="CUDA hardware capability" value={nodeInfo.CUDA.HardwareCapability} />
217                                         </Grid>
218                                     </>
219                                 }
220                             </Grid>}
221                     </Grid>
222                 </Grid>
223                 <Typography>* RAM available to the program is limited to Requested RAM, not Instance RAM</Typography>
224             </CardContent>
225         </Card >;
226     }
227 ));