mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(influxdb): progress with new influxdb editor
This commit is contained in:
@@ -16,7 +16,10 @@ class InfluxQuery {
|
||||
this.target = target;
|
||||
|
||||
target.tags = target.tags || [];
|
||||
target.groupBy = target.groupBy || [{type: 'time', params: ['$interval']}];
|
||||
target.groupBy = target.groupBy || [
|
||||
{type: 'time', params: ['$interval']},
|
||||
{type: 'fill', params: ['null']},
|
||||
];
|
||||
target.select = target.select || [[
|
||||
{type: 'field', params: ['value']},
|
||||
{type: 'mean', params: []},
|
||||
@@ -160,13 +163,18 @@ class InfluxQuery {
|
||||
query += conditions.join(' ');
|
||||
query += (conditions.length > 0 ? ' AND ' : '') + '$timeFilter';
|
||||
|
||||
query += ' GROUP BY ';
|
||||
var groupBySection = "";
|
||||
for (i = 0; i < this.groupByParts.length; i++) {
|
||||
var part = this.groupByParts[i];
|
||||
if (i > 0) {
|
||||
query += ', ';
|
||||
// for some reason fill has no seperator
|
||||
groupBySection += part.def.type === 'fill' ? ' ' : ', ';
|
||||
}
|
||||
query += part.render('');
|
||||
groupBySection += part.render('');
|
||||
}
|
||||
|
||||
if (groupBySection.length) {
|
||||
query += ' GROUP BY ' + groupBySection;
|
||||
}
|
||||
|
||||
if (target.fill) {
|
||||
|
||||
@@ -70,7 +70,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
|
||||
.then(function(tags) {
|
||||
var options = [];
|
||||
if (!$scope.queryModel.hasFill()) {
|
||||
options.push(uiSegmentSrv.newSegment({value: 'fill(option)'}));
|
||||
options.push(uiSegmentSrv.newSegment({value: 'fill(null)'}));
|
||||
}
|
||||
if (!$scope.queryModel.hasGroupByTime()) {
|
||||
options.push(uiSegmentSrv.newSegment({value: 'time($interval)'}));
|
||||
|
||||
@@ -71,6 +71,13 @@ function quotedIdentityRenderer(part, innerExpr) {
|
||||
return '"' + part.params[0] + '"';
|
||||
}
|
||||
|
||||
function fieldRenderer(part, innerExpr) {
|
||||
if (part.params[0] === '*') {
|
||||
return '*';
|
||||
}
|
||||
return '"' + part.params[0] + '"';
|
||||
}
|
||||
|
||||
function replaceAggregationAddStrategy(selectParts, partModel) {
|
||||
// look for existing aggregation
|
||||
for (var i = 0; i < selectParts.length; i++) {
|
||||
@@ -146,7 +153,7 @@ QueryPartDef.register({
|
||||
category: categories.Fields,
|
||||
params: [{type: 'field'}],
|
||||
defaultParams: ['value'],
|
||||
renderer: quotedIdentityRenderer,
|
||||
renderer: fieldRenderer,
|
||||
});
|
||||
|
||||
QueryPartDef.register({
|
||||
@@ -184,12 +191,20 @@ QueryPartDef.register({
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
QueryPartDef.register({
|
||||
type: 'fill',
|
||||
category: groupByTimeFunctions,
|
||||
params: [{ name: "fill", type: "string", options: ['none', 'null', '0', 'previous'] }],
|
||||
defaultParams: ['null'],
|
||||
renderer: functionRenderer,
|
||||
});
|
||||
|
||||
QueryPartDef.register({
|
||||
type: 'tag',
|
||||
category: groupByTimeFunctions,
|
||||
params: [{name: 'tag', type: 'string'}],
|
||||
defaultParams: ['tag'],
|
||||
renderer: quotedIdentityRenderer,
|
||||
renderer: fieldRenderer,
|
||||
});
|
||||
|
||||
QueryPartDef.register({
|
||||
|
||||
@@ -4,18 +4,18 @@ import InfluxQuery = require('../influx_query');
|
||||
|
||||
describe.only('InfluxQuery', function() {
|
||||
|
||||
describe('series with mesurement only', function() {
|
||||
describe('render series with mesurement only', function() {
|
||||
it('should generate correct query', function() {
|
||||
var query = new InfluxQuery({
|
||||
measurement: 'cpu',
|
||||
});
|
||||
|
||||
var queryText = query.render();
|
||||
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($interval)');
|
||||
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('series with math and alias', function() {
|
||||
describe('render series with math and alias', function() {
|
||||
it('should generate correct query', function() {
|
||||
var query = new InfluxQuery({
|
||||
measurement: 'cpu',
|
||||
@@ -30,7 +30,31 @@ describe.only('InfluxQuery', function() {
|
||||
});
|
||||
|
||||
var queryText = query.render();
|
||||
expect(queryText).to.be('SELECT mean("value") /100 AS "text" FROM "cpu" WHERE $timeFilter GROUP BY time($interval)');
|
||||
expect(queryText).to.be('SELECT mean("value") /100 AS "text" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
|
||||
});
|
||||
});
|
||||
|
||||
describe('render series without group by', function() {
|
||||
it('should generate correct query', function() {
|
||||
var query = new InfluxQuery({
|
||||
measurement: 'cpu',
|
||||
select: [[{type: 'field', params: ['value']}]],
|
||||
groupBy: [],
|
||||
});
|
||||
var queryText = query.render();
|
||||
expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter');
|
||||
});
|
||||
});
|
||||
|
||||
describe('render series without group by and fill', function() {
|
||||
it('should generate correct query', function() {
|
||||
var query = new InfluxQuery({
|
||||
measurement: 'cpu',
|
||||
select: [[{type: 'field', params: ['value']}]],
|
||||
groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
|
||||
});
|
||||
var queryText = query.render();
|
||||
expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(0)');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user