cr changes
[arvados.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 { getDiffTime } from '~/common/formatters';
11
12 export interface Process {
13     containerRequest: ContainerRequestResource;
14     container?: ContainerResource;
15 }
16
17 export const getProcess = (uuid: string) => (resources: ResourcesState): Process | undefined => {
18     const containerRequest = getResource<ContainerRequestResource>(uuid)(resources);
19     if (containerRequest) {
20         if (containerRequest.containerUuid) {
21             const container = getResource<ContainerResource>(containerRequest.containerUuid)(resources);
22             if (container) {
23                 return { containerRequest, container };
24             }
25         }
26         return { containerRequest };
27     }
28     return;
29 };
30
31 export const getSubprocesses = (uuid: string) => (resources: ResourcesState) => {
32     const process = getProcess(uuid)(resources);
33     if (process && process.container) {
34         const containerRequests = filterResources(isSubprocess(process.container.uuid))(resources) as ContainerRequestResource[];
35         return containerRequests.reduce((subprocesses, { uuid }) => {
36             const process = getProcess(uuid)(resources);
37             return process
38                 ? [...subprocesses, process]
39                 : subprocesses;
40         }, []);
41     }
42     return [];
43 };
44
45 export const getProcessRuntime = (subprocess: Process) =>
46     subprocess.container
47         ? getDiffTime(subprocess.container.finishedAt || '', subprocess.container.startedAt || '')
48         : 0;
49
50 export const getProcessStatus = (process: Process) =>
51     process.container
52         ? process.container.state
53         : process.containerRequest.state;
54
55 const isSubprocess = (containerUuid: string) => (resource: Resource) =>
56     resource.kind === ResourceKind.CONTAINER_REQUEST
57     && (resource as ContainerRequestResource).requestingContainerUuid === containerUuid;