Fixes filter on values that are not in the result (#20608) (#20713)

This commit is contained in:
Peter Pisljar 2018-07-12 14:55:54 +02:00 committed by GitHub
parent 930822eade
commit 0d243398e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 11 deletions

View file

@ -154,7 +154,7 @@ export function RegionMapsVisualizationProvider(Private, Notifier, config) {
}
const rowIndex = this._chartData.tables[0].rows.findIndex(row => row[0] === event);
this._vis.API.events.addFilter(this._chartData.tables[0], 0, rowIndex);
this._vis.API.events.addFilter(this._chartData.tables[0], 0, rowIndex, event);
});
this._choroplethLayer.on('styleChanged', (event) => {
const shouldShowWarning = this._vis.params.isDisplayWarning && config.get('visualization:regionmap:showWarnings');

View file

@ -44,6 +44,14 @@ describe('Vis Class', function () {
listeners: { click: _.noop }
};
// Wrap the given vis type definition in a state, that can be passed to vis
const state = (type) => ({
type: {
visConfig: { defaults: {} },
...type,
}
});
beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function (Private) {
Vis = Private(VisProvider);
@ -111,14 +119,6 @@ describe('Vis Class', function () {
describe('inspector', () => {
// Wrap the given vis type definition in a state, that can be passed to vis
const state = (type) => ({
type: {
visConfig: { defaults: {} },
...type,
}
});
describe('hasInspector()', () => {
it('should forward to inspectors hasInspector', () => {
const vis = new Vis(indexPattern, state({
@ -256,4 +256,40 @@ describe('Vis Class', function () {
});
describe('vis addFilter method', () => {
let aggConfig;
let data;
beforeEach(() => {
aggConfig = {
type: { name: 'terms' },
params: {},
createFilter: sinon.stub()
};
data = {
columns: [{
title: 'test',
aggConfig
}],
rows: [['US']]
};
});
it('adds a simple filter', () => {
const vis = new Vis(indexPattern, state({ requestHandler: 'none' }));
vis.API.events.addFilter(data, 0, 0);
expect(aggConfig.createFilter.callCount).to.be(1);
expect(aggConfig.createFilter.getCall(0).args[0]).to.be('US');
});
it('adds a filter if value is provided instead of row index', () => {
const vis = new Vis(indexPattern, state({ requestHandler: 'none' }));
vis.API.events.addFilter(data, 0, -1, 'UK');
expect(aggConfig.createFilter.callCount).to.be(1);
expect(aggConfig.createFilter.getCall(0).args[0]).to.be('UK');
});
});
});

View file

@ -44,6 +44,10 @@ import { Inspector } from '../inspector';
import { RequestAdapter, DataAdapter } from '../inspector/adapters';
const getTerms = (table, columnIndex, rowIndex) => {
if (rowIndex === -1) {
return [];
}
// get only rows where cell value matches current row for all the fields before columnIndex
const rows = table.rows.filter(row => row.every((cell, i) => cell === table.rows[rowIndex][i] || i >= columnIndex));
const terms = rows.map(row => row[columnIndex]);
@ -94,10 +98,13 @@ export function VisProvider(Private, indexPatterns, getAppState) {
const appState = getAppState();
filterBarClickHandler(appState)(event);
},
addFilter: (data, columnIndex, rowIndex) => {
addFilter: (data, columnIndex, rowIndex, cellValue) => {
const agg = data.columns[columnIndex].aggConfig;
let filter = [];
const value = data.rows[rowIndex][columnIndex];
const value = rowIndex > -1 ? data.rows[rowIndex][columnIndex] : cellValue;
if (!value) {
return;
}
if (agg.type.name === 'terms' && agg.params.otherBucket) {
const terms = getTerms(data, columnIndex, rowIndex);
filter = agg.createFilter(value, { terms });