15672: Clears data explorer when there're no subprocesses to show.
[arvados-workbench2.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 } from '~/services/api/filter-builder';
21 import { subprocessPanelActions } from './subprocess-panel-actions';
22 import { DataColumns } from '~/components/data-table/data-table';
23
24 export class SubprocessMiddlewareService extends DataExplorerMiddlewareService {
25     constructor(private services: ServiceRepository, id: string) {
26         super(id);
27     }
28
29     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
30         const state = api.getState();
31         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
32         const columns = dataExplorer.columns as DataColumns<string>;
33         const statusFilters = getDataExplorerColumnFilters(columns, 'Status');
34         const activeStatusFilter = Object.keys(statusFilters).find(
35             filterName => statusFilters[filterName].selected
36         );
37
38         try {
39             const parentContainerRequestUuid = state.processPanel.containerRequestUuid;
40             if (parentContainerRequestUuid === "") {
41                 api.dispatch(subprocessPanelActions.CLEAR());
42                 return;
43             }
44
45             const parentContainerRequest = await this.services.containerRequestService.get(parentContainerRequestUuid);
46             if (!parentContainerRequest.containerUuid) {
47                 api.dispatch(subprocessPanelActions.CLEAR());
48                 return;
49             }
50
51             // Get all the subprocess' container requests and containers.
52             const fb = new FilterBuilder().addEqual('requesting_container_uuid', parentContainerRequest.containerUuid);
53             if (activeStatusFilter !== undefined && activeStatusFilter !== 'All') {
54                 fb.addEqual('container.state', activeStatusFilter);
55             }
56             const containerRequests = await this.services.containerRequestService.list(
57                 { ...getParams(dataExplorer), filters: fb.getFilters() });
58             if (containerRequests.items.length === 0) {
59                 api.dispatch(subprocessPanelActions.CLEAR());
60                 return;
61             }
62             const containerUuids: string[] = containerRequests.items.reduce(
63                 (uuids, { containerUuid }) =>
64                     containerUuid
65                         ? [...uuids, containerUuid]
66                         : uuids, []);
67             const containers = await this.services.containerService.list({
68                 filters: new FilterBuilder().addIn('uuid', containerUuids).getFilters()
69             });
70
71             // Populate the actual user view
72             api.dispatch(updateResources(containerRequests.items));
73             api.dispatch(updateResources(containers.items));
74             api.dispatch(setItems(containerRequests));
75         } catch {
76             api.dispatch(couldNotFetchSubprocesses());
77         }
78     }
79 }
80
81 export const getParams = (dataExplorer: DataExplorer) => ({
82     ...dataExplorerToListParams(dataExplorer),
83     order: getOrder(dataExplorer)
84 });
85
86 const getOrder = (dataExplorer: DataExplorer) => {
87     const sortColumn = getSortColumn(dataExplorer);
88     const order = new OrderBuilder<ProcessResource>();
89     if (sortColumn) {
90         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
91             ? OrderDirection.ASC
92             : OrderDirection.DESC;
93
94         const columnName = sortColumn && sortColumn.name === SubprocessPanelColumnNames.NAME ? "name" : "modifiedAt";
95         return order
96             .addOrder(sortDirection, columnName)
97             .getOrder();
98     } else {
99         return order.getOrder();
100     }
101 };
102
103 export const setItems = (listResults: ListResults<ProcessResource>) =>
104     subprocessPanelActions.SET_ITEMS({
105         ...listResultsToDataExplorerItemsMeta(listResults),
106         items: listResults.items.map(resource => resource.uuid),
107     });
108
109 const couldNotFetchSubprocesses = () =>
110     snackbarActions.OPEN_SNACKBAR({
111         message: 'Could not fetch subprocesses.',
112         kind: SnackbarKind.ERROR
113     });