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',
29 export const getProcess = (uuid: string) => (resources: ResourcesState): Process | undefined => {
30 if (extractUuidKind(uuid) === ResourceKind.CONTAINER_REQUEST) {
31 const containerRequest = getResource<ContainerRequestResource>(uuid)(resources);
32 if (containerRequest) {
33 if (containerRequest.containerUuid) {
34 const container = getResource<ContainerResource>(containerRequest.containerUuid)(resources);
36 return { containerRequest, container };
39 return { containerRequest };
45 export const getSubprocesses = (uuid: string) => (resources: ResourcesState) => {
46 const process = getProcess(uuid)(resources);
47 if (process && process.container) {
48 const containerRequests = filterResources(isSubprocess(process.container.uuid))(resources) as ContainerRequestResource[];
49 return containerRequests.reduce((subprocesses, { uuid }) => {
50 const process = getProcess(uuid)(resources);
52 ? [...subprocesses, process]
59 export const getProcessRuntime = ({ container }: Process) => {
61 if (container.startedAt === null) {
64 if (container.finishedAt === null) {
66 return new Date().getTime() - new Date(container.startedAt).getTime();
68 return getTimeDiff(container.finishedAt, container.startedAt);
74 export const getProcessStatusColor = (status: string, { customs, palette }: ArvadosTheme) => {
76 case ProcessStatus.RUNNING:
77 return customs.colors.blue500;
78 case ProcessStatus.COMPLETED:
79 return customs.colors.green700;
80 case ProcessStatus.CANCELLED:
81 case ProcessStatus.FAILED:
82 return customs.colors.red900;
84 return palette.grey["500"];
88 export const getProcessStatus = ({ containerRequest, container }: Process): ProcessStatus => {
90 case containerRequest.state === ContainerRequestState.UNCOMMITTED:
91 return ProcessStatus.DRAFT;
93 case containerRequest.priority === 0:
94 case container && container.state === ContainerState.CANCELLED:
95 return ProcessStatus.CANCELLED;
97 case container && container.state === ContainerState.QUEUED:
98 return ProcessStatus.QUEUED;
100 case container && container.state === ContainerState.LOCKED:
101 return ProcessStatus.LOCKED;
103 case container && container.state === ContainerState.RUNNING:
104 return ProcessStatus.RUNNING;
106 case container && container.state === ContainerState.COMPLETE && container.exitCode === 0:
107 return ProcessStatus.COMPLETED;
109 case container && container.state === ContainerState.COMPLETE && container.exitCode !== 0:
110 return ProcessStatus.FAILED;
113 return ProcessStatus.UNKNOWN;
117 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
118 resource.kind === ResourceKind.CONTAINER_REQUEST
119 && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;