Commit 80574334 by Torkel Ödegaard

Changed variable replacement works for InfluxDB, now , and

parent dd4eaa07
<div class="editor-row">
<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">
<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>
......
......@@ -27,13 +27,13 @@ function () {
}
query += target.function + '(' + target.column + ')';
query += ' from ' + seriesName + ' where [[$timeFilter]]';
query += ' from ' + seriesName + ' where $timeFilter';
if (target.condition) {
query += ' and ' + target.condition;
}
query += ' group by time([[$interval]])';
query += ' group by time($interval)';
if (target.groupby_field) {
query += ', ' + target.groupby_field;
......@@ -55,30 +55,11 @@ function () {
var queryElements = query.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) {
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(" ");
};
......
......@@ -44,9 +44,11 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
var queryBuilder = new InfluxQueryBuilder(target);
var query = queryBuilder.build();
// replace grafana variables
query = query.replace('$timeFilter', timeFilter);
query = query.replace('$interval', (target.interval || options.interval));
// replace templated variables
templateSrv.setGrafanaVariable('$timeFilter', timeFilter);
templateSrv.setGrafanaVariable('$interval', (target.interval || options.interval));
query = templateSrv.replace(query);
var alias = target.alias ? templateSrv.replace(target.alias) : '';
......@@ -63,7 +65,8 @@ function (angular, _, kbn, InfluxSeries, InfluxQueryBuilder) {
InfluxDatasource.prototype.annotationQuery = function(annotation, 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 new InfluxSeries({ seriesList: results, annotation: annotation }).getAnnotations();
......
......@@ -17,8 +17,8 @@ define([
var query = builder.build();
it('should generate correct query', function() {
expect(query).to.be('select code, mean(value) from "google.test" where [[$timeFilter]] and code=1 ' +
'group by time([[$interval]]), code order asc');
expect(query).to.be('select code, mean(value) from "google.test" where $timeFilter and code=1 ' +
'group by time($interval), code order asc');
});
it('should expose groupByFiled', function() {
......@@ -38,32 +38,12 @@ define([
var query = builder.build();
it('should generate correct query', function() {
expect(query).to.be('select mean(value) from "google.test" where [[$timeFilter]] ' +
'group by time([[$interval]]) fill(0) order asc');
expect(query).to.be('select mean(value) from "google.test" where $timeFilter ' +
'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([
describe('When querying influxdb with one raw query', function() {
var results;
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 = {
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 = [];
......
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