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, isResourceUuid, extractUuidKind, 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 { ClusterObjectType, PropertyValue, SearchBarAdvanceFormData } from '~/models/search-bar';
+import { PropertyValue, SearchBarAdvanceFormData } from '~/models/search-bar';
import { debounce } from 'debounce';
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';
export const searchBarActions = unionize({
SET_CURRENT_VIEW: ofType<string>(),
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));
export const searchAdvanceData = (data: SearchBarAdvanceFormData) =>
async (dispatch: Dispatch) => {
dispatch<any>(saveQuery(data));
+ dispatch(searchResultsPanelActions.CLEAR());
dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
dispatch(navigateToSearchResults);
} 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);
+ }
};
-const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
const startSearch = () =>
(dispatch: Dispatch, getState: () => RootState) => {
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);
+ 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 (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), '');
}
dateFrom: data.dateFrom,
dateTo: data.dateTo,
};
- (data.properties || []).forEach(p => fo[`prop-${p.key}`] = p.value);
+ (data.properties || []).forEach(p => fo[`prop-"${p.key}"`] = `"${p.value}"`);
return fo;
};
['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.key}"`, `prop-"${p.key}"`]));
if (prevData) {
+ const fd = flatData(data);
+ const pfd = flatData(prevData);
const obj = getModifiedKeysValues(flatData(data), flatData(prevData));
value = buildQueryFromKeyMap({
searchValue: data.searchValue,
return value;
};
-export interface 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 };
-};
-
-const getFirstProp = (sq: ParseSearchQuery, name: string) => sq.properties[name] && sq.properties[name][0];
-const getPropValue = (sq: ParseSearchQuery, name: string, value: string) => sq.properties[name] && sq.properties[name].find((v: string) => v === value);
-const getProperties = (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);
-
+ const { tokens, searchString } = parser.parseSearchQuery(query);
+ const getValue = parser.getValue(tokens);
return {
- searchValue: sq.values.join(' '),
- type: getFirstProp(sq, 'type') as ResourceKind,
- cluster: getFirstProp(sq, 'cluster') as ClusterObjectType,
- projectUuid: getFirstProp(sq, 'project'),
- inTrash: getPropValue(sq, 'is', 'trashed') !== undefined,
- dateFrom: getFirstProp(sq, 'from'),
- dateTo: getFirstProp(sq, 'to'),
- properties: getProperties(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: parser.getProperties(tokens),
saveQuery: false,
queryName: ''
};
};
-export const getFilters = (filterName: string, searchValue: string): string => {
+export const getSearchSessions = (clusterId: string | undefined, sessions: Session[]): Session[] => {
+ return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId));
+};
+
+export const queryToFilters = (query: string) => {
+ const data = getAdvancedDataFromQuery(query);
const filter = new FilterBuilder();
- const sq = parseSearchQuery(searchValue);
-
- const resourceKind = getFirstProp(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;
- }
+ const resourceKind = data.type;
- if (!sq.hasKeywords) {
- filter
- .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
- .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
- .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT);
- } 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.PROCESS)
- .addILike(filterName, v, GroupContentsResourcePrefix.PROJECT);
- });
- }
+ if (data.searchValue) {
+ filter.addFullTextSearch(data.searchValue);
+ }
- if (getPropValue(sq, 'is', 'trashed')) {
- filter.addEqual("is_trashed", true);
- }
+ if (data.projectUuid) {
+ filter.addEqual('ownerUuid', data.projectUuid);
+ }
- const projectUuid = getFirstProp(sq, 'project');
- if (projectUuid) {
- filter.addEqual('uuid', projectUuid, prefix);
- }
+ if (data.dateFrom) {
+ filter.addGte('modified_at', buildDateFilter(data.dateFrom));
+ }
- const dateFrom = getFirstProp(sq, 'from');
- if (dateFrom) {
- filter.addGte('modified_at', buildDateFilter(dateFrom));
- }
+ if (data.dateTo) {
+ filter.addLte('modified_at', buildDateFilter(data.dateTo));
+ }
- const dateTo = getFirstProp(sq, 'to');
- if (dateTo) {
- filter.addLte('modified_at', buildDateFilter(dateTo));
+ data.properties.forEach(p => {
+ if (p.value) {
+ filter
+ .addILike(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT)
+ .addILike(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION);
}
-
- const props = getProperties(sq);
- props.forEach(p => {
- // 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();
};