From d81e7330a4a4d02163b12ae07cd5d938140f6741 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Tue, 19 Mar 2019 16:50:25 -0700 Subject: [PATCH 1/3] test: add more filter elements --- .../state/selectors/__tests__/workpad.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js b/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js index 5cfdf726dd258..622006234b66e 100644 --- a/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js +++ b/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js @@ -33,6 +33,39 @@ describe('workpad selectors', () => { }, ], }, + 'element-3': { + type: 'expression', + chain: [ + { + type: 'function', + function: 'demodata', + arguments: {}, + }, + { + type: 'function', + function: 'dropdownControl', + arguments: { + valueColumn: ['project'], + filterColumn: ['project'], + }, + }, + { + type: 'function', + function: 'render', + arguments: {}, + }, + ], + }, + 'element-4': { + type: 'expression', + chain: [ + { + type: 'function', + function: 'timefilterControl', + arguments: { compact: [true], column: ['@timestamp'] }, + }, + ], + }, }; state = { @@ -63,10 +96,23 @@ describe('workpad selectors', () => { { id: 'element-0', expression: 'markdown', + filter: '', }, { id: 'element-1', expression: 'demodata', + filter: '', + }, + { + id: 'element-3', + expression: + 'demodata | dropdownControl valueColumn=project filterColumn=project | render', + filter: 'exactly value="beats" column="project"', + }, + { + id: 'element-4', + expression: 'timefilterControl compact=true column=@timestamp', + filter: 'timefilter column=@timestamp from=now-24h to=now', }, ], }, @@ -135,6 +181,7 @@ describe('workpad selectors', () => { it('returns all elements on the page', () => { const { elements } = state.persistent.workpad.pages[0]; + const expected = elements.map(element => ({ ...element, ast: asts[element.id], From 5c7c958d500a10106ba7e79633a2f5051cf5b895 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Tue, 19 Mar 2019 16:58:11 -0700 Subject: [PATCH 2/3] test: add getGlobalFilterExpression test --- .../canvas/public/state/selectors/__tests__/workpad.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js b/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js index 622006234b66e..bd56e1cd6e1d2 100644 --- a/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js +++ b/x-pack/plugins/canvas/public/state/selectors/__tests__/workpad.js @@ -245,6 +245,15 @@ describe('workpad selectors', () => { }); }); + describe('getGlobalFilterExpression', () => { + it('gets filters from all elements', () => { + const filters = selector.getGlobalFilterExpression(state); + expect(filters).to.equal( + 'exactly value="beats" column="project" | timefilter column=@timestamp from=now-24h to=now' + ); + }); + }); + describe('isWriteable', () => { it('returns boolean for if the workpad is writeable', () => { expect(selector.isWriteable(state)).to.equal(false); From 7fb2fcb65e3803434282315829629f7849ba7668 Mon Sep 17 00:00:00 2001 From: joe fleming Date: Tue, 19 Mar 2019 17:06:44 -0700 Subject: [PATCH 3/3] chore: only iterate filter elements once --- .../plugins/canvas/public/state/selectors/workpad.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/canvas/public/state/selectors/workpad.js b/x-pack/plugins/canvas/public/state/selectors/workpad.js index 66e82f0cbab86..103bed6e52831 100644 --- a/x-pack/plugins/canvas/public/state/selectors/workpad.js +++ b/x-pack/plugins/canvas/public/state/selectors/workpad.js @@ -116,8 +116,14 @@ export function getElementStats(state) { export function getGlobalFilterExpression(state) { return getAllElements(state) - .map(el => el.filter) - .filter(str => str != null && str.length) + .reduce((acc, el) => { + // check that a filter is defined + if (el.filter != null && el.filter.length) { + return acc.concat(el.filter); + } + + return acc; + }, []) .join(' | '); }