From 23a136d9ef040cd98a9ee96cb90dab471a6b40eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 1 Mar 2016 14:36:55 +0100 Subject: [PATCH] feat(templating): moved template interpolation into query building --- .../plugins/datasource/influxdb/datasource.ts | 4 +-- .../datasource/influxdb/influx_query.ts | 11 ++++-- .../plugins/datasource/influxdb/query_ctrl.ts | 2 +- .../influxdb/specs/influx_query_specs.ts | 35 ++++++++++--------- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index d6583477c94..56591078f93 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -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) { diff --git a/public/app/plugins/datasource/influxdb/influx_query.ts b/public/app/plugins/datasource/influxdb/influx_query.ts index cbdb61bca24..6770da0708e 100644 --- a/public/app/plugins/datasource/influxdb/influx_query.ts +++ b/public/app/plugins/datasource/influxdb/influx_query.ts @@ -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; diff --git a/public/app/plugins/datasource/influxdb/query_ctrl.ts b/public/app/plugins/datasource/influxdb/query_ctrl.ts index 2275bac559b..38dbcacae64 100644 --- a/public/app/plugins/datasource/influxdb/query_ctrl.ts +++ b/public/app/plugins/datasource/influxdb/query_ctrl.ts @@ -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 = [ diff --git a/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts b/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts index 3893b8fbc2d..157b9ed206a 100644 --- a/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts +++ b/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts @@ -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);