Browse Source

Merge pull request #2104 from masaori335/kairosdb-metric-find-query

Add metricFindQuery in KairosDB Datasource Plugin
Torkel Ödegaard 10 years ago
parent
commit
4f0a4ea7e7
1 changed files with 77 additions and 0 deletions
  1. 77 0
      public/app/plugins/datasource/kairosdb/datasource.js

+ 77 - 0
public/app/plugins/datasource/kairosdb/datasource.js

@@ -91,6 +91,34 @@ function (angular, _, kbn) {
       });
     };
 
+    KairosDBDatasource.prototype.performListTagNames = function() {
+      var options = {
+        url : this.url + '/api/v1/tagnames',
+        method : 'GET'
+      };
+
+      return $http(options).then(function(response) {
+        if (!response.data) {
+          return [];
+        }
+        return response.data.results;
+      });
+    };
+
+    KairosDBDatasource.prototype.performListTagValues = function() {
+      var options = {
+        url : this.url + '/api/v1/tagvalues',
+        method : 'GET'
+      };
+
+      return $http(options).then(function(response) {
+        if (!response.data) {
+          return [];
+        }
+        return response.data.results;
+      });
+    };
+
     KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname) {
       var options = {
         url : this.url + '/api/v1/datapoints/query/tags',
@@ -112,6 +140,55 @@ function (angular, _, kbn) {
       });
     };
 
+    KairosDBDatasource.prototype.metricFindQuery = function(query) {
+      function format(results, query) {
+        return _.chain(results)
+          .filter(function(result) {
+            return result.indexOf(query) >= 0;
+          })
+          .map(function(result) {
+            return {
+              text: result,
+              expandable: true
+            };
+          })
+          .value();
+      }
+
+      var interpolated;
+      try {
+        interpolated = templateSrv.replace(query);
+      }
+      catch (err) {
+        return $q.reject(err);
+      }
+
+      var metrics_regex = /metrics\((.*)\)/;
+      var tag_names_regex = /tag_names\((.*)\)/;
+      var tag_values_regex = /tag_values\((.*)\)/;
+
+      var metrics_query = interpolated.match(metrics_regex);
+      if (metrics_query) {
+        return this.performMetricSuggestQuery().then(function(metrics) {
+          return format(metrics, metrics_query[1]);
+        });
+      }
+
+      var tag_names_query = interpolated.match(tag_names_regex);
+      if (tag_names_query) {
+        return this.performListTagNames().then(function(tag_names) {
+          return format(tag_names, tag_names_query[1]);
+        });
+      }
+
+      var tag_values_query = interpolated.match(tag_values_regex);
+      if (tag_values_query) {
+        return this.performListTagValues().then(function(tag_values) {
+          return format(tag_values, tag_values_query[1]);
+        });
+      }
+    };
+
     /////////////////////////////////////////////////////////////////////////
     /// Formatting methods
     ////////////////////////////////////////////////////////////////////////