Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.x] [Visualize] Duplicated query filters in es request (#60106) #60519

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ esFilters: {
getPhraseFilterField: (filter: import("../common").PhraseFilter) => string;
getPhraseFilterValue: (filter: import("../common").PhraseFilter) => string | number | boolean;
getDisplayValueFromFilter: typeof getDisplayValueFromFilter;
compareFilters: (first: import("../common").Filter | import("../common").Filter[], second: import("../common").Filter | import("../common").Filter[], comparatorOptions?: import("./query/filter_manager/lib/compare_filters").FilterCompareOptions) => boolean;
COMPARE_ALL_OPTIONS: import("./query/filter_manager/lib/compare_filters").FilterCompareOptions;
compareFilters: (first: import("../common").Filter | import("../common").Filter[], second: import("../common").Filter | import("../common").Filter[], comparatorOptions?: import("../common").FilterCompareOptions) => boolean;
COMPARE_ALL_OPTIONS: import("../common").FilterCompareOptions;
generateFilters: typeof generateFilters;
onlyDisabledFiltersChanged: (newFilters?: import("../common").Filter[] | undefined, oldFilters?: import("../common").Filter[] | undefined) => boolean;
changeTimeFilter: typeof changeTimeFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { compareFilters, COMPARE_ALL_OPTIONS } from './compare_filters';
import { buildEmptyFilter, buildQueryFilter, FilterStateStore } from '../../../../common';
import { buildEmptyFilter, buildQueryFilter, FilterStateStore } from '../../es_query';

describe('filter manager utilities', () => {
describe('compare filters', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { defaults, isEqual, omit, map } from 'lodash';
import { FilterMeta, Filter } from '../../../../common';
import { FilterMeta, Filter } from '../../es_query';

export interface FilterCompareOptions {
disabled?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@
*/

import { dedupFilters } from './dedup_filters';
import {
Filter,
IIndexPattern,
IFieldType,
buildRangeFilter,
buildQueryFilter,
FilterStateStore,
} from '../../../../common';
import { Filter, buildRangeFilter, buildQueryFilter, FilterStateStore } from '../../es_query';
import { IIndexPattern, IFieldType } from '../../index_patterns';

describe('filter manager utilities', () => {
let indexPattern: IIndexPattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import { filter, find } from 'lodash';
import { compareFilters, FilterCompareOptions } from './compare_filters';
import { Filter } from '../../../../common';
import { Filter } from '../../es_query';

/**
* Combine 2 filter collections, removing duplicates
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/data/common/query/filter_manager/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export { dedupFilters } from './dedup_filters';
export { uniqFilters } from './uniq_filters';
export { compareFilters, COMPARE_ALL_OPTIONS, FilterCompareOptions } from './compare_filters';
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { uniqFilters } from './uniq_filters';
import { buildQueryFilter, Filter, FilterStateStore } from '../../../../common';
import { buildQueryFilter, Filter, FilterStateStore } from '../../es_query';

describe('filter manager utilities', () => {
describe('niqFilter', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/
import { each, union } from 'lodash';
import { Filter } from '../../es_query';
import { dedupFilters } from './dedup_filters';
import { Filter } from '../../../../common';

/**
* Remove duplicate filters from an array of filters
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/common/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
* under the License.
*/

export * from './filter_manager';
export * from './types';
4 changes: 2 additions & 2 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ import {
isQueryStringFilter,
isRangeFilter,
toggleFilterNegated,
compareFilters,
COMPARE_ALL_OPTIONS,
} from '../common';

import { FilterLabel } from './ui/filter_bar';

import {
compareFilters,
COMPARE_ALL_OPTIONS,
generateFilters,
onlyDisabledFiltersChanged,
changeTimeFilter,
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ export const esFilters: {
getPhraseFilterField: (filter: import("../common").PhraseFilter) => string;
getPhraseFilterValue: (filter: import("../common").PhraseFilter) => string | number | boolean;
getDisplayValueFromFilter: typeof getDisplayValueFromFilter;
compareFilters: (first: import("../common").Filter | import("../common").Filter[], second: import("../common").Filter | import("../common").Filter[], comparatorOptions?: import("./query/filter_manager/lib/compare_filters").FilterCompareOptions) => boolean;
COMPARE_ALL_OPTIONS: import("./query/filter_manager/lib/compare_filters").FilterCompareOptions;
compareFilters: (first: import("../common").Filter | import("../common").Filter[], second: import("../common").Filter | import("../common").Filter[], comparatorOptions?: import("../common").FilterCompareOptions) => boolean;
COMPARE_ALL_OPTIONS: import("../common").FilterCompareOptions;
generateFilters: typeof generateFilters;
onlyDisabledFiltersChanged: (newFilters?: import("../common").Filter[] | undefined, oldFilters?: import("../common").Filter[] | undefined) => boolean;
changeTimeFilter: typeof changeTimeFilter;
Expand Down Expand Up @@ -1843,8 +1843,8 @@ export type TSearchStrategyProvider<T extends TStrategyTypes> = (context: ISearc
// src/plugins/data/public/index.ts:405:1 - (ae-forgotten-export) The symbol "parseInterval" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:406:1 - (ae-forgotten-export) The symbol "propFilter" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/index.ts:409:1 - (ae-forgotten-export) The symbol "toAbsoluteDates" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:34:33 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:38:1 - (ae-forgotten-export) The symbol "QueryStateChange" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:33:33 - (ae-forgotten-export) The symbol "FilterStateStore" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/query/state_sync/connect_to_query_state.ts:37:1 - (ae-forgotten-export) The symbol "QueryStateChange" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/types.ts:52:5 - (ae-forgotten-export) The symbol "createFiltersFromEvent" needs to be exported by the entry point index.d.ts
// src/plugins/data/public/types.ts:60:5 - (ae-forgotten-export) The symbol "IndexPatternSelectProps" needs to be exported by the entry point index.d.ts

Expand Down
12 changes: 9 additions & 3 deletions src/plugins/data/public/query/filter_manager/filter_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ import { Subject } from 'rxjs';

import { IUiSettingsClient } from 'src/core/public';

import { COMPARE_ALL_OPTIONS, compareFilters } from './lib/compare_filters';
import { sortFilters } from './lib/sort_filters';
import { mapAndFlattenFilters } from './lib/map_and_flatten_filters';
import { uniqFilters } from './lib/uniq_filters';
import { onlyDisabledFiltersChanged } from './lib/only_disabled';
import { PartitionedFilters } from './types';
import { FilterStateStore, Filter, isFilterPinned } from '../../../common';

import {
FilterStateStore,
Filter,
uniqFilters,
isFilterPinned,
compareFilters,
COMPARE_ALL_OPTIONS,
} from '../../../common';

export class FilterManager {
private filters: Filter[] = [];
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/data/public/query/filter_manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

export { FilterManager } from './filter_manager';

export { uniqFilters } from './lib/uniq_filters';
export { mapAndFlattenFilters } from './lib/map_and_flatten_filters';
export { onlyDisabledFiltersChanged } from './lib/only_disabled';
export { generateFilters } from './lib/generate_filters';
export { compareFilters, COMPARE_ALL_OPTIONS } from './lib/compare_filters';
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/

import { filter } from 'lodash';
import { Filter } from '../../../../common';
import { compareFilters, COMPARE_ALL_OPTIONS } from './compare_filters';
import { Filter, compareFilters, COMPARE_ALL_OPTIONS } from '../../../../common';

const isEnabled = (f: Filter) => f && f.meta && !f.meta.disabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import { Subscription } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import _ from 'lodash';
import { BaseStateContainer } from '../../../../kibana_utils/public';
import { COMPARE_ALL_OPTIONS, compareFilters } from '../filter_manager/lib/compare_filters';
import { QuerySetup, QueryStart } from '../query_service';
import { QueryState, QueryStateChange } from './types';
import { FilterStateStore } from '../../../common/es_query/filters';
import { FilterStateStore, COMPARE_ALL_OPTIONS, compareFilters } from '../../../common';

/**
* Helper to setup two-way syncing of global data and a state container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import { Observable, Subscription } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { TimefilterSetup } from '../timefilter';
import { COMPARE_ALL_OPTIONS, compareFilters, FilterManager } from '../filter_manager';
import { FilterManager } from '../filter_manager';
import { QueryState, QueryStateChange } from './index';
import { createStateContainer } from '../../../../kibana_utils/public';
import { isFilterPinned } from '../../../common/es_query/filters';
import { isFilterPinned, compareFilters, COMPARE_ALL_OPTIONS } from '../../../common';

export function createQueryStateObservable({
timefilter: { timefilter },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/

import { uniq } from 'lodash';
import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../../expression_functions';
import { KibanaContext } from '../../expression_types';
import { Query, uniqFilters } from '../../../../data/common';

interface Arguments {
q?: string | null;
Expand All @@ -35,6 +36,15 @@ export type ExpressionFunctionKibanaContext = ExpressionFunctionDefinition<
Promise<KibanaContext>
>;

const getParsedValue = (data: any, defaultValue: any) =>
typeof data === 'string' && data.length ? JSON.parse(data) || defaultValue : defaultValue;

const mergeQueries = (first: Query | Query[] = [], second: Query | Query[]) =>
uniq<Query>(
[...(Array.isArray(first) ? first : [first]), ...(Array.isArray(second) ? second : [second])],
(n: any) => JSON.stringify(n.query)
);

export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
name: 'kibana_context',
type: 'kibana_context',
Expand Down Expand Up @@ -75,9 +85,9 @@ export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
},

async fn(input, args, { getSavedObject }) {
const queryArg = args.q ? JSON.parse(args.q) : [];
let queries = Array.isArray(queryArg) ? queryArg : [queryArg];
let filters = args.filters ? JSON.parse(args.filters) : [];
const timeRange = getParsedValue(args.timeRange, input?.timeRange);
let queries = mergeQueries(input?.query, getParsedValue(args?.q, []));
let filters = [...(input?.filters || []), ...getParsedValue(args?.filters, [])];

if (args.savedSearchId) {
if (typeof getSavedObject !== 'function') {
Expand All @@ -89,29 +99,20 @@ export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
}
const obj = await getSavedObject('search', args.savedSearchId);
const search = obj.attributes.kibanaSavedObjectMeta as { searchSourceJSON: string };
const data = JSON.parse(search.searchSourceJSON) as { query: string; filter: any[] };
queries = queries.concat(data.query);
filters = filters.concat(data.filter);
}
const { query, filter } = getParsedValue(search.searchSourceJSON, {});

if (input && input.query) {
queries = queries.concat(input.query);
}

if (input && input.filters) {
filters = filters.concat(input.filters).filter((f: any) => !f.meta.disabled);
if (query) {
queries = mergeQueries(queries, query);
}
if (filter) {
filters = [...filters, ...(Array.isArray(filter) ? filter : [filter])];
}
}

const timeRange = args.timeRange
? JSON.parse(args.timeRange)
: input
? input.timeRange
: undefined;

return {
type: 'kibana_context',
query: queries,
filters,
filters: uniqFilters(filters).filter((f: any) => !f.meta?.disabled),
timeRange,
};
},
Expand Down