fixed-type-filter-in-search-results
[arvados-workbench2.git] / src / store / search-bar / search-bar-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ofType, unionize, UnionOf } from "~/common/unionize";
6 import { GroupContentsResource, GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
7 import { Dispatch } from 'redux';
8 import { arrayPush, change, initialize } from 'redux-form';
9 import { RootState } from '~/store/store';
10 import { initUserProject, treePickerActions } from '~/store/tree-picker/tree-picker-actions';
11 import { ServiceRepository } from '~/services/services';
12 import { FilterBuilder } from "~/services/api/filter-builder";
13 import { ResourceKind } from '~/models/resource';
14 import { GroupClass } from '~/models/group';
15 import { SearchView } from '~/store/search-bar/search-bar-reducer';
16 import { navigateTo, navigateToSearchResults } from '~/store/navigation/navigation-action';
17 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
18 import { PropertyValue, SearchBarAdvanceFormData } from '~/models/search-bar';
19 import { debounce } from 'debounce';
20 import * as _ from "lodash";
21 import { getModifiedKeysValues } from "~/common/objects";
22 import { activateSearchBarProject } from "~/store/search-bar/search-bar-tree-actions";
23 import { Session } from "~/models/session";
24 import { searchResultsPanelActions } from "~/store/search-results-panel/search-results-panel-actions";
25 import { ListResults } from "~/services/common-service/common-service";
26
27 export const searchBarActions = unionize({
28     SET_CURRENT_VIEW: ofType<string>(),
29     OPEN_SEARCH_VIEW: ofType<{}>(),
30     CLOSE_SEARCH_VIEW: ofType<{}>(),
31     SET_SEARCH_RESULTS: ofType<GroupContentsResource[]>(),
32     SET_SEARCH_VALUE: ofType<string>(),
33     SET_SAVED_QUERIES: ofType<SearchBarAdvanceFormData[]>(),
34     SET_RECENT_QUERIES: ofType<string[]>(),
35     UPDATE_SAVED_QUERY: ofType<SearchBarAdvanceFormData[]>(),
36     SET_SELECTED_ITEM: ofType<string>(),
37     MOVE_UP: ofType<{}>(),
38     MOVE_DOWN: ofType<{}>(),
39     SELECT_FIRST_ITEM: ofType<{}>()
40 });
41
42 export type SearchBarActions = UnionOf<typeof searchBarActions>;
43
44 export const SEARCH_BAR_ADVANCE_FORM_NAME = 'searchBarAdvanceFormName';
45
46 export const SEARCH_BAR_ADVANCE_FORM_PICKER_ID = 'searchBarAdvanceFormPickerId';
47
48 export const DEFAULT_SEARCH_DEBOUNCE = 1000;
49
50 export const goToView = (currentView: string) => searchBarActions.SET_CURRENT_VIEW(currentView);
51
52 export const saveRecentQuery = (query: string) =>
53     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) =>
54         services.searchService.saveRecentQuery(query);
55
56
57 export const loadRecentQueries = () =>
58     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
59         const recentQueries = services.searchService.getRecentQueries();
60         dispatch(searchBarActions.SET_RECENT_QUERIES(recentQueries));
61         return recentQueries;
62     };
63
64 export const searchData = (searchValue: string) =>
65     async (dispatch: Dispatch, getState: () => RootState) => {
66         const currentView = getState().searchBar.currentView;
67         dispatch(searchResultsPanelActions.CLEAR());
68         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
69         if (searchValue.length > 0) {
70             dispatch<any>(searchGroups(searchValue, 5));
71             if (currentView === SearchView.BASIC) {
72                 dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
73                 dispatch(navigateToSearchResults);
74             }
75         }
76     };
77
78 export const searchAdvanceData = (data: SearchBarAdvanceFormData) =>
79     async (dispatch: Dispatch) => {
80         dispatch<any>(saveQuery(data));
81         dispatch(searchResultsPanelActions.CLEAR());
82         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
83         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
84         dispatch(navigateToSearchResults);
85     };
86
87 export const setSearchValueFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) =>
88     (dispatch: Dispatch, getState: () => RootState) => {
89         const searchValue = getState().searchBar.searchValue;
90         const value = getQueryFromAdvancedData({
91             ...data,
92             searchValue
93         }, prevData);
94         dispatch(searchBarActions.SET_SEARCH_VALUE(value));
95     };
96
97 export const setAdvancedDataFromSearchValue = (search: string) =>
98     async (dispatch: Dispatch) => {
99         const data = getAdvancedDataFromQuery(search);
100         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
101         if (data.projectUuid) {
102             await dispatch<any>(activateSearchBarProject(data.projectUuid));
103             dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID, id: data.projectUuid }));
104         }
105     };
106
107 const saveQuery = (data: SearchBarAdvanceFormData) =>
108     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
109         const savedQueries = services.searchService.getSavedQueries();
110         if (data.saveQuery && data.queryName) {
111             const filteredQuery = savedQueries.find(query => query.queryName === data.queryName);
112             data.searchValue = getState().searchBar.searchValue;
113             if (filteredQuery) {
114                 services.searchService.editSavedQueries(data);
115                 dispatch(searchBarActions.UPDATE_SAVED_QUERY(savedQueries));
116                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully updated', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
117             } else {
118                 services.searchService.saveQuery(data);
119                 dispatch(searchBarActions.SET_SAVED_QUERIES(savedQueries));
120                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Query has been successfully saved', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
121             }
122         }
123     };
124
125 export const deleteSavedQuery = (id: number) =>
126     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
127         services.searchService.deleteSavedQuery(id);
128         const savedSearchQueries = services.searchService.getSavedQueries();
129         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
130         return savedSearchQueries || [];
131     };
132
133 export const editSavedQuery = (data: SearchBarAdvanceFormData) =>
134     (dispatch: Dispatch<any>) => {
135         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.ADVANCED));
136         dispatch(searchBarActions.SET_SEARCH_VALUE(getQueryFromAdvancedData(data)));
137         dispatch<any>(initialize(SEARCH_BAR_ADVANCE_FORM_NAME, data));
138     };
139
140 export const openSearchView = () =>
141     (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
142         const savedSearchQueries = services.searchService.getSavedQueries();
143         dispatch(searchBarActions.SET_SAVED_QUERIES(savedSearchQueries));
144         dispatch(loadRecentQueries());
145         dispatch(searchBarActions.OPEN_SEARCH_VIEW());
146         dispatch(searchBarActions.SELECT_FIRST_ITEM());
147     };
148
149 export const closeSearchView = () =>
150     (dispatch: Dispatch<any>) => {
151         dispatch(searchBarActions.SET_SELECTED_ITEM(''));
152         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
153     };
154
155 export const closeAdvanceView = () =>
156     (dispatch: Dispatch<any>) => {
157         dispatch(searchBarActions.SET_SEARCH_VALUE(''));
158         dispatch(treePickerActions.DEACTIVATE_TREE_PICKER_NODE({ pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
159         dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
160     };
161
162 export const navigateToItem = (uuid: string) =>
163     (dispatch: Dispatch<any>) => {
164         dispatch(searchBarActions.SET_SELECTED_ITEM(''));
165         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
166         dispatch(navigateTo(uuid));
167     };
168
169 export const changeData = (searchValue: string) =>
170     (dispatch: Dispatch, getState: () => RootState) => {
171         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
172         const currentView = getState().searchBar.currentView;
173         const searchValuePresent = searchValue.length > 0;
174
175         if (currentView === SearchView.ADVANCED) {
176             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
177         } else if (searchValuePresent) {
178             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
179             dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue));
180             debounceStartSearch(dispatch);
181         } else {
182             dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
183             dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
184             dispatch(searchBarActions.SELECT_FIRST_ITEM());
185         }
186     };
187
188 export const submitData = (event: React.FormEvent<HTMLFormElement>) =>
189     (dispatch: Dispatch, getState: () => RootState) => {
190         event.preventDefault();
191         const searchValue = getState().searchBar.searchValue;
192         dispatch<any>(saveRecentQuery(searchValue));
193         dispatch<any>(loadRecentQueries());
194         dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
195         dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
196         dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
197         dispatch(searchResultsPanelActions.CLEAR());
198         dispatch(navigateToSearchResults);
199     };
200
201 const debounceStartSearch = debounce((dispatch: Dispatch) => dispatch<any>(startSearch()), DEFAULT_SEARCH_DEBOUNCE);
202
203 const startSearch = () =>
204     (dispatch: Dispatch, getState: () => RootState) => {
205         const searchValue = getState().searchBar.searchValue;
206         dispatch<any>(searchData(searchValue));
207     };
208
209 const searchGroups = (searchValue: string, limit: number) =>
210     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
211         const currentView = getState().searchBar.currentView;
212
213         if (searchValue || currentView === SearchView.ADVANCED) {
214             const sq = parseSearchQuery(searchValue);
215             const clusterId = getSearchQueryFirstProp(sq, 'cluster');
216             const sessions = getSearchSessions(clusterId, getState().auth.sessions);
217             const lists: ListResults<GroupContentsResource>[] = await Promise.all(sessions.map(session => {
218                 const filters = getFilters('name', searchValue, sq);
219                 return services.groupsService.contents('', {
220                     filters,
221                     limit,
222                     recursive: true
223                 }, session);
224             }));
225
226             const items = lists.reduce((items, list) => items.concat(list.items), [] as GroupContentsResource[]);
227             dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
228         }
229     };
230
231 const buildQueryFromKeyMap = (data: any, keyMap: string[][], mode: 'rebuild' | 'reuse') => {
232     let value = data.searchValue;
233
234     const addRem = (field: string, key: string) => {
235         const v = data[key];
236
237         if (data.hasOwnProperty(key)) {
238             const pattern = v === false
239                 ? `${field.replace(':', '\\:\\s*')}\\s*`
240                 : `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`;
241             value = value.replace(new RegExp(pattern), '');
242         }
243
244         if (v) {
245             const nv = v === true
246                 ? `${field}`
247                 : `${field}:${v}`;
248
249             if (mode === 'rebuild') {
250                 value = value + ' ' + nv;
251             } else {
252                 value = nv + ' ' + value;
253             }
254         }
255     };
256
257     keyMap.forEach(km => addRem(km[0], km[1]));
258
259     return value;
260 };
261
262 export const getQueryFromAdvancedData = (data: SearchBarAdvanceFormData, prevData?: SearchBarAdvanceFormData) => {
263     let value = '';
264
265     const flatData = (data: SearchBarAdvanceFormData) => {
266         const fo = {
267             searchValue: data.searchValue,
268             type: data.type,
269             cluster: data.cluster,
270             projectUuid: data.projectUuid,
271             inTrash: data.inTrash,
272             dateFrom: data.dateFrom,
273             dateTo: data.dateTo,
274         };
275         (data.properties || []).forEach(p => fo[`prop-${p.key}`] = p.value);
276         return fo;
277     };
278
279     const keyMap = [
280         ['type', 'type'],
281         ['cluster', 'cluster'],
282         ['project', 'projectUuid'],
283         ['is:trashed', 'inTrash'],
284         ['from', 'dateFrom'],
285         ['to', 'dateTo']
286     ];
287     _.union(data.properties, prevData ? prevData.properties : [])
288         .forEach(p => keyMap.push([`has:${p.key}`, `prop-${p.key}`]));
289
290     if (prevData) {
291         const obj = getModifiedKeysValues(flatData(data), flatData(prevData));
292         value = buildQueryFromKeyMap({
293             searchValue: data.searchValue,
294             ...obj
295         } as SearchBarAdvanceFormData, keyMap, "reuse");
296     } else {
297         value = buildQueryFromKeyMap(flatData(data), keyMap, "rebuild");
298     }
299
300     value = value.trim();
301     return value;
302 };
303
304 export class ParseSearchQuery {
305     hasKeywords: boolean;
306     values: string[];
307     properties: {
308         [key: string]: string[]
309     };
310 }
311
312 export const parseSearchQuery: (query: string) => ParseSearchQuery = (searchValue: string) => {
313     const keywords = [
314         'type:',
315         'cluster:',
316         'project:',
317         'is:',
318         'from:',
319         'to:',
320         'has:'
321     ];
322
323     const hasKeywords = (search: string) => keywords.reduce((acc, keyword) => acc + (search.includes(keyword) ? 1 : 0), 0);
324     let keywordsCnt = 0;
325
326     const properties = {};
327
328     keywords.forEach(k => {
329         let p = searchValue.indexOf(k);
330         const key = k.substr(0, k.length - 1);
331
332         while (p >= 0) {
333             const l = searchValue.length;
334             keywordsCnt += 1;
335
336             let v = '';
337             let i = p + k.length;
338             while (i < l && searchValue[i] === ' ') {
339                 ++i;
340             }
341             const vp = i;
342             while (i < l && searchValue[i] !== ' ') {
343                 v += searchValue[i];
344                 ++i;
345             }
346
347             if (hasKeywords(v)) {
348                 searchValue = searchValue.substr(0, p) + searchValue.substr(vp);
349             } else {
350                 if (v !== '') {
351                     if (!properties[key]) {
352                         properties[key] = [];
353                     }
354                     properties[key].push(v);
355                 }
356                 searchValue = searchValue.substr(0, p) + searchValue.substr(i);
357             }
358             p = searchValue.indexOf(k);
359         }
360     });
361
362     const values = _.uniq(searchValue.split(' ').filter(v => v.length > 0));
363
364     return { hasKeywords: keywordsCnt > 0, values, properties };
365 };
366
367 export const getSearchQueryFirstProp = (sq: ParseSearchQuery, name: string) => sq.properties[name] && sq.properties[name][0];
368 export const getSearchQueryPropValue = (sq: ParseSearchQuery, name: string, value: string) => sq.properties[name] && sq.properties[name].find((v: string) => v === value);
369 export const getSearchQueryProperties = (sq: ParseSearchQuery): PropertyValue[] => {
370     if (sq.properties.has) {
371         return sq.properties.has.map((value: string) => {
372             const v = value.split(':');
373             return {
374                 key: v[0],
375                 value: v[1]
376             };
377         });
378     }
379     return [];
380 };
381
382 export const getAdvancedDataFromQuery = (query: string): SearchBarAdvanceFormData => {
383     const sq = parseSearchQuery(query);
384
385     return {
386         searchValue: sq.values.join(' '),
387         type: getSearchQueryFirstProp(sq, 'type') as ResourceKind,
388         cluster: getSearchQueryFirstProp(sq, 'cluster'),
389         projectUuid: getSearchQueryFirstProp(sq, 'project'),
390         inTrash: getSearchQueryPropValue(sq, 'is', 'trashed') !== undefined,
391         dateFrom: getSearchQueryFirstProp(sq, 'from'),
392         dateTo: getSearchQueryFirstProp(sq, 'to'),
393         properties: getSearchQueryProperties(sq),
394         saveQuery: false,
395         queryName: ''
396     };
397 };
398
399 export const getSearchSessions = (clusterId: string | undefined, sessions: Session[]): Session[] => {
400     return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId));
401 };
402
403 export const getFilters = (filterName: string, searchValue: string, sq: ParseSearchQuery): string => {
404     const filter = new FilterBuilder();
405
406     const resourceKind = getSearchQueryFirstProp(sq, 'type') as ResourceKind;
407
408     let prefix = '';
409     switch (resourceKind) {
410         case ResourceKind.COLLECTION:
411             prefix = GroupContentsResourcePrefix.COLLECTION;
412             break;
413         case ResourceKind.PROCESS:
414             prefix = GroupContentsResourcePrefix.PROCESS;
415             break;
416         default:
417             prefix = GroupContentsResourcePrefix.PROJECT;
418             break;
419     }
420
421     const isTrashed = getSearchQueryPropValue(sq, 'is', 'trashed');
422
423     if (!sq.hasKeywords) {
424         filter
425             .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
426             .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
427             .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS);
428
429         if (isTrashed) {
430             filter.addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS);
431         }
432     } else {
433         if (prefix) {
434             sq.values.forEach(v =>
435                 filter.addILike(filterName, v, prefix)
436             );
437         } else {
438             sq.values.forEach(v => {
439                 filter
440                     .addILike(filterName, v, GroupContentsResourcePrefix.COLLECTION)
441                     .addILike(filterName, v, GroupContentsResourcePrefix.PROJECT);
442
443                 if (isTrashed) {
444                     filter.addILike(filterName, v, GroupContentsResourcePrefix.PROCESS);
445                 }
446             });
447         }
448
449         if (isTrashed) {
450             filter.addEqual("is_trashed", true);
451             console.log(filter);
452         }
453
454         const projectUuid = getSearchQueryFirstProp(sq, 'project');
455         if (projectUuid) {
456             filter.addEqual('uuid', projectUuid, prefix);
457         }
458
459         const dateFrom = getSearchQueryFirstProp(sq, 'from');
460         if (dateFrom) {
461             filter.addGte('modified_at', buildDateFilter(dateFrom));
462         }
463
464         const dateTo = getSearchQueryFirstProp(sq, 'to');
465         if (dateTo) {
466             filter.addLte('modified_at', buildDateFilter(dateTo));
467         }
468
469         const props = getSearchQueryProperties(sq);
470         props.forEach(p => {
471             if (p.value) {
472                 filter.addILike(`properties.${p.key}`, p.value);
473             }
474             filter.addExists(p.key);
475         });
476     }
477
478     return filter
479         .addEqual('groupClass', GroupClass.PROJECT, GroupContentsResourcePrefix.PROJECT)
480         .addIsA("uuid", buildUuidFilter(resourceKind))
481         .getFilters();
482 };
483
484 const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => {
485     return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS];
486 };
487
488 const buildDateFilter = (date?: string): string => {
489     return date ? date : '';
490 };
491
492 export const initAdvanceFormProjectsTree = () =>
493     (dispatch: Dispatch) => {
494         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
495     };
496
497 export const changeAdvanceFormProperty = (property: string, value: PropertyValue[] | string = '') =>
498     (dispatch: Dispatch) => {
499         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
500     };
501
502 export const updateAdvanceFormProperties = (propertyValues: PropertyValue) =>
503     (dispatch: Dispatch) => {
504         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
505     };
506
507 export const moveUp = () =>
508     (dispatch: Dispatch) => {
509         dispatch(searchBarActions.MOVE_UP());
510     };
511
512 export const moveDown = () =>
513     (dispatch: Dispatch) => {
514         dispatch(searchBarActions.MOVE_DOWN());
515     };