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);
77 export const getProcessStatusStyles = (status: string, theme: ArvadosTheme): React.CSSProperties => {
78 let color = theme.customs.colors.grey500;
81 case ProcessStatus.RUNNING:
82 color = theme.customs.colors.green800;
85 case ProcessStatus.COMPLETED:
86 color = theme.customs.colors.green800;
88 case ProcessStatus.WARNING:
89 color = theme.customs.colors.green800;
92 case ProcessStatus.FAILING:
93 color = theme.customs.colors.red900;
96 case ProcessStatus.CANCELLED:
97 case ProcessStatus.FAILED:
98 color = theme.customs.colors.red900;
100 case ProcessStatus.QUEUED:
101 color = theme.customs.colors.grey600;
105 color = theme.customs.colors.grey600;
109 // Using color and running we build the text, border, and background style properties
111 // Set background color when not running, otherwise use white
112 backgroundColor: running ? theme.palette.common.white : color,
113 // Set text color to status color when running, else use white text for solid button
114 color: running ? color : theme.palette.common.white,
115 // Set border color when running, else omit the style entirely
116 ...(running ? {border: `2px solid ${color}`} : {}),
120 export const getProcessStatus = ({ containerRequest, container }: Process): ProcessStatus => {
122 case containerRequest.state === ContainerRequestState.FINAL &&
123 container?.state !== ContainerState.COMPLETE:
124 // Request was finalized before its container started (or the
125 // container was cancelled)
126 return ProcessStatus.CANCELLED;
128 case containerRequest.state === ContainerRequestState.UNCOMMITTED:
129 return ProcessStatus.DRAFT;
131 case container?.state === ContainerState.COMPLETE:
132 if (container?.exitCode === 0) {
133 return ProcessStatus.COMPLETED;
135 return ProcessStatus.FAILED;
137 case container?.state === ContainerState.CANCELLED:
138 return ProcessStatus.CANCELLED;
140 case container?.state === ContainerState.QUEUED ||
141 container?.state === ContainerState.LOCKED:
142 if (containerRequest.priority === 0) {
143 return ProcessStatus.ONHOLD;
145 return ProcessStatus.QUEUED;
147 case container?.state === ContainerState.RUNNING:
148 if (!!container?.runtimeStatus.error) {
149 return ProcessStatus.FAILING;
151 if (!!container?.runtimeStatus.warning) {
152 return ProcessStatus.WARNING;
154 return ProcessStatus.RUNNING;
157 return ProcessStatus.UNKNOWN;
161 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
162 resource.kind === ResourceKind.CONTAINER_REQUEST
163 && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;