Explorar el Código

fix(elasticsearch): made interval template variable appear in group by time interval dropdown, fixes #3241

Torkel Ödegaard hace 10 años
padre
commit
a1afd2328d

+ 19 - 0
public/app/core/services/segment_srv.js

@@ -7,6 +7,7 @@ function (angular, _, coreModule) {
   'use strict';
 
   coreModule.service('uiSegmentSrv', function($sce, templateSrv) {
+    var self = this;
 
     function MetricSegment(options) {
       if (options === '*' || options.value === '*') {
@@ -74,6 +75,24 @@ function (angular, _, coreModule) {
       });
     };
 
+    this.transformToSegments = function(addTemplateVars, variableTypeFilter) {
+      return function(results) {
+        var segments = _.map(results, function(segment) {
+          return self.newSegment({ value: segment.text, expandable: segment.expandable });
+        });
+
+        if (addTemplateVars) {
+          _.each(templateSrv.variables, function(variable) {
+            if (variableTypeFilter === void 0 || variableTypeFilter === variable.type) {
+              segments.unshift(self.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
+            }
+          });
+        }
+
+        return segments;
+      };
+    };
+
     this.newSelectMetric = function() {
       return new MetricSegment({value: 'select metric', fake: true});
     };

+ 4 - 1
public/app/plugins/datasource/elasticsearch/bucket_agg.js

@@ -15,7 +15,6 @@ function (angular, _, queryDef) {
     $scope.bucketAggTypes = queryDef.bucketAggTypes;
     $scope.orderOptions = queryDef.orderOptions;
     $scope.sizeOptions = queryDef.sizeOptions;
-    $scope.intervalOptions = queryDef.intervalOptions;
 
     $rootScope.onAppEvent('elastic-query-updated', function() {
       $scope.validateModel();
@@ -128,6 +127,10 @@ function (angular, _, queryDef) {
       }
     };
 
+    $scope.getIntervalOptions = function() {
+      return $q.when(uiSegmentSrv.transformToSegments(true, 'interval')(queryDef.intervalOptions));
+    };
+
     $scope.addBucketAgg = function() {
       // if last is date histogram add it before
       var lastBucket = bucketAggs[bucketAggs.length - 1];

+ 1 - 1
public/app/plugins/datasource/elasticsearch/partials/bucketAgg.html

@@ -41,7 +41,7 @@
 					Interval
 				</li>
 				<li>
-					<metric-segment-model property="agg.settings.interval" options="intervalOptions" on-change="onChangeInternal()" css-class="last" custom="true"></metric-segment-model>
+					<metric-segment-model property="agg.settings.interval" get-options="getIntervalOptions()" on-change="onChangeInternal()" css-class="last" custom="true"></metric-segment-model>
 				</li>
 			</ul>
 			<div class="clearfix"></div>

+ 3 - 19
public/app/plugins/datasource/elasticsearch/query_ctrl.js

@@ -1,13 +1,12 @@
 define([
   'angular',
-  'lodash',
 ],
-function (angular, _) {
+function (angular) {
   'use strict';
 
   var module = angular.module('grafana.controllers');
 
-  module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv, templateSrv) {
+  module.controller('ElasticQueryCtrl', function($scope, $timeout, uiSegmentSrv) {
 
     $scope.init = function() {
       var target = $scope.target;
@@ -21,7 +20,7 @@ function (angular, _) {
     $scope.getFields = function(type) {
       var jsonStr = angular.toJson({find: 'fields', type: type});
       return $scope.datasource.metricFindQuery(jsonStr)
-      .then($scope.transformToSegments(false))
+      .then(uiSegmentSrv.transformToSegments(false))
       .then(null, $scope.handleQueryError);
     };
 
@@ -35,21 +34,6 @@ function (angular, _) {
       $scope.appEvent('elastic-query-updated');
     };
 
-    $scope.transformToSegments = function(addTemplateVars) {
-      return function(results) {
-        var segments = _.map(results, function(segment) {
-          return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
-        });
-
-        if (addTemplateVars) {
-          _.each(templateSrv.variables, function(variable) {
-            segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
-          });
-        }
-        return segments;
-      };
-    };
-
     $scope.handleQueryError = function(err) {
       $scope.parserError = err.message || 'Failed to issue metric query';
       return [];