From 463a750c0772312b825b6646477acff2d9bcca47 Mon Sep 17 00:00:00 2001 From: Lex Herbert Date: Thu, 4 Jun 2015 17:45:09 -0700 Subject: [PATCH 1/2] OpenTSDB: Restrict typeahead tag keys and values When selecting metric tag keys, we only are interested in keys which are associated with this metric. Likewise, when selecting a value for a certain key, we only want to consider values which apply to the given key and metric. --- .../plugins/datasource/opentsdb/datasource.js | 49 +++++++++++++++++++ .../plugins/datasource/opentsdb/queryCtrl.js | 4 +- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 1e60bf1e54e..57559cc6687 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -90,6 +90,55 @@ function (angular, _, kbn) { }); }; + OpenTSDBDatasource.prototype.performMetricKeyValueLookup = function(metric, key) { + if(metric === "") { + throw "Metric not set."; + } else if(key === "") { + throw "Key not set."; + } + var m = metric + "{" + key + "=*}"; + var options = { + method: 'GET', + url: this.url + '/api/search/lookup', + params: { + m: m, + } + }; + return backendSrv.datasourceRequest(options).then(function(result) { + result = result.data.results; + var tagvs = []; + _.each(result, function(r) { + tagvs.push(r.tags[key]); + }); + return tagvs; + }); + }; + + OpenTSDBDatasource.prototype.performMetricKeyLookup = function(metric) { + if(metric === "") { + throw "Metric not set."; + } + var options = { + method: 'GET', + url: this.url + '/api/search/lookup', + params: { + m: metric, + } + }; + return backendSrv.datasourceRequest(options).then(function(result) { + result = result.data.results; + var tagks = []; + _.each(result, function(r) { + _.each(r.tags, function(tagv, tagk) { + if(tagks.indexOf(tagk) === -1) { + tagks.push(tagk); + } + }); + }); + return tagks; + }); + }; + OpenTSDBDatasource.prototype.testDatasource = function() { return this.performSuggestQuery('cpu', 'metrics').then(function () { return { status: "success", message: "Data source is working", title: "Success" }; diff --git a/public/app/plugins/datasource/opentsdb/queryCtrl.js b/public/app/plugins/datasource/opentsdb/queryCtrl.js index 576517a50b6..17a2f4bd640 100644 --- a/public/app/plugins/datasource/opentsdb/queryCtrl.js +++ b/public/app/plugins/datasource/opentsdb/queryCtrl.js @@ -50,13 +50,13 @@ function (angular, _, kbn) { $scope.suggestTagKeys = function(query, callback) { $scope.datasource - .performSuggestQuery(query, 'tagk') + .performMetricKeyLookup($scope.target.metric) .then(callback); }; $scope.suggestTagValues = function(query, callback) { $scope.datasource - .performSuggestQuery(query, 'tagv') + .performMetricKeyValueLookup($scope.target.metric, $scope.target.currentTagKey) .then(callback); }; From 97f54ac3853b7567da0d0786ff6c70be2a0f5c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 30 Jun 2015 12:27:42 +0200 Subject: [PATCH 2/2] Small fix to PR 2119 --- public/app/plugins/datasource/opentsdb/datasource.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 57559cc6687..d244b56436e 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -91,11 +91,10 @@ function (angular, _, kbn) { }; OpenTSDBDatasource.prototype.performMetricKeyValueLookup = function(metric, key) { - if(metric === "") { - throw "Metric not set."; - } else if(key === "") { - throw "Key not set."; + if(!metric || !key) { + return $q.when([]); } + var m = metric + "{" + key + "=*}"; var options = { method: 'GET', @@ -104,6 +103,7 @@ function (angular, _, kbn) { m: m, } }; + return backendSrv.datasourceRequest(options).then(function(result) { result = result.data.results; var tagvs = [];