Просмотр исходного кода

refactor: polishing OpenTSDB related PR #1646, added caching of aggregators request so only one call is made

Torkel Ödegaard 10 лет назад
Родитель
Сommit
5c55617585

+ 8 - 5
public/app/plugins/datasource/opentsdb/datasource.js

@@ -175,14 +175,17 @@ function (angular, _, dateMath) {
       });
     };
 
-    OpenTSDBDatasource.prototype.performAggregatorsQuery = function() {
-      return this._get('/api/aggregators', {}).then(function(result) {
-        if (result.data instanceof Array) {
+    var aggregatorsPromise = null;
+    OpenTSDBDatasource.prototype.getAggregators = function() {
+      if (aggregatorsPromise) { return aggregatorsPromise; }
+
+      aggregatorsPromise =  this._get('/api/aggregators').then(function(result) {
+        if (result.data && _.isArray(result.data)) {
           return result.data.sort();
-        } else {
-          return result.data;
         }
+        return [];
       });
+      return aggregatorsPromise;
     };
 
     function transformMetricData(md, groupByTags, target, options) {

+ 4 - 6
public/app/plugins/datasource/opentsdb/queryCtrl.js

@@ -14,12 +14,6 @@ function (angular, _, kbn) {
       $scope.target.errors = validateTarget($scope.target);
       $scope.aggregators = ['avg', 'sum', 'min', 'max', 'dev', 'zimsum', 'mimmin', 'mimmax'];
 
-      $scope.datasource.performAggregatorsQuery().then(function(result) {
-        if (result) {
-          $scope.aggregators = result;
-        }
-      });
-
       if (!$scope.target.aggregator) {
         $scope.target.aggregator = 'sum';
       }
@@ -27,6 +21,10 @@ function (angular, _, kbn) {
       if (!$scope.target.downsampleAggregator) {
         $scope.target.downsampleAggregator = 'avg';
       }
+
+      $scope.datasource.getAggregators().then(function(aggs) {
+        $scope.aggregators = aggs;
+      });
     };
 
     $scope.targetBlur = function() {