1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ServiceRepository } from '~/services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
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';
24 export class SubprocessMiddlewareService extends DataExplorerMiddlewareService {
25 constructor(private services: ServiceRepository, id: string) {
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
39 const parentContainerRequestUuid = state.processPanel.containerRequestUuid;
40 if (parentContainerRequestUuid === "") {
41 api.dispatch(subprocessPanelActions.CLEAR());
45 const parentContainerRequest = await this.services.containerRequestService.get(parentContainerRequestUuid);
46 if (!parentContainerRequest.containerUuid) {
47 api.dispatch(subprocessPanelActions.CLEAR());
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);
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());
62 const containerUuids: string[] = containerRequests.items.reduce(
63 (uuids, { containerUuid }) =>
65 ? [...uuids, containerUuid]
67 const containers = await this.services.containerService.list({
68 filters: new FilterBuilder().addIn('uuid', containerUuids).getFilters()
71 // Populate the actual user view
72 api.dispatch(updateResources(containerRequests.items));
73 api.dispatch(updateResources(containers.items));
74 api.dispatch(setItems(containerRequests));
76 api.dispatch(couldNotFetchSubprocesses());
81 export const getParams = (dataExplorer: DataExplorer) => ({
82 ...dataExplorerToListParams(dataExplorer),
83 order: getOrder(dataExplorer)
86 const getOrder = (dataExplorer: DataExplorer) => {
87 const sortColumn = getSortColumn(dataExplorer);
88 const order = new OrderBuilder<ProcessResource>();
90 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
92 : OrderDirection.DESC;
94 const columnName = sortColumn && sortColumn.name === SubprocessPanelColumnNames.NAME ? "name" : "modifiedAt";
96 .addOrder(sortDirection, columnName)
99 return order.getOrder();
103 export const setItems = (listResults: ListResults<ProcessResource>) =>
104 subprocessPanelActions.SET_ITEMS({
105 ...listResultsToDataExplorerItemsMeta(listResults),
106 items: listResults.items.map(resource => resource.uuid),
109 const couldNotFetchSubprocesses = () =>
110 snackbarActions.OPEN_SNACKBAR({
111 message: 'Could not fetch subprocesses.',
112 kind: SnackbarKind.ERROR