From 2c6510311f7c7ab44ed0e134844bce007f732a3c Mon Sep 17 00:00:00 2001 From: trevan Date: Wed, 19 Jul 2017 14:42:28 -0600 Subject: [PATCH] Use the customMetric's formatter for pipeline aggregations (#11933) This is a manual backport and required edits. --- .../__tests__/metrics/parent_pipeline.js | 34 +++++++++++++++++++ .../__tests__/metrics/sibling_pipeline.js | 19 +++++++++++ src/ui/public/agg_types/metrics/bucket_avg.js | 3 +- src/ui/public/agg_types/metrics/bucket_max.js | 3 +- src/ui/public/agg_types/metrics/bucket_min.js | 3 +- src/ui/public/agg_types/metrics/bucket_sum.js | 3 +- .../agg_types/metrics/cumulative_sum.js | 3 +- src/ui/public/agg_types/metrics/derivative.js | 3 +- .../metrics/lib/parent_pipeline_agg_helper.js | 9 +++++ .../lib/sibling_pipeline_agg_helper.js | 3 ++ src/ui/public/agg_types/metrics/moving_avg.js | 3 +- .../public/agg_types/metrics/serial_diff.js | 3 +- 12 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/ui/public/agg_types/__tests__/metrics/parent_pipeline.js b/src/ui/public/agg_types/__tests__/metrics/parent_pipeline.js index 89eb6757bc9d3..2074813f62826 100644 --- a/src/ui/public/agg_types/__tests__/metrics/parent_pipeline.js +++ b/src/ui/public/agg_types/__tests__/metrics/parent_pipeline.js @@ -27,6 +27,7 @@ describe('parent pipeline aggs', function () { ngMock.inject(function (Private) { const Vis = Private(VisProvider); const indexPattern = Private(StubbedIndexPattern); + indexPattern.stubSetFieldFormat('bytes', 'bytes'); metricAgg = Private(metric.provider); const params = settings || { @@ -52,6 +53,12 @@ describe('parent pipeline aggs', function () { type: metric.name, schema: 'metric', params + }, + { + id: '3', + type: 'max', + params: { field: '@timestamp' }, + schema: 'metric' } ], listeners: {} @@ -137,6 +144,33 @@ describe('parent pipeline aggs', function () { expect(aggDsl.parentAggs['2-metric'][metric.name].buckets_path).to.be('2-metric-metric'); }); + it('should have correct formatter', function () { + init({ + metricAgg: '3' + }); + expect(metricAgg.getFormat(aggConfig).type.id).to.be('date'); + }); + + it('should have correct customMetric nested formatter', function () { + init({ + metricAgg: 'custom', + customMetric: { + id:'2-metric', + type: metric.name, + params: { + buckets_path: 'custom', + customMetric: { + id:'2-metric-metric', + type: 'max', + params: { field: 'bytes' }, + schema: 'orderAgg' + } + }, + schema: 'orderAgg' + } + }); + expect(metricAgg.getFormat(aggConfig).type.id).to.be('bytes'); + }); }); }); diff --git a/src/ui/public/agg_types/__tests__/metrics/sibling_pipeline.js b/src/ui/public/agg_types/__tests__/metrics/sibling_pipeline.js index dcb41b28a4a59..bcd6acff9bab5 100644 --- a/src/ui/public/agg_types/__tests__/metrics/sibling_pipeline.js +++ b/src/ui/public/agg_types/__tests__/metrics/sibling_pipeline.js @@ -27,6 +27,7 @@ describe('sibling pipeline aggs', function () { ngMock.inject(function (Private) { const Vis = Private(VisProvider); const indexPattern = Private(StubbedIndexPattern); + indexPattern.stubSetFieldFormat('bytes', 'bytes'); metricAgg = Private(metric.provider); const params = settings || { @@ -103,6 +104,24 @@ describe('sibling pipeline aggs', function () { expect(aggDsl.parentAggs['2-bucket'].aggs['2-metric'].avg.field).to.equal('bytes'); }); + it('should have correct formatter', function () { + init({ + customMetric: { + id: '5', + type: 'avg', + schema: 'metric', + params: { field: 'bytes' }, + }, + customBucket: { + id: '6', + type: 'date_histogram', + schema: 'bucket', + params: { field: '@timestamp' }, + } + }); + expect(metricAgg.getFormat(aggConfig).type.id).to.be('bytes'); + }); + }); }); diff --git a/src/ui/public/agg_types/metrics/bucket_avg.js b/src/ui/public/agg_types/metrics/bucket_avg.js index 4124e0d1667a3..048168a74ac5c 100644 --- a/src/ui/public/agg_types/metrics/bucket_avg.js +++ b/src/ui/public/agg_types/metrics/bucket_avg.js @@ -13,6 +13,7 @@ export function AggTypesMetricsBucketAvgProvider(Private) { subtype: siblingPipelineHelper.subtype, params: [ ...siblingPipelineHelper.params() - ] + ], + getFormat: siblingPipelineHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/bucket_max.js b/src/ui/public/agg_types/metrics/bucket_max.js index 0d37be22b2b00..b3e9230d0b67c 100644 --- a/src/ui/public/agg_types/metrics/bucket_max.js +++ b/src/ui/public/agg_types/metrics/bucket_max.js @@ -13,6 +13,7 @@ export function AggTypesMetricsBucketMaxProvider(Private) { subtype: siblingPipelineHelper.subtype, params: [ ...siblingPipelineHelper.params() - ] + ], + getFormat: siblingPipelineHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/bucket_min.js b/src/ui/public/agg_types/metrics/bucket_min.js index 3ed3cd6d2e88c..817d5bea75af5 100644 --- a/src/ui/public/agg_types/metrics/bucket_min.js +++ b/src/ui/public/agg_types/metrics/bucket_min.js @@ -13,6 +13,7 @@ export function AggTypesMetricsBucketMinProvider(Private) { subtype: siblingPipelineHelper.subtype, params: [ ...siblingPipelineHelper.params() - ] + ], + getFormat: siblingPipelineHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/bucket_sum.js b/src/ui/public/agg_types/metrics/bucket_sum.js index f10dca7fb848a..7ec30eebec9a6 100644 --- a/src/ui/public/agg_types/metrics/bucket_sum.js +++ b/src/ui/public/agg_types/metrics/bucket_sum.js @@ -13,6 +13,7 @@ export function AggTypesMetricsBucketSumProvider(Private) { subtype: siblingPipelineHelper.subtype, params: [ ...siblingPipelineHelper.params() - ] + ], + getFormat: siblingPipelineHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/cumulative_sum.js b/src/ui/public/agg_types/metrics/cumulative_sum.js index 4a4024ce65edc..bf7ac57360869 100644 --- a/src/ui/public/agg_types/metrics/cumulative_sum.js +++ b/src/ui/public/agg_types/metrics/cumulative_sum.js @@ -13,6 +13,7 @@ export function AggTypesMetricsCumulativeSumProvider(Private) { makeLabel: agg => makeNestedLabel(agg, 'cumulative sum'), params: [ ...parentPipelineAggHelper.params() - ] + ], + getFormat: parentPipelineAggHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/derivative.js b/src/ui/public/agg_types/metrics/derivative.js index 9fbbd82918582..959d1e32d6fbd 100644 --- a/src/ui/public/agg_types/metrics/derivative.js +++ b/src/ui/public/agg_types/metrics/derivative.js @@ -13,6 +13,7 @@ export function AggTypesMetricsDerivativeProvider(Private) { makeLabel: agg => makeNestedLabel(agg, 'derivative'), params: [ ...parentPipelineAggHelper.params() - ] + ], + getFormat: parentPipelineAggHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js b/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js index 1c063d8e1530a..5228d7b4890d2 100644 --- a/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js +++ b/src/ui/public/agg_types/metrics/lib/parent_pipeline_agg_helper.js @@ -56,6 +56,15 @@ export const ParentPipelineAggHelperProvider = function (Private) { write: parentPipelineAggWritter } ]; + }, + getFormat: function (agg) { + let subAgg; + if (agg.params.customMetric) { + subAgg = agg.params.customMetric; + } else { + subAgg = agg.vis.aggs.byId[agg.params.metricAgg]; + } + return subAgg.type.getFormat(subAgg); } }; }; diff --git a/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js b/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js index 9e79b5d105da0..5e18f2dfa2071 100644 --- a/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js +++ b/src/ui/public/agg_types/metrics/lib/sibling_pipeline_agg_helper.js @@ -83,6 +83,9 @@ export const SiblingPipelineAggHelperProvider = function (Private) { write: siblingPipelineAggWritter } ]; + }, + getFormat: function (agg) { + return agg.params.customMetric.type.getFormat(agg.params.customMetric); } }; diff --git a/src/ui/public/agg_types/metrics/moving_avg.js b/src/ui/public/agg_types/metrics/moving_avg.js index 32bb389d5f809..c808fb1cf5079 100644 --- a/src/ui/public/agg_types/metrics/moving_avg.js +++ b/src/ui/public/agg_types/metrics/moving_avg.js @@ -13,6 +13,7 @@ export function AggTypesMetricsMovingAvgProvider(Private) { makeLabel: agg => makeNestedLabel(agg, 'moving avg'), params: [ ...parentPipelineAggHelper.params() - ] + ], + getFormat: parentPipelineAggHelper.getFormat }); } diff --git a/src/ui/public/agg_types/metrics/serial_diff.js b/src/ui/public/agg_types/metrics/serial_diff.js index 36caa7c281f99..e93583af80cdb 100644 --- a/src/ui/public/agg_types/metrics/serial_diff.js +++ b/src/ui/public/agg_types/metrics/serial_diff.js @@ -13,6 +13,7 @@ export function AggTypesMetricsSerialDiffProvider(Private) { makeLabel: agg => makeNestedLabel(agg, 'serial diff'), params: [ ...parentPipelineAggHelper.params() - ] + ], + getFormat: parentPipelineAggHelper.getFormat }); }