Merge branch '14744-inappropriate-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         if (k) {
330             let p = searchValue.indexOf(k);
331             const key = k.substr(0, k.length - 1);
332
333             while (p >= 0) {
334                 const l = searchValue.length;
335                 keywordsCnt += 1;
336
337                 let v = '';
338                 let i = p + k.length;
339                 while (i < l && searchValue[i] === ' ') {
340                     ++i;
341                 }
342                 const vp = i;
343                 while (i < l && searchValue[i] !== ' ') {
344                     v += searchValue[i];
345                     ++i;
346                 }
347
348                 if (hasKeywords(v)) {
349                     searchValue = searchValue.substr(0, p) + searchValue.substr(vp);
350                 } else {
351                     if (v !== '') {
352                         if (!properties[key]) {
353                             properties[key] = [];
354                         }
355                         properties[key].push(v);
356                     }
357                     searchValue = searchValue.substr(0, p) + searchValue.substr(i);
358                 }
359                 p = searchValue.indexOf(k);
360             }
361         }
362     });
363
364     const values = _.uniq(searchValue.split(' ').filter(v => v.length > 0));
365
366     return { hasKeywords: keywordsCnt > 0, values, properties };
367 };
368
369 export const getSearchQueryFirstProp = (sq: ParseSearchQuery, name: string) => sq.properties[name] && sq.properties[name][0];
370 export const getSearchQueryPropValue = (sq: ParseSearchQuery, name: string, value: string) => sq.properties[name] && sq.properties[name].find((v: string) => v === value);
371 export const getSearchQueryProperties = (sq: ParseSearchQuery): PropertyValue[] => {
372     if (sq.properties.has) {
373         return sq.properties.has.map((value: string) => {
374             const v = value.split(':');
375             return {
376                 key: v[0],
377                 value: v[1]
378             };
379         });
380     }
381     return [];
382 };
383
384 export const getAdvancedDataFromQuery = (query: string): SearchBarAdvanceFormData => {
385     const sq = parseSearchQuery(query);
386
387     return {
388         searchValue: sq.values.join(' '),
389         type: getSearchQueryFirstProp(sq, 'type') as ResourceKind,
390         cluster: getSearchQueryFirstProp(sq, 'cluster'),
391         projectUuid: getSearchQueryFirstProp(sq, 'project'),
392         inTrash: getSearchQueryPropValue(sq, 'is', 'trashed') !== undefined,
393         dateFrom: getSearchQueryFirstProp(sq, 'from'),
394         dateTo: getSearchQueryFirstProp(sq, 'to'),
395         properties: getSearchQueryProperties(sq),
396         saveQuery: false,
397         queryName: ''
398     };
399 };
400
401 export const getSearchSessions = (clusterId: string | undefined, sessions: Session[]): Session[] => {
402     return sessions.filter(s => s.loggedIn && (!clusterId || s.clusterId === clusterId));
403 };
404
405 export const getFilters = (filterName: string, searchValue: string, sq: ParseSearchQuery): string => {
406     const filter = new FilterBuilder();
407
408     const resourceKind = getSearchQueryFirstProp(sq, 'type') as ResourceKind;
409
410     let prefix = '';
411     switch (resourceKind) {
412         case ResourceKind.COLLECTION:
413             prefix = GroupContentsResourcePrefix.COLLECTION;
414             break;
415         case ResourceKind.PROCESS:
416             prefix = GroupContentsResourcePrefix.PROCESS;
417             break;
418         default:
419             prefix = GroupContentsResourcePrefix.PROJECT;
420             break;
421     }
422
423     const isTrashed = getSearchQueryPropValue(sq, 'is', 'trashed');
424
425     if (!sq.hasKeywords) {
426         filter
427             .addILike(filterName, searchValue, GroupContentsResourcePrefix.COLLECTION)
428             .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROJECT)
429             .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS)
430             .addEqual('is_trashed', false, GroupContentsResourcePrefix.COLLECTION)
431             .addEqual('is_trashed', false, GroupContentsResourcePrefix.PROJECT);
432
433         if (isTrashed) {
434             filter.addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS);
435         }
436     } else {
437         if (prefix) {
438             sq.values.forEach(v =>
439                 filter.addILike(filterName, v, prefix)
440             );
441         } else {
442             sq.values.forEach(v => {
443                 filter
444                     .addILike(filterName, v, GroupContentsResourcePrefix.COLLECTION)
445                     .addILike(filterName, v, GroupContentsResourcePrefix.PROJECT)
446                     .addILike(filterName, v, GroupContentsResourcePrefix.PROCESS)
447                     .addEqual('is_trashed', false, GroupContentsResourcePrefix.COLLECTION)
448                     .addEqual('is_trashed', false, GroupContentsResourcePrefix.PROJECT);
449
450                 if (isTrashed) {
451                     filter.addILike(filterName, v, GroupContentsResourcePrefix.PROCESS);
452                 }
453             });
454         }
455
456         if (isTrashed) {
457             sq.values.forEach(v => {
458                 filter.addEqual('is_trashed', true, GroupContentsResourcePrefix.COLLECTION)
459                     .addEqual('is_trashed', true, GroupContentsResourcePrefix.PROJECT)
460                     .addILike(filterName, v, GroupContentsResourcePrefix.COLLECTION)
461                     .addILike(filterName, searchValue, GroupContentsResourcePrefix.PROCESS);
462             });
463         }
464
465         const projectUuid = getSearchQueryFirstProp(sq, 'project');
466         if (projectUuid) {
467             filter.addEqual('uuid', projectUuid, prefix);
468         }
469
470         const dateFrom = getSearchQueryFirstProp(sq, 'from');
471         if (dateFrom) {
472             filter.addGte('modified_at', buildDateFilter(dateFrom));
473         }
474
475         const dateTo = getSearchQueryFirstProp(sq, 'to');
476         if (dateTo) {
477             filter.addLte('modified_at', buildDateFilter(dateTo));
478         }
479
480         const props = getSearchQueryProperties(sq);
481         props.forEach(p => {
482             if (p.value) {
483                 filter.addILike(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.PROJECT)
484                     .addILike(`properties.${p.key}`, p.value, GroupContentsResourcePrefix.COLLECTION);
485             }
486             filter.addExists(p.key);
487         });
488     }
489
490     return filter
491         .addIsA("uuid", buildUuidFilter(resourceKind))
492         .getFilters();
493 };
494
495 const buildUuidFilter = (type?: ResourceKind): ResourceKind[] => {
496     return type ? [type] : [ResourceKind.PROJECT, ResourceKind.COLLECTION, ResourceKind.PROCESS];
497 };
498
499 const buildDateFilter = (date?: string): string => {
500     return date ? date : '';
501 };
502
503 export const initAdvanceFormProjectsTree = () =>
504     (dispatch: Dispatch) => {
505         dispatch<any>(initUserProject(SEARCH_BAR_ADVANCE_FORM_PICKER_ID));
506     };
507
508 export const changeAdvanceFormProperty = (property: string, value: PropertyValue[] | string = '') =>
509     (dispatch: Dispatch) => {
510         dispatch(change(SEARCH_BAR_ADVANCE_FORM_NAME, property, value));
511     };
512
513 export const updateAdvanceFormProperties = (propertyValues: PropertyValue) =>
514     (dispatch: Dispatch) => {
515         dispatch(arrayPush(SEARCH_BAR_ADVANCE_FORM_NAME, 'properties', propertyValues));
516     };
517
518 export const moveUp = () =>
519     (dispatch: Dispatch) => {
520         dispatch(searchBarActions.MOVE_UP());
521     };
522
523 export const moveDown = () =>
524     (dispatch: Dispatch) => {
525         dispatch(searchBarActions.MOVE_DOWN());
526     };