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 { 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';
31 const renderName = (dispatch: Dispatch, item: GroupContentsResource) =>
32 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
37 <Typography color="primary" style={{ width: 'auto', cursor: 'pointer' }} onClick={() => dispatch<any>(navigateTo(item.uuid))}>
38 {item.kind === ResourceKind.PROJECT || item.kind === ResourceKind.COLLECTION
39 ? <IllegalNamingWarning name={item.name} />
45 <Typography variant="caption">
46 <FavoriteStar resourceUuid={item.uuid} />
47 <PublicFavoriteStar resourceUuid={item.uuid} />
52 export const ResourceName = connect(
53 (state: RootState, props: { uuid: string }) => {
54 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
56 })((resource: GroupContentsResource & DispatchProp<any>) => renderName(resource.dispatch, resource));
58 const renderIcon = (item: GroupContentsResource) => {
60 case ResourceKind.PROJECT:
61 return <ProjectIcon />;
62 case ResourceKind.COLLECTION:
63 if (item.uuid === item.currentVersionUuid) {
64 return <CollectionIcon />;
66 return <CollectionOldVersionIcon />;
67 case ResourceKind.PROCESS:
68 return <ProcessIcon />;
69 case ResourceKind.WORKFLOW:
70 return <WorkflowIcon />;
72 return <DefaultIcon />;
76 const renderDate = (date?: string) => {
77 return <Typography noWrap style={{ minWidth: '100px' }}>{formatDate(date)}</Typography>;
80 const renderWorkflowName = (item: WorkflowResource) =>
81 <Grid container alignItems="center" wrap="nowrap" spacing={16}>
86 <Typography color="primary" style={{ width: '100px' }}>
92 export const ResourceWorkflowName = connect(
93 (state: RootState, props: { uuid: string }) => {
94 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
96 })(renderWorkflowName);
98 const getPublicUuid = (uuidPrefix: string) => {
99 return `${uuidPrefix}-tpzed-anonymouspublic`;
102 const resourceShare = (dispatch: Dispatch, uuidPrefix: string, ownerUuid?: string, uuid?: string) => {
103 const isPublic = ownerUuid === getPublicUuid(uuidPrefix);
106 {!isPublic && uuid &&
107 <Tooltip title="Share">
108 <IconButton onClick={() => dispatch<any>(openSharingDialog(uuid))}>
117 export const ResourceShare = connect(
118 (state: RootState, props: { uuid: string }) => {
119 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
120 const uuidPrefix = getUuidPrefix(state);
122 uuid: resource ? resource.uuid : '',
123 ownerUuid: resource ? resource.ownerUuid : '',
126 })((props: { ownerUuid?: string, uuidPrefix: string, uuid?: string } & DispatchProp<any>) =>
127 resourceShare(props.dispatch, props.uuidPrefix, props.ownerUuid, props.uuid));
129 const renderFirstName = (item: { firstName: string }) => {
130 return <Typography noWrap>{item.firstName}</Typography>;
134 export const ResourceFirstName = connect(
135 (state: RootState, props: { uuid: string }) => {
136 const resource = getResource<UserResource>(props.uuid)(state.resources);
137 return resource || { firstName: '' };
140 const renderLastName = (item: { lastName: string }) =>
141 <Typography noWrap>{item.lastName}</Typography>;
143 export const ResourceLastName = connect(
144 (state: RootState, props: { uuid: string }) => {
145 const resource = getResource<UserResource>(props.uuid)(state.resources);
146 return resource || { lastName: '' };
149 const renderUuid = (item: { uuid: string }) =>
150 <Typography noWrap>{item.uuid}</Typography>;
152 export const ResourceUuid = connect(
153 (state: RootState, props: { uuid: string }) => {
154 const resource = getResource<UserResource>(props.uuid)(state.resources);
155 return resource || { uuid: '' };
158 const renderEmail = (item: { email: string }) =>
159 <Typography noWrap>{item.email}</Typography>;
161 export const ResourceEmail = connect(
162 (state: RootState, props: { uuid: string }) => {
163 const resource = getResource<UserResource>(props.uuid)(state.resources);
164 return resource || { email: '' };
167 const renderIsActive = (props: { uuid: string, isActive: boolean, toggleIsActive: (uuid: string) => void }) =>
170 checked={props.isActive}
171 onClick={() => props.toggleIsActive(props.uuid)} />;
173 export const ResourceIsActive = connect(
174 (state: RootState, props: { uuid: string }) => {
175 const resource = getResource<UserResource>(props.uuid)(state.resources);
176 return resource || { isActive: false };
177 }, { toggleIsActive }
180 const renderIsAdmin = (props: { uuid: string, isAdmin: boolean, toggleIsAdmin: (uuid: string) => void }) =>
183 checked={props.isAdmin}
184 onClick={() => props.toggleIsAdmin(props.uuid)} />;
186 export const ResourceIsAdmin = connect(
187 (state: RootState, props: { uuid: string }) => {
188 const resource = getResource<UserResource>(props.uuid)(state.resources);
189 return resource || { isAdmin: false };
193 const renderUsername = (item: { username: string }) =>
194 <Typography noWrap>{item.username}</Typography>;
196 export const ResourceUsername = connect(
197 (state: RootState, props: { uuid: string }) => {
198 const resource = getResource<UserResource>(props.uuid)(state.resources);
199 return resource || { username: '' };
203 const renderCommonData = (data: string) =>
204 <Typography noWrap>{data}</Typography>;
206 const renderCommonDate = (date: string) =>
207 <Typography noWrap>{formatDate(date)}</Typography>;
209 export const CommonUuid = withResourceData('uuid', renderCommonData);
211 // Api Client Authorizations
212 export const TokenApiClientId = withResourceData('apiClientId', renderCommonData);
214 export const TokenApiToken = withResourceData('apiToken', renderCommonData);
216 export const TokenCreatedByIpAddress = withResourceData('createdByIpAddress', renderCommonDate);
218 export const TokenDefaultOwnerUuid = withResourceData('defaultOwnerUuid', renderCommonData);
220 export const TokenExpiresAt = withResourceData('expiresAt', renderCommonDate);
222 export const TokenLastUsedAt = withResourceData('lastUsedAt', renderCommonDate);
224 export const TokenLastUsedByIpAddress = withResourceData('lastUsedByIpAddress', renderCommonData);
226 export const TokenScopes = withResourceData('scopes', renderCommonData);
228 export const TokenUserId = withResourceData('userId', renderCommonData);
230 // Compute Node Resources
231 const renderNodeInfo = (data: string) => {
232 return <Typography>{JSON.stringify(data, null, 4)}</Typography>;
235 const clusterColors = [
243 export const ResourceCluster = (props: { uuid: string }) => {
244 const CLUSTER_ID_LENGTH = 5;
245 const pos = props.uuid.length > CLUSTER_ID_LENGTH ? props.uuid.indexOf('-') : 5;
246 const clusterId = pos >= CLUSTER_ID_LENGTH ? props.uuid.substr(0, pos) : '';
247 const ci = pos >= CLUSTER_ID_LENGTH ? (((((
248 (props.uuid.charCodeAt(0) * props.uuid.charCodeAt(1))
249 + props.uuid.charCodeAt(2))
250 * props.uuid.charCodeAt(3))
251 + props.uuid.charCodeAt(4))) % clusterColors.length) : 0;
252 return <span style={{
253 backgroundColor: clusterColors[ci][0],
254 color: clusterColors[ci][1],
257 }}>{clusterId}</span>;
260 export const ComputeNodeInfo = withResourceData('info', renderNodeInfo);
262 export const ComputeNodeDomain = withResourceData('domain', renderCommonData);
264 export const ComputeNodeFirstPingAt = withResourceData('firstPingAt', renderCommonDate);
266 export const ComputeNodeHostname = withResourceData('hostname', renderCommonData);
268 export const ComputeNodeIpAddress = withResourceData('ipAddress', renderCommonData);
270 export const ComputeNodeJobUuid = withResourceData('jobUuid', renderCommonData);
272 export const ComputeNodeLastPingAt = withResourceData('lastPingAt', renderCommonDate);
275 const renderLinkName = (item: { name: string }) =>
276 <Typography noWrap>{item.name || '(none)'}</Typography>;
278 export const ResourceLinkName = connect(
279 (state: RootState, props: { uuid: string }) => {
280 const resource = getResource<LinkResource>(props.uuid)(state.resources);
281 return resource || { name: '' };
284 const renderLinkClass = (item: { linkClass: string }) =>
285 <Typography noWrap>{item.linkClass}</Typography>;
287 export const ResourceLinkClass = connect(
288 (state: RootState, props: { uuid: string }) => {
289 const resource = getResource<LinkResource>(props.uuid)(state.resources);
290 return resource || { linkClass: '' };
293 const renderLinkTail = (dispatch: Dispatch, item: { uuid: string, tailUuid: string, tailKind: string }) => {
294 const currentLabel = resourceLabel(item.tailKind);
295 const isUnknow = currentLabel === "Unknown";
298 renderLink(dispatch, item.tailUuid, currentLabel)
300 <Typography noWrap color="default">
307 const renderLink = (dispatch: Dispatch, uuid: string, label: string) =>
308 <Typography noWrap color="primary" style={{ 'cursor': 'pointer' }} onClick={() => dispatch<any>(navigateTo(uuid))}>
312 export const ResourceLinkTail = connect(
313 (state: RootState, props: { uuid: string }) => {
314 const resource = getResource<LinkResource>(props.uuid)(state.resources);
316 item: resource || { uuid: '', tailUuid: '', tailKind: ResourceKind.NONE }
318 })((props: { item: any } & DispatchProp<any>) =>
319 renderLinkTail(props.dispatch, props.item));
321 const renderLinkHead = (dispatch: Dispatch, item: { uuid: string, headUuid: string, headKind: ResourceKind }) =>
322 renderLink(dispatch, item.headUuid, resourceLabel(item.headKind));
324 export const ResourceLinkHead = connect(
325 (state: RootState, props: { uuid: string }) => {
326 const resource = getResource<LinkResource>(props.uuid)(state.resources);
328 item: resource || { uuid: '', headUuid: '', headKind: ResourceKind.NONE }
330 })((props: { item: any } & DispatchProp<any>) =>
331 renderLinkHead(props.dispatch, props.item));
333 export const ResourceLinkUuid = connect(
334 (state: RootState, props: { uuid: string }) => {
335 const resource = getResource<LinkResource>(props.uuid)(state.resources);
336 return resource || { uuid: '' };
340 const resourceRunProcess = (dispatch: Dispatch, uuid: string) => {
344 <Tooltip title="Run process">
345 <IconButton onClick={() => dispatch<any>(openRunProcess(uuid))}>
353 export const ResourceRunProcess = connect(
354 (state: RootState, props: { uuid: string }) => {
355 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
357 uuid: resource ? resource.uuid : ''
359 })((props: { uuid: string } & DispatchProp<any>) =>
360 resourceRunProcess(props.dispatch, props.uuid));
362 const renderWorkflowStatus = (uuidPrefix: string, ownerUuid?: string) => {
363 if (ownerUuid === getPublicUuid(uuidPrefix)) {
364 return renderStatus(WorkflowStatus.PUBLIC);
366 return renderStatus(WorkflowStatus.PRIVATE);
370 const renderStatus = (status: string) =>
371 <Typography noWrap style={{ width: '60px' }}>{status}</Typography>;
373 export const ResourceWorkflowStatus = connect(
374 (state: RootState, props: { uuid: string }) => {
375 const resource = getResource<WorkflowResource>(props.uuid)(state.resources);
376 const uuidPrefix = getUuidPrefix(state);
378 ownerUuid: resource ? resource.ownerUuid : '',
381 })((props: { ownerUuid?: string, uuidPrefix: string }) => renderWorkflowStatus(props.uuidPrefix, props.ownerUuid));
383 export const ResourceLastModifiedDate = connect(
384 (state: RootState, props: { uuid: string }) => {
385 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
386 return { date: resource ? resource.modifiedAt : '' };
387 })((props: { date: string }) => renderDate(props.date));
389 export const ResourceCreatedAtDate = connect(
390 (state: RootState, props: { uuid: string }) => {
391 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
392 return { date: resource ? resource.createdAt : '' };
393 })((props: { date: string }) => renderDate(props.date));
395 export const ResourceTrashDate = connect(
396 (state: RootState, props: { uuid: string }) => {
397 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
398 return { date: resource ? resource.trashAt : '' };
399 })((props: { date: string }) => renderDate(props.date));
401 export const ResourceDeleteDate = connect(
402 (state: RootState, props: { uuid: string }) => {
403 const resource = getResource<TrashableResource>(props.uuid)(state.resources);
404 return { date: resource ? resource.deleteAt : '' };
405 })((props: { date: string }) => renderDate(props.date));
407 export const renderFileSize = (fileSize?: number) =>
408 <Typography noWrap style={{ minWidth: '45px' }}>
409 {formatFileSize(fileSize)}
412 export const ResourceFileSize = connect(
413 (state: RootState, props: { uuid: string }) => {
414 const resource = getResource<CollectionResource>(props.uuid)(state.resources);
415 return { fileSize: resource ? resource.fileSizeTotal : 0 };
416 })((props: { fileSize?: number }) => renderFileSize(props.fileSize));
418 const renderOwner = (owner: string) =>
423 export const ResourceOwner = connect(
424 (state: RootState, props: { uuid: string }) => {
425 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
426 return { owner: resource ? resource.ownerUuid : '' };
427 })((props: { owner: string }) => renderOwner(props.owner));
429 export const ResourceOwnerName = connect(
430 (state: RootState, props: { uuid: string }) => {
431 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
432 const ownerNameState = state.ownerName;
433 const ownerName = ownerNameState.find(it => it.uuid === resource!.ownerUuid);
434 return { owner: ownerName ? ownerName!.name : resource!.ownerUuid };
435 })((props: { owner: string }) => renderOwner(props.owner));
437 const renderType = (type: string) =>
439 {resourceLabel(type)}
442 export const ResourceType = connect(
443 (state: RootState, props: { uuid: string }) => {
444 const resource = getResource<GroupContentsResource>(props.uuid)(state.resources);
445 return { type: resource ? resource.kind : '' };
446 })((props: { type: string }) => renderType(props.type));
448 export const ResourceStatus = connect((state: RootState, props: { uuid: string }) => {
449 return { resource: getResource<GroupContentsResource>(props.uuid)(state.resources) };
450 })((props: { resource: GroupContentsResource }) =>
451 (props.resource && props.resource.kind === ResourceKind.COLLECTION)
452 ? <CollectionStatus uuid={props.resource.uuid} />
453 : <ProcessStatus uuid={props.resource.uuid} />
456 export const CollectionStatus = connect((state: RootState, props: { uuid: string }) => {
457 return { collection: getResource<CollectionResource>(props.uuid)(state.resources) };
458 })((props: { collection: CollectionResource }) =>
459 (props.collection.uuid !== props.collection.currentVersionUuid)
460 ? <Typography>version {props.collection.version}</Typography>
461 : <Typography>head version</Typography>
464 export const ProcessStatus = compose(
465 connect((state: RootState, props: { uuid: string }) => {
466 return { process: getProcess(props.uuid)(state.resources) };
468 withStyles({}, { withTheme: true }))
469 ((props: { process?: Process, theme: ArvadosTheme }) => {
470 const status = props.process ? getProcessStatus(props.process) : "-";
473 style={{ color: getProcessStatusColor(status, props.theme) }} >
478 export const ProcessStartDate = connect(
479 (state: RootState, props: { uuid: string }) => {
480 const process = getProcess(props.uuid)(state.resources);
481 return { date: ( process && process.container ) ? process.container.startedAt : '' };
482 })((props: { date: string }) => renderDate(props.date));
484 export const renderRunTime = (time: number) =>
485 <Typography noWrap style={{ minWidth: '45px' }}>
486 {formatTime(time, true)}
489 interface ContainerRunTimeProps {
493 interface ContainerRunTimeState {
497 export const ContainerRunTime = connect((state: RootState, props: { uuid: string }) => {
498 return { process: getProcess(props.uuid)(state.resources) };
499 })(class extends React.Component<ContainerRunTimeProps, ContainerRunTimeState> {
502 constructor(props: ContainerRunTimeProps) {
504 this.state = { runtime: this.getRuntime() };
508 return this.props.process ? getProcessRuntime(this.props.process) : 0;
512 this.setState({ runtime: this.getRuntime() });
515 componentDidMount() {
516 this.timer = setInterval(this.updateRuntime.bind(this), 5000);
519 componentWillUnmount() {
520 clearInterval(this.timer);
524 return renderRunTime(this.state.runtime);