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';
7 import { DataExplorerMiddlewareService, dataExplorerToListParams, listResultsToDataExplorerItemsMeta, getDataExplorerColumnFilters } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { SortDirection } from 'components/data-table/data-column';
13 import { OrderDirection, OrderBuilder } from 'services/api/order-builder';
14 import { GroupContentsResource, GroupContentsResourcePrefix } from "services/groups-service/groups-service";
15 import { ListResults } from 'services/common-service/common-service';
16 import { searchResultsPanelActions } from 'store/search-results-panel/search-results-panel-actions';
20 getAdvancedDataFromQuery
21 } from 'store/search-bar/search-bar-actions';
22 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
23 import { joinFilters } from 'services/api/filter-builder';
24 import { DataColumns } from 'components/data-table/data-table';
25 import { serializeResourceTypeFilters } from 'store//resource-type-filters/resource-type-filters';
26 import { ProjectPanelColumnNames } from 'views/project-panel/project-panel';
27 import { Resource } from 'models/resource';
29 export class SearchResultsMiddlewareService extends DataExplorerMiddlewareService {
30 constructor(private services: ServiceRepository, id: string) {
34 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean) {
35 const state = api.getState();
36 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
37 const searchValue = state.searchBar.searchValue;
38 const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
39 const sessions = getSearchSessions(clusterId, state.auth.sessions);
41 if (searchValue.trim() === '') {
47 items: [] as GroupContentsResource[],
53 if (criteriaChanged) {
54 api.dispatch(setItems(initial));
57 sessions.forEach(session => {
58 const params = getParams(dataExplorer, searchValue, session.apiRevision);
59 this.services.groupsService.contents('', params, session)
61 api.dispatch(updateResources(response.items));
62 api.dispatch(appendItems(response));
64 api.dispatch(couldNotFetchSearchResults(session.clusterId));
71 const typeFilters = (columns: DataColumns<string>) => serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
73 export const getParams = (dataExplorer: DataExplorer, query: string, apiRevision: number) => ({
74 ...dataExplorerToListParams(dataExplorer),
76 queryToFilters(query, apiRevision),
77 typeFilters(dataExplorer.columns)
79 order: getOrder(dataExplorer),
80 includeTrash: getAdvancedDataFromQuery(query).inTrash,
81 includeOldVersions: getAdvancedDataFromQuery(query).pastVersions
84 const getOrder = (dataExplorer: DataExplorer) => {
85 const sortColumn = getSortColumn(dataExplorer);
86 const order = new OrderBuilder<GroupContentsResource>();
88 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
90 : OrderDirection.DESC;
93 .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.COLLECTION)
94 .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.PROCESS)
95 .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.PROJECT)
98 return order.getOrder();
102 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
103 searchResultsPanelActions.SET_ITEMS({
104 ...listResultsToDataExplorerItemsMeta(listResults),
105 items: listResults.items.map(resource => resource.uuid),
108 export const appendItems = (listResults: ListResults<GroupContentsResource>) =>
109 searchResultsPanelActions.APPEND_ITEMS({
110 ...listResultsToDataExplorerItemsMeta(listResults),
111 items: listResults.items.map(resource => resource.uuid),
114 const couldNotFetchSearchResults = (cluster: string) =>
115 snackbarActions.OPEN_SNACKBAR({
116 message: `Could not fetch search results from ${cluster}.`,
117 kind: SnackbarKind.ERROR