Skip to content

Commit

Permalink
[Security Solution] Fix timeline querybar filters persistance (#80320) (
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski authored Oct 14, 2020
1 parent 61a39fe commit 62d18bf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ import { mockBrowserFields } from '../../../../common/containers/source/mock';
import { convertKueryToElasticSearchQuery } from '../../../../common/lib/keury';
import { mockIndexPattern, TestProviders } from '../../../../common/mock';
import { QueryBar } from '../../../../common/components/query_bar';
import { FilterManager } from '../../../../../../../../src/plugins/data/public';
import { esFilters, FilterManager } from '../../../../../../../../src/plugins/data/public';
import { mockDataProviders } from '../data_providers/mock/mock_data_providers';
import { buildGlobalQuery } from '../helpers';

import { QueryBarTimeline, QueryBarTimelineComponentProps, getDataProviderFilter } from './index';
import {
QueryBarTimeline,
QueryBarTimelineComponentProps,
getDataProviderFilter,
TIMELINE_FILTER_DROP_AREA,
} from './index';

const mockUiSettingsForFilterManager = coreMock.createStart().uiSettings;

Expand All @@ -39,13 +44,43 @@ describe('Timeline QueryBar ', () => {
});

test('check if we format the appropriate props to QueryBar', () => {
const filters = [
{
$state: { store: esFilters.FilterStateStore.APP_STATE },
meta: {
alias: null,
controlledBy: TIMELINE_FILTER_DROP_AREA,
disabled: false,
index: undefined,
key: 'event.category',
negate: true,
params: { query: 'file' },
type: 'phrase',
},
query: { match: { 'event.category': { query: 'file', type: 'phrase' } } },
},
{
$state: { store: esFilters.FilterStateStore.APP_STATE },
meta: {
alias: null,
controlledBy: undefined,
disabled: false,
index: undefined,
key: 'event.category',
negate: true,
params: { query: 'process' },
type: 'phrase',
},
query: { match: { 'event.category': { query: 'process', type: 'phrase' } } },
},
];
const wrapper = mount(
<TestProviders>
<QueryBarTimeline
applyKqlFilterQuery={mockApplyKqlFilterQuery}
browserFields={mockBrowserFields}
dataProviders={mockDataProviders}
filters={[]}
filters={filters}
filterManager={new FilterManager(mockUiSettingsForFilterManager)}
filterQuery={{ expression: 'here: query', kind: 'kuery' }}
filterQueryDraft={{ expression: 'here: query', kind: 'kuery' }}
Expand All @@ -72,6 +107,8 @@ describe('Timeline QueryBar ', () => {
expect(queryBarProps.dateRangeTo).toEqual('now');
expect(queryBarProps.filterQuery).toEqual({ query: 'here: query', language: 'kuery' });
expect(queryBarProps.savedQuery).toEqual(null);
expect(queryBarProps.filters).toHaveLength(1);
expect(queryBarProps.filters[0].query).toEqual(filters[1].query);
});

describe('#onChangeQuery', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ export interface QueryBarTimelineComponentProps {
updateReduxTime: DispatchUpdateReduxTime;
}

const timelineFilterDropArea = 'timeline-filter-drop-area';
export const TIMELINE_FILTER_DROP_AREA = 'timeline-filter-drop-area';

const getNonDropAreaFilters = (filters: Filter[] = []) =>
filters.filter((f: Filter) => f.meta.controlledBy !== TIMELINE_FILTER_DROP_AREA);

export const QueryBarTimeline = memo<QueryBarTimelineComponentProps>(
({
Expand Down Expand Up @@ -91,7 +94,9 @@ export const QueryBarTimeline = memo<QueryBarTimelineComponentProps>(
query: filterQuery != null ? filterQuery.expression : '',
language: filterQuery != null ? filterQuery.kind : 'kuery',
});
const [queryBarFilters, setQueryBarFilters] = useState<Filter[]>([]);
const [queryBarFilters, setQueryBarFilters] = useState<Filter[]>(
getNonDropAreaFilters(filters)
);
const [dataProvidersDsl, setDataProvidersDsl] = useState<string>(
convertKueryToElasticSearchQuery(buildGlobalQuery(dataProviders, browserFields), indexPattern)
);
Expand All @@ -106,9 +111,7 @@ export const QueryBarTimeline = memo<QueryBarTimelineComponentProps>(
filterManager.getUpdates$().subscribe({
next: () => {
if (isSubscribed) {
const filterWithoutDropArea = filterManager
.getFilters()
.filter((f: Filter) => f.meta.controlledBy !== timelineFilterDropArea);
const filterWithoutDropArea = getNonDropAreaFilters(filterManager.getFilters());
setFilters(filterWithoutDropArea);
setQueryBarFilters(filterWithoutDropArea);
}
Expand All @@ -124,9 +127,7 @@ export const QueryBarTimeline = memo<QueryBarTimelineComponentProps>(
}, []);

useEffect(() => {
const filterWithoutDropArea = filterManager
.getFilters()
.filter((f: Filter) => f.meta.controlledBy !== timelineFilterDropArea);
const filterWithoutDropArea = getNonDropAreaFilters(filterManager.getFilters());
if (!deepEqual(filters, filterWithoutDropArea)) {
filterManager.setFilters(filters);
}
Expand Down Expand Up @@ -175,7 +176,7 @@ export const QueryBarTimeline = memo<QueryBarTimelineComponentProps>(
...mySavedQuery,
attributes: {
...mySavedQuery.attributes,
filters: filters.filter((f) => f.meta.controlledBy !== timelineFilterDropArea),
filters: getNonDropAreaFilters(filters),
},
});
}
Expand Down Expand Up @@ -250,7 +251,7 @@ export const QueryBarTimeline = memo<QueryBarTimelineComponentProps>(
const dataProviderFilterExists =
newSavedQuery.attributes.filters != null
? newSavedQuery.attributes.filters.findIndex(
(f) => f.meta.controlledBy === timelineFilterDropArea
(f) => f.meta.controlledBy === TIMELINE_FILTER_DROP_AREA
)
: -1;
savedQueryServices.saveQuery(
Expand Down Expand Up @@ -311,8 +312,8 @@ export const getDataProviderFilter = (dataProviderDsl: string): Filter => {
return {
...dslObject,
meta: {
alias: timelineFilterDropArea,
controlledBy: timelineFilterDropArea,
alias: TIMELINE_FILTER_DROP_AREA,
controlledBy: TIMELINE_FILTER_DROP_AREA,
negate: false,
disabled: false,
type: 'custom',
Expand Down

0 comments on commit 62d18bf

Please sign in to comment.