From 521072daea675a90518d9a913f69799437e6694a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Fjellv=C3=A6r=20Olsen?= Date: Tue, 7 Jul 2015 19:57:36 +0200 Subject: [PATCH 1/6] KairosDB: Streamline the Templating with the very related OpenTSDB plugin Since KairosDB is a fork of OpenTSDB it makes sense to (At least for the time being) keep their code bases similar-ish. --- .../plugins/datasource/kairosdb/datasource.js | 102 ++++++++++-------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/public/app/plugins/datasource/kairosdb/datasource.js b/public/app/plugins/datasource/kairosdb/datasource.js index d0cddca3bec..a47b6a9e83f 100644 --- a/public/app/plugins/datasource/kairosdb/datasource.js +++ b/public/app/plugins/datasource/kairosdb/datasource.js @@ -76,7 +76,7 @@ function (angular, _, kbn) { * Gets the list of metrics * @returns {*|Promise} */ - KairosDBDatasource.prototype.performMetricSuggestQuery = function() { + KairosDBDatasource.prototype._performMetricSuggestQuery = function(metric) { var options = { url : this.url + '/api/v1/metricnames', method : 'GET' @@ -84,46 +84,74 @@ function (angular, _, kbn) { return $http(options).then(function(response) { if (!response.data) { - return []; + return $q.when([]); } - return response.data.results; + var metrics = []; + _.each(response.data.results, function(r) { + if (r.indexOf(metric) >= 0) { + metrics.push(r); + } + }); + return metrics; }); }; - KairosDBDatasource.prototype.performListTagNames = function() { + KairosDBDatasource.prototype._performMetricKeyLookup = function(metric) { + if(!metric) { return $q.when([]); } + var options = { - url : this.url + '/api/v1/tagnames', - method : 'GET' + method: 'POST', + url: this.url + '/api/v1/datapoints/query/tags', + data: { + metrics : [{ name : metric }], + cache_time : 0, + start_absolute: 0 + } }; - return $http(options).then(function(response) { - if (!response.data) { - return []; + return $http(options).then(function(result) { + if (!result.data) { + return $q.when([]); } - return response.data.results; + var tagks = []; + _.each(result.data.queries[0].results[0].tags, function(tagv, tagk) { + if(tagks.indexOf(tagk) === -1) { + tagks.push(tagk); + } + }); + return tagks; }); }; - KairosDBDatasource.prototype.performListTagValues = function() { + KairosDBDatasource.prototype._performMetricKeyValueLookup = function(metric, key) { + if(!metric || !key) { + return $q.when([]); + } + var options = { - url : this.url + '/api/v1/tagvalues', - method : 'GET' + method: 'POST', + url: this.url + '/api/v1/datapoints/query/tags', + data: { + metrics : [{ name : metric }], + cache_time : 0, + start_absolute: 0 + } }; - return $http(options).then(function(response) { - if (!response.data) { - return []; + return $http(options).then(function(result) { + if (!result.data) { + return $q.when([]); } - return response.data.results; + return result.data.queries[0].results[0].tags[key]; }); }; - KairosDBDatasource.prototype.performTagSuggestQuery = function(metricname) { + KairosDBDatasource.prototype.performTagSuggestQuery = function(metric) { var options = { url : this.url + '/api/v1/datapoints/query/tags', method : 'POST', data : { - metrics : [{ name : metricname }], + metrics : [{ name : metric }], cache_time : 0, start_absolute: 0 } @@ -140,19 +168,7 @@ 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(); - } + if (!query) { return $q.when([]); } var interpolated; try { @@ -162,30 +178,32 @@ function (angular, _, kbn) { return $q.reject(err); } + var responseTransform = function(result) { + return _.map(result, function(value) { + return {text: value}; + }); + }; + var metrics_regex = /metrics\((.*)\)/; var tag_names_regex = /tag_names\((.*)\)/; - var tag_values_regex = /tag_values\((.*)\)/; + var tag_values_regex = /tag_values\((.*),\s?(.*?)\)/; var metrics_query = interpolated.match(metrics_regex); if (metrics_query) { - return this.performMetricSuggestQuery().then(function(metrics) { - return format(metrics, metrics_query[1]); - }); + return this._performMetricSuggestQuery(metrics_query[1]).then(responseTransform); } 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]); - }); + return this._performMetricKeyLookup(tag_names_query[1]).then(responseTransform); } 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]); - }); + return this._performMetricKeyValueLookup(tag_values_query[1], tag_values_query[2]).then(responseTransform); } + + return $q.when([]); }; ///////////////////////////////////////////////////////////////////////// From 2255cb53f0867087c994c7dd20a5b83adfcee72f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Fjellv=C3=A6r=20Olsen?= Date: Wed, 8 Jul 2015 09:18:02 +0200 Subject: [PATCH 2/6] Close the gap between the key and the value in the js objects --- .../plugins/datasource/kairosdb/datasource.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/public/app/plugins/datasource/kairosdb/datasource.js b/public/app/plugins/datasource/kairosdb/datasource.js index a47b6a9e83f..ad2dd0f52de 100644 --- a/public/app/plugins/datasource/kairosdb/datasource.js +++ b/public/app/plugins/datasource/kairosdb/datasource.js @@ -78,8 +78,8 @@ function (angular, _, kbn) { */ KairosDBDatasource.prototype._performMetricSuggestQuery = function(metric) { var options = { - url : this.url + '/api/v1/metricnames', - method : 'GET' + url: this.url + '/api/v1/metricnames', + method: 'GET' }; return $http(options).then(function(response) { @@ -103,8 +103,8 @@ function (angular, _, kbn) { method: 'POST', url: this.url + '/api/v1/datapoints/query/tags', data: { - metrics : [{ name : metric }], - cache_time : 0, + metrics: [{ name: metric }], + cache_time: 0, start_absolute: 0 } }; @@ -132,8 +132,8 @@ function (angular, _, kbn) { method: 'POST', url: this.url + '/api/v1/datapoints/query/tags', data: { - metrics : [{ name : metric }], - cache_time : 0, + metrics: [{ name: metric }], + cache_time: 0, start_absolute: 0 } }; @@ -148,11 +148,11 @@ function (angular, _, kbn) { KairosDBDatasource.prototype.performTagSuggestQuery = function(metric) { var options = { - url : this.url + '/api/v1/datapoints/query/tags', - method : 'POST', - data : { - metrics : [{ name : metric }], - cache_time : 0, + url: this.url + '/api/v1/datapoints/query/tags', + method: 'POST', + data: { + metrics: [{ name: metric }], + cache_time: 0, start_absolute: 0 } }; From 3980d25c23e2fd59b3c5eb6ed7a0b32e190e71a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Fjellv=C3=A6r=20Olsen?= Date: Wed, 8 Jul 2015 09:18:47 +0200 Subject: [PATCH 3/6] Remove superfluous whitespaces --- .../plugins/datasource/kairosdb/datasource.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/public/app/plugins/datasource/kairosdb/datasource.js b/public/app/plugins/datasource/kairosdb/datasource.js index ad2dd0f52de..a718d4f0e44 100644 --- a/public/app/plugins/datasource/kairosdb/datasource.js +++ b/public/app/plugins/datasource/kairosdb/datasource.js @@ -100,13 +100,13 @@ function (angular, _, kbn) { if(!metric) { return $q.when([]); } var options = { - method: 'POST', - url: this.url + '/api/v1/datapoints/query/tags', - data: { + method: 'POST', + url: this.url + '/api/v1/datapoints/query/tags', + data: { metrics: [{ name: metric }], cache_time: 0, start_absolute: 0 - } + } }; return $http(options).then(function(result) { @@ -129,13 +129,13 @@ function (angular, _, kbn) { } var options = { - method: 'POST', - url: this.url + '/api/v1/datapoints/query/tags', - data: { + method: 'POST', + url: this.url + '/api/v1/datapoints/query/tags', + data: { metrics: [{ name: metric }], cache_time: 0, start_absolute: 0 - } + } }; return $http(options).then(function(result) { From 3e05eb23fd489d5d4b60692e948544144733d922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Fjellv=C3=A6r=20Olsen?= Date: Wed, 8 Jul 2015 09:24:05 +0200 Subject: [PATCH 4/6] Update documentation based on templating changes --- docs/sources/datasources/kairosdb.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/sources/datasources/kairosdb.md b/docs/sources/datasources/kairosdb.md index f0d52b91548..f7ead4897ab 100644 --- a/docs/sources/datasources/kairosdb.md +++ b/docs/sources/datasources/kairosdb.md @@ -36,12 +36,13 @@ KairosDB Datasource Plugin provides following functions in `Variables values que Name | Description ---- | ---- -`metrics(query)` | Returns a list of metric names. If nothing is given, returns a list of all metric names. -`tag_names(query)` | Returns a list of tag names. If nothing is given, returns a list of all tag names. -`tag_values(query)` | Returns a list of tag values. If nothing is given, returns a list of all tag values. +`metrics(query)` | Returns a list of metric names matching `query`. If nothing is given, returns a list of all metric names. +`tag_names(query)` | Returns a list of tag names matching `query`. If nothing is given, returns a list of all tag names. +`tag_values(metric, tag)` | Returns a list of values for `tag` from the given `metric`. For details of `metric names`, `tag names`, and `tag values`, please refer to the KairosDB documentations. - [List Metric Names - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/ListMetricNames.html) - [List Tag Names - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/ListTagNames.html) - [List Tag Values - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/ListTagValues.html) +- [Query Metrics - KairosDB 0.9.4 documentation](http://kairosdb.github.io/kairosdocs/restapi/QueryMetrics.html). From 48975e6533e2427603a47ae4119fc5c96bb45cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Fjellv=C3=A6r=20Olsen?= Date: Wed, 8 Jul 2015 09:36:25 +0200 Subject: [PATCH 5/6] Keep QueryController up to date as well --- public/app/plugins/datasource/kairosdb/queryCtrl.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/kairosdb/queryCtrl.js b/public/app/plugins/datasource/kairosdb/queryCtrl.js index 9e8c5817dd1..a647b3f84a3 100644 --- a/public/app/plugins/datasource/kairosdb/queryCtrl.js +++ b/public/app/plugins/datasource/kairosdb/queryCtrl.js @@ -53,7 +53,7 @@ function (angular, _) { return metricList; } else { - $scope.datasource.performMetricSuggestQuery().then(function(result) { + $scope.datasource._performMetricSuggestQuery().then(function(result) { metricList = result; callback(metricList); }); @@ -69,7 +69,7 @@ function (angular, _) { } } - $scope.datasource.performTagSuggestQuery($scope.target.metric).then(function(result) { + $scope.datasource._performTagSuggestQuery($scope.target.metric).then(function(result) { if (!_.isEmpty(result)) { tagList.push(result); callback(_.keys(result.tags)); From d97f24cfc3775006162db8c8af967cad8275ee49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Espen=20Fjellv=C3=A6r=20Olsen?= Date: Wed, 8 Jul 2015 13:00:22 +0200 Subject: [PATCH 6/6] Update QueryController to conform with OpenTSDB Controller --- .../plugins/datasource/kairosdb/queryCtrl.js | 52 +++++-------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/public/app/plugins/datasource/kairosdb/queryCtrl.js b/public/app/plugins/datasource/kairosdb/queryCtrl.js index a647b3f84a3..4cf1b8c0f21 100644 --- a/public/app/plugins/datasource/kairosdb/queryCtrl.js +++ b/public/app/plugins/datasource/kairosdb/queryCtrl.js @@ -6,8 +6,6 @@ function (angular, _) { 'use strict'; var module = angular.module('grafana.controllers'); - var metricList = []; - var tagList = []; module.controller('KairosDBQueryCtrl', function($scope) { @@ -48,50 +46,26 @@ function (angular, _) { _.move($scope.panel.targets, fromIndex, toIndex); }; + $scope.getTextValues = function(metricFindResult) { + return _.map(metricFindResult, function(value) { return value.text; }); + }; + $scope.suggestMetrics = function(query, callback) { - if (!_.isEmpty(metricList)) { - return metricList; - } - else { - $scope.datasource._performMetricSuggestQuery().then(function(result) { - metricList = result; - callback(metricList); - }); - } + $scope.datasource.metricFindQuery('metrics(' + query + ')') + .then($scope.getTextValues) + .then(callback); }; $scope.suggestTagKeys = function(query, callback) { - 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.datasource.metricFindQuery('tag_names(' + $scope.target.metric + ')') + .then($scope.getTextValues) + .then(callback); }; $scope.suggestTagValues = function(query, callback) { - 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]); - } - }); + $scope.datasource.metricFindQuery('tag_values(' + $scope.target.metric + ',' + $scope.target.currentTagKey + ')') + .then($scope.getTextValues) + .then(callback); }; // Filter metric by tag