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, 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';
29 export class SubprocessMiddlewareService extends DataExplorerMiddlewareService {
30 constructor(private services: ServiceRepository, id: string) {
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());
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(
45 ...getParams(dataExplorer, parentContainerRequest) ,
46 select: containerRequestFieldsNoMounts
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));
55 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
56 api.dispatch(couldNotFetchSubprocesses());
61 export const getParams = (
62 dataExplorer: DataExplorer,
63 parentContainerRequest: ContainerRequestResource) => ({
64 ...dataExplorerToListParams(dataExplorer),
65 order: getOrder(dataExplorer),
66 filters: getFilters(dataExplorer, parentContainerRequest)
69 const getOrder = (dataExplorer: DataExplorer) => {
70 const sortColumn = getSortColumn(dataExplorer);
71 const order = new OrderBuilder<ProcessResource>();
73 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
75 : OrderDirection.DESC;
77 const columnName = sortColumn && sortColumn.name === SubprocessPanelColumnNames.NAME ? "name" : "modifiedAt";
79 .addOrder(sortDirection, columnName)
82 return order.getOrder();
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;
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();
99 const nameFilters = dataExplorer.searchValue
100 ? new FilterBuilder()
101 .addILike("name", dataExplorer.searchValue)
111 export const setItems = (listResults: ListResults<ProcessResource>) =>
112 subprocessPanelActions.SET_ITEMS({
113 ...listResultsToDataExplorerItemsMeta(listResults),
114 items: listResults.items.map(resource => resource.uuid),
117 const couldNotFetchSubprocesses = () =>
118 snackbarActions.OPEN_SNACKBAR({
119 message: 'Could not fetch subprocesses.',
120 kind: SnackbarKind.ERROR