Commit 03aed21d by Marcus Efraimsson Committed by GitHub

Merge pull request #12598 from kichristensen/TimeRangeVariables

Support timeFilter in query templating for InfluxDB
parents 766d0bef 8c52e2cd
......@@ -187,6 +187,11 @@ export default class InfluxDatasource {
return this.$q.when({ results: [] });
}
if (options && options.range) {
var timeFilter = this.getTimeFilter({ rangeRaw: options.range });
query = query.replace('$timeFilter', timeFilter);
}
return this._influxRequest('GET', '/query', { q: query, epoch: 'ms' }, options);
}
......
import InfluxDatasource from '../datasource';
import $q from 'q';
import { TemplateSrvStub } from 'test/specs/helpers';
describe('InfluxDataSource', () => {
let ctx: any = {
backendSrv: {},
$q: $q,
templateSrv: new TemplateSrvStub(),
instanceSettings: { url: 'url', name: 'influxDb', jsonData: {} },
};
beforeEach(function() {
ctx.instanceSettings.url = '/api/datasources/proxy/1';
ctx.ds = new InfluxDatasource(ctx.instanceSettings, ctx.$q, ctx.backendSrv, ctx.templateSrv);
});
describe('When issuing metricFindQuery', () => {
let query = 'SELECT max(value) FROM measurement WHERE $timeFilter';
let queryOptions: any = {
range: {
from: '2018-01-01T00:00:00Z',
to: '2018-01-02T00:00:00Z',
},
};
let requestQuery;
beforeEach(async () => {
ctx.backendSrv.datasourceRequest = function(req) {
requestQuery = req.params.q;
return ctx.$q.when({
results: [
{
series: [
{
name: 'measurement',
columns: ['max'],
values: [[1]],
},
],
},
],
});
};
await ctx.ds.metricFindQuery(query, queryOptions).then(function(_) {});
});
it('should replace $timefilter', () => {
expect(requestQuery).toMatch('time >= 1514764800000ms and time <= 1514851200000ms');
});
});
});
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