Commit 23a136d9 by Torkel Ödegaard

feat(templating): moved template interpolation into query building

parent 2d9a2506
......@@ -34,7 +34,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
queryTargets.push(target);
// build query
var queryModel = new InfluxQuery(target);
var queryModel = new InfluxQuery(target, templateSrv, options.scopedVars);
var query = queryModel.render();
query = query.replace(/\$interval/g, (target.interval || options.interval));
return query;
......@@ -45,7 +45,7 @@ export function InfluxDatasource(instanceSettings, $q, backendSrv, templateSrv)
allQueries = allQueries.replace(/\$timeFilter/g, timeFilter);
// replace templated variables
allQueries = templateSrv.replace(allQueries, options.scopedVars, 'regex');
allQueries = templateSrv.replace(allQueries, options.scopedVars);
return this._seriesQuery(allQueries).then(function(data): any {
if (!data || !data.results) {
......
......@@ -8,9 +8,13 @@ export default class InfluxQuery {
selectModels: any[];
queryBuilder: any;
groupByParts: any;
templateSrv: any;
scopedVars: any;
constructor(target) {
constructor(target, templateSrv, scopedVars) {
this.target = target;
this.templateSrv = templateSrv;
this.scopedVars = scopedVars;
target.policy = target.policy || 'default';
target.dsType = 'influxdb';
......@@ -135,7 +139,7 @@ export default class InfluxQuery {
}
if (!operator) {
if (/^\/.*\/$/.test(tag.value)) {
if (/^\/.*\/$/.test(value)) {
operator = '=~';
} else {
operator = '=';
......@@ -144,7 +148,10 @@ export default class InfluxQuery {
// quote value unless regex
if (operator !== '=~' && operator !== '!~') {
value = this.templateSrv.replace(value, this.scopedVars);
value = "'" + value.replace('\\', '\\\\') + "'";
} else {
value = this.templateSrv.replace(value, this.scopedVars, 'regex');
}
return str + '"' + tag.key + '" ' + operator + ' ' + value;
......
......@@ -28,7 +28,7 @@ export class InfluxQueryCtrl extends QueryCtrl {
super($scope, $injector);
this.target = this.target;
this.queryModel = new InfluxQuery(this.target);
this.queryModel = new InfluxQuery(this.target, templateSrv, this.panel.scopedVars);
this.queryBuilder = new InfluxQueryBuilder(this.target, this.datasource.database);
this.groupBySegment = this.uiSegmentSrv.newPlusButton();
this.resultFormats = [
......
......@@ -3,12 +3,13 @@ import {describe, beforeEach, it, sinon, expect} from 'test/lib/common';
import InfluxQuery from '../influx_query';
describe('InfluxQuery', function() {
var templateSrv = {replace: val => val};
describe('render series with mesurement only', function() {
it('should generate correct query', function() {
var query = new InfluxQuery({
measurement: 'cpu',
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
......@@ -20,7 +21,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
policy: '5m_avg'
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") FROM "5m_avg"."cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
......@@ -39,7 +40,7 @@ describe('InfluxQuery', function() {
{type: 'alias', params: ['text']},
]
]
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") /100 AS "text" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
......@@ -52,7 +53,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
groupBy: [{type: 'time', params: ['auto']}],
tags: [{key: 'hostname', value: 'server\\1'}]
});
}, templateSrv, {});
var queryText = query.render();
......@@ -65,7 +66,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
groupBy: [{type: 'time', params: ['auto']}],
tags: [{key: 'app', value: '/e.*/'}]
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "app" =~ /e.*/ AND $timeFilter GROUP BY time($interval)');
......@@ -78,7 +79,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
groupBy: [{type: 'time', params: ['auto']}],
tags: [{key: 'hostname', value: 'server1'}, {key: 'app', value: 'email', condition: "AND"}]
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' AND "app" = \'email\' AND ' +
......@@ -92,7 +93,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
groupBy: [{type: 'time', params: ['auto']}],
tags: [{key: 'hostname', value: 'server1'}, {key: 'hostname', value: 'server2', condition: "OR"}]
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE "hostname" = \'server1\' OR "hostname" = \'server2\' AND ' +
......@@ -106,7 +107,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
tags: [],
groupBy: [{type: 'time', interval: 'auto'}, {type: 'tag', params: ['host']}],
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT mean("value") FROM "cpu" WHERE $timeFilter ' +
......@@ -120,7 +121,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}]],
groupBy: [],
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter');
});
......@@ -132,7 +133,7 @@ describe('InfluxQuery', function() {
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}]],
groupBy: [{type: 'time'}, {type: 'fill', params: ['0']}],
});
}, templateSrv, {});
var queryText = query.render();
expect(queryText).to.be('SELECT "value" FROM "cpu" WHERE $timeFilter GROUP BY time($interval) fill(0)');
});
......@@ -144,7 +145,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
groupBy: [{type: 'time'}, {type: 'fill'}]
});
}, templateSrv, {});
query.addGroupBy('tag(host)');
expect(query.target.groupBy.length).to.be(3);
......@@ -157,7 +158,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
groupBy: []
});
}, templateSrv, {});
query.addGroupBy('tag(host)');
expect(query.target.groupBy.length).to.be(1);
......@@ -172,7 +173,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}]]
});
}, templateSrv, {});
query.addSelectPart(query.selectModels[0], 'mean');
expect(query.target.select[0].length).to.be(2);
......@@ -183,7 +184,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
});
}, templateSrv, {});
query.addSelectPart(query.selectModels[0], 'sum');
expect(query.target.select[0].length).to.be(2);
......@@ -194,7 +195,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'alias'}]]
});
}, templateSrv, {});
query.addSelectPart(query.selectModels[0], 'math');
expect(query.target.select[0].length).to.be(4);
......@@ -205,7 +206,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}, {type: 'mean'}]]
});
}, templateSrv, {});
query.addSelectPart(query.selectModels[0], 'math');
expect(query.target.select[0].length).to.be(3);
......@@ -216,7 +217,7 @@ describe('InfluxQuery', function() {
var query = new InfluxQuery({
measurement: 'cpu',
select: [[{type: 'field', params: ['value']}, {type: 'mean'}, {type: 'math'}]]
});
}, templateSrv, {});
query.addSelectPart(query.selectModels[0], 'math');
expect(query.target.select[0].length).to.be(3);
......
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