diff --git a/public/app/directives/metric.segment.js b/public/app/directives/metric.segment.js index b6392ec2a0f..e9d014d195c 100644 --- a/public/app/directives/metric.segment.js +++ b/public/app/directives/metric.segment.js @@ -20,9 +20,9 @@ function (angular, app, _, $) { return { scope: { segment: "=", - disableCustom: "=", - getAltSegments: "&", - onValueChanged: "&", + noCustom: "=", + getOptions: "&", + onChange: "&", }, link: function($scope, elem) { @@ -48,14 +48,14 @@ function (angular, app, _, $) { segment.fake = false; segment.expandable = selected.expandable; } - else if ($scope.disableCustom === false) { + else if ($scope.noCustom === false) { segment.value = value; segment.html = $sce.trustAsHtml(value); segment.expandable = true; segment.fake = false; } - $scope.onValueChanged(); + $scope.onChange(); }); }; @@ -78,12 +78,12 @@ function (angular, app, _, $) { if (options) { return options; } $scope.$apply(function() { - $scope.getAltSegments().then(function(altSegments) { + $scope.getOptions().then(function(altSegments) { $scope.altSegments = altSegments; options = _.map($scope.altSegments, function(alt) { return alt.value; }); // add custom values - if ($scope.disableCustom === false) { + if ($scope.noCustom === false) { if (!segment.fake && _.indexOf(options, segment.value) === -1) { options.unshift(segment.value); } @@ -161,11 +161,12 @@ function (angular, app, _, $) { .module('grafana.directives') .directive('metricSegmentModel', function(uiSegmentSrv, $q) { return { - template: '', + template: '', restrict: 'E', scope: { property: "=", options: "=", + getOptions: "&", onChange: "&", }, link: { @@ -180,17 +181,26 @@ function (angular, app, _, $) { } }; - $scope.getOptions = function() { - var optionSegments = _.map($scope.options, function(option) { - return uiSegmentSrv.newSegment({value: option.text}); - }); - return $q.when(optionSegments); + $scope.getOptionsInternal = function() { + if ($scope.options) { + var optionSegments = _.map($scope.options, function(option) { + return uiSegmentSrv.newSegment({value: option.text}); + }); + return $q.when(optionSegments); + } else { + return $scope.getOptions(); + } }; $scope.onSegmentChange = function() { - var option = _.findWhere($scope.options, {text: $scope.segment.value}); - if (option && option.value !== $scope.property) { - $scope.property = option.value; + if ($scope.options) { + var option = _.findWhere($scope.options, {text: $scope.segment.value}); + if (option && option.value !== $scope.property) { + $scope.property = option.value; + $scope.onChange(); + } + } else { + $scope.property = $scope.segment.value; $scope.onChange(); } }; diff --git a/public/app/plugins/datasource/elasticsearch/datasource.js b/public/app/plugins/datasource/elasticsearch/datasource.js index be503c02cc5..c28792e2d95 100644 --- a/public/app/plugins/datasource/elasticsearch/datasource.js +++ b/public/app/plugins/datasource/elasticsearch/datasource.js @@ -191,7 +191,7 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) { seriesName = parentName; if (metric.field) { - seriesName += ' ' + metric.field + ' ' + metric.agg; + seriesName += ' ' + metric.field + ' ' + metric.type; value = bucket['m' + y.toString()].value; } else { seriesName += ' count'; diff --git a/public/app/plugins/datasource/elasticsearch/directives.js b/public/app/plugins/datasource/elasticsearch/directives.js index 40df508ab26..bd8b23de905 100644 --- a/public/app/plugins/datasource/elasticsearch/directives.js +++ b/public/app/plugins/datasource/elasticsearch/directives.js @@ -1,6 +1,7 @@ define([ 'angular', './bucketAgg', + './metricAgg', ], function (angular) { 'use strict'; diff --git a/public/app/plugins/datasource/elasticsearch/metricAgg.js b/public/app/plugins/datasource/elasticsearch/metricAgg.js new file mode 100644 index 00000000000..0cc43bcc2cc --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/metricAgg.js @@ -0,0 +1,69 @@ +define([ + 'angular', + 'lodash', + 'jquery', +], +function (angular, _, $) { + 'use strict'; + + var module = angular.module('grafana.directives'); + + module.controller('ElasticMetricAggCtrl', function($scope, uiSegmentSrv, $q) { + var metricAggs = $scope.target.metrics; + + $scope.metricAggTypes = [ + {text: "Count", value: 'count' }, + {text: "Average of", value: 'avg' }, + {text: "Sum of", value: 'sum' }, + {text: "Max of", value: 'max' }, + {text: "Min of", value: 'min' }, + {text: "Standard Deviations", value: 'std_dev' }, + ]; + + $scope.init = function() { + $scope.agg = metricAggs[$scope.index]; + if (!$scope.agg.field) { + $scope.agg.field = 'select field'; + } + } + + $scope.$watchCollection("target.metrics", function() { + $scope.isFirst = $scope.index === 0; + $scope.isLast = $scope.index === metricAggs.length - 1; + $scope.isSingle = metricAggs.length === 1; + }); + + $scope.toggleOptions = function() { + $scope.showOptions = !$scope.showOptions; + } + + $scope.addMetricAgg = function() { + var addIndex = metricAggs.length; + metricAggs.splice(addIndex, 0, {type: "count", field: "select field" }); + }; + + $scope.removeMetricAgg = function(index) { + metricAggs.splice(index, 1); + $scope.onChange(); + }; + + $scope.init(); + + }); + + module.directive('elasticMetricAgg', function() { + return { + templateUrl: 'app/plugins/datasource/elasticsearch/partials/metricAgg.html', + controller: 'ElasticMetricAggCtrl', + restrict: 'E', + scope: { + target: "=", + index: "=", + onChange: "&", + getFields: "&", + }, + link: function postLink($scope, elem) { + } + }; + }); +}); diff --git a/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html b/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html index 8b0662bad7d..85ae79a0ae0 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html +++ b/public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html @@ -14,10 +14,10 @@ diff --git a/public/app/plugins/datasource/elasticsearch/partials/metricAgg.html b/public/app/plugins/datasource/elasticsearch/partials/metricAgg.html new file mode 100644 index 00000000000..e42f2fe3f52 --- /dev/null +++ b/public/app/plugins/datasource/elasticsearch/partials/metricAgg.html @@ -0,0 +1,28 @@ +
+ + + +
+
+ + diff --git a/public/app/plugins/datasource/elasticsearch/partials/query.editor.html b/public/app/plugins/datasource/elasticsearch/partials/query.editor.html index a7a2b6648a1..74d9df874a1 100644 --- a/public/app/plugins/datasource/elasticsearch/partials/query.editor.html +++ b/public/app/plugins/datasource/elasticsearch/partials/query.editor.html @@ -57,25 +57,13 @@
-
- - -
+
+ +
diff --git a/public/app/plugins/datasource/elasticsearch/queryBuilder.js b/public/app/plugins/datasource/elasticsearch/queryBuilder.js index 39a67c4a415..52373b03249 100644 --- a/public/app/plugins/datasource/elasticsearch/queryBuilder.js +++ b/public/app/plugins/datasource/elasticsearch/queryBuilder.js @@ -70,7 +70,7 @@ function (angular) { var metric = target.metrics[i]; if (metric.field) { var aggField = {}; - aggField[metric.agg] = {field: metric.field}; + aggField[metric.type] = {field: metric.field}; nestedAggs.aggs['m' + i] = aggField; } diff --git a/public/app/plugins/datasource/elasticsearch/queryCtrl.js b/public/app/plugins/datasource/elasticsearch/queryCtrl.js index 49547295160..2bfaad6e0ab 100644 --- a/public/app/plugins/datasource/elasticsearch/queryCtrl.js +++ b/public/app/plugins/datasource/elasticsearch/queryCtrl.js @@ -10,18 +10,12 @@ function (angular, _, ElasticQueryBuilder) { module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv, $q) { - $scope.metricAggregations = { - "Count": { value: 'count' }, - "Average of": { value: 'avg' }, - "Max of": { value: 'max' }, - }; - $scope.init = function() { var target = $scope.target; if (!target) { return; } target.timeField = target.timeField || '@timestamp'; - target.metrics = target.metrics || [{ agg: 'count' }]; + target.metrics = target.metrics || [{ type: 'count' }]; target.bucketAggs = target.bucketAggs || []; target.bucketAggs = [ { diff --git a/public/test/specs/elasticsearch-querybuilder-specs.js b/public/test/specs/elasticsearch-querybuilder-specs.js index c18b598c45b..58dd269d482 100644 --- a/public/test/specs/elasticsearch-querybuilder-specs.js +++ b/public/test/specs/elasticsearch-querybuilder-specs.js @@ -9,7 +9,7 @@ define([ var builder = new ElasticQueryBuilder(); var query = builder.build({ - metrics: [{agg: 'Count'}], + metrics: [{type: 'Count'}], timeField: '@timestamp', bucketAggs: [{type: 'date_histogram', field: '@timestamp'}], }); @@ -22,7 +22,7 @@ define([ var builder = new ElasticQueryBuilder(); var query = builder.build({ - metrics: [{agg: 'Count'}], + metrics: [{type: 'Count'}], timeField: '@timestamp', bucketAggs: [ {type: 'terms', field: '@host'}, @@ -39,7 +39,7 @@ define([ var builder = new ElasticQueryBuilder(); var query = builder.build({ - metrics: [{agg: 'avg', field: '@value'}], + metrics: [{type: 'avg', field: '@value'}], bucketAggs: [{type: 'date_histogram', field: '@timestamp'}], }, 100, 1000);