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',
30 CANCELLING = 'Cancelling',
33 export const getProcess = (uuid: string) => (resources: ResourcesState): Process | undefined => {
34 if (extractUuidKind(uuid) === ResourceKind.CONTAINER_REQUEST) {
35 const containerRequest = getResource<ContainerRequestResource>(uuid)(resources);
36 if (containerRequest) {
37 if (containerRequest.containerUuid) {
38 const container = getResource<ContainerResource>(containerRequest.containerUuid)(resources);
40 return { containerRequest, container };
43 return { containerRequest };
49 export const getSubprocesses = (uuid: string) => (resources: ResourcesState) => {
50 const process = getProcess(uuid)(resources);
51 if (process && process.container) {
52 const containerRequests = filterResources(isSubprocess(process.container.uuid))(resources) as ContainerRequestResource[];
53 return containerRequests.reduce((subprocesses, { uuid }) => {
54 const process = getProcess(uuid)(resources);
56 ? [...subprocesses, process]
63 export const getProcessRuntime = ({ container }: Process) => {
65 if (container.startedAt === null) {
68 if (container.finishedAt === null) {
70 return new Date().getTime() - new Date(container.startedAt).getTime();
72 return getTimeDiff(container.finishedAt, container.startedAt);
79 export const getProcessStatusStyles = (status: string, theme: ArvadosTheme): React.CSSProperties => {
80 let color = theme.customs.colors.grey500;
83 case ProcessStatus.RUNNING:
84 color = theme.customs.colors.green800;
87 case ProcessStatus.COMPLETED:
88 case ProcessStatus.REUSED:
89 color = theme.customs.colors.green800;
91 case ProcessStatus.WARNING:
92 color = theme.customs.colors.green800;
95 case ProcessStatus.FAILING:
96 color = theme.customs.colors.red900;
99 case ProcessStatus.CANCELLING:
100 color = theme.customs.colors.red900;
103 case ProcessStatus.CANCELLED:
104 case ProcessStatus.FAILED:
105 color = theme.customs.colors.red900;
107 case ProcessStatus.QUEUED:
108 color = theme.customs.colors.grey600;
112 color = theme.customs.colors.grey600;
116 // Using color and running we build the text, border, and background style properties
118 // Set background color when not running, otherwise use white
119 backgroundColor: running ? theme.palette.common.white : color,
120 // Set text color to status color when running, else use white text for solid button
121 color: running ? color : theme.palette.common.white,
122 // Set border color when running, else omit the style entirely
123 ...(running ? { border: `2px solid ${color}` } : {}),
127 export const getProcessStatus = ({ containerRequest, container }: Process): ProcessStatus => {
129 case containerRequest.containerUuid && !container:
130 return ProcessStatus.UNKNOWN;
132 case containerRequest.state === ContainerRequestState.FINAL &&
133 container?.state !== ContainerState.COMPLETE:
134 // Request was finalized before its container started (or the
135 // container was cancelled)
136 return ProcessStatus.CANCELLED;
138 case containerRequest.state === ContainerRequestState.UNCOMMITTED:
139 return ProcessStatus.DRAFT;
141 case container && container.state === ContainerState.COMPLETE:
142 if (container?.exitCode === 0) {
143 if (containerRequest && container.finishedAt) {
144 // don't compare on createdAt because the container can
145 // have a slightly earlier creation time when it is created
146 // in the same transaction as the container request.
147 // use finishedAt because most people will assume "reused" means
148 // no additional work needed to be done, it's possible
149 // to share a running container but calling it "reused" in that case
150 // is more likely to just be confusing.
151 const finishedAt = new Date(container.finishedAt).getTime();
152 const createdAt = new Date(containerRequest.createdAt).getTime();
153 if (finishedAt < createdAt) {
154 return ProcessStatus.REUSED;
157 return ProcessStatus.COMPLETED;
159 return ProcessStatus.FAILED;
161 case container?.state === ContainerState.CANCELLED:
162 return ProcessStatus.CANCELLED;
164 case container?.state === ContainerState.QUEUED ||
165 container?.state === ContainerState.LOCKED:
166 if (containerRequest.priority === 0) {
167 return ProcessStatus.ONHOLD;
169 return ProcessStatus.QUEUED;
171 case container?.state === ContainerState.RUNNING:
172 if (container?.priority === 0) {
173 return ProcessStatus.CANCELLING;
175 if (!!container?.runtimeStatus.error) {
176 return ProcessStatus.FAILING;
178 if (!!container?.runtimeStatus.warning) {
179 return ProcessStatus.WARNING;
181 return ProcessStatus.RUNNING;
184 return ProcessStatus.UNKNOWN;
188 export const isProcessRunnable = ({ containerRequest }: Process): boolean => (
189 containerRequest.state === ContainerRequestState.UNCOMMITTED
192 export const isProcessResumable = ({ containerRequest, container }: Process): boolean => (
193 containerRequest.state === ContainerRequestState.COMMITTED &&
194 containerRequest.priority === 0 &&
195 // Don't show run button when container is present & running or cancelled
196 !(container && (container.state === ContainerState.RUNNING ||
197 container.state === ContainerState.CANCELLED ||
198 container.state === ContainerState.COMPLETE))
201 export const isProcessCancelable = ({ containerRequest, container }: Process): boolean => (
202 containerRequest.priority !== null &&
203 containerRequest.priority > 0 &&
204 container !== undefined &&
205 (container.state === ContainerState.QUEUED ||
206 container.state === ContainerState.LOCKED ||
207 container.state === ContainerState.RUNNING)
210 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
211 resource.kind === ResourceKind.CONTAINER_REQUEST
212 && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;