//
// SPDX-License-Identifier: AGPL-3.0
-import { unionize, ofType, UnionOf } from "~/common/unionize";
-import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
+import { ofType, unionize, UnionOf } from "common/unionize";
+import { GroupContentsResource, GroupContentsResourcePrefix } from 'services/groups-service/groups-service';
import { Dispatch } from 'redux';
-import { change, arrayPush } from 'redux-form';
-import { RootState } from '~/store/store';
-import { initUserProject } 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 { SearchView } from '~/store/search-bar/search-bar-reducer';
-import { navigateToSearchResults, navigateTo } from '~/store/navigation/navigation-action';
-import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
-import { initialize } from 'redux-form';
-import { SearchBarAdvanceFormData, PropertyValues, ClusterObjectType } from '~/models/search-bar';
-import { debounce } from 'debounce';
-import * as _ from "lodash";
+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 { 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";
export const searchBarActions = unionize({
SET_CURRENT_VIEW: ofType<string>(),
CLOSE_SEARCH_VIEW: ofType<{}>(),
SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
SET_SEARCH_VALUE: ofType<string>(),
- SET_SAVED_QUERIES: ofType<SearchBarAdvanceFormData[]>(),
+ SET_SAVED_QUERIES: ofType<SearchBarAdvancedFormData[]>(),
SET_RECENT_QUERIES: ofType<string[]>(),
- UPDATE_SAVED_QUERY: ofType<SearchBarAdvanceFormData[]>(),
+ UPDATE_SAVED_QUERY: ofType<SearchBarAdvancedFormData[]>(),
SET_SELECTED_ITEM: ofType<string>(),
MOVE_UP: ofType<{}>(),
MOVE_DOWN: ofType<{}>(),
export type SearchBarActions = UnionOf<typeof searchBarActions>;
-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;
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<any>(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<any>(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) =>
- (dispatch: Dispatch) => {
- let value = '';
- if (data.type) {
- value += ` type:${data.type}`;
- }
- if (data.cluster) {
- value += ` cluster:${data.cluster}`;
- }
+export const setSearchValueFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) =>
+ (dispatch: Dispatch, getState: () => RootState) => {
+ const searchValue = getState().searchBar.searchValue;
+ const value = getQueryFromAdvancedData({
+ ...data,
+ searchValue
+ }, prevData);
+ dispatch(searchBarActions.SET_SEARCH_VALUE(value));
+ };
+
+export const setAdvancedDataFromSearchValue = (search: string, vocabulary: Vocabulary) =>
+ async (dispatch: Dispatch) => {
+ const data = getAdvancedDataFromQuery(search, vocabulary);
+ dispatch<any>(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data));
if (data.projectUuid) {
- value += ` project:${data.projectUuid}`;
+ await dispatch<any>(activateSearchBarProject(data.projectUuid));
+ dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID, id: data.projectUuid }));
}
- if (data.inTrash) {
- value += ` is:trashed`;
- }
- if (data.dateFrom) {
- value += ` from:${data.dateFrom}`;
- }
- if (data.dateTo) {
- value += ` from:${data.dateTo}`;
- }
- value = value.substr(1) + data.searchQuery;
- dispatch(searchBarActions.SET_SEARCH_VALUE(value));
};
-const saveQuery = (data: SearchBarAdvanceFormData) =>
+const saveQuery = (data: SearchBarAdvancedFormData) =>
(dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
const savedQueries = services.searchService.getSavedQueries();
- if (data.saveQuery && data.searchQuery) {
- const filteredQuery = savedQueries.find(query => query.searchQuery === data.searchQuery);
+ if (data.saveQuery && data.queryName) {
+ const filteredQuery = savedQueries.find(query => query.queryName === data.queryName);
+ data.searchValue = getState().searchBar.searchValue;
if (filteredQuery) {
services.searchService.editSavedQueries(data);
dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedQueries));
return savedSearchQueries || [];
};
-export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
+export const editSavedQuery = (data: SearchBarAdvancedFormData) =>
(dispatch: Dispatch<any>) => {
dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
- dispatch(searchBarActions.SET_SEARCH_VALUE(data.searchQuery));
- dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
+ dispatch(searchBarActions.SET_SEARCH_VALUE(getQueryFromAdvancedData(data)));
+ dispatch<any>(initialize(SEARCH_BAR_ADVANCED_FORM_NAME, data));
};
export const openSearchView = () =>
export const closeAdvanceView = () =>
(dispatch: Dispatch<any>) => {
dispatch(searchBarActions.SET_SEARCH_VALUE(''));
+ dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
};
const searchValuePresent = searchValue.length > 0;
if (currentView === SearchView.ADVANCED) {
-
+ dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
} 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([]));
dispatch<any>(saveRecentQuery(searchValue));
dispatch<any>(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<any>(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<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
-
-const startSearch = () =>
- (dispatch: Dispatch, getState: () => RootState) => {
- const searchValue = getState().searchBar.searchValue;
- dispatch<any>(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 filters = getFilters('name', searchValue);
- const { items } = await services.groupsService.contents('', {
- filters,
- limit,
- recursive: true
- });
+ const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
+ const sessions = getSearchSessions(clusterId, getState().auth.sessions);
+ const lists: ListResults<GroupContentsResource>[] = await Promise.all(sessions.map(session => {
+ const filters = queryToFilters(searchValue, session.apiRevision);
+ 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));
}
};
-export const parseQuery: (searchValue: string) => { hasKeywords: boolean; values: string[]; properties: any } = (searchValue: string) => {
- const keywords = [
- 'type:',
- 'cluster:',
- 'project:',
- 'is:',
- 'from:',
- 'to:'
- ];
-
- const hasKeywords = (search: string) => keywords.reduce((acc, keyword) => acc + search.indexOf(keyword) >= 0 ? 1 : 0, 0);
- let keywordsCnt = 0;
+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) {
+ pattern = `${field.replace(':', '\\:\\s*')}\\s*`;
+ } else if (key.startsWith('prop-')) {
+ // On properties, only remove key:value duplicates, allowing
+ // multiple properties with the same key.
+ 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}`;
+ // 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;
+};
- const properties = {};
+export const getQueryFromAdvancedData = (data: SearchBarAdvancedFormData, prevData?: SearchBarAdvancedFormData) => {
+ let value = '';
+
+ const flatData = (data: SearchBarAdvancedFormData) => {
+ const fo = {
+ searchValue: data.searchValue,
+ type: data.type,
+ 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}"`] = `"${p.valueID || p.value}"`
+ );
+ return fo;
+ };
- keywords.forEach(k => {
- const p = searchValue.indexOf(k);
- const l = searchValue.length;
- if (p >= 0) {
- keywordsCnt += 1;
+ const keyMap = [
+ ['type', 'type'],
+ ['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}":"${p.valueID || p.value}"`]
+ ));
- 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;
- }
+ const modified = getModifiedKeysValues(flatData(data), prevData ? flatData(prevData):{});
+ value = buildQueryFromKeyMap(
+ {searchValue: data.searchValue, ...modified} as SearchBarAdvancedFormData, keyMap);
- if (hasKeywords(v)) {
- searchValue = searchValue.substr(0, p) + searchValue.substr(vp);
- } else {
- if (v !== '') {
- properties[k.substr(0, k.length - 1)] = v;
- }
- searchValue = searchValue.substr(0, p) + searchValue.substr(i);
- }
- }
- });
+ value = value.trim();
+ return value;
+};
- const values = _.uniq(searchValue.split(' ').filter(v => v.length > 0));
+export const getAdvancedDataFromQuery = (query: string, vocabulary?: Vocabulary): SearchBarAdvancedFormData => {
+ const { tokens, searchString } = parser.parseSearchQuery(query);
+ const getValue = parser.getValue(tokens);
+ return {
+ searchValue: searchString,
+ type: getValue(Keywords.TYPE) as ResourceKind,
+ 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
+ ? 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: ''
+ };
+};
- return { hasKeywords: keywordsCnt > 0, values, properties };
+export const getSearchSessions = (clusterId: string | undefined, sessions: Session[]): Session[] => {
+ return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId));
};
-export const getFilters = (filterName: string, searchValue: string): string => {
+export const queryToFilters = (query: string, apiRevision: number) => {
+ const data = getAdvancedDataFromQuery(query);
const filter = new FilterBuilder();
+ const resourceKind = data.type;
- const pq = parseQuery(searchValue);
-
- if (!pq.hasKeywords) {
- filter
- .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
- .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
- .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT);
- } else {
-
- if (pq.properties.type) {
- pq.values.forEach(v => {
- let prefix = '';
- switch (ResourceKind[pq.properties.type]) {
- case ResourceKind.PROJECT:
- prefix = GroupContentsResourcePrefix.PROJECT;
- break;
- case ResourceKind.COLLECTION:
- prefix = GroupContentsResourcePrefix.COLLECTION;
- break;
- case ResourceKind.PROCESS:
- prefix = GroupContentsResourcePrefix.PROCESS;
- break;
- }
- if (prefix !== '') {
- filter.addILike(filterName, v, prefix);
- }
- });
- } else {
- pq.values.forEach(v => {
- filter
- .addILike(filterName, v, GroupContentsResourcePrefix.COLLECTION)
- .addILike(filterName, v, GroupContentsResourcePrefix.PROCESS)
- .addILike(filterName, v, GroupContentsResourcePrefix.PROJECT);
- });
- }
+ if (data.searchValue) {
+ filter.addFullTextSearch(data.searchValue);
+ }
- if (pq.properties.is && pq.properties.is === 'trashed') {
- }
+ if (data.projectUuid) {
+ filter.addEqual('owner_uuid', data.projectUuid);
+ }
- if (pq.properties.project) {
- filter.addEqual('owner_uuid', pq.properties.project, GroupContentsResourcePrefix.PROJECT);
- }
+ if (data.dateFrom) {
+ filter.addGte('modified_at', buildDateFilter(data.dateFrom));
+ }
- if (pq.properties.from) {
- filter.addGte('modified_at', buildDateFilter(pq.properties.from));
- }
+ if (data.dateTo) {
+ filter.addLte('modified_at', buildDateFilter(data.dateTo));
+ }
- if (pq.properties.to) {
- filter.addLte('modified_at', buildDateFilter(pq.properties.to));
+ data.properties.forEach(p => {
+ if (p.value) {
+ 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
- // .addIsA("uuid", buildUuidFilter(resourceKind))
- // .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
- // .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
- // .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
- // .addLte('modified_at', buildDateFilter(dateTo))
- // .addGte('modified_at', buildDateFilter(dateFrom))
- // .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
- }
+ filter.addExists(p.key);
+ });
return filter
- .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
+ .addIsA("uuid", buildUuidFilter(resourceKind))
.getFilters();
};
return date ? date : '';
};
-export const initAdvanceFormProjectsTree = () =>
+export const initAdvancedFormProjectsTree = () =>
(dispatch: Dispatch) => {
- dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
+ dispatch<any>(initUserProject(SEARCH_BAR_ADVANCED_FORM_PICKER_ID));
};
-export const changeAdvanceFormProperty = (property: string, value: PropertyValues[] | 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: PropertyValues) =>
+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 = () =>