-
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.
Sum, Avg, Min and Max bucket pipeline aggregation (#10070)
* allow parent aggs to have sub aggs defined * adding bucket_sum aggregation * adding bucket_avg, bucket_min and bucket_max * fixing based on UI review * adding tests * disable terms sorting on pipeline aggs * fixing based on Staceys review * adding defaults * updated based on review * fixing error with stacking
- Loading branch information
Showing
20 changed files
with
351 additions
and
4 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
110 changes: 110 additions & 0 deletions
110
src/ui/public/agg_types/__tests__/metrics/sibling_pipeline.js
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,110 @@ | ||
import _ from 'lodash'; | ||
import expect from 'expect.js'; | ||
import ngMock from 'ng_mock'; | ||
import BucketSum from 'ui/agg_types/metrics/bucket_sum'; | ||
import BucketAvg from 'ui/agg_types/metrics/bucket_avg'; | ||
import BucketMin from 'ui/agg_types/metrics/bucket_min'; | ||
import BucketMax from 'ui/agg_types/metrics/bucket_max'; | ||
import VisProvider from 'ui/vis'; | ||
import StubbedIndexPattern from 'fixtures/stubbed_logstash_index_pattern'; | ||
|
||
const metrics = [ | ||
{ name: 'sum_bucket', title: 'Overall Sum', provider: BucketSum }, | ||
{ name: 'avg_bucket', title: 'Overall Average', provider: BucketAvg }, | ||
{ name: 'min_bucket', title: 'Overall Min', provider: BucketMin }, | ||
{ name: 'max_bucket', title: 'Overall Max', provider: BucketMax }, | ||
]; | ||
|
||
describe('sibling pipeline aggs', function () { | ||
metrics.forEach(metric => { | ||
describe(`${metric.title} metric`, function () { | ||
|
||
let aggDsl; | ||
let metricAgg; | ||
let aggConfig; | ||
|
||
function init(settings) { | ||
ngMock.module('kibana'); | ||
ngMock.inject(function (Private) { | ||
const Vis = Private(VisProvider); | ||
const indexPattern = Private(StubbedIndexPattern); | ||
metricAgg = Private(metric.provider); | ||
|
||
const params = settings || { | ||
customMetric: { | ||
id: '5', | ||
type: 'count', | ||
schema: 'metric' | ||
}, | ||
customBucket: { | ||
id: '6', | ||
type: 'date_histogram', | ||
schema: 'bucket', | ||
params: { field: '@timestamp' } | ||
} | ||
}; | ||
|
||
const vis = new Vis(indexPattern, { | ||
title: 'New Visualization', | ||
type: 'metric', | ||
params: { | ||
fontSize: 60, | ||
handleNoResults: true | ||
}, | ||
aggs: [ | ||
{ | ||
id: '1', | ||
type: 'count', | ||
schema: 'metric' | ||
}, | ||
{ | ||
id: '2', | ||
type: metric.name, | ||
schema: 'metric', | ||
params | ||
} | ||
], | ||
listeners: {} | ||
}); | ||
|
||
// Grab the aggConfig off the vis (we don't actually use the vis for anything else) | ||
aggConfig = vis.aggs[1]; | ||
aggDsl = aggConfig.toDsl(); | ||
}); | ||
} | ||
|
||
it(`should return a label prefixed with ${metric.title} of`, function () { | ||
init(); | ||
expect(metricAgg.makeLabel(aggConfig)).to.eql(`${metric.title} of Count`); | ||
}); | ||
|
||
it('should set parent aggs', function () { | ||
init(); | ||
expect(aggDsl[metric.name].buckets_path).to.be('2-bucket>_count'); | ||
expect(aggDsl.parentAggs['2-bucket'].date_histogram).to.not.be.undefined; | ||
}); | ||
|
||
it('should set nested parent aggs', function () { | ||
init({ | ||
customMetric: { | ||
id: '5', | ||
type: 'avg', | ||
schema: 'metric', | ||
params: { field: 'bytes' }, | ||
}, | ||
customBucket: { | ||
id: '6', | ||
type: 'date_histogram', | ||
schema: 'bucket', | ||
params: { field: '@timestamp' }, | ||
} | ||
}); | ||
expect(aggDsl[metric.name].buckets_path).to.be('2-bucket>2-metric'); | ||
expect(aggDsl.parentAggs['2-bucket'].date_histogram).to.not.be.undefined; | ||
expect(aggDsl.parentAggs['2-bucket'].aggs['2-metric'].avg.field).to.equal('bytes'); | ||
}); | ||
|
||
}); | ||
}); | ||
|
||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<div ng-controller="aggParam.controller"> | ||
<div class="form-group" ng-if="agg.params[aggType]"> | ||
<label>{{aggTitle}}</label> | ||
<div class="vis-editor-agg-order-agg"> | ||
<ng-form name="{{aggType}}Form"> | ||
<vis-editor-agg-params | ||
agg="agg.params[aggType]" | ||
group-name="'{{aggGroup}}'"> | ||
</vis-editor-agg-params> | ||
</ng-form> | ||
</div> | ||
</div> | ||
|
||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import AggTypesMetricsMetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type'; | ||
import { makeNestedLabel } from './lib/make_nested_label'; | ||
import SiblingPipelineAggHelperProvider from './lib/sibling_pipeline_agg_helper'; | ||
|
||
export default function AggTypesMetricsBucketAvgProvider(Private) { | ||
const MetricAggType = Private(AggTypesMetricsMetricAggTypeProvider); | ||
const siblingPipelineHelper = Private(SiblingPipelineAggHelperProvider); | ||
|
||
return new MetricAggType({ | ||
name: 'avg_bucket', | ||
title: 'Average Bucket', | ||
makeLabel: agg => makeNestedLabel(agg, 'overall average'), | ||
subtype: siblingPipelineHelper.subtype, | ||
params: [ | ||
...siblingPipelineHelper.params() | ||
] | ||
}); | ||
} |
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,18 @@ | ||
import AggTypesMetricsMetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type'; | ||
import { makeNestedLabel } from './lib/make_nested_label'; | ||
import SiblingPipelineAggHelperProvider from './lib/sibling_pipeline_agg_helper'; | ||
|
||
export default function AggTypesMetricsBucketMaxProvider(Private) { | ||
const MetricAggType = Private(AggTypesMetricsMetricAggTypeProvider); | ||
const siblingPipelineHelper = Private(SiblingPipelineAggHelperProvider); | ||
|
||
return new MetricAggType({ | ||
name: 'max_bucket', | ||
title: 'Max Bucket', | ||
makeLabel: agg => makeNestedLabel(agg, 'overall max'), | ||
subtype: siblingPipelineHelper.subtype, | ||
params: [ | ||
...siblingPipelineHelper.params() | ||
] | ||
}); | ||
} |
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,18 @@ | ||
import AggTypesMetricsMetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type'; | ||
import { makeNestedLabel } from './lib/make_nested_label'; | ||
import SiblingPipelineAggHelperProvider from './lib/sibling_pipeline_agg_helper'; | ||
|
||
export default function AggTypesMetricsBucketMinProvider(Private) { | ||
const MetricAggType = Private(AggTypesMetricsMetricAggTypeProvider); | ||
const siblingPipelineHelper = Private(SiblingPipelineAggHelperProvider); | ||
|
||
return new MetricAggType({ | ||
name: 'min_bucket', | ||
title: 'Min Bucket', | ||
makeLabel: agg => makeNestedLabel(agg, 'overall min'), | ||
subtype: siblingPipelineHelper.subtype, | ||
params: [ | ||
...siblingPipelineHelper.params() | ||
] | ||
}); | ||
} |
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,18 @@ | ||
import AggTypesMetricsMetricAggTypeProvider from 'ui/agg_types/metrics/metric_agg_type'; | ||
import { makeNestedLabel } from './lib/make_nested_label'; | ||
import SiblingPipelineAggHelperProvider from './lib/sibling_pipeline_agg_helper'; | ||
|
||
export default function AggTypesMetricsBucketSumProvider(Private) { | ||
const MetricAggType = Private(AggTypesMetricsMetricAggTypeProvider); | ||
const siblingPipelineHelper = Private(SiblingPipelineAggHelperProvider); | ||
|
||
return new MetricAggType({ | ||
name: 'sum_bucket', | ||
title: 'Sum Bucket', | ||
makeLabel: agg => makeNestedLabel(agg, 'overall sum'), | ||
subtype: siblingPipelineHelper.subtype, | ||
params: [ | ||
...siblingPipelineHelper.params() | ||
] | ||
}); | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_controller.js
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,23 @@ | ||
import safeMakeLabel from './safe_make_label'; | ||
|
||
const siblingPipelineAggController = function (type) { | ||
return function ($scope) { | ||
|
||
$scope.aggType = type; | ||
$scope.aggTitle = type === 'customMetric' ? 'Metric' : 'Bucket'; | ||
$scope.aggGroup = type === 'customMetric' ? 'metrics' : 'buckets'; | ||
$scope.safeMakeLabel = safeMakeLabel; | ||
|
||
function updateAgg() { | ||
const agg = $scope.agg; | ||
const params = agg.params; | ||
const paramDef = agg.type.params.byName[type]; | ||
|
||
params[type] = params[type] || paramDef.makeAgg(agg); | ||
} | ||
|
||
updateAgg(); | ||
}; | ||
}; | ||
|
||
export { siblingPipelineAggController }; |
92 changes: 92 additions & 0 deletions
92
src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js
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,92 @@ | ||
import _ from 'lodash'; | ||
import VisAggConfigProvider from 'ui/vis/agg_config'; | ||
import VisSchemasProvider from 'ui/vis/schemas'; | ||
|
||
import { siblingPipelineAggController } from './sibling_pipeline_agg_controller'; | ||
import { siblingPipelineAggWritter } from './sibling_pipeline_agg_writter'; | ||
import metricAggTemplate from 'ui/agg_types/controls/sub_metric.html'; | ||
|
||
const SiblingPipelineAggHelperProvider = function (Private) { | ||
|
||
const AggConfig = Private(VisAggConfigProvider); | ||
const Schemas = Private(VisSchemasProvider); | ||
|
||
const metricAggFilter = [ | ||
'!top_hits', '!percentiles', '!percentile_ranks', '!median', '!std_dev', | ||
'!sum_bucket', '!avg_bucket', '!min_bucket', '!max_bucket', | ||
'!derivative', '!moving_avg', '!serial_diff', '!cumulative_sum' | ||
]; | ||
|
||
const metricAggSchema = (new Schemas([ | ||
{ | ||
group: 'none', | ||
name: 'metricAgg', | ||
title: 'Metric Agg', | ||
aggFilter: metricAggFilter | ||
} | ||
])).all[0]; | ||
|
||
const bucketAggFilter = []; | ||
const bucketAggSchema = (new Schemas([ | ||
{ | ||
group: 'none', | ||
title: 'Bucket Agg', | ||
name: 'bucketAgg', | ||
aggFilter: bucketAggFilter | ||
} | ||
])).all[0]; | ||
|
||
return { | ||
subtype: 'Sibling Pipeline Aggregations', | ||
params: function () { | ||
return [ | ||
{ | ||
name: 'customBucket', | ||
type: AggConfig, | ||
default: null, | ||
serialize: function (customMetric) { | ||
return customMetric.toJSON(); | ||
}, | ||
deserialize: function (state, agg) { | ||
return this.makeAgg(agg, state); | ||
}, | ||
makeAgg: function (agg, state) { | ||
state = state || { type: 'date_histogram' }; | ||
state.schema = bucketAggSchema; | ||
const orderAgg = new AggConfig(agg.vis, state); | ||
orderAgg.id = agg.id + '-bucket'; | ||
return orderAgg; | ||
}, | ||
editor: metricAggTemplate, | ||
controller: siblingPipelineAggController('customBucket'), | ||
write: _.noop | ||
}, | ||
{ | ||
name: 'customMetric', | ||
type: AggConfig, | ||
default: null, | ||
serialize: function (customMetric) { | ||
return customMetric.toJSON(); | ||
}, | ||
deserialize: function (state, agg) { | ||
return this.makeAgg(agg, state); | ||
}, | ||
makeAgg: function (agg, state) { | ||
state = state || { type: 'count' }; | ||
state.schema = metricAggSchema; | ||
const orderAgg = new AggConfig(agg.vis, state); | ||
orderAgg.id = agg.id + '-metric'; | ||
return orderAgg; | ||
}, | ||
editor: metricAggTemplate, | ||
controller: siblingPipelineAggController('customMetric'), | ||
write: siblingPipelineAggWritter | ||
} | ||
]; | ||
} | ||
}; | ||
|
||
|
||
}; | ||
|
||
export default SiblingPipelineAggHelperProvider; |
Oops, something went wrong.