X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/3b7eae043b41eaebd5cd78d3a74584bcc1290ee7..32c4bd51747de281957d7bbcd3f90879270ad6fa:/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 cfa07ebc..c12fcbe4 100644 --- a/src/store/search-bar/search-bar-actions.ts +++ b/src/store/search-bar/search-bar-actions.ts @@ -5,22 +5,25 @@ import { ofType, unionize, UnionOf } from "~/common/unionize"; import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service'; import { Dispatch } from 'redux'; -import { arrayPush, change, initialize } from 'redux-form'; +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 } from '~/models/resource'; -import { GroupClass } from '~/models/group'; +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, SearchBarAdvanceFormData } from '~/models/search-bar'; -import { debounce } from 'debounce'; +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 * as parser from './search-query/arv-parser'; +import { Keywords } from './search-query/arv-parser'; +import { Vocabulary, getTagKeyLabel, getTagValueLabel } from "~/models/vocabulary"; export const searchBarActions = unionize({ SET_CURRENT_VIEW: ofType(), @@ -28,9 +31,9 @@ export const searchBarActions = unionize({ CLOSE_SEARCH_VIEW: ofType<{}>(), SET_SEARCH_RESULTS: ofType(), SET_SEARCH_VALUE: ofType(), - SET_SAVED_QUERIES: ofType(), + SET_SAVED_QUERIES: ofType(), SET_RECENT_QUERIES: ofType(), - UPDATE_SAVED_QUERY: ofType(), + UPDATE_SAVED_QUERY: ofType(), SET_SELECTED_ITEM: ofType(), MOVE_UP: ofType<{}>(), MOVE_DOWN: ofType<{}>(), @@ -39,9 +42,9 @@ export const searchBarActions = unionize({ export type SearchBarActions = UnionOf; -export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName'; +export const SEARCH_BAR_ADVANCED_FORM_NAME = 'searchBarAdvancedFormName'; -export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId'; +export const SEARCH_BAR_ADVANCED_FORM_PICKER_ID = 'searchBarAdvancedFormPickerId'; export const DEFAULT_SEARCH_DEBOUNCE = 1000; @@ -62,25 +65,28 @@ export const loadRecentQueries = () => export const searchData = (searchValue: string) => 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)); if (currentView === SearchView.BASIC) { dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); - dispatch(navigateToSearchResults); + dispatch(navigateToSearchResults(searchValue)); } } }; -export const searchAdvanceData = (data: SearchBarAdvanceFormData) => - async (dispatch: Dispatch) => { +export const searchAdvancedData = (data: SearchBarAdvancedFormData) => + async (dispatch: Dispatch, getState: () => RootState) => { dispatch(saveQuery(data)); + const searchValue = getState().searchBar.searchValue; + dispatch(searchResultsPanelActions.CLEAR()); dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); - dispatch(navigateToSearchResults); + dispatch(navigateToSearchResults(searchValue)); }; -export const setSearchValueFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => +export const setSearchValueFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) => (dispatch: Dispatch, getState: () => RootState) => { const searchValue = getState().searchBar.searchValue; const value = getQueryFromAdvancedData({ @@ -90,17 +96,17 @@ export const setSearchValueFromAdvancedData = (data: SearchBarAdvanceFormData, p dispatch(searchBarActions.SET_SEARCH_VALUE(value)); }; -export const setAdvancedDataFromSearchValue = (search: string) => +export const setAdvancedDataFromSearchValue = (search: string, vocabulary: Vocabulary) => async (dispatch: Dispatch) => { - const data = getAdvancedDataFromQuery(search); - dispatch(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data)); + const data = getAdvancedDataFromQuery(search, vocabulary); + dispatch(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data)); if (data.projectUuid) { await dispatch(activateSearchBarProject(data.projectUuid)); - dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID, id: data.projectUuid })); + dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID, id: data.projectUuid })); } }; -const saveQuery = (data: SearchBarAdvanceFormData) => +const saveQuery = (data: SearchBarAdvancedFormData) => (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const savedQueries = services.searchService.getSavedQueries(); if (data.saveQuery && data.queryName) { @@ -126,11 +132,11 @@ export const deleteSavedQuery = (id: number) => return savedSearchQueries || []; }; -export const editSavedQuery = (data: SearchBarAdvanceFormData) => +export const editSavedQuery = (data: SearchBarAdvancedFormData) => (dispatch: Dispatch) => { dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED)); dispatch(searchBarActions.SET_SEARCH_VALUE(getQueryFromAdvancedData(data))); - dispatch(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data)); + dispatch(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data)); }; export const openSearchView = () => @@ -151,7 +157,7 @@ export const closeSearchView = () => export const closeAdvanceView = () => (dispatch: Dispatch) => { dispatch(searchBarActions.SET_SEARCH_VALUE('')); - dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID })); + dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID })); dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); }; @@ -173,7 +179,6 @@ export const changeData = (searchValue: string) => } else if (searchValuePresent) { dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE)); dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue)); - debounceStartSearch(dispatch); } else { dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC)); dispatch(searchBarActions.SET_SEARCH_RESULTS([])); @@ -188,36 +193,35 @@ export const submitData = (event: React.FormEvent) => dispatch(saveRecentQuery(searchValue)); dispatch(loadRecentQueries()); dispatch(searchBarActions.CLOSE_SEARCH_VIEW()); - dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); - dispatch(searchBarActions.SET_SEARCH_RESULTS([])); - dispatch(navigateToSearchResults); + if (RESOURCE_UUID_REGEX.exec(searchValue) || COLLECTION_PDH_REGEX.exec(searchValue)) { + dispatch(navigateTo(searchValue)); + } else { + dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue)); + dispatch(searchBarActions.SET_SEARCH_RESULTS([])); + dispatch(searchResultsPanelActions.CLEAR()); + dispatch(navigateToSearchResults(searchValue)); + } }; -const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch(startSearch()), DEFAULT_SEARCH_DEBOUNCE); - -const startSearch = () => - (dispatch: Dispatch, getState: () => RootState) => { - const searchValue = getState().searchBar.searchValue; - dispatch(searchData(searchValue)); - }; const searchGroups = (searchValue: string, limit: number) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const currentView = getState().searchBar.currentView; if (searchValue || currentView === SearchView.ADVANCED) { - const sq = parseSearchQuery(searchValue); - const clusterId = getSearchQueryFirstProp(sq, 'cluster'); + const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue); const sessions = getSearchSessions(clusterId, getState().auth.sessions); - sessions.forEach(async session => { - const filters = getFilters('name', searchValue, sq); - const { items } = await services.groupsService.contents('', { + const lists: ListResults[] = await Promise.all(sessions.map(session => { + const filters = queryToFilters(searchValue); + return services.groupsService.contents('', { filters, limit, recursive: true }, session); - dispatch(searchBarActions.SET_SEARCH_RESULTS(items)); - }); + })); + + const items = lists.reduce((items, list) => items.concat(list.items), [] as GroupContentsResource[]); + dispatch(searchBarActions.SET_SEARCH_RESULTS(items)); } }; @@ -230,7 +234,7 @@ const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | ' if (data.hasOwnProperty(key)) { const pattern = v === false ? `${field.replace(':', '\\:\\s*')}\\s*` - : `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`; + : `${field.replace(':', '\\:\\s*')}\\:\\s*"[\\w|\\#|\\-|\\/]*"\\s*`; value = value.replace(new RegExp(pattern), ''); } @@ -252,10 +256,10 @@ const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | ' return value; }; -export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => { +export const getQueryFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) => { let value = ''; - const flatData = (data: SearchBarAdvanceFormData) => { + const flatData = (data: SearchBarAdvancedFormData) => { const fo = { searchValue: data.searchValue, type: data.type, @@ -265,7 +269,7 @@ export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevDat dateFrom: data.dateFrom, dateTo: data.dateTo, }; - (data.properties || []).forEach(p => fo[`prop-${p.key}`] = p.value); + (data.properties || []).forEach(p => fo[`prop-"${p.keyID || p.key}"`] = `"${p.valueID || p.value}"`); return fo; }; @@ -273,19 +277,19 @@ export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevDat ['type', 'type'], ['cluster', 'cluster'], ['project', 'projectUuid'], - ['is:trashed', 'inTrash'], + [`is:${parser.States.TRASHED}`, 'inTrash'], ['from', 'dateFrom'], ['to', 'dateTo'] ]; _.union(data.properties, prevData ? prevData.properties : []) - .forEach(p => keyMap.push([`has:${p.key}`, `prop-${p.key}`])); + .forEach(p => keyMap.push([`has:"${p.keyID || p.key}"`, `prop-"${p.keyID || p.key}"`])); if (prevData) { const obj = getModifiedKeysValues(flatData(data), flatData(prevData)); value = buildQueryFromKeyMap({ searchValue: data.searchValue, ...obj - } as SearchBarAdvanceFormData, keyMap, "reuse"); + } as SearchBarAdvancedFormData, keyMap, "reuse"); } else { value = buildQueryFromKeyMap(flatData(data), keyMap, "rebuild"); } @@ -294,96 +298,28 @@ export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevDat return value; }; -export class ParseSearchQuery { - hasKeywords: boolean; - values: string[]; - properties: { - [key: string]: string[] - }; -} - -export const parseSearchQuery: (query: string) => ParseSearchQuery = (searchValue: string) => { - const keywords = [ - 'type:', - 'cluster:', - 'project:', - 'is:', - 'from:', - 'to:', - 'has:' - ]; - - const hasKeywords = (search: string) => keywords.reduce((acc, keyword) => acc + (search.includes(keyword) ? 1 : 0), 0); - let keywordsCnt = 0; - - const properties = {}; - - keywords.forEach(k => { - let p = searchValue.indexOf(k); - const key = k.substr(0, k.length - 1); - - while (p >= 0) { - const l = searchValue.length; - keywordsCnt += 1; - - let v = ''; - let i = p + k.length; - while (i < l && searchValue[i] === ' ') { - ++i; - } - const vp = i; - while (i < l && searchValue[i] !== ' ') { - v += searchValue[i]; - ++i; - } - - if (hasKeywords(v)) { - searchValue = searchValue.substr(0, p) + searchValue.substr(vp); - } else { - if (v !== '') { - if (!properties[key]) { - properties[key] = []; - } - properties[key].push(v); - } - searchValue = searchValue.substr(0, p) + searchValue.substr(i); - } - p = searchValue.indexOf(k); - } - }); - - const values = _.uniq(searchValue.split(' ').filter(v => v.length > 0)); - - return { hasKeywords: keywordsCnt > 0, values, properties }; -}; - -export const getSearchQueryFirstProp = (sq: ParseSearchQuery, name: string) => sq.properties[name] && sq.properties[name][0]; -export const getSearchQueryPropValue = (sq: ParseSearchQuery, name: string, value: string) => sq.properties[name] && sq.properties[name].find((v: string) => v === value); -export const getSearchQueryProperties = (sq: ParseSearchQuery): PropertyValue[] => { - if (sq.properties.has) { - return sq.properties.has.map((value: string) => { - const v = value.split(':'); - return { - key: v[0], - value: v[1] - }; - }); - } - return []; -}; - -export const getAdvancedDataFromQuery = (query: string): SearchBarAdvanceFormData => { - const sq = parseSearchQuery(query); - +export const getAdvancedDataFromQuery = (query: string, vocabulary?: Vocabulary): SearchBarAdvancedFormData => { + const { tokens, searchString } = parser.parseSearchQuery(query); + const getValue = parser.getValue(tokens); return { - searchValue: sq.values.join(' '), - type: getSearchQueryFirstProp(sq, 'type') as ResourceKind, - cluster: getSearchQueryFirstProp(sq, 'cluster'), - projectUuid: getSearchQueryFirstProp(sq, 'project'), - inTrash: getSearchQueryPropValue(sq, 'is', 'trashed') !== undefined, - dateFrom: getSearchQueryFirstProp(sq, 'from'), - dateTo: getSearchQueryFirstProp(sq, 'to'), - properties: getSearchQueryProperties(sq), + searchValue: searchString, + type: getValue(Keywords.TYPE) as ResourceKind, + cluster: getValue(Keywords.CLUSTER), + projectUuid: getValue(Keywords.PROJECT), + inTrash: parser.isTrashed(tokens), + dateFrom: getValue(Keywords.FROM) || '', + dateTo: getValue(Keywords.TO) || '', + properties: vocabulary + ? parser.getProperties(tokens).map( + p => { + return { + keyID: p.key, + key: getTagKeyLabel(p.key, vocabulary), + valueID: p.value, + value: getTagValueLabel(p.key, p.value, vocabulary), + }; + }) + : parser.getProperties(tokens), saveQuery: false, queryName: '' }; @@ -393,81 +329,37 @@ export const getSearchSessions = (clusterId: string | undefined, sessions: Sessi return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId)); }; -export const getFilters = (filterName: string, searchValue: string, sq: ParseSearchQuery): string => { +export const queryToFilters = (query: string) => { + const data = getAdvancedDataFromQuery(query); const filter = new FilterBuilder(); + const resourceKind = data.type; - const resourceKind = getSearchQueryFirstProp(sq, 'type') as ResourceKind; - - let prefix = ''; - switch (resourceKind) { - case ResourceKind.COLLECTION: - prefix = GroupContentsResourcePrefix.COLLECTION; - break; - case ResourceKind.PROCESS: - prefix = GroupContentsResourcePrefix.PROCESS; - break; - default: - prefix = GroupContentsResourcePrefix.PROJECT; - break; + if (data.searchValue) { + filter.addFullTextSearch(data.searchValue); } - const isTrashed = getSearchQueryPropValue(sq, 'is', 'trashed'); - - if (!sq.hasKeywords) { - filter - .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION) - .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT); - - if (isTrashed) { - filter.addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS); - } - } else { - if (prefix) { - sq.values.forEach(v => - filter.addILike(filterName, v, prefix) - ); - } else { - sq.values.forEach(v => { - filter - .addILike(filterName, v, GroupContentsResourcePrefix.COLLECTION) - .addILike(filterName, v, GroupContentsResourcePrefix.PROJECT); - - if (isTrashed) { - filter.addILike(filterName, v, GroupContentsResourcePrefix.PROCESS); - } - }); - } - - if (isTrashed) { - filter.addEqual("is_trashed", true); - } + if (data.projectUuid) { + filter.addEqual('owner_uuid', data.projectUuid); + } - const projectUuid = getSearchQueryFirstProp(sq, 'project'); - if (projectUuid) { - filter.addEqual('uuid', projectUuid, prefix); - } + if (data.dateFrom) { + filter.addGte('modified_at', buildDateFilter(data.dateFrom)); + } - const dateFrom = getSearchQueryFirstProp(sq, 'from'); - if (dateFrom) { - filter.addGte('modified_at', buildDateFilter(dateFrom)); - } + if (data.dateTo) { + filter.addLte('modified_at', buildDateFilter(data.dateTo)); + } - const dateTo = getSearchQueryFirstProp(sq, 'to'); - if (dateTo) { - filter.addLte('modified_at', buildDateFilter(dateTo)); + data.properties.forEach(p => { + if (p.value) { + filter + .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT) + .addContains(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION); } - - const props = getSearchQueryProperties(sq); - props.forEach(p => { - if (p.value) { - filter.addILike(`properties.${p.key}`, p.value); - } - filter.addExists(p.key); - }); - } + filter.addExists(p.key); + }); return filter - .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT) .addIsA("uuid", buildUuidFilter(resourceKind)) .getFilters(); }; @@ -480,19 +372,20 @@ const buildDateFilter = (date?: string): string => { return date ? date : ''; }; -export const initAdvanceFormProjectsTree = () => +export const initAdvancedFormProjectsTree = () => (dispatch: Dispatch) => { - dispatch(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)); + dispatch(initUserProject(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)); }; -export const changeAdvanceFormProperty = (property: string, value: PropertyValue[] | string = '') => +export const changeAdvancedFormProperty = (propertyField: string, value: PropertyValue[] | string = '') => (dispatch: Dispatch) => { - dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value)); + dispatch(change(SEARCH_BAR_ADVANCED_FORM_NAME, propertyField, value)); }; -export const updateAdvanceFormProperties = (propertyValues: PropertyValue) => +export const resetAdvancedFormProperty = (propertyField: string) => (dispatch: Dispatch) => { - dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues)); + dispatch(change(SEARCH_BAR_ADVANCED_FORM_NAME, propertyField, null)); + dispatch(untouch(SEARCH_BAR_ADVANCED_FORM_NAME, propertyField)); }; export const moveUp = () =>