From 50795adcf0acf21b14b1eabf890f1945cd919972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 9 Jul 2015 16:36:47 +0200 Subject: [PATCH] feat(influxdb 0.9): field lookup and other enhancements, #2311 --- .../plugins/datasource/influxdb/funcEditor.js | 26 ++++++++++++++++--- .../influxdb/partials/query.editor.html | 2 +- .../datasource/influxdb/queryBuilder.js | 18 ++++++++++--- .../plugins/datasource/influxdb/queryCtrl.js | 14 ++-------- .../test/specs/influx09-querybuilder-specs.js | 14 ++++++++++ 5 files changed, 54 insertions(+), 20 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/funcEditor.js b/public/app/plugins/datasource/influxdb/funcEditor.js index 05bcae6835b..7631363fa12 100644 --- a/public/app/plugins/datasource/influxdb/funcEditor.js +++ b/public/app/plugins/datasource/influxdb/funcEditor.js @@ -16,14 +16,32 @@ function (angular, _, $) { var paramTemplate = ''; + var functionList = [ + 'count', 'mean', 'sum', 'min', 'max', 'mode', 'distinct', 'median', + 'derivative', 'stddev', 'first', 'last', 'difference' + ]; + + var functionMenu = _.map(functionList, function(func) { + return { text: func, click: "changeFunction('" + func + "');" }; + }); + return { restrict: 'A', scope: { field: "=", + getFields: "&", + onChange: "&", }, link: function postLink($scope, elem) { var $funcLink = $(funcSpanTemplate); + $scope.functionMenu = functionMenu; + + $scope.changeFunction = function(func) { + $scope.field.func = func; + $scope.onChange(); + }; + function clickFuncParam() { /*jshint validthis:true */ @@ -55,7 +73,7 @@ function (angular, _, $) { $link.text($input.val()); $scope.field.name = $input.val(); - $scope.$apply($scope.get_data); + $scope.$apply($scope.onChange()); } $input.hide(); @@ -79,8 +97,10 @@ function (angular, _, $) { $input.attr('data-provide', 'typeahead'); $input.typeahead({ - source: function () { - return $scope.getFields.apply(null, arguments); + source: function (query, callback) { + return $scope.getFields().then(function(results) { + callback(results); + }); }, minLength: 0, items: 20, diff --git a/public/app/plugins/datasource/influxdb/partials/query.editor.html b/public/app/plugins/datasource/influxdb/partials/query.editor.html index 81e0b07cbd9..216f099d125 100644 --- a/public/app/plugins/datasource/influxdb/partials/query.editor.html +++ b/public/app/plugins/datasource/influxdb/partials/query.editor.html @@ -66,7 +66,7 @@ SELECT diff --git a/public/app/plugins/datasource/influxdb/queryBuilder.js b/public/app/plugins/datasource/influxdb/queryBuilder.js index ca7bb02496d..717ae22847f 100644 --- a/public/app/plugins/datasource/influxdb/queryBuilder.js +++ b/public/app/plugins/datasource/influxdb/queryBuilder.js @@ -76,15 +76,25 @@ function (_) { throw "Metric measurement is missing"; } - var query = 'SELECT '; - var measurement = target.measurement; - var aggregationFunc = target.function || 'mean'; + if (!target.fields) { + target.fields = [{name: 'value', func: target.function || 'mean'}]; + } + var query = 'SELECT '; + var i; + for (i = 0; i < target.fields.length; i++) { + var field = target.fields[i]; + if (i > 0) { + query += ', '; + } + query += field.func + '(' + field.name + ')'; + } + + var measurement = target.measurement; if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) { measurement = '"' + measurement+ '"'; } - query += aggregationFunc + '(value)'; query += ' FROM ' + measurement + ' WHERE '; var conditions = _.map(target.tags, function(tag, index) { return renderTagCondition(tag, index); diff --git a/public/app/plugins/datasource/influxdb/queryCtrl.js b/public/app/plugins/datasource/influxdb/queryCtrl.js index 16e74c8e6bf..e28c6bc034d 100644 --- a/public/app/plugins/datasource/influxdb/queryCtrl.js +++ b/public/app/plugins/datasource/influxdb/queryCtrl.js @@ -10,15 +10,6 @@ function (angular, _, InfluxQueryBuilder) { module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) { - $scope.functionList = [ - 'count', 'mean', 'sum', 'min', 'max', 'mode', 'distinct', 'median', - 'derivative', 'stddev', 'first', 'last', 'difference' - ]; - - $scope.functionMenu = _.map($scope.functionList, function(func) { - return { text: func, click: "changeFunction('" + func + "');" }; - }); - $scope.init = function() { var target = $scope.target; target.tags = target.tags || []; @@ -97,12 +88,11 @@ function (angular, _, InfluxQueryBuilder) { $scope.$parent.get_data(); }; - $scope.getFields = function(query, callback) { + $scope.getFields = function() { var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS'); return $scope.datasource.metricFindQuery(fieldsQuery) .then(function(results) { - var fields = _.pluck(results, 'text'); - callback(fields); + return _.pluck(results, 'text'); }); }; diff --git a/public/test/specs/influx09-querybuilder-specs.js b/public/test/specs/influx09-querybuilder-specs.js index 6a18f0013bd..b515cfab6fb 100644 --- a/public/test/specs/influx09-querybuilder-specs.js +++ b/public/test/specs/influx09-querybuilder-specs.js @@ -38,6 +38,20 @@ define([ }); }); + describe('series with multiple fields', function() { + var builder = new InfluxQueryBuilder({ + measurement: 'cpu', + tags: [], + fields: [{ name: 'tx_in', func: 'sum' }, { name: 'tx_out', func: 'mean' }] + }); + + var query = builder.build(); + + it('should generate correct query', function() { + expect(query).to.be('SELECT sum(tx_in), mean(tx_out) FROM "cpu" WHERE $timeFilter GROUP BY time($interval) ORDER BY asc'); + }); + }); + describe('series with multiple tags only', function() { var builder = new InfluxQueryBuilder({ measurement: 'cpu',