mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
* Fix forwarding of modifyAggConfigOnSearchRequestStart * Add unit tests
This commit is contained in:
parent
19a5def7f7
commit
2868776aaa
5 changed files with 87 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
|||
import expect from 'expect.js';
|
||||
import sinon from 'sinon';
|
||||
import ngMock from 'ng_mock';
|
||||
import { AggTypesMetricsDerivativeProvider } from '../../metrics/derivative';
|
||||
import { AggTypesMetricsCumulativeSumProvider } from '../../metrics/cumulative_sum';
|
||||
|
@ -171,6 +172,31 @@ describe('parent pipeline aggs', function () {
|
|||
});
|
||||
expect(metricAgg.getFormat(aggConfig).type.id).to.be('bytes');
|
||||
});
|
||||
|
||||
it('should call modifyAggConfigOnSearchRequestStart for its customMetric\'s parameters', () => {
|
||||
init({
|
||||
metricAgg: 'custom',
|
||||
customMetric: {
|
||||
id: '2-metric',
|
||||
type: 'max',
|
||||
params: { field: 'bytes' },
|
||||
schema: 'orderAgg'
|
||||
}
|
||||
});
|
||||
|
||||
const searchSource = {};
|
||||
const request = {};
|
||||
const customMetricSpy = sinon.spy();
|
||||
const customMetric = aggConfig.params.customMetric;
|
||||
|
||||
// Attach a modifyAggConfigOnSearchRequestStart with a spy to the first parameter
|
||||
customMetric.type.params[0].modifyAggConfigOnSearchRequestStart = customMetricSpy;
|
||||
|
||||
aggConfig.type.params.forEach(param => {
|
||||
param.modifyAggConfigOnSearchRequestStart(aggConfig, searchSource, request);
|
||||
});
|
||||
expect(customMetricSpy.calledWith(customMetric, searchSource, request)).to.be(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import expect from 'expect.js';
|
||||
import sinon from 'sinon';
|
||||
import ngMock from 'ng_mock';
|
||||
import { AggTypesMetricsBucketSumProvider } from '../../metrics/bucket_sum';
|
||||
import { AggTypesMetricsBucketAvgProvider } from '../../metrics/bucket_avg';
|
||||
|
@ -122,6 +123,26 @@ describe('sibling pipeline aggs', function () {
|
|||
expect(metricAgg.getFormat(aggConfig).type.id).to.be('bytes');
|
||||
});
|
||||
|
||||
it('should call modifyAggConfigOnSearchRequestStart for nested aggs\' parameters', () => {
|
||||
init();
|
||||
|
||||
const searchSource = {};
|
||||
const request = {};
|
||||
const customMetricSpy = sinon.spy();
|
||||
const customBucketSpy = sinon.spy();
|
||||
const { customMetric, customBucket } = aggConfig.params;
|
||||
|
||||
// Attach a modifyAggConfigOnSearchRequestStart with a spy to the first parameter
|
||||
customMetric.type.params[0].modifyAggConfigOnSearchRequestStart = customMetricSpy;
|
||||
customBucket.type.params[0].modifyAggConfigOnSearchRequestStart = customBucketSpy;
|
||||
|
||||
aggConfig.type.params.forEach(param => {
|
||||
param.modifyAggConfigOnSearchRequestStart(aggConfig, searchSource, request);
|
||||
});
|
||||
expect(customMetricSpy.calledWith(customMetric, searchSource, request)).to.be(true);
|
||||
expect(customBucketSpy.calledWith(customBucket, searchSource, request)).to.be(true);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
|
|
35
src/ui/public/agg_types/metrics/lib/nested_agg_helpers.js
Normal file
35
src/ui/public/agg_types/metrics/lib/nested_agg_helpers.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* Forwards modifyAggConfigOnSearchRequestStart calls to a nested AggConfig.
|
||||
* This must be used for each parameter, that accepts a nested aggregation, otherwise
|
||||
* some paramters of the nested aggregation might not work properly (like auto interval
|
||||
* on a nested date histogram).
|
||||
* You should assign the return value of this function to the modifyAggConfigOnSearchRequestStart
|
||||
* of the parameter that accepts a nested aggregation. Example:
|
||||
* {
|
||||
* name: 'customBucket',
|
||||
* modifyAggConfigOnSearchRequestStart: forwardModifyAggConfigOnSearchRequestStart('customBucket')
|
||||
* }
|
||||
*
|
||||
* @param {string} paramName - The name of the parameter, that this function should foward
|
||||
* calls to. That should match the name of the parameter the function is called on.
|
||||
* @returns {function} A function, that fowards the calls.
|
||||
*/
|
||||
function forwardModifyAggConfigOnSearchRequestStart(paramName) {
|
||||
return (aggConfig, ...args) => {
|
||||
if (!aggConfig || !aggConfig.params) {
|
||||
return;
|
||||
}
|
||||
const nestedAggConfig = aggConfig.params[paramName];
|
||||
if (nestedAggConfig && nestedAggConfig.type && nestedAggConfig.type.params) {
|
||||
nestedAggConfig.type.params.forEach(param => {
|
||||
// Check if this parameter of the nested aggConfig has a modifyAggConfigOnSearchRequestStart
|
||||
// function, that needs to be called.
|
||||
if (param.modifyAggConfigOnSearchRequestStart) {
|
||||
param.modifyAggConfigOnSearchRequestStart(nestedAggConfig, ...args);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export { forwardModifyAggConfigOnSearchRequestStart };
|
|
@ -4,6 +4,7 @@ import { AggConfig } from '../../../vis/agg_config';
|
|||
import { Schemas } from '../../../vis/editors/default/schemas';
|
||||
import { parentPipelineAggController } from './parent_pipeline_agg_controller';
|
||||
import { parentPipelineAggWritter } from './parent_pipeline_agg_writter';
|
||||
import { forwardModifyAggConfigOnSearchRequestStart } from './nested_agg_helpers';
|
||||
|
||||
|
||||
const metricAggFilter = ['!top_hits', '!percentiles', '!percentile_ranks', '!median', '!std_dev'];
|
||||
|
@ -38,6 +39,7 @@ const parentPipelineAggHelper = {
|
|||
metricAgg.id = termsAgg.id + '-metric';
|
||||
return metricAgg;
|
||||
},
|
||||
modifyAggConfigOnSearchRequestStart: forwardModifyAggConfigOnSearchRequestStart('customMetric'),
|
||||
write: _.noop
|
||||
},
|
||||
{
|
||||
|
|
|
@ -5,7 +5,7 @@ import { Schemas } from '../../../vis/editors/default/schemas';
|
|||
import { siblingPipelineAggController } from './sibling_pipeline_agg_controller';
|
||||
import { siblingPipelineAggWritter } from './sibling_pipeline_agg_writter';
|
||||
import metricAggTemplate from '../../controls/sub_metric.html';
|
||||
|
||||
import { forwardModifyAggConfigOnSearchRequestStart } from './nested_agg_helpers';
|
||||
|
||||
const metricAggFilter = [
|
||||
'!top_hits', '!percentiles', '!percentile_ranks', '!median', '!std_dev',
|
||||
|
@ -55,6 +55,7 @@ const siblingPipelineAggHelper = {
|
|||
},
|
||||
editor: metricAggTemplate,
|
||||
controller: siblingPipelineAggController('customBucket'),
|
||||
modifyAggConfigOnSearchRequestStart: forwardModifyAggConfigOnSearchRequestStart('customBucket'),
|
||||
write: _.noop
|
||||
},
|
||||
{
|
||||
|
@ -76,6 +77,7 @@ const siblingPipelineAggHelper = {
|
|||
},
|
||||
editor: metricAggTemplate,
|
||||
controller: siblingPipelineAggController('customMetric'),
|
||||
modifyAggConfigOnSearchRequestStart: forwardModifyAggConfigOnSearchRequestStart('customMetric'),
|
||||
write: siblingPipelineAggWritter
|
||||
}
|
||||
];
|
||||
|
@ -86,5 +88,3 @@ const siblingPipelineAggHelper = {
|
|||
};
|
||||
|
||||
export { siblingPipelineAggHelper };
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue