1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ContainerRequestResource, ContainerRequestState } from '../../models/container-request';
6 import { ContainerResource, ContainerState } from '../../models/container';
7 import { ResourcesState, getResource } from 'store/resources/resources';
8 import { filterResources } from '../resources/resources';
9 import { ResourceKind, Resource, extractUuidKind } from 'models/resource';
10 import { getTimeDiff } from 'common/formatters';
11 import { ArvadosTheme } from 'common/custom-theme';
13 export interface Process {
14 containerRequest: ContainerRequestResource;
15 container?: ContainerResource;
18 export enum ProcessStatus {
19 CANCELLED = 'Cancelled',
20 COMPLETED = 'Completed',
31 export const getProcess = (uuid: string) => (resources: ResourcesState): Process | undefined => {
32 if (extractUuidKind(uuid) === ResourceKind.CONTAINER_REQUEST) {
33 const containerRequest = getResource<ContainerRequestResource>(uuid)(resources);
34 if (containerRequest) {
35 if (containerRequest.containerUuid) {
36 const container = getResource<ContainerResource>(containerRequest.containerUuid)(resources);
38 return { containerRequest, container };
41 return { containerRequest };
47 export const getSubprocesses = (uuid: string) => (resources: ResourcesState) => {
48 const process = getProcess(uuid)(resources);
49 if (process && process.container) {
50 const containerRequests = filterResources(isSubprocess(process.container.uuid))(resources) as ContainerRequestResource[];
51 return containerRequests.reduce((subprocesses, { uuid }) => {
52 const process = getProcess(uuid)(resources);
54 ? [...subprocesses, process]
61 export const getProcessRuntime = ({ container }: Process) => {
63 if (container.startedAt === null) {
66 if (container.finishedAt === null) {
68 return new Date().getTime() - new Date(container.startedAt).getTime();
70 return getTimeDiff(container.finishedAt, container.startedAt);
76 export const getProcessStatusColor = (status: string, { customs }: ArvadosTheme) => {
78 case ProcessStatus.RUNNING:
79 return customs.colors.blue500;
80 case ProcessStatus.COMPLETED:
81 return customs.colors.green700;
82 case ProcessStatus.WARNING:
83 return customs.colors.yellow700;
84 case ProcessStatus.FAILING:
85 case ProcessStatus.CANCELLED:
86 case ProcessStatus.FAILED:
87 return customs.colors.red900;
89 return customs.colors.grey500;
93 export const getProcessStatus = ({ containerRequest, container }: Process): ProcessStatus => {
95 case containerRequest.state === ContainerRequestState.UNCOMMITTED:
96 return ProcessStatus.DRAFT;
98 case containerRequest.priority === 0:
99 return ProcessStatus.ONHOLD;
101 case container && container.state === ContainerState.COMPLETE && container.exitCode === 0:
102 return ProcessStatus.COMPLETED;
104 case container && container.state === ContainerState.CANCELLED:
105 return ProcessStatus.CANCELLED;
107 case container && (container.state === ContainerState.QUEUED ||
108 container.state === ContainerState.LOCKED):
109 return ProcessStatus.QUEUED;
111 case container && container.state === ContainerState.RUNNING &&
112 !!container.runtimeStatus.error:
113 return ProcessStatus.FAILING;
115 case container && container.state === ContainerState.RUNNING &&
116 !!container.runtimeStatus.warning:
117 return ProcessStatus.WARNING;
119 case container && container.state === ContainerState.RUNNING:
120 return ProcessStatus.RUNNING;
122 case container && container.state === ContainerState.COMPLETE && container.exitCode !== 0:
123 return ProcessStatus.FAILED;
126 return ProcessStatus.UNKNOWN;
130 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
131 resource.kind === ResourceKind.CONTAINER_REQUEST
132 && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;