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

Merge pull request #1887 from masaori335/kairosdb-tag-suggention

Refactoring of tag suggestion in KairosDB Plugin
Torkel Ödegaard 10 лет назад
Родитель
Сommit
ff403efe09

+ 13 - 36
public/app/plugins/datasource/kairosdb/datasource.js

@@ -8,7 +8,6 @@ function (angular, _, kbn) {
   'use strict';
 
   var module = angular.module('grafana.services');
-  var tagList = null;
 
   module.factory('KairosDBDatasource', function($q, $http) {
 
@@ -92,31 +91,24 @@ function (angular, _, kbn) {
       });
     };
 
-    KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname, range, type, keyValue) {
-      if (tagList && (metricname === tagList.metricName) && (range.from === tagList.range.from) &&
-          (range.to === tagList.range.to)) {
-        return getTagListFromResponse(tagList.results, type, keyValue);
-      }
-      tagList = {
-        metricName: metricname,
-        range: range
-      };
-      var body = {
-        metrics : [{name : metricname}]
-      };
-
-      convertToKairosTime(range.from, body, 'start');
-      convertToKairosTime(range.to, body, 'end');
-
+    KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname) {
       var options = {
         url : this.url + '/api/v1/datapoints/query/tags',
         method : 'POST',
-        data : body
+        data : {
+          metrics : [{ name : metricname }],
+          cache_time : 0,
+          start_absolute: 0
+        }
       };
 
-      return $http(options).then(function(results) {
-        tagList.results = results;
-        return getTagListFromResponse(results, type, keyValue);
+      return $http(options).then(function(response) {
+        if (!response.data) {
+          return [];
+        }
+        else {
+          return response.data.queries[0].results[0];
+        }
       });
     };
 
@@ -124,21 +116,6 @@ function (angular, _, kbn) {
     /// Formatting methods
     ////////////////////////////////////////////////////////////////////////
 
-    function getTagListFromResponse(results, type, keyValue) {
-      if (!results.data) {
-        return [];
-      }
-      else if (type === "key") {
-        return _.keys(results.data.queries[0].results[0].tags);
-      }
-      else if (type === "value" && _.has(results.data.queries[0].results[0].tags, keyValue)) {
-        return results.data.queries[0].results[0].tags[keyValue];
-      }
-      else {
-        return [];
-      }
-    }
-
     /**
      * Requires a verion of KairosDB with every CORS defects fixed
      * @param results

+ 29 - 6
public/app/plugins/datasource/kairosdb/queryCtrl.js

@@ -7,7 +7,7 @@ function (angular, _) {
 
   var module = angular.module('grafana.controllers');
   var metricList = [];
-  var targetLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
+  var tagList = [];
 
   module.controller('KairosDBQueryCtrl', function($scope) {
 
@@ -20,8 +20,6 @@ function (angular, _) {
         $scope.target.downsampling = $scope.panel.downsampling;
         $scope.target.sampling = $scope.panel.sampling;
       }
-      $scope.targetLetters = targetLetters;
-      $scope.updateMetricList();
       $scope.target.errors = validateTarget($scope.target);
     };
 
@@ -63,12 +61,37 @@ function (angular, _) {
     };
 
     $scope.suggestTagKeys = function(query, callback) {
-      callback($scope.datasource.performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'key', ''));
+      if (!_.isEmpty(tagList)) {
+        var result = _.find(tagList, { name : $scope.target.metric });
+
+        if (!_.isEmpty(result)) {
+          return _.keys(result.tags);
+        }
+      }
+
+      $scope.datasource.performTagSuggestQuery($scope.target.metric).then(function(result) {
+        if (!_.isEmpty(result)) {
+          tagList.push(result);
+          callback(_.keys(result.tags));
+        }
+      });
     };
 
     $scope.suggestTagValues = function(query, callback) {
-      callback($scope.datasource
-        .performTagSuggestQuery($scope.target.metric, $scope.rangeUnparsed, 'value', $scope.target.currentTagKey));
+      if (!_.isEmpty(tagList)) {
+        var result = _.find(tagList, { name : $scope.target.metric });
+
+        if (!_.isEmpty(result)) {
+          return result.tags[$scope.target.currentTagKey];
+        }
+      }
+
+      $scope.datasource.performTagSuggestQuery($scope.target.metric).then(function(result) {
+        if (!_.isEmpty(result)) {
+          tagList.push(result);
+          callback(result.tags[$scope.target.currentTagKey]);
+        }
+      });
     };
 
     // Filter metric by tag