15672: Cleans & simplifies test code.
[arvados-workbench2.git] / src / store / processes / process.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
12
13 export interface Process {
14     containerRequest: ContainerRequestResource;
15     container?: ContainerResource;
16 }
17
18 export enum ProcessStatus {
19     CANCELLED = 'Cancelled',
20     COMPLETED = 'Completed',
21     DRAFT = 'Draft',
22     FAILED = 'Failed',
23     LOCKED = 'Locked',
24     QUEUED = 'Queued',
25     RUNNING = 'Running',
26     UNKNOWN = 'Unknown',
27 }
28
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);
35                 if (container) {
36                     return { containerRequest, container };
37                 }
38             }
39             return { containerRequest };
40         }
41     }
42     return;
43 };
44
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);
51             return process
52                 ? [...subprocesses, process]
53                 : subprocesses;
54         }, []);
55     }
56     return [];
57 };
58
59 export const getProcessRuntime = ({ container }: Process) => {
60     if (container) {
61         if (container.startedAt === null) {
62             return 0;
63         }
64         if (container.finishedAt === null) {
65             // Count it from now
66             return new Date().getTime() - new Date(container.startedAt).getTime();
67         }
68         return getTimeDiff(container.finishedAt, container.startedAt);
69     } else {
70         return 0;
71     }
72 };
73
74 export const getProcessStatusColor = (status: string, { customs, palette }: ArvadosTheme) => {
75     switch (status) {
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;
83         default:
84             return palette.grey["500"];
85     }
86 };
87
88 export const getProcessStatus = ({ containerRequest, container }: Process): ProcessStatus => {
89     switch (true) {
90         case containerRequest.state === ContainerRequestState.UNCOMMITTED:
91             return ProcessStatus.DRAFT;
92
93         case containerRequest.priority === 0:
94         case container && container.state === ContainerState.CANCELLED:
95             return ProcessStatus.CANCELLED;
96
97         case container && container.state === ContainerState.QUEUED:
98             return ProcessStatus.QUEUED;
99
100         case container && container.state === ContainerState.LOCKED:
101             return ProcessStatus.LOCKED;
102
103         case container && container.state === ContainerState.RUNNING:
104             return ProcessStatus.RUNNING;
105
106         case container && container.state === ContainerState.COMPLETE && container.exitCode === 0:
107             return ProcessStatus.COMPLETED;
108
109         case container && container.state === ContainerState.COMPLETE && container.exitCode !== 0:
110             return ProcessStatus.FAILED;
111
112         default:
113             return ProcessStatus.UNKNOWN;
114     }
115 };
116
117 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
118     resource.kind === ResourceKind.CONTAINER_REQUEST
119     && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;