From b4763290de1f8ab88165fe13cb68550797d3d09b Mon Sep 17 00:00:00 2001 From: carl bergquist Date: Wed, 9 Dec 2015 08:31:01 +0100 Subject: [PATCH] move target extraction logic to query def --- .../datasource/elasticsearch/query_def.js | 14 +++++- .../elasticsearch/specs/query_def_specs.ts | 48 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 public/app/plugins/datasource/elasticsearch/specs/query_def_specs.ts diff --git a/public/app/plugins/datasource/elasticsearch/query_def.js b/public/app/plugins/datasource/elasticsearch/query_def.js index ec5141c1e8d..94401de778b 100644 --- a/public/app/plugins/datasource/elasticsearch/query_def.js +++ b/public/app/plugins/datasource/elasticsearch/query_def.js @@ -13,7 +13,7 @@ function (_) { {text: "Min", value: 'min', requiresField: true}, {text: "Extended Stats", value: 'extended_stats', requiresField: true}, {text: "Percentiles", value: 'percentiles', requiresField: true}, - {text: "Moving Avg", value: 'moving_avg', requiresField: true, requiresBucketsPath: true}, + {text: "Moving Avg", value: 'moving_avg', requiresField: false, requiresBucketsPath: true}, {text: "Unique Count", value: "cardinality", requiresField: true}, {text: "Raw Document", value: "raw_document", requiresField: false} ], @@ -67,6 +67,18 @@ function (_) { {text: '1d', value: '1d'}, ], + getMovingAverageSourceOptions: function(targets) { + var self = this; + var result = []; + _.each(targets.metrics, function(metric) { + if (metric.type !== 'moving_avg') { + result.push({text: self.describeMetric(metric), value: metric.id }); + } + }); + + return result; + }, + getOrderByOptions: function(target) { var self = this; var metricRefs = []; diff --git a/public/app/plugins/datasource/elasticsearch/specs/query_def_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/query_def_specs.ts new file mode 100644 index 00000000000..18d22f794d5 --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/specs/query_def_specs.ts @@ -0,0 +1,48 @@ +/// +/// + +import {describe, beforeEach, it, sinon, expect, angularMocks} from 'test/lib/common'; + +declare var helpers: any; +declare var QueryDef: any; + +describe('ElasticQueryDef', function() { + + describe('with zero targets', function() { + var response = QueryDef.getMovingAverageSourceOptions([]); + + it('should return zero', function() { + expect(response.length).to.be(0); + }); + }); + + describe('with count and sum targets', function() { + var targets = { + metrics: [ + { type: 'count', field: '@value' }, + { type: 'sum', field: '@value' } + ] + }; + + var response = QueryDef.getMovingAverageSourceOptions(targets); + + it('should return zero', function() { + expect(response.length).to.be(2); + }); + }); + + describe('with count and moving average targets', function() { + var targets = { + metrics: [ + { type: 'count', field: '@value' }, + { type: 'moving_avg', field: '@value' } + ] + }; + + var response = QueryDef.getMovingAverageSourceOptions(targets); + + it('should return zero', function() { + expect(response.length).to.be(1); + }); + }); +});