-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[expressions] select_filter and remove_filter functions (#113533)
- Loading branch information
Showing
8 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
src/plugins/data/common/search/expressions/remove_filter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
69
src/plugins/data/common/search/expressions/remove_filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}) || [], | ||
}; | ||
}, | ||
}; |
Oops, something went wrong.