Fix histogram range update when x axis is scripted field (#25379)

This commit is contained in:
Nikita Glashenko 2019-05-23 13:35:18 +04:00 committed by Peter Pisljar
parent 79f5e230b3
commit 34cab97307
4 changed files with 17 additions and 8 deletions

View file

@ -63,8 +63,8 @@ describe('Filter Manager', function () {
it('should wrap painless scripts in comparator lambdas', function () {
const field = getField(indexPattern, 'script date');
const expected = `boolean gte(Supplier s, def v) {return s.get() >= v} ` +
`boolean lte(Supplier s, def v) {return s.get() <= v}` +
const expected = `boolean gte(Supplier s, def v) {return !s.get().toInstant().isBefore(Instant.parse(v))} ` +
`boolean lte(Supplier s, def v) {return !s.get().toInstant().isAfter(Instant.parse(v))}` +
`gte(() -> { ${field.script} }, params.gte) && ` +
`lte(() -> { ${field.script} }, params.lte)`;

View file

@ -32,6 +32,13 @@ const comparators = {
lt: 'boolean lt(Supplier s, def v) {return s.get() < v}',
};
const dateComparators = {
gt: 'boolean gt(Supplier s, def v) {return s.get().toInstant().isAfter(Instant.parse(v))}',
gte: 'boolean gte(Supplier s, def v) {return !s.get().toInstant().isBefore(Instant.parse(v))}',
lte: 'boolean lte(Supplier s, def v) {return !s.get().toInstant().isAfter(Instant.parse(v))}',
lt: 'boolean lt(Supplier s, def v) {return s.get().toInstant().isBefore(Instant.parse(v))}',
};
function formatValue(field, params) {
return _.map(params, (val, key) => operators[key] + format(field, val)).join(' ');
}
@ -87,7 +94,8 @@ export function getRangeScript(field, params) {
// We must wrap painless scripts in a lambda in case they're more than a simple expression
if (field.lang === 'painless') {
const currentComparators = _.reduce(knownParams, (acc, val, key) => acc.concat(comparators[key]), []).join(' ');
const comp = field.type === 'date' ? dateComparators : comparators;
const currentComparators = _.reduce(knownParams, (acc, val, key) => acc.concat(comp[key]), []).join(' ');
const comparisons = _.map(knownParams, function (val, key) {
return `${key}(() -> { ${field.script} }, params.${key})`;

View file

@ -231,6 +231,7 @@ describe('brushEvent', () => {
test('by updating the existing scripted filter', () => {
const event = _.cloneDeep(numberEvent);
event.range = [3, 7];
event.aggConfigs[0].params.field.scripted = true;
$state.filters.push({
meta: {
key: 'numberField'

View file

@ -82,17 +82,17 @@ export function onBrushEvent(event, $state) {
if (_.has(existingFilter, 'range')) {
existingFilter.range[fieldName] = range;
} else if (_.has(existingFilter, 'script.script.params.gte')
&& _.has(existingFilter, 'script.script.params.lt')) {
existingFilter.script.script.params.gte = min;
existingFilter.script.script.params.lt = max;
} else {
const newFilter = buildRangeFilter(
field,
range,
indexPattern,
event.data.xAxisFormatter);
$state.$newFilters = [newFilter];
if (existingFilter) {
Object.assign(existingFilter, newFilter);
} else {
$state.$newFilters = [newFilter];
}
}
}
}