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

[Visualize] Duplicated query filters in es request #60106

Merged
merged 9 commits into from
Mar 18, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
* specific language governing permissions and limitations
* under the License.
*/

import { uniq } from 'lodash';
import { stringify } from 'query-string';
import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from '../../expression_functions';
import { KibanaContext } from '../../expression_types';
import { Query, Filter } from '../../../../data/common';

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

export const mergeInput = <T = any>(object: T | T[] = [], other: string | T | T[]): T[] => {
const parsed: T | T[] = typeof other === 'string' ? JSON.parse(other || '[]') : other;
return uniq<T>(
[
...(Array.isArray(parsed) ? parsed : [parsed]),
...(Array.isArray(object) ? object : [object]),
],
(n: any) => stringify(n)
);
};

export const kibanaContextFunction: ExpressionFunctionKibanaContext = {
name: 'kibana_context',
type: 'kibana_context',
Expand Down Expand Up @@ -75,9 +88,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 = args.timeRange ? JSON.parse(args.timeRange) : input?.timeRange;
let queries = mergeInput<Query>(input?.query, args?.q || '[]');
let filters = mergeInput<Filter>(input?.filters, args?.filters || '[]');

if (args.savedSearchId) {
if (typeof getSavedObject !== 'function') {
Expand All @@ -89,29 +102,16 @@ 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 data = JSON.parse(search.searchSourceJSON) as { query: Query[]; filter: Filter[] };

if (input && input.query) {
queries = queries.concat(input.query);
queries = mergeInput(queries, data.query);
filters = mergeInput(filters, data.filter);
}

if (input && input.filters) {
filters = filters.concat(input.filters).filter((f: any) => !f.meta.disabled);
}

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

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