Skip to content

Commit

Permalink
[expressions] select_filter and remove_filter functions (#113533)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar authored Oct 21, 2021
1 parent 933ece4 commit 800cfb5
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/kbn-es-query/src/filters/build_filters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export type FilterMeta = {
negate?: boolean;
// controlledBy is there to identify who owns the filter
controlledBy?: string;
// allows grouping of filters
group?: string;
// index and type are optional only because when you create a new filter, there are no defaults
index?: string;
isMultiIndex?: boolean;
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/common/search/expressions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export * from './field';
export * from './phrase_filter';
export * from './exists_filter';
export * from './range_filter';
export * from './remove_filter';
export * from './select_filter';
export * from './kibana_filter';
export * from './filters_to_ast';
export * from './timerange';
Expand Down
206 changes: 206 additions & 0 deletions src/plugins/data/common/search/expressions/remove_filter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { createMockContext } from '../../../../expressions/common';
import { functionWrapper } from './utils';
import { removeFilterFunction } from './remove_filter';
import { KibanaContext } from './kibana_context_type';

describe('interpreter/functions#removeFilter', () => {
const fn = functionWrapper(removeFilterFunction);
const kibanaContext: KibanaContext = {
type: 'kibana_context',
filters: [
{
meta: {
group: 'g1',
},
query: {},
},
{
meta: {
group: 'g2',
},
query: {},
},
{
meta: {
group: 'g1',
controlledBy: 'i1',
},
query: {},
},
{
meta: {
group: 'g1',
controlledBy: 'i2',
},
query: {},
},
{
meta: {
controlledBy: 'i1',
},
query: {},
},
],
};

it('removes all filters when called without arguments', () => {
const actual = fn(kibanaContext, {}, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [],
"type": "kibana_context",
}
`);
});

it('removes filters belonging to certain group', () => {
const actual = fn(kibanaContext, { group: 'g1' }, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [
Object {
"meta": Object {
"group": "g2",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i1",
},
"query": Object {},
},
],
"type": "kibana_context",
}
`);
});

it('removes ungrouped filters', () => {
const actual = fn(kibanaContext, { ungrouped: true }, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [
Object {
"meta": Object {
"group": "g1",
},
"query": Object {},
},
Object {
"meta": Object {
"group": "g2",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i1",
"group": "g1",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i2",
"group": "g1",
},
"query": Object {},
},
],
"type": "kibana_context",
}
`);
});

it('removes ungrouped filters and filters matching a group', () => {
const actual = fn(kibanaContext, { group: 'g1', ungrouped: true }, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [
Object {
"meta": Object {
"group": "g2",
},
"query": Object {},
},
],
"type": "kibana_context",
}
`);
});

it('removes filters controlled by specified id', () => {
const actual = fn(kibanaContext, { from: 'i1' }, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [
Object {
"meta": Object {
"group": "g1",
},
"query": Object {},
},
Object {
"meta": Object {
"group": "g2",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i2",
"group": "g1",
},
"query": Object {},
},
],
"type": "kibana_context",
}
`);
});

it('removes filters controlled by specified id and matching a group', () => {
const actual = fn(kibanaContext, { group: 'g1', from: 'i1' }, createMockContext());
expect(actual).toMatchInlineSnapshot(`
Object {
"filters": Array [
Object {
"meta": Object {
"group": "g1",
},
"query": Object {},
},
Object {
"meta": Object {
"group": "g2",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i2",
"group": "g1",
},
"query": Object {},
},
Object {
"meta": Object {
"controlledBy": "i1",
},
"query": Object {},
},
],
"type": "kibana_context",
}
`);
});
});
69 changes: 69 additions & 0 deletions src/plugins/data/common/search/expressions/remove_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common';
import { KibanaContext } from './kibana_context_type';

interface Arguments {
group?: string;
from?: string;
ungrouped?: boolean;
}

export type ExpressionFunctionRemoveFilter = ExpressionFunctionDefinition<
'removeFilter',
KibanaContext,
Arguments,
KibanaContext
>;

export const removeFilterFunction: ExpressionFunctionRemoveFilter = {
name: 'removeFilter',
type: 'kibana_context',
inputTypes: ['kibana_context'],
help: i18n.translate('data.search.functions.removeFilter.help', {
defaultMessage: 'Removes filters from context',
}),
args: {
group: {
types: ['string'],
aliases: ['_'],
help: i18n.translate('data.search.functions.removeFilter.group.help', {
defaultMessage: 'Removes only filters belonging to the provided group',
}),
},
from: {
types: ['string'],
help: i18n.translate('data.search.functions.removeFilter.from.help', {
defaultMessage: 'Removes only filters owned by the provided id',
}),
},
ungrouped: {
types: ['boolean'],
aliases: ['nogroup', 'nogroups'],
default: false,
help: i18n.translate('data.search.functions.removeFilter.ungrouped.help', {
defaultMessage: 'Should filters without group be removed',
}),
},
},

fn(input, { group, from, ungrouped }) {
return {
...input,
filters:
input.filters?.filter(({ meta }) => {
const isGroupMatching =
(!group && !ungrouped) || group === meta.group || (ungrouped && !meta.group);
const isOriginMatching = !from || from === meta.controlledBy;
return !isGroupMatching || !isOriginMatching;
}) || [],
};
},
};
Loading

0 comments on commit 800cfb5

Please sign in to comment.