Commit 50795adc by Torkel Ödegaard

feat(influxdb 0.9): field lookup and other enhancements, #2311

parent 597c1f6a
...@@ -16,14 +16,32 @@ function (angular, _, $) { ...@@ -16,14 +16,32 @@ function (angular, _, $) {
var paramTemplate = '<input type="text" style="display:none"' + var paramTemplate = '<input type="text" style="display:none"' +
' class="input-mini tight-form-func-param"></input>'; ' class="input-mini tight-form-func-param"></input>';
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 { return {
restrict: 'A', restrict: 'A',
scope: { scope: {
field: "=", field: "=",
getFields: "&",
onChange: "&",
}, },
link: function postLink($scope, elem) { link: function postLink($scope, elem) {
var $funcLink = $(funcSpanTemplate); var $funcLink = $(funcSpanTemplate);
$scope.functionMenu = functionMenu;
$scope.changeFunction = function(func) {
$scope.field.func = func;
$scope.onChange();
};
function clickFuncParam() { function clickFuncParam() {
/*jshint validthis:true */ /*jshint validthis:true */
...@@ -55,7 +73,7 @@ function (angular, _, $) { ...@@ -55,7 +73,7 @@ function (angular, _, $) {
$link.text($input.val()); $link.text($input.val());
$scope.field.name = $input.val(); $scope.field.name = $input.val();
$scope.$apply($scope.get_data); $scope.$apply($scope.onChange());
} }
$input.hide(); $input.hide();
...@@ -79,8 +97,10 @@ function (angular, _, $) { ...@@ -79,8 +97,10 @@ function (angular, _, $) {
$input.attr('data-provide', 'typeahead'); $input.attr('data-provide', 'typeahead');
$input.typeahead({ $input.typeahead({
source: function () { source: function (query, callback) {
return $scope.getFields.apply(null, arguments); return $scope.getFields().then(function(results) {
callback(results);
});
}, },
minLength: 0, minLength: 0,
items: 20, items: 20,
......
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
SELECT SELECT
</li> </li>
<li class="dropdown" ng-repeat="field in target.fields"> <li class="dropdown" ng-repeat="field in target.fields">
<span influxdb-func-editor field="field" class="tight-form-item tight-form-func"> <span influxdb-func-editor field="field" get-fields="getFields()" on-change="get_data()" class="tight-form-item">
</span> </span>
</li> </li>
</ul> </ul>
......
...@@ -76,15 +76,25 @@ function (_) { ...@@ -76,15 +76,25 @@ function (_) {
throw "Metric measurement is missing"; throw "Metric measurement is missing";
} }
if (!target.fields) {
target.fields = [{name: 'value', func: target.function || 'mean'}];
}
var query = 'SELECT '; var query = 'SELECT ';
var measurement = target.measurement; var i;
var aggregationFunc = target.function || 'mean'; 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\(.*\)/)) { if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
measurement = '"' + measurement+ '"'; measurement = '"' + measurement+ '"';
} }
query += aggregationFunc + '(value)';
query += ' FROM ' + measurement + ' WHERE '; query += ' FROM ' + measurement + ' WHERE ';
var conditions = _.map(target.tags, function(tag, index) { var conditions = _.map(target.tags, function(tag, index) {
return renderTagCondition(tag, index); return renderTagCondition(tag, index);
......
...@@ -10,15 +10,6 @@ function (angular, _, InfluxQueryBuilder) { ...@@ -10,15 +10,6 @@ function (angular, _, InfluxQueryBuilder) {
module.controller('InfluxQueryCtrl', function($scope, $timeout, $sce, templateSrv, $q) { 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() { $scope.init = function() {
var target = $scope.target; var target = $scope.target;
target.tags = target.tags || []; target.tags = target.tags || [];
...@@ -97,12 +88,11 @@ function (angular, _, InfluxQueryBuilder) { ...@@ -97,12 +88,11 @@ function (angular, _, InfluxQueryBuilder) {
$scope.$parent.get_data(); $scope.$parent.get_data();
}; };
$scope.getFields = function(query, callback) { $scope.getFields = function() {
var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS'); var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
return $scope.datasource.metricFindQuery(fieldsQuery) return $scope.datasource.metricFindQuery(fieldsQuery)
.then(function(results) { .then(function(results) {
var fields = _.pluck(results, 'text'); return _.pluck(results, 'text');
callback(fields);
}); });
}; };
......
...@@ -38,6 +38,20 @@ define([ ...@@ -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() { describe('series with multiple tags only', function() {
var builder = new InfluxQueryBuilder({ var builder = new InfluxQueryBuilder({
measurement: 'cpu', measurement: 'cpu',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment