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 { SearchResultsPanelColumnNames } from '~/views/search-results-panel/search-results-panel-view';
14 import { OrderDirection, OrderBuilder } from '~/services/api/order-builder';
15 import { GroupContentsResource, GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
16 import { ListResults } from '~/services/common-service/common-service';
17 import { searchResultsPanelActions } from '~/store/search-results-panel/search-results-panel-actions';
21 getAdvancedDataFromQuery
22 } from '~/store/search-bar/search-bar-actions';
23 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
24 import { joinFilters } from '~/services/api/filter-builder';
25 import { DataColumns } from '~/components/data-table/data-table';
26 import { serializeResourceTypeFilters } from '~/store//resource-type-filters/resource-type-filters';
27 import { ProjectPanelColumnNames } from '~/views/project-panel/project-panel';
28 import * as _ from 'lodash';
29 import { Resource } from '~/models/resource';
31 export class SearchResultsMiddlewareService extends DataExplorerMiddlewareService {
32 constructor(private services: ServiceRepository, id: string) {
36 async requestItems(api: MiddlewareAPI<Dispatch, RootState>, criteriaChanged?: boolean) {
37 const state = api.getState();
38 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
39 const searchValue = state.searchBar.searchValue;
40 const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
41 const sessions = getSearchSessions(clusterId, state.auth.sessions);
43 if (searchValue.trim() === '') {
48 const params = getParams(dataExplorer, searchValue);
50 const responses = await Promise.all(sessions.map(session =>
51 this.services.groupsService.contents('', params, session)
56 items: [] as GroupContentsResource[],
62 const mergedResponse = responses.reduce((merged, current) => ({
64 itemsAvailable: merged.itemsAvailable + current.itemsAvailable,
65 items: merged.items.concat(current.items)
68 api.dispatch(updateResources(mergedResponse.items));
70 api.dispatch(criteriaChanged
71 ? setItems(mergedResponse)
72 : appendItems(mergedResponse));
75 api.dispatch(couldNotFetchSearchResults());
80 const typeFilters = (columns: DataColumns<string>) => serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
82 export const getParams = (dataExplorer: DataExplorer, query: string) => ({
83 ...dataExplorerToListParams(dataExplorer),
85 queryToFilters(query),
86 typeFilters(dataExplorer.columns)
88 order: getOrder(dataExplorer),
89 includeTrash: getAdvancedDataFromQuery(query).inTrash
92 const getOrder = (dataExplorer: DataExplorer) => {
93 const sortColumn = getSortColumn(dataExplorer);
94 const order = new OrderBuilder<GroupContentsResource>();
96 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
98 : OrderDirection.DESC;
101 .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.COLLECTION)
102 .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.PROCESS)
103 .addOrder(sortDirection, sortColumn.name as keyof Resource, GroupContentsResourcePrefix.PROJECT)
106 return order.getOrder();
110 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
111 searchResultsPanelActions.SET_ITEMS({
112 ...listResultsToDataExplorerItemsMeta(listResults),
113 items: listResults.items.map(resource => resource.uuid),
116 export const appendItems = (listResults: ListResults<GroupContentsResource>) =>
117 searchResultsPanelActions.APPEND_ITEMS({
118 ...listResultsToDataExplorerItemsMeta(listResults),
119 items: listResults.items.map(resource => resource.uuid),
122 const couldNotFetchSearchResults = () =>
123 snackbarActions.OPEN_SNACKBAR({
124 message: `Could not fetch search results for some sessions.`,
125 kind: SnackbarKind.ERROR