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, FilterGroupIcon, 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';
31 import { GroupClass } from '~/models/group';
33 const renderName = (dispatch: Dispatch, item: GroupContentsResource) =>
34 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
39 <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
40 {item.kind === ResourceKind.PROJECT || item.kind === ResourceKind.COLLECTION
41 ? <IllegalNamingWarning name={item.name} />
47 <Typography variant="caption">
48 <FavoriteStar resourceUuid={item.uuid} />
49 <PublicFavoriteStar resourceUuid={item.uuid} />
54 export const ResourceName = connect(
55 (state: RootState, props: { uuid: string }) => {
56 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
58 })((resource: GroupContentsResource & DispatchProp<any>) => renderName(resource.dispatch, resource));
60 const renderIcon = (item: GroupContentsResource) => {
62 case ResourceKind.PROJECT:
63 if (item.groupClass === GroupClass.FILTER) {
64 return <FilterGroupIcon />;
66 return <ProjectIcon />;
67 case ResourceKind.COLLECTION:
68 if (item.uuid === item.currentVersionUuid) {
69 return <CollectionIcon />;
71 return <CollectionOldVersionIcon />;
72 case ResourceKind.PROCESS:
73 return <ProcessIcon />;
74 case ResourceKind.WORKFLOW:
75 return <WorkflowIcon />;
77 return <DefaultIcon />;
81 const renderDate = (date?: string) => {
82 return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
85 const renderWorkflowName = (item: WorkflowResource) =>
86 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
91 <Typography color="primary" style={{ width: '100px' }}>
97 export const ResourceWorkflowName = connect(
98 (state: RootState, props: { uuid: string }) => {
99 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
101 })(renderWorkflowName);
103 const getPublicUuid = (uuidPrefix: string) => {
104 return `${uuidPrefix}-tpzed-anonymouspublic`;
107 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
108 const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
111 {!isPublic && uuid &&
112 <Tooltip title="Share">
113 <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
122 export const ResourceShare = connect(
123 (state: RootState, props: { uuid: string }) => {
124 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
125 const uuidPrefix = getUuidPrefix(state);
127 uuid: resource ? resource.uuid : '',
128 ownerUuid: resource ? resource.ownerUuid : '',
131 })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
132 resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
134 const renderFirstName = (item: { firstName: string }) => {
135 return <Typography noWrap>{item.firstName}</Typography>;
139 export const ResourceFirstName = connect(
140 (state: RootState, props: { uuid: string }) => {
141 const resource = getResource<UserResource>(props.uuid)(state.resources);
142 return resource || { firstName: '' };
145 const renderLastName = (item: { lastName: string }) =>
146 <Typography noWrap>{item.lastName}</Typography>;
148 export const ResourceLastName = connect(
149 (state: RootState, props: { uuid: string }) => {
150 const resource = getResource<UserResource>(props.uuid)(state.resources);
151 return resource || { lastName: '' };
154 const renderUuid = (item: { uuid: string }) =>
155 <Typography noWrap>{item.uuid}</Typography>;
157 export const ResourceUuid = connect(
158 (state: RootState, props: { uuid: string }) => {
159 const resource = getResource<UserResource>(props.uuid)(state.resources);
160 return resource || { uuid: '' };
163 const renderEmail = (item: { email: string }) =>
164 <Typography noWrap>{item.email}</Typography>;
166 export const ResourceEmail = connect(
167 (state: RootState, props: { uuid: string }) => {
168 const resource = getResource<UserResource>(props.uuid)(state.resources);
169 return resource || { email: '' };
172 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
175 checked={props.isActive}
176 onClick={() => props.toggleIsActive(props.uuid)} />;
178 export const ResourceIsActive = connect(
179 (state: RootState, props: { uuid: string }) => {
180 const resource = getResource<UserResource>(props.uuid)(state.resources);
181 return resource || { isActive: false };
182 }, { toggleIsActive }
185 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
188 checked={props.isAdmin}
189 onClick={() => props.toggleIsAdmin(props.uuid)} />;
191 export const ResourceIsAdmin = connect(
192 (state: RootState, props: { uuid: string }) => {
193 const resource = getResource<UserResource>(props.uuid)(state.resources);
194 return resource || { isAdmin: false };
198 const renderUsername = (item: { username: string }) =>
199 <Typography noWrap>{item.username}</Typography>;
201 export const ResourceUsername = connect(
202 (state: RootState, props: { uuid: string }) => {
203 const resource = getResource<UserResource>(props.uuid)(state.resources);
204 return resource || { username: '' };
208 const renderCommonData = (data: string) =>
209 <Typography noWrap>{data}</Typography>;
211 const renderCommonDate = (date: string) =>
212 <Typography noWrap>{formatDate(date)}</Typography>;
214 export const CommonUuid = withResourceData('uuid', renderCommonData);
216 // Api Client Authorizations
217 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
219 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
221 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
223 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
225 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
227 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
229 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
231 export const TokenScopes = withResourceData('scopes', renderCommonData);
233 export const TokenUserId = withResourceData('userId', renderCommonData);
235 // Compute Node Resources
236 const renderNodeInfo = (data: string) => {
237 return <Typography>{JSON.stringify(data, null, 4)}</Typography>;
240 const clusterColors = [
248 export const ResourceCluster = (props: { uuid: string }) => {
249 const CLUSTER_ID_LENGTH = 5;
250 const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
251 const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
252 const ci = pos >= CLUSTER_ID_LENGTH ? (((((
253 (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
254 + props.uuid.charCodeAt(2))
255 * props.uuid.charCodeAt(3))
256 + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
257 return <span style={{
258 backgroundColor: clusterColors[ci][0],
259 color: clusterColors[ci][1],
262 }}>{clusterId}</span>;
265 export const ComputeNodeInfo = withResourceData('info', renderNodeInfo);
267 export const ComputeNodeDomain = withResourceData('domain', renderCommonData);
269 export const ComputeNodeFirstPingAt = withResourceData('firstPingAt', renderCommonDate);
271 export const ComputeNodeHostname = withResourceData('hostname', renderCommonData);
273 export const ComputeNodeIpAddress = withResourceData('ipAddress', renderCommonData);
275 export const ComputeNodeJobUuid = withResourceData('jobUuid', renderCommonData);
277 export const ComputeNodeLastPingAt = withResourceData('lastPingAt', renderCommonDate);
280 const renderLinkName = (item: { name: string }) =>
281 <Typography noWrap>{item.name || '(none)'}</Typography>;
283 export const ResourceLinkName = connect(
284 (state: RootState, props: { uuid: string }) => {
285 const resource = getResource<LinkResource>(props.uuid)(state.resources);
286 return resource || { name: '' };
289 const renderLinkClass = (item: { linkClass: string }) =>
290 <Typography noWrap>{item.linkClass}</Typography>;
292 export const ResourceLinkClass = connect(
293 (state: RootState, props: { uuid: string }) => {
294 const resource = getResource<LinkResource>(props.uuid)(state.resources);
295 return resource || { linkClass: '' };
298 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
299 const currentLabel = resourceLabel(item.tailKind);
300 const isUnknow = currentLabel === "Unknown";
303 renderLink(dispatch, item.tailUuid, currentLabel)
305 <Typography noWrap color="default">
312 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
313 <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
317 export const ResourceLinkTail = connect(
318 (state: RootState, props: { uuid: string }) => {
319 const resource = getResource<LinkResource>(props.uuid)(state.resources);
321 item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
323 })((props: { item: any } & DispatchProp<any>) =>
324 renderLinkTail(props.dispatch, props.item));
326 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
327 renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
329 export const ResourceLinkHead = connect(
330 (state: RootState, props: { uuid: string }) => {
331 const resource = getResource<LinkResource>(props.uuid)(state.resources);
333 item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
335 })((props: { item: any } & DispatchProp<any>) =>
336 renderLinkHead(props.dispatch, props.item));
338 export const ResourceLinkUuid = connect(
339 (state: RootState, props: { uuid: string }) => {
340 const resource = getResource<LinkResource>(props.uuid)(state.resources);
341 return resource || { uuid: '' };
345 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
349 <Tooltip title="Run process">
350 <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
358 export const ResourceRunProcess = connect(
359 (state: RootState, props: { uuid: string }) => {
360 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
362 uuid: resource ? resource.uuid : ''
364 })((props: { uuid: string } & DispatchProp<any>) =>
365 resourceRunProcess(props.dispatch, props.uuid));
367 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
368 if (ownerUuid === getPublicUuid(uuidPrefix)) {
369 return renderStatus(WorkflowStatus.PUBLIC);
371 return renderStatus(WorkflowStatus.PRIVATE);
375 const renderStatus = (status: string) =>
376 <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
378 export const ResourceWorkflowStatus = connect(
379 (state: RootState, props: { uuid: string }) => {
380 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
381 const uuidPrefix = getUuidPrefix(state);
383 ownerUuid: resource ? resource.ownerUuid : '',
386 })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
388 export const ResourceLastModifiedDate = connect(
389 (state: RootState, props: { uuid: string }) => {
390 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
391 return { date: resource ? resource.modifiedAt : '' };
392 })((props: { date: string }) => renderDate(props.date));
394 export const ResourceCreatedAtDate = connect(
395 (state: RootState, props: { uuid: string }) => {
396 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
397 return { date: resource ? resource.createdAt : '' };
398 })((props: { date: string }) => renderDate(props.date));
400 export const ResourceTrashDate = connect(
401 (state: RootState, props: { uuid: string }) => {
402 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
403 return { date: resource ? resource.trashAt : '' };
404 })((props: { date: string }) => renderDate(props.date));
406 export const ResourceDeleteDate = connect(
407 (state: RootState, props: { uuid: string }) => {
408 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
409 return { date: resource ? resource.deleteAt : '' };
410 })((props: { date: string }) => renderDate(props.date));
412 export const renderFileSize = (fileSize?: number) =>
413 <Typography noWrap style={{ minWidth: '45px' }}>
414 {formatFileSize(fileSize)}
417 export const ResourceFileSize = connect(
418 (state: RootState, props: { uuid: string }) => {
419 const resource = getResource<CollectionResource>(props.uuid)(state.resources);
420 return { fileSize: resource ? resource.fileSizeTotal : 0 };
421 })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
423 const renderOwner = (owner: string) =>
428 export const ResourceOwner = connect(
429 (state: RootState, props: { uuid: string }) => {
430 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
431 return { owner: resource ? resource.ownerUuid : '' };
432 })((props: { owner: string }) => renderOwner(props.owner));
434 export const ResourceOwnerName = connect(
435 (state: RootState, props: { uuid: string }) => {
436 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
437 const ownerNameState = state.ownerName;
438 const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
439 return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
440 })((props: { owner: string }) => renderOwner(props.owner));
442 export const ResourceOwnerWithName =
445 (state: RootState, props: { uuid: string }) => {
447 const resource = getResource<GroupContentsResource & UserResource>(props.uuid)(state.resources);
450 ownerName = getUserFullname(resource as User) || (resource as GroupContentsResource).name;
453 return { uuid: props.uuid, ownerName };
455 withStyles({}, { withTheme: true }))
456 ((props: { uuid: string, ownerName: string, dispatch: Dispatch, theme: ArvadosTheme }) => {
457 const { uuid, ownerName, dispatch, theme } = props;
459 if (ownerName === '') {
460 dispatch<any>(loadResource(uuid, false));
461 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
466 return <Typography style={{ color: theme.palette.primary.main }} inline noWrap>
471 const renderType = (type: string, subtype: string) =>
473 {resourceLabel(type, subtype)}
476 export const ResourceType = connect(
477 (state: RootState, props: { uuid: string }) => {
478 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
479 return { type: resource ? resource.kind : '', subtype: resource && resource.kind === ResourceKind.GROUP ? resource.groupClass : '' };
480 })((props: { type: string, subtype: string }) => renderType(props.type, props.subtype));
482 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
483 return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
484 })((props: { resource: GroupContentsResource }) =>
485 (props.resource && props.resource.kind === ResourceKind.COLLECTION)
486 ? <CollectionStatus uuid={props.resource.uuid} />
487 : <ProcessStatus uuid={props.resource.uuid} />
490 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
491 return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
492 })((props: { collection: CollectionResource }) =>
493 (props.collection.uuid !== props.collection.currentVersionUuid)
494 ? <Typography>version {props.collection.version}</Typography>
495 : <Typography>head version</Typography>
498 export const ProcessStatus = compose(
499 connect((state: RootState, props: { uuid: string }) => {
500 return { process: getProcess(props.uuid)(state.resources) };
502 withStyles({}, { withTheme: true }))
503 ((props: { process?: Process, theme: ArvadosTheme }) => {
504 const status = props.process ? getProcessStatus(props.process) : "-";
507 style={{ color: getProcessStatusColor(status, props.theme) }} >
512 export const ProcessStartDate = connect(
513 (state: RootState, props: { uuid: string }) => {
514 const process = getProcess(props.uuid)(state.resources);
515 return { date: (process && process.container) ? process.container.startedAt : '' };
516 })((props: { date: string }) => renderDate(props.date));
518 export const renderRunTime = (time: number) =>
519 <Typography noWrap style={{ minWidth: '45px' }}>
520 {formatTime(time, true)}
523 interface ContainerRunTimeProps {
527 interface ContainerRunTimeState {
531 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
532 return { process: getProcess(props.uuid)(state.resources) };
533 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
536 constructor(props: ContainerRunTimeProps) {
538 this.state = { runtime: this.getRuntime() };
542 return this.props.process ? getProcessRuntime(this.props.process) : 0;
546 this.setState({ runtime: this.getRuntime() });
549 componentDidMount() {
550 this.timer = setInterval(this.updateRuntime.bind(this), 5000);
553 componentWillUnmount() {
554 clearInterval(this.timer);
558 return renderRunTime(this.state.runtime);