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';
28 export class SubprocessMiddlewareService extends DataExplorerMiddlewareService {
29 constructor(private services: ServiceRepository, id: string) {
33 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
34 const state = api.getState();
35 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
38 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
39 const parentContainerRequestUuid = state.processPanel.containerRequestUuid;
40 const parentContainerRequest = await this.services.containerRequestService.get(parentContainerRequestUuid);
41 const containerRequests = await this.services.containerRequestService.list(
42 { ...getParams(dataExplorer, parentContainerRequest) });
44 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
45 api.dispatch(updateResources(containerRequests.items));
46 await api.dispatch<any>(loadMissingProcessesInformation(containerRequests.items));
47 // Populate the actual user view
48 api.dispatch(setItems(containerRequests));
50 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
51 api.dispatch(couldNotFetchSubprocesses());
56 export const getParams = (
57 dataExplorer: DataExplorer,
58 parentContainerRequest: ContainerRequestResource) => ({
59 ...dataExplorerToListParams(dataExplorer),
60 order: getOrder(dataExplorer),
61 filters: getFilters(dataExplorer, parentContainerRequest)
64 const getOrder = (dataExplorer: DataExplorer) => {
65 const sortColumn = getSortColumn(dataExplorer);
66 const order = new OrderBuilder<ProcessResource>();
68 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
70 : OrderDirection.DESC;
72 const columnName = sortColumn && sortColumn.name === SubprocessPanelColumnNames.NAME ? "name" : "modifiedAt";
74 .addOrder(sortDirection, columnName)
77 return order.getOrder();
81 export const getFilters = (
82 dataExplorer: DataExplorer,
83 parentContainerRequest: ContainerRequestResource) => {
84 const columns = dataExplorer.columns as DataColumns<string>;
85 const statusColumnFilters = getDataExplorerColumnFilters(columns, 'Status');
86 const activeStatusFilter = Object.keys(statusColumnFilters).find(
87 filterName => statusColumnFilters[filterName].selected
88 ) || ProcessStatusFilter.ALL;
90 // Get all the subprocess' container requests and containers.
91 const fb = new FilterBuilder().addEqual('requesting_container_uuid', parentContainerRequest.containerUuid);
92 const statusFilters = buildProcessStatusFilters(fb, activeStatusFilter).getFilters();
94 const nameFilters = dataExplorer.searchValue
96 .addILike("name", dataExplorer.searchValue)
106 export const setItems = (listResults: ListResults<ProcessResource>) =>
107 subprocessPanelActions.SET_ITEMS({
108 ...listResultsToDataExplorerItemsMeta(listResults),
109 items: listResults.items.map(resource => resource.uuid),
112 const couldNotFetchSubprocesses = () =>
113 snackbarActions.OPEN_SNACKBAR({
114 message: 'Could not fetch subprocesses.',
115 kind: SnackbarKind.ERROR