Merge branch 'master'
[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 } from '../../models/container-request';
6 import { ContainerResource } from '../../models/container';
7 import { ResourcesState, getResource } from '~/store/resources/resources';
8 import { filterResources } from '../resources/resources';
9 import { ResourceKind, Resource } from '~/models/resource';
10 import { getTimeDiff } from '~/common/formatters';
11 import { SubprocessesStatus } from '~/views/process-panel/process-subprocesses-card';
12 import { ArvadosTheme } from '~/common/custom-theme';
13
14 export interface Process {
15     containerRequest: ContainerRequestResource;
16     container?: ContainerResource;
17 }
18
19 export const getProcess = (uuid: string) => (resources: ResourcesState): Process | undefined => {
20     const containerRequest = getResource<ContainerRequestResource>(uuid)(resources);
21     if (containerRequest) {
22         if (containerRequest.containerUuid) {
23             const container = getResource<ContainerResource>(containerRequest.containerUuid)(resources);
24             if (container) {
25                 return { containerRequest, container };
26             }
27         }
28         return { containerRequest };
29     }
30     return;
31 };
32
33 export const getSubprocesses = (uuid: string) => (resources: ResourcesState) => {
34     const process = getProcess(uuid)(resources);
35     if (process && process.container) {
36         const containerRequests = filterResources(isSubprocess(process.container.uuid))(resources) as ContainerRequestResource[];
37         return containerRequests.reduce((subprocesses, { uuid }) => {
38             const process = getProcess(uuid)(resources);
39             return process
40                 ? [...subprocesses, process]
41                 : subprocesses;
42         }, []);
43     }
44     return [];
45 };
46
47 export const getProcessRuntime = ({ container }: Process) =>
48     container
49         ? getTimeDiff(container.finishedAt || '', container.startedAt || '')
50         : 0;
51
52 export const getProcessStatusColor = (status: string, { customs }: ArvadosTheme) => {
53     switch (status) {
54         case SubprocessesStatus.COMPLETED:
55             return customs.colors.green700;
56         case SubprocessesStatus.CANCELED:
57             return customs.colors.red900;
58         case SubprocessesStatus.QUEUED:
59             return customs.colors.grey500;
60         case SubprocessesStatus.FAILED:
61             return customs.colors.red900;
62         case SubprocessesStatus.ACTIVE:
63             return customs.colors.blue500;
64         default:
65             return customs.colors.grey500;
66     }
67 };
68
69 export const getProcessStatus = (process: Process) =>
70     process.container
71         ? process.container.state
72         : process.containerRequest.state;
73
74 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
75     resource.kind === ResourceKind.CONTAINER_REQUEST
76     && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;
77