Commit 80574334 by Torkel Ödegaard

Changed variable replacement works for InfluxDB, now , and

parent dd4eaa07
<div class="editor-row"> <div class="editor-row">
<div class="section"> <div class="section">
<h5>InfluxDB Query <tip>Example: select text from events where [[$timeFilter]]</tip></h5> <h5>InfluxDB Query <tip>Example: select text from events where $timeFilter</tip></h5>
<div class="editor-option"> <div class="editor-option">
<input type="text" class="span10" ng-model='currentAnnotation.query' placeholder="select text from events where [[$timeFilter]]"></input> <input type="text" class="span10" ng-model='currentAnnotation.query' placeholder="select text from events where $timeFilter"></input>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -27,13 +27,13 @@ function () { ...@@ -27,13 +27,13 @@ function () {
} }
query += target.function + '(' + target.column + ')'; query += target.function + '(' + target.column + ')';
query += ' from ' + seriesName + ' where [[$timeFilter]]'; query += ' from ' + seriesName + ' where $timeFilter';
if (target.condition) { if (target.condition) {
query += ' and ' + target.condition; query += ' and ' + target.condition;
} }
query += ' group by time([[$interval]])'; query += ' group by time($interval)';
if (target.groupby_field) { if (target.groupby_field) {
query += ', ' + target.groupby_field; query += ', ' + target.groupby_field;
...@@ -55,30 +55,11 @@ function () { ...@@ -55,30 +55,11 @@ function () {
var queryElements = query.split(" "); var queryElements = query.split(" ");
var lowerCaseQueryElements = query.toLowerCase().split(" "); var lowerCaseQueryElements = query.toLowerCase().split(" ");
var whereIndex = lowerCaseQueryElements.indexOf("where");
var groupByIndex = lowerCaseQueryElements.indexOf("group");
var orderIndex = lowerCaseQueryElements.indexOf("order");
if (lowerCaseQueryElements[1].indexOf(',') !== -1) { if (lowerCaseQueryElements[1].indexOf(',') !== -1) {
this.groupByField = lowerCaseQueryElements[1].replace(',', ''); this.groupByField = lowerCaseQueryElements[1].replace(',', '');
} }
if (whereIndex !== -1) {
queryElements.splice(whereIndex + 1, 0, '[[$timeFilter]]', "and");
}
else {
if (groupByIndex !== -1) {
queryElements.splice(groupByIndex, 0, "where", '[[$timeFilter]]');
}
else if (orderIndex !== -1) {
queryElements.splice(orderIndex, 0, "where", '[[$timeFilter]]');
}
else {
queryElements.push("where");
queryElements.push('[[$timeFilter]]');
}
}
return queryElements.join(" "); return queryElements.join(" ");
}; };
......
...@@ -44,9 +44,11 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -44,9 +44,11 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
var queryBuilder = new InfluxQueryBuilder(target); var queryBuilder = new InfluxQueryBuilder(target);
var query = queryBuilder.build(); var query = queryBuilder.build();
// replace grafana variables
query = query.replace('$timeFilter', timeFilter);
query = query.replace('$interval', (target.interval || options.interval));
// replace templated variables // replace templated variables
templateSrv.setGrafanaVariable('$timeFilter', timeFilter);
templateSrv.setGrafanaVariable('$interval', (target.interval || options.interval));
query = templateSrv.replace(query); query = templateSrv.replace(query);
var alias = target.alias ? templateSrv.replace(target.alias) : ''; var alias = target.alias ? templateSrv.replace(target.alias) : '';
...@@ -63,7 +65,8 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) { ...@@ -63,7 +65,8 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
InfluxDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) { InfluxDatasource.prototype.annotationQuery = function(annotation, rangeUnparsed) {
var timeFilter = getTimeFilter({ range: rangeUnparsed }); var timeFilter = getTimeFilter({ range: rangeUnparsed });
var query = _.template(annotation.query, { timeFilter: timeFilter, "$timeFilter": timeFilter }, this.templateSettings); var query = annotation.query.replace('$timeFilter', timeFilter);
query = templateSrv.replace(annotation.query);
return this._seriesQuery(query).then(function(results) { return this._seriesQuery(query).then(function(results) {
return new InfluxSeries({ seriesList: results, annotation: annotation }).getAnnotations(); return new InfluxSeries({ seriesList: results, annotation: annotation }).getAnnotations();
......
...@@ -17,8 +17,8 @@ define([ ...@@ -17,8 +17,8 @@ define([
var query = builder.build(); var query = builder.build();
it('should generate correct query', function() { it('should generate correct query', function() {
expect(query).to.be('select code, mean(value) from "google.test" where [[$timeFilter]] and code=1 ' + expect(query).to.be('select code, mean(value) from "google.test" where $timeFilter and code=1 ' +
'group by time([[$interval]]), code order asc'); 'group by time($interval), code order asc');
}); });
it('should expose groupByFiled', function() { it('should expose groupByFiled', function() {
...@@ -38,32 +38,12 @@ define([ ...@@ -38,32 +38,12 @@ define([
var query = builder.build(); var query = builder.build();
it('should generate correct query', function() { it('should generate correct query', function() {
expect(query).to.be('select mean(value) from "google.test" where [[$timeFilter]] ' + expect(query).to.be('select mean(value) from "google.test" where $timeFilter ' +
'group by time([[$interval]]) fill(0) order asc'); 'group by time($interval) fill(0) order asc');
}); });
}); });
describe('old style raw query', function() {
var builder = new InfluxQueryBuilder({
query: 'select host, mean(value) from asd.asd where time > now() - 1h group by time(1s), code order asc',
rawQuery: true
});
var query = builder.build();
it('should generate correct query', function() {
expect(query).to.be('select host, mean(value) from asd.asd where [[$timeFilter]] and time > now() - 1h ' +
'group by time(1s), code order asc');
});
it('should expose groupByFiled', function() {
expect(builder.groupByField).to.be('host');
});
});
}); });
}); });
...@@ -50,10 +50,10 @@ define([ ...@@ -50,10 +50,10 @@ define([
describe('When querying influxdb with one raw query', function() { describe('When querying influxdb with one raw query', function() {
var results; var results;
var urlExpected = "/series?p=mupp&q=select+value+from+series"+ var urlExpected = "/series?p=mupp&q=select+value+from+series"+
"+where+time+%3E+now()+-+1h+and+time+%3E+1&time_precision=s"; "+where+time+%3E+now()+-+1h&time_precision=s";
var query = { var query = {
range: { from: 'now-1h', to: 'now' }, range: { from: 'now-1h', to: 'now' },
targets: [{ query: "select value from series where time > 1", rawQuery: true }] targets: [{ query: "select value from series where $timeFilter", rawQuery: true }]
}; };
var response = []; var response = [];
......
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