X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/f6093a73debdd7135870582088202da459386f17..8e6de8fb13348e6e0b0ab47d91ab65f99033e174:/src/store/search-bar/search-bar-actions.ts diff --git a/src/store/search-bar/search-bar-actions.ts b/src/store/search-bar/search-bar-actions.ts index 4af132ea..af40e86a 100644 --- a/src/store/search-bar/search-bar-actions.ts +++ b/src/store/search-bar/search-bar-actions.ts @@ -2,28 +2,29 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { ofType, unionize, UnionOf } from "~/common/unionize"; -import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service'; +import axios from "axios"; +import { ofType, unionize, UnionOf } from "common/unionize"; +import { GroupContentsResource, GroupContentsResourcePrefix } from 'services/groups-service/groups-service'; import { Dispatch } from 'redux'; import { change, initialize, untouch } from 'redux-form'; -import { RootState } from '~/store/store'; -import { initUserProject, treePickerActions } from '~/store/tree-picker/tree-picker-actions'; -import { ServiceRepository } from '~/services/services'; -import { FilterBuilder } from "~/services/api/filter-builder"; -import { ResourceKind, RESOURCE_UUID_REGEX, COLLECTION_PDH_REGEX } from '~/models/resource'; -import { SearchView } from '~/store/search-bar/search-bar-reducer'; -import { navigateTo, navigateToSearchResults } from '~/store/navigation/navigation-action'; -import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions'; -import { PropertyValue, SearchBarAdvancedFormData } from '~/models/search-bar'; -import * as _ from "lodash"; -import { getModifiedKeysValues } from "~/common/objects"; -import { activateSearchBarProject } from "~/store/search-bar/search-bar-tree-actions"; -import { Session } from "~/models/session"; -import { searchResultsPanelActions } from "~/store/search-results-panel/search-results-panel-actions"; -import { ListResults } from "~/services/common-service/common-service"; +import { RootState } from 'store/store'; +import { initUserProject, treePickerActions } from 'store/tree-picker/tree-picker-actions'; +import { ServiceRepository } from 'services/services'; +import { FilterBuilder } from "services/api/filter-builder"; +import { ResourceKind, RESOURCE_UUID_REGEX, COLLECTION_PDH_REGEX } from 'models/resource'; +import { SearchView } from 'store/search-bar/search-bar-reducer'; +import { navigateTo, navigateToSearchResults } from 'store/navigation/navigation-action'; +import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions'; +import { PropertyValue, SearchBarAdvancedFormData } from 'models/search-bar'; +import { union } from "lodash"; +import { getModifiedKeysValues } from "common/objects"; +import { activateSearchBarProject } from "store/search-bar/search-bar-tree-actions"; +import { Session } from "models/session"; +import { searchResultsPanelActions } from "store/search-results-panel/search-results-panel-actions"; +import { ListResults } from "services/common-service/common-service"; import * as parser from './search-query/arv-parser'; import { Keywords } from './search-query/arv-parser'; -import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "~/models/vocabulary"; +import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "models/vocabulary"; export const searchBarActions = unionize({ SET_CURRENT_VIEW: ofType(), @@ -62,13 +63,13 @@ export const loadRecentQueries = () => return recentQueries; }; -export const searchData = (searchValue: string) => +export const searchData = (searchValue: string, useCancel = false) => async (dispatch: Dispatch, getState: () => RootState) => { const currentView = getState().searchBar.currentView; dispatch(searchResultsPanelActions.CLEAR()); dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); if (searchValue.length > 0) { - dispatch(searchGroups(searchValue, 5)); + dispatch(searchGroups(searchValue, 5, useCancel)); if (currentView === SearchView.BASIC) { dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); dispatch(navigateToSearchResults(searchValue)); @@ -88,6 +89,9 @@ export const searchAdvancedData = (data: SearchBarAdvancedFormData) => export const setSearchValueFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) => (dispatch: Dispatch, getState: () => RootState) => { + if (data.projectObject) { + data.projectUuid = data.projectObject.uuid; + } const searchValue = getState().searchBar.searchValue; const value = getQueryFromAdvancedData({ ...data, @@ -97,8 +101,11 @@ export const setSearchValueFromAdvancedData = (data: SearchBarAdvancedFormData, }; export const setAdvancedDataFromSearchValue = (search: string, vocabulary: Vocabulary) => - async (dispatch: Dispatch) => { + async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const data = getAdvancedDataFromQuery(search, vocabulary); + if (data.projectUuid) { + data.projectObject = await services.projectService.get(data.projectUuid); + } dispatch(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data)); if (data.projectUuid) { await dispatch(activateSearchBarProject(data.projectUuid)); @@ -203,34 +210,49 @@ export const submitData = (event: React.FormEvent) => } }; - -const searchGroups = (searchValue: string, limit: number) => +let cancelTokens: any[] = []; +const searchGroups = (searchValue: string, limit: number, useCancel = false) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const currentView = getState().searchBar.currentView; - if (searchValue || currentView === SearchView.ADVANCED) { - const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue); - const sessions = getSearchSessions(clusterId, getState().auth.sessions); - const lists: ListResults[] = await Promise.all(sessions.map(session => { - const filters = queryToFilters(searchValue); - return services.groupsService.contents('', { - filters, - limit, - recursive: true - }, session); - })); - - const items = lists.reduce((items, list) => items.concat(list.items), [] as GroupContentsResource[]); - dispatch(searchBarActions.SET_SEARCH_RESULTS(items)); + if (cancelTokens.length > 0 && useCancel) { + cancelTokens.forEach(cancelToken => (cancelToken as any).cancel('New search request triggered.')); + cancelTokens = []; } + + setTimeout(async () => { + if (searchValue || currentView === SearchView.ADVANCED) { + const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue); + const sessions = getSearchSessions(clusterId, getState().auth.sessions); + const lists: ListResults[] = await Promise.all(sessions.map((session, index) => { + cancelTokens.push(axios.CancelToken.source()); + const filters = queryToFilters(searchValue, session.apiRevision); + return services.groupsService.contents('', { + filters, + limit, + recursive: true + }, session, cancelTokens[index].token); + })); + + cancelTokens = []; + + const items = lists.reduce((items, list) => items.concat(list.items), [] as GroupContentsResource[]); + + if (lists.filter(list => !!(list as any).items).length !== lists.length) { + dispatch(searchBarActions.SET_SEARCH_RESULTS([])); + } else { + dispatch(searchBarActions.SET_SEARCH_RESULTS(items)); + } + } + }, 10); }; -const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | 'reuse') => { +const buildQueryFromKeyMap = (data: any, keyMap: string[][]) => { let value = data.searchValue; const addRem = (field: string, key: string) => { const v = data[key]; - + // Remove previous search expression. if (data.hasOwnProperty(key)) { let pattern: string; if (v === false) { @@ -238,28 +260,23 @@ const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | ' } else if (key.startsWith('prop-')) { // On properties, only remove key:value duplicates, allowing // multiple properties with the same key. - pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*${v}\\s*`; + const oldValue = key.slice(5).split(':')[1]; + pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*${oldValue}\\s*`; } else { pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`; } value = value.replace(new RegExp(pattern), ''); } - + // Re-add it with the current search value. if (v) { const nv = v === true ? `${field}` : `${field}:${v}`; - - if (mode === 'rebuild') { - value = value + ' ' + nv; - } else { - value = nv + ' ' + value; - } + // Always append to the end to keep user-entered text at the start. + value = value + ' ' + nv; } }; - keyMap.forEach(km => addRem(km[0], km[1])); - return value; }; @@ -273,10 +290,13 @@ export const getQueryFromAdvancedData = (data: SearchBarAdvancedFormData, prevDa cluster: data.cluster, projectUuid: data.projectUuid, inTrash: data.inTrash, + pastVersions: data.pastVersions, dateFrom: data.dateFrom, dateTo: data.dateTo, }; - (data.properties || []).forEach(p => fo[`prop-"${p.keyID || p.key}"`] = `"${p.valueID || p.value}"`); + (data.properties || []).forEach(p => + fo[`prop-"${p.keyID || p.key}":"${p.valueID || p.value}"`] = `"${p.valueID || p.value}"` + ); return fo; }; @@ -285,21 +305,18 @@ export const getQueryFromAdvancedData = (data: SearchBarAdvancedFormData, prevDa ['cluster', 'cluster'], ['project', 'projectUuid'], [`is:${parser.States.TRASHED}`, 'inTrash'], + [`is:${parser.States.PAST_VERSION}`, 'pastVersions'], ['from', 'dateFrom'], ['to', 'dateTo'] ]; - _.union(data.properties, prevData ? prevData.properties : []) - .forEach(p => keyMap.push([`has:"${p.keyID || p.key}"`, `prop-"${p.keyID || p.key}"`])); + union(data.properties, prevData ? prevData.properties : []) + .forEach(p => keyMap.push( + [`has:"${p.keyID || p.key}"`, `prop-"${p.keyID || p.key}":"${p.valueID || p.value}"`] + )); - if (prevData) { - const obj = getModifiedKeysValues(flatData(data), flatData(prevData)); - value = buildQueryFromKeyMap({ - searchValue: data.searchValue, - ...obj - } as SearchBarAdvancedFormData, keyMap, "reuse"); - } else { - value = buildQueryFromKeyMap(flatData(data), keyMap, "rebuild"); - } + const modified = getModifiedKeysValues(flatData(data), prevData ? flatData(prevData) : {}); + value = buildQueryFromKeyMap( + { searchValue: data.searchValue, ...modified } as SearchBarAdvancedFormData, keyMap); value = value.trim(); return value; @@ -314,6 +331,7 @@ export const getAdvancedDataFromQuery = (query: string, vocabulary?: Vocabulary) cluster: getValue(Keywords.CLUSTER), projectUuid: getValue(Keywords.PROJECT), inTrash: parser.isTrashed(tokens), + pastVersions: parser.isPastVersion(tokens), dateFrom: getValue(Keywords.FROM) || '', dateTo: getValue(Keywords.TO) || '', properties: vocabulary @@ -336,7 +354,7 @@ export const getSearchSessions = (clusterId: string | undefined, sessions: Sessi return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId)); }; -export const queryToFilters = (query: string) => { +export const queryToFilters = (query: string, apiRevision: number) => { const data = getAdvancedDataFromQuery(query); const filter = new FilterBuilder(); const resourceKind = data.type; @@ -359,9 +377,17 @@ export const queryToFilters = (query: string) => { data.properties.forEach(p => { if (p.value) { - filter - .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT) - .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION); + if (apiRevision < 20200212) { + filter + .addEqual(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT) + .addEqual(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION) + .addEqual(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROCESS); + } else { + filter + .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT) + .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION) + .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROCESS); + } } filter.addExists(p.key); });