mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(influxdb 0.9): field lookup and other enhancements, #2311
This commit is contained in:
parent
597c1f6a41
commit
50795adcf0
public
app/plugins/datasource/influxdb
test/specs
@ -16,14 +16,32 @@ function (angular, _, $) {
|
||||
var paramTemplate = '<input type="text" style="display:none"' +
|
||||
' 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 {
|
||||
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,
|
||||
|
@ -66,7 +66,7 @@
|
||||
SELECT
|
||||
</li>
|
||||
<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>
|
||||
</li>
|
||||
</ul>
|
||||
|
@ -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);
|
||||
|
@ -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');
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -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',
|
||||
|
Loading…
Reference in New Issue
Block a user