18368: initial impl
[arvados.git] / src / store / subprocess-panel / subprocess-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import {
8     DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta, getDataExplorerColumnFilters
9 } from 'store/data-explorer/data-explorer-middleware-service';
10 import { RootState } from 'store/store';
11 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
12 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
13 import { updateResources } from 'store/resources/resources-actions';
14 import { SortDirection } from 'components/data-table/data-column';
15 import { OrderDirection, OrderBuilder } from 'services/api/order-builder';
16 import { ListResults } from 'services/common-service/common-service';
17 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
18 import { ProcessResource } from 'models/process';
19 import { SubprocessPanelColumnNames } from 'views/subprocess-panel/subprocess-panel-root';
20 import { FilterBuilder, joinFilters } from 'services/api/filter-builder';
21 import { subprocessPanelActions } from './subprocess-panel-actions';
22 import { DataColumns } from 'components/data-table/data-table';
23 import { ProcessStatusFilter, buildProcessStatusFilters } from '../resource-type-filters/resource-type-filters';
24 import { ContainerRequestResource } from 'models/container-request';
25 import { progressIndicatorActions } from '../progress-indicator/progress-indicator-actions';
26 import { loadMissingProcessesInformation } from '../project-panel/project-panel-middleware-service';
27 import { containerRequestFieldsNoMounts } from 'store/all-processes-panel/all-processes-panel-middleware-service';
28
29 export class SubprocessMiddlewareService extends DataExplorerMiddlewareService {
30     constructor(private services: ServiceRepository, id: string) {
31         super(id);
32     }
33
34     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
35         const state = api.getState();
36         const parentContainerRequestUuid = state.processPanel.containerRequestUuid;
37         if (parentContainerRequestUuid === "") { return; }
38         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
39
40         try {
41             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
42             const parentContainerRequest = await this.services.containerRequestService.get(parentContainerRequestUuid);
43             const containerRequests = await this.services.containerRequestService.list(
44                 {
45                     ...getParams(dataExplorer, parentContainerRequest) ,
46                     select: containerRequestFieldsNoMounts
47                 });
48
49             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
50             api.dispatch(updateResources(containerRequests.items));
51             await api.dispatch<any>(loadMissingProcessesInformation(containerRequests.items));
52             // Populate the actual user view
53             api.dispatch(setItems(containerRequests));
54         } catch {
55             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
56             api.dispatch(couldNotFetchSubprocesses());
57         }
58     }
59 }
60
61 export const getParams = (
62     dataExplorer: DataExplorer,
63     parentContainerRequest: ContainerRequestResource) => ({
64         ...dataExplorerToListParams(dataExplorer),
65         order: getOrder(dataExplorer),
66         filters: getFilters(dataExplorer, parentContainerRequest)
67     });
68
69 const getOrder = (dataExplorer: DataExplorer) => {
70     const sortColumn = getSortColumn(dataExplorer);
71     const order = new OrderBuilder<ProcessResource>();
72     if (sortColumn) {
73         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
74             ? OrderDirection.ASC
75             : OrderDirection.DESC;
76
77         const columnName = sortColumn && sortColumn.name === SubprocessPanelColumnNames.NAME ? "name" : "modifiedAt";
78         return order
79             .addOrder(sortDirection, columnName)
80             .getOrder();
81     } else {
82         return order.getOrder();
83     }
84 };
85
86 export const getFilters = (
87     dataExplorer: DataExplorer,
88     parentContainerRequest: ContainerRequestResource) => {
89         const columns = dataExplorer.columns as DataColumns<string>;
90         const statusColumnFilters = getDataExplorerColumnFilters(columns, 'Status');
91         const activeStatusFilter = Object.keys(statusColumnFilters).find(
92             filterName => statusColumnFilters[filterName].selected
93         ) || ProcessStatusFilter.ALL;
94
95         // Get all the subprocess' container requests and containers.
96         const fb = new FilterBuilder().addEqual('requesting_container_uuid', parentContainerRequest.containerUuid);
97         const statusFilters = buildProcessStatusFilters(fb, activeStatusFilter).getFilters();
98
99         const nameFilters = dataExplorer.searchValue
100             ? new FilterBuilder()
101                 .addILike("name", dataExplorer.searchValue)
102                 .getFilters()
103             : '';
104
105         return joinFilters(
106             nameFilters,
107             statusFilters
108         );
109     };
110
111 export const setItems = (listResults: ListResults<ProcessResource>) =>
112     subprocessPanelActions.SET_ITEMS({
113         ...listResultsToDataExplorerItemsMeta(listResults),
114         items: listResults.items.map(resource => resource.uuid),
115     });
116
117 const couldNotFetchSubprocesses = () =>
118     snackbarActions.OPEN_SNACKBAR({
119         message: 'Could not fetch subprocesses.',
120         kind: SnackbarKind.ERROR
121     });