1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import { Grid, Typography, withStyles, Tooltip, IconButton, Checkbox } from '@material-ui/core';
7 import { FavoriteStar, PublicFavoriteStar } from '../favorite-star/favorite-star';
8 import { ResourceKind, TrashableResource } from '~/models/resource';
9 import { ProjectIcon, CollectionIcon, ProcessIcon, DefaultIcon, ShareIcon, CollectionOldVersionIcon, WorkflowIcon } from '~/components/icon/icon';
10 import { formatDate, formatFileSize, formatTime } 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, getProcessRuntime } 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 as WorkflowStatus } from '~/views/workflow-panel/workflow-panel-view';
21 import { getUuidPrefix, openRunProcess } from '~/store/workflow-panel/workflow-panel-actions';
22 import { openSharingDialog } from '~/store/sharing-dialog/sharing-dialog-actions';
23 import { getUserFullname, User, UserResource } from '~/models/user';
24 import { toggleIsActive, toggleIsAdmin } from '~/store/users/users-actions';
25 import { LinkResource } from '~/models/link';
26 import { navigateTo } from '~/store/navigation/navigation-action';
27 import { withResourceData } from '~/views-components/data-explorer/with-resources';
28 import { CollectionResource } from '~/models/collection';
29 import { IllegalNamingWarning } from '~/components/warning/warning';
30 import { loadResource } from '~/store/resources/resources-actions';
32 const renderName = (dispatch: Dispatch, item: GroupContentsResource) =>
33 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
38 <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
39 {item.kind === ResourceKind.PROJECT || item.kind === ResourceKind.COLLECTION
40 ? <IllegalNamingWarning name={item.name} />
46 <Typography variant="caption">
47 <FavoriteStar resourceUuid={item.uuid} />
48 <PublicFavoriteStar resourceUuid={item.uuid} />
53 export const ResourceName = connect(
54 (state: RootState, props: { uuid: string }) => {
55 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
57 })((resource: GroupContentsResource & DispatchProp<any>) => renderName(resource.dispatch, resource));
59 const renderIcon = (item: GroupContentsResource) => {
61 case ResourceKind.PROJECT:
62 return <ProjectIcon />;
63 case ResourceKind.COLLECTION:
64 if (item.uuid === item.currentVersionUuid) {
65 return <CollectionIcon />;
67 return <CollectionOldVersionIcon />;
68 case ResourceKind.PROCESS:
69 return <ProcessIcon />;
70 case ResourceKind.WORKFLOW:
71 return <WorkflowIcon />;
73 return <DefaultIcon />;
77 const renderDate = (date?: string) => {
78 return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
81 const renderWorkflowName = (item: WorkflowResource) =>
82 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
87 <Typography color="primary" style={{ width: '100px' }}>
93 export const ResourceWorkflowName = connect(
94 (state: RootState, props: { uuid: string }) => {
95 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
97 })(renderWorkflowName);
99 const getPublicUuid = (uuidPrefix: string) => {
100 return `${uuidPrefix}-tpzed-anonymouspublic`;
103 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
104 const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
107 {!isPublic && uuid &&
108 <Tooltip title="Share">
109 <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
118 export const ResourceShare = connect(
119 (state: RootState, props: { uuid: string }) => {
120 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
121 const uuidPrefix = getUuidPrefix(state);
123 uuid: resource ? resource.uuid : '',
124 ownerUuid: resource ? resource.ownerUuid : '',
127 })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
128 resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
130 const renderFirstName = (item: { firstName: string }) => {
131 return <Typography noWrap>{item.firstName}</Typography>;
135 export const ResourceFirstName = connect(
136 (state: RootState, props: { uuid: string }) => {
137 const resource = getResource<UserResource>(props.uuid)(state.resources);
138 return resource || { firstName: '' };
141 const renderLastName = (item: { lastName: string }) =>
142 <Typography noWrap>{item.lastName}</Typography>;
144 export const ResourceLastName = connect(
145 (state: RootState, props: { uuid: string }) => {
146 const resource = getResource<UserResource>(props.uuid)(state.resources);
147 return resource || { lastName: '' };
150 const renderUuid = (item: { uuid: string }) =>
151 <Typography noWrap>{item.uuid}</Typography>;
153 export const ResourceUuid = connect(
154 (state: RootState, props: { uuid: string }) => {
155 const resource = getResource<UserResource>(props.uuid)(state.resources);
156 return resource || { uuid: '' };
159 const renderEmail = (item: { email: string }) =>
160 <Typography noWrap>{item.email}</Typography>;
162 export const ResourceEmail = connect(
163 (state: RootState, props: { uuid: string }) => {
164 const resource = getResource<UserResource>(props.uuid)(state.resources);
165 return resource || { email: '' };
168 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
171 checked={props.isActive}
172 onClick={() => props.toggleIsActive(props.uuid)} />;
174 export const ResourceIsActive = connect(
175 (state: RootState, props: { uuid: string }) => {
176 const resource = getResource<UserResource>(props.uuid)(state.resources);
177 return resource || { isActive: false };
178 }, { toggleIsActive }
181 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
184 checked={props.isAdmin}
185 onClick={() => props.toggleIsAdmin(props.uuid)} />;
187 export const ResourceIsAdmin = connect(
188 (state: RootState, props: { uuid: string }) => {
189 const resource = getResource<UserResource>(props.uuid)(state.resources);
190 return resource || { isAdmin: false };
194 const renderUsername = (item: { username: string }) =>
195 <Typography noWrap>{item.username}</Typography>;
197 export const ResourceUsername = connect(
198 (state: RootState, props: { uuid: string }) => {
199 const resource = getResource<UserResource>(props.uuid)(state.resources);
200 return resource || { username: '' };
204 const renderCommonData = (data: string) =>
205 <Typography noWrap>{data}</Typography>;
207 const renderCommonDate = (date: string) =>
208 <Typography noWrap>{formatDate(date)}</Typography>;
210 export const CommonUuid = withResourceData('uuid', renderCommonData);
212 // Api Client Authorizations
213 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
215 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
217 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
219 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
221 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
223 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
225 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
227 export const TokenScopes = withResourceData('scopes', renderCommonData);
229 export const TokenUserId = withResourceData('userId', renderCommonData);
231 // Compute Node Resources
232 const renderNodeInfo = (data: string) => {
233 return <Typography>{JSON.stringify(data, null, 4)}</Typography>;
236 const clusterColors = [
244 export const ResourceCluster = (props: { uuid: string }) => {
245 const CLUSTER_ID_LENGTH = 5;
246 const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
247 const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
248 const ci = pos >= CLUSTER_ID_LENGTH ? (((((
249 (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
250 + props.uuid.charCodeAt(2))
251 * props.uuid.charCodeAt(3))
252 + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
253 return <span style={{
254 backgroundColor: clusterColors[ci][0],
255 color: clusterColors[ci][1],
258 }}>{clusterId}</span>;
261 export const ComputeNodeInfo = withResourceData('info', renderNodeInfo);
263 export const ComputeNodeDomain = withResourceData('domain', renderCommonData);
265 export const ComputeNodeFirstPingAt = withResourceData('firstPingAt', renderCommonDate);
267 export const ComputeNodeHostname = withResourceData('hostname', renderCommonData);
269 export const ComputeNodeIpAddress = withResourceData('ipAddress', renderCommonData);
271 export const ComputeNodeJobUuid = withResourceData('jobUuid', renderCommonData);
273 export const ComputeNodeLastPingAt = withResourceData('lastPingAt', renderCommonDate);
276 const renderLinkName = (item: { name: string }) =>
277 <Typography noWrap>{item.name || '(none)'}</Typography>;
279 export const ResourceLinkName = connect(
280 (state: RootState, props: { uuid: string }) => {
281 const resource = getResource<LinkResource>(props.uuid)(state.resources);
282 return resource || { name: '' };
285 const renderLinkClass = (item: { linkClass: string }) =>
286 <Typography noWrap>{item.linkClass}</Typography>;
288 export const ResourceLinkClass = connect(
289 (state: RootState, props: { uuid: string }) => {
290 const resource = getResource<LinkResource>(props.uuid)(state.resources);
291 return resource || { linkClass: '' };
294 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
295 const currentLabel = resourceLabel(item.tailKind);
296 const isUnknow = currentLabel === "Unknown";
299 renderLink(dispatch, item.tailUuid, currentLabel)
301 <Typography noWrap color="default">
308 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
309 <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
313 export const ResourceLinkTail = connect(
314 (state: RootState, props: { uuid: string }) => {
315 const resource = getResource<LinkResource>(props.uuid)(state.resources);
317 item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
319 })((props: { item: any } & DispatchProp<any>) =>
320 renderLinkTail(props.dispatch, props.item));
322 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
323 renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
325 export const ResourceLinkHead = connect(
326 (state: RootState, props: { uuid: string }) => {
327 const resource = getResource<LinkResource>(props.uuid)(state.resources);
329 item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
331 })((props: { item: any } & DispatchProp<any>) =>
332 renderLinkHead(props.dispatch, props.item));
334 export const ResourceLinkUuid = connect(
335 (state: RootState, props: { uuid: string }) => {
336 const resource = getResource<LinkResource>(props.uuid)(state.resources);
337 return resource || { uuid: '' };
341 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
345 <Tooltip title="Run process">
346 <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
354 export const ResourceRunProcess = connect(
355 (state: RootState, props: { uuid: string }) => {
356 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
358 uuid: resource ? resource.uuid : ''
360 })((props: { uuid: string } & DispatchProp<any>) =>
361 resourceRunProcess(props.dispatch, props.uuid));
363 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
364 if (ownerUuid === getPublicUuid(uuidPrefix)) {
365 return renderStatus(WorkflowStatus.PUBLIC);
367 return renderStatus(WorkflowStatus.PRIVATE);
371 const renderStatus = (status: string) =>
372 <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
374 export const ResourceWorkflowStatus = connect(
375 (state: RootState, props: { uuid: string }) => {
376 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
377 const uuidPrefix = getUuidPrefix(state);
379 ownerUuid: resource ? resource.ownerUuid : '',
382 })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
384 export const ResourceLastModifiedDate = connect(
385 (state: RootState, props: { uuid: string }) => {
386 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
387 return { date: resource ? resource.modifiedAt : '' };
388 })((props: { date: string }) => renderDate(props.date));
390 export const ResourceCreatedAtDate = connect(
391 (state: RootState, props: { uuid: string }) => {
392 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
393 return { date: resource ? resource.createdAt : '' };
394 })((props: { date: string }) => renderDate(props.date));
396 export const ResourceTrashDate = connect(
397 (state: RootState, props: { uuid: string }) => {
398 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
399 return { date: resource ? resource.trashAt : '' };
400 })((props: { date: string }) => renderDate(props.date));
402 export const ResourceDeleteDate = connect(
403 (state: RootState, props: { uuid: string }) => {
404 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
405 return { date: resource ? resource.deleteAt : '' };
406 })((props: { date: string }) => renderDate(props.date));
408 export const renderFileSize = (fileSize?: number) =>
409 <Typography noWrap style={{ minWidth: '45px' }}>
410 {formatFileSize(fileSize)}
413 export const ResourceFileSize = connect(
414 (state: RootState, props: { uuid: string }) => {
415 const resource = getResource<CollectionResource>(props.uuid)(state.resources);
416 return { fileSize: resource ? resource.fileSizeTotal : 0 };
417 })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
419 const renderOwner = (owner: string) =>
424 export const ResourceOwner = connect(
425 (state: RootState, props: { uuid: string }) => {
426 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
427 return { owner: resource ? resource.ownerUuid : '' };
428 })((props: { owner: string }) => renderOwner(props.owner));
430 export const ResourceOwnerName = connect(
431 (state: RootState, props: { uuid: string }) => {
432 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
433 const ownerNameState = state.ownerName;
434 const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
435 return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
436 })((props: { owner: string }) => renderOwner(props.owner));
438 export const ResourceOwnerWithName =
441 (state: RootState, props: { uuid: string }) => {
443 const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
446 ownerName = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
449 return { uuid: props.uuid, ownerName };
451 withStyles({}, { withTheme: true }))
452 ((props: { uuid: string, ownerName: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
453 const { uuid, ownerName, dispatch, theme } = props;
455 if (ownerName === '') {
456 dispatch<any>(loadResource(uuid, false));
457 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
462 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
467 const renderType = (type: string) =>
469 {resourceLabel(type)}
472 export const ResourceType = connect(
473 (state: RootState, props: { uuid: string }) => {
474 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
475 return { type: resource ? resource.kind : '' };
476 })((props: { type: string }) => renderType(props.type));
478 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
479 return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
480 })((props: { resource: GroupContentsResource }) =>
481 (props.resource && props.resource.kind === ResourceKind.COLLECTION)
482 ? <CollectionStatus uuid={props.resource.uuid} />
483 : <ProcessStatus uuid={props.resource.uuid} />
486 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
487 return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
488 })((props: { collection: CollectionResource }) =>
489 (props.collection.uuid !== props.collection.currentVersionUuid)
490 ? <Typography>version {props.collection.version}</Typography>
491 : <Typography>head version</Typography>
494 export const ProcessStatus = compose(
495 connect((state: RootState, props: { uuid: string }) => {
496 return { process: getProcess(props.uuid)(state.resources) };
498 withStyles({}, { withTheme: true }))
499 ((props: { process?: Process, theme: ArvadosTheme }) => {
500 const status = props.process ? getProcessStatus(props.process) : "-";
503 style={{ color: getProcessStatusColor(status, props.theme) }} >
508 export const ProcessStartDate = connect(
509 (state: RootState, props: { uuid: string }) => {
510 const process = getProcess(props.uuid)(state.resources);
511 return { date: (process && process.container) ? process.container.startedAt : '' };
512 })((props: { date: string }) => renderDate(props.date));
514 export const renderRunTime = (time: number) =>
515 <Typography noWrap style={{ minWidth: '45px' }}>
516 {formatTime(time, true)}
519 interface ContainerRunTimeProps {
523 interface ContainerRunTimeState {
527 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
528 return { process: getProcess(props.uuid)(state.resources) };
529 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
532 constructor(props: ContainerRunTimeProps) {
534 this.state = { runtime: this.getRuntime() };
538 return this.props.process ? getProcessRuntime(this.props.process) : 0;
542 this.setState({ runtime: this.getRuntime() });
545 componentDidMount() {
546 this.timer = setInterval(this.updateRuntime.bind(this), 5000);
549 componentWillUnmount() {
550 clearInterval(this.timer);
554 return renderRunTime(this.state.runtime);