add compute node middleware, data explorer and remove reducer
[arvados-workbench2.git] / src / views-components / data-explorer / renderers.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { Grid, Typography, withStyles, Tooltip, IconButton, Checkbox, Button } from '@material-ui/core';
7 import { FavoriteStar } from '../favorite-star/favorite-star';
8 import { ResourceKind, TrashableResource } from '~/models/resource';
9 import { ProjectIcon, CollectionIcon, ProcessIcon, DefaultIcon, WorkflowIcon, ShareIcon } from '~/components/icon/icon';
10 import { formatDate, formatFileSize } from '~/common/formatters';
11 import { resourceLabel } from '~/common/labels';
12 import { connect, DispatchProp } from 'react-redux';
13 import { RootState } from '~/store/store';
14 import { getResource } from '~/store/resources/resources';
15 import { GroupContentsResource } from '~/services/groups-service/groups-service';
16 import { getProcess, Process, getProcessStatus, getProcessStatusColor } from '~/store/processes/process';
17 import { ArvadosTheme } from '~/common/custom-theme';
18 import { compose, Dispatch } from 'redux';
19 import { WorkflowResource } from '~/models/workflow';
20 import { ResourceStatus } from '~/views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from '~/store/workflow-panel/workflow-panel-actions';
22 import { getResourceData } from "~/store/resources-data/resources-data";
23 import { openSharingDialog } from '~/store/sharing-dialog/sharing-dialog-actions';
24 import { UserResource } from '~/models/user';
25 import { toggleIsActive, toggleIsAdmin } from '~/store/users/users-actions';
26 import { LinkResource } from '~/models/link';
27 import { navigateTo } from '~/store/navigation/navigation-action';
28 import { Link } from 'react-router-dom';
29 import { NodeResource } from '../../models/node';
30 import { NodeInfo } from '~/models/node';
31
32 const renderName = (item: { name: string; uuid: string, kind: string }) =>
33     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
34         <Grid item>
35             {renderIcon(item)}
36         </Grid>
37         <Grid item>
38             <Typography color="primary" style={{ width: '450px' }}>
39                 {item.name}
40             </Typography>
41         </Grid>
42         <Grid item>
43             <Typography variant="caption">
44                 <FavoriteStar resourceUuid={item.uuid} />
45             </Typography>
46         </Grid>
47     </Grid>;
48
49 export const ResourceName = connect(
50     (state: RootState, props: { uuid: string }) => {
51         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
52         return resource || { name: '', uuid: '', kind: '' };
53     })(renderName);
54
55 const renderIcon = (item: { kind: string }) => {
56     switch (item.kind) {
57         case ResourceKind.PROJECT:
58             return <ProjectIcon />;
59         case ResourceKind.COLLECTION:
60             return <CollectionIcon />;
61         case ResourceKind.PROCESS:
62             return <ProcessIcon />;
63         case ResourceKind.WORKFLOW:
64             return <WorkflowIcon />;
65         default:
66             return <DefaultIcon />;
67     }
68 };
69
70 const renderDate = (date?: string) => {
71     return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
72 };
73
74 const renderWorkflowName = (item: { name: string; uuid: string, kind: string, ownerUuid: string }) =>
75     <Grid container alignItems="center" wrap="nowrap" spacing={16}>
76         <Grid item>
77             {renderIcon(item)}
78         </Grid>
79         <Grid item>
80             <Typography color="primary" style={{ width: '100px' }}>
81                 {item.name}
82             </Typography>
83         </Grid>
84     </Grid>;
85
86 export const RosurceWorkflowName = connect(
87     (state: RootState, props: { uuid: string }) => {
88         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
89         return resource || { name: '', uuid: '', kind: '', ownerUuid: '' };
90     })(renderWorkflowName);
91
92 const getPublicUuid = (uuidPrefix: string) => {
93     return `${uuidPrefix}-tpzed-anonymouspublic`;
94 };
95
96 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
97     const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
98     return (
99         <div>
100             {!isPublic && uuid &&
101                 <Tooltip title="Share">
102                     <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
103                         <ShareIcon />
104                     </IconButton>
105                 </Tooltip>
106             }
107         </div>
108     );
109 };
110
111 export const ResourceShare = connect(
112     (state: RootState, props: { uuid: string }) => {
113         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
114         const uuidPrefix = getUuidPrefix(state);
115         return {
116             uuid: resource ? resource.uuid : '',
117             ownerUuid: resource ? resource.ownerUuid : '',
118             uuidPrefix
119         };
120     })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
121         resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
122
123 const renderFirstName = (item: { firstName: string }) => {
124     return <Typography noWrap>{item.firstName}</Typography>;
125 };
126
127 // User Resources
128 export const ResourceFirstName = connect(
129     (state: RootState, props: { uuid: string }) => {
130         const resource = getResource<UserResource>(props.uuid)(state.resources);
131         return resource || { firstName: '' };
132     })(renderFirstName);
133
134 const renderLastName = (item: { lastName: string }) =>
135     <Typography noWrap>{item.lastName}</Typography>;
136
137 export const ResourceLastName = connect(
138     (state: RootState, props: { uuid: string }) => {
139         const resource = getResource<UserResource>(props.uuid)(state.resources);
140         return resource || { lastName: '' };
141     })(renderLastName);
142
143 const renderUuid = (item: { uuid: string }) =>
144     <Typography noWrap>{item.uuid}</Typography>;
145
146 export const ResourceUuid = connect(
147     (state: RootState, props: { uuid: string }) => {
148         const resource = getResource<UserResource>(props.uuid)(state.resources);
149         return resource || { uuid: '' };
150     })(renderUuid);
151
152 const renderEmail = (item: { email: string }) =>
153     <Typography noWrap>{item.email}</Typography>;
154
155 export const ResourceEmail = connect(
156     (state: RootState, props: { uuid: string }) => {
157         const resource = getResource<UserResource>(props.uuid)(state.resources);
158         return resource || { email: '' };
159     })(renderEmail);
160
161 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
162     <Checkbox
163         color="primary"
164         checked={props.isActive}
165         onClick={() => props.toggleIsActive(props.uuid)} />;
166
167 export const ResourceIsActive = connect(
168     (state: RootState, props: { uuid: string }) => {
169         const resource = getResource<UserResource>(props.uuid)(state.resources);
170         return resource || { isActive: false };
171     }, { toggleIsActive }
172 )(renderIsActive);
173
174 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
175     <Checkbox
176         color="primary"
177         checked={props.isAdmin}
178         onClick={() => props.toggleIsAdmin(props.uuid)} />;
179
180 export const ResourceIsAdmin = connect(
181     (state: RootState, props: { uuid: string }) => {
182         const resource = getResource<UserResource>(props.uuid)(state.resources);
183         return resource || { isAdmin: false };
184     }, { toggleIsAdmin }
185 )(renderIsAdmin);
186
187 const renderUsername = (item: { username: string }) =>
188     <Typography noWrap>{item.username}</Typography>;
189
190 export const ResourceUsername = connect(
191     (state: RootState, props: { uuid: string }) => {
192         const resource = getResource<UserResource>(props.uuid)(state.resources);
193         return resource || { username: '' };
194     })(renderUsername);
195
196 // Compute Node Resources
197 const renderNodeDate = (date?: string) =>
198     <Typography noWrap>{formatDate(date) || '(none)'}</Typography>;
199
200 const renderNodeData = (property?: string) => 
201     <Typography noWrap>{property || '(none)'}</Typography>;
202
203 const renderNodeInfo = (item: { info: NodeInfo }) =>
204     <Typography>
205         {JSON.stringify(item.info, null, 4)}
206     </Typography>;
207
208 export const ResourceNodeInfo = connect(
209     (state: RootState, props: { uuid: string }) => {
210         const resource = getResource<NodeResource>(props.uuid)(state.resources);
211         return resource || { info: {} };
212     })(renderNodeInfo);
213
214 export const ResourceNodeDomain = connect(
215     (state: RootState, props: { uuid: string }) => {
216         const resource = getResource<NodeResource>(props.uuid)(state.resources);
217         return { property: resource ? resource.domain : '' };
218     })((props: { property: string }) => renderNodeData(props.property));
219
220 export const ResourceNodeFirstPingAt = connect(
221     (state: RootState, props: { uuid: string }) => {
222         const resource = getResource<NodeResource>(props.uuid)(state.resources);
223         return { date: resource ? resource.firstPingAt : '' };
224     })((props: { date: string }) => renderNodeDate(props.date));
225
226 export const ResourceNodeHostname = connect(
227     (state: RootState, props: { uuid: string }) => {
228         const resource = getResource<NodeResource>(props.uuid)(state.resources);
229         return { property: resource ? resource.hostname : '' };
230     })((props: { property: string }) => renderNodeData(props.property));
231
232 export const ResourceNodeIpAddress = connect(
233     (state: RootState, props: { uuid: string }) => {
234         const resource = getResource<NodeResource>(props.uuid)(state.resources);
235         return { property: resource ? resource.ipAddress : '' };
236     })((props: { property: string }) => renderNodeData(props.property));
237
238 export const ResourceNodeJobUuid = connect(
239     (state: RootState, props: { uuid: string }) => {
240         const resource = getResource<NodeResource>(props.uuid)(state.resources);
241         return { property: resource ? resource.jobUuid : '' };
242     })((props: { property: string }) => renderNodeData(props.property));
243
244 export const ResourceNodeLastPingAt = connect(
245     (state: RootState, props: { uuid: string }) => {
246         const resource = getResource<NodeResource>(props.uuid)(state.resources);
247         return { date: resource ? resource.lastPingAt : '' };
248     })((props: { date: string }) => renderNodeDate(props.date));
249
250 // Links Resources
251 const renderLinkName = (item: { name: string }) =>
252     <Typography noWrap>{item.name || '(none)'}</Typography>;
253
254 export const ResourceLinkName = connect(
255     (state: RootState, props: { uuid: string }) => {
256         const resource = getResource<LinkResource>(props.uuid)(state.resources);
257         return resource || { name: '' };
258     })(renderLinkName);
259
260 const renderLinkClass = (item: { linkClass: string }) =>
261     <Typography noWrap>{item.linkClass}</Typography>;
262
263 export const ResourceLinkClass = connect(
264     (state: RootState, props: { uuid: string }) => {
265         const resource = getResource<LinkResource>(props.uuid)(state.resources);
266         return resource || { linkClass: '' };
267     })(renderLinkClass);
268
269 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
270     const currentLabel = resourceLabel(item.tailKind);
271     const isUnknow = currentLabel === "Unknown";
272     return (<div>
273         { !isUnknow  ? (
274                 renderLink(dispatch, item.tailUuid, currentLabel)
275             ) : (
276                 <Typography noWrap color="default">
277                     {item.tailUuid}
278                 </Typography>
279         )}
280     </div>);
281 };
282
283 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
284     <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
285         {label}: {uuid}
286     </Typography>;
287
288 export const ResourceLinkTail = connect(
289     (state: RootState, props: { uuid: string }) => {
290         const resource = getResource<LinkResource>(props.uuid)(state.resources);
291         return {
292             item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
293         };
294     })((props: { item: any } & DispatchProp<any>) =>
295         renderLinkTail(props.dispatch, props.item));
296
297 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
298     renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
299
300 export const ResourceLinkHead = connect(
301     (state: RootState, props: { uuid: string }) => {
302         const resource = getResource<LinkResource>(props.uuid)(state.resources);
303         return {
304             item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
305         };
306     })((props: { item: any } & DispatchProp<any>) =>
307         renderLinkHead(props.dispatch, props.item));
308
309 export const ResourceLinkUuid = connect(
310     (state: RootState, props: { uuid: string }) => {
311         const resource = getResource<LinkResource>(props.uuid)(state.resources);
312         return resource || { uuid: '' };
313     })(renderUuid);
314
315 // Process Resources
316 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
317     return (
318         <div>
319             {uuid &&
320                 <Tooltip title="Run process">
321                     <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
322                         <ProcessIcon />
323                     </IconButton>
324                 </Tooltip>}
325         </div>
326     );
327 };
328
329 export const ResourceRunProcess = connect(
330     (state: RootState, props: { uuid: string }) => {
331         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
332         return {
333             uuid: resource ? resource.uuid : ''
334         };
335     })((props: { uuid: string } & DispatchProp<any>) =>
336         resourceRunProcess(props.dispatch, props.uuid));
337
338 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
339     if (ownerUuid === getPublicUuid(uuidPrefix)) {
340         return renderStatus(ResourceStatus.PUBLIC);
341     } else {
342         return renderStatus(ResourceStatus.PRIVATE);
343     }
344 };
345
346 const renderStatus = (status: string) =>
347     <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
348
349 export const ResourceWorkflowStatus = connect(
350     (state: RootState, props: { uuid: string }) => {
351         const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
352         const uuidPrefix = getUuidPrefix(state);
353         return {
354             ownerUuid: resource ? resource.ownerUuid : '',
355             uuidPrefix
356         };
357     })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
358
359 export const ResourceLastModifiedDate = connect(
360     (state: RootState, props: { uuid: string }) => {
361         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
362         return { date: resource ? resource.modifiedAt : '' };
363     })((props: { date: string }) => renderDate(props.date));
364
365 export const ResourceTrashDate = connect(
366     (state: RootState, props: { uuid: string }) => {
367         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
368         return { date: resource ? resource.trashAt : '' };
369     })((props: { date: string }) => renderDate(props.date));
370
371 export const ResourceDeleteDate = connect(
372     (state: RootState, props: { uuid: string }) => {
373         const resource = getResource<TrashableResource>(props.uuid)(state.resources);
374         return { date: resource ? resource.deleteAt : '' };
375     })((props: { date: string }) => renderDate(props.date));
376
377 export const renderFileSize = (fileSize?: number) =>
378     <Typography noWrap style={{ minWidth: '45px' }}>
379         {formatFileSize(fileSize)}
380     </Typography>;
381
382 export const ResourceFileSize = connect(
383     (state: RootState, props: { uuid: string }) => {
384         const resource = getResourceData(props.uuid)(state.resourcesData);
385         return { fileSize: resource ? resource.fileSize : 0 };
386     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
387
388 const renderOwner = (owner: string) =>
389     <Typography noWrap color="primary" >
390         {owner}
391     </Typography>;
392
393 export const ResourceOwner = connect(
394     (state: RootState, props: { uuid: string }) => {
395         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
396         return { owner: resource ? resource.ownerUuid : '' };
397     })((props: { owner: string }) => renderOwner(props.owner));
398
399 const renderType = (type: string) =>
400     <Typography noWrap>
401         {resourceLabel(type)}
402     </Typography>;
403
404 export const ResourceType = connect(
405     (state: RootState, props: { uuid: string }) => {
406         const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
407         return { type: resource ? resource.kind : '' };
408     })((props: { type: string }) => renderType(props.type));
409
410 export const ProcessStatus = compose(
411     connect((state: RootState, props: { uuid: string }) => {
412         return { process: getProcess(props.uuid)(state.resources) };
413     }),
414     withStyles({}, { withTheme: true }))
415     ((props: { process?: Process, theme: ArvadosTheme }) => {
416         const status = props.process ? getProcessStatus(props.process) : "-";
417         return <Typography
418             noWrap
419             align="center"
420             style={{ color: getProcessStatusColor(status, props.theme) }} >
421             {status}
422         </Typography>;
423     });