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 return customs.colors.orange;
86 case ProcessStatus.CANCELLED:
87 case ProcessStatus.FAILED:
88 return customs.colors.red900;
90 return customs.colors.grey500;
94 export const getProcessStatus = ({ containerRequest, container }: Process): ProcessStatus => {
96 case containerRequest.state === ContainerRequestState.FINAL &&
97 container?.state !== ContainerState.COMPLETE:
98 // Request was finalized before its container started (or the
99 // container was cancelled)
100 return ProcessStatus.CANCELLED;
102 case containerRequest.state === ContainerRequestState.UNCOMMITTED:
103 return ProcessStatus.DRAFT;
105 case container?.state === ContainerState.COMPLETE:
106 if (container?.exitCode === 0) {
107 return ProcessStatus.COMPLETED;
109 return ProcessStatus.FAILED;
111 case container?.state === ContainerState.CANCELLED:
112 return ProcessStatus.CANCELLED;
114 case container?.state === ContainerState.QUEUED ||
115 container?.state === ContainerState.LOCKED:
116 if (containerRequest.priority === 0) {
117 return ProcessStatus.ONHOLD;
119 return ProcessStatus.QUEUED;
121 case container?.state === ContainerState.RUNNING:
122 if (!!container?.runtimeStatus.error) {
123 return ProcessStatus.FAILING;
125 if (!!container?.runtimeStatus.warning) {
126 return ProcessStatus.WARNING;
128 return ProcessStatus.RUNNING;
131 return ProcessStatus.UNKNOWN;
135 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
136 resource.kind === ResourceKind.CONTAINER_REQUEST
137 && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;