Commit e2188373 by Torkel Ödegaard

began work on built in template variables, #7190

parent e57c7644
......@@ -191,6 +191,13 @@ class MetricsPanelCtrl extends PanelCtrl {
return this.$q.when([]);
}
// make shallow copy of scoped vars,
// and add built in variables interval and interval_ms
var scopedVars = Object.assign({}, this.panel.scopedVars, {
"__interval": {text: this.interval, value: this.interval},
"__interval_ms": {text: this.intervalMs, value: this.intervalMs},
});
var metricsQuery = {
panelId: this.panel.id,
range: this.range,
......@@ -200,7 +207,7 @@ class MetricsPanelCtrl extends PanelCtrl {
targets: this.panel.targets,
format: this.panel.renderer === 'png' ? 'png' : 'json',
maxDataPoints: this.resolution,
scopedVars: this.panel.scopedVars,
scopedVars: scopedVars,
cacheTimeout: this.panel.cacheTimeout
};
......
......@@ -239,4 +239,16 @@ describe('templateSrv', function() {
expect(target).to.be('Server: All, period: 13m');
});
});
describe('built in interval variables', function() {
beforeEach(function() {
initTemplateSrv([]);
});
it('should replace $interval_ms with interval milliseconds', function() {
var target = _templateSrv.replace('10 * $interval_ms', {"interval_ms": {text: "100", value: "100"}});
expect(target).to.be('10 * 100');
});
});
});
......@@ -100,10 +100,10 @@ function (angular, _, kbn) {
return this.distributeVariable(value, variable.name);
}
default: {
if (typeof value === 'string') {
return value;
if (_.isArray(value)) {
return '{' + value.join(',') + '}';
}
return '{' + value.join(',') + '}';
return value;
}
}
};
......
......@@ -197,15 +197,9 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
target = options.targets[i];
if (target.hide) {continue;}
var queryObj = this.queryBuilder.build(target, adhocFilters);
var queryString = templateSrv.replace(target.query || '*', options.scopedVars, 'lucene');
var queryObj = this.queryBuilder.build(target, adhocFilters, queryString);
var esQuery = angular.toJson(queryObj);
var luceneQuery = target.query || '*';
luceneQuery = templateSrv.replace(luceneQuery, options.scopedVars, 'lucene');
luceneQuery = angular.toJson(luceneQuery);
// remove inner quotes
luceneQuery = luceneQuery.substr(1, luceneQuery.length - 2);
esQuery = esQuery.replace("$lucene_query", luceneQuery);
var searchType = (queryObj.size === 0 && this.esVersion < 5) ? 'count' : 'query_then_fetch';
var header = this.getQueryHeader(searchType, options.range.from, options.range.to);
......@@ -219,12 +213,6 @@ function (angular, _, moment, kbn, ElasticQueryBuilder, IndexPattern, ElasticRes
return $q.when([]);
}
// intervalSec: interval in seconds
if (options.interval && options.interval.match(kbn.interval_regex)) {
payload = payload.replace(/\$intervalSec/g, kbn.interval_to_seconds(options.interval));
payload = payload.replace(/\$intervalMs/g, kbn.interval_to_ms(options.interval));
}
payload = payload.replace(/\$interval/g, options.interval);
payload = payload.replace(/\$timeFrom/g, options.range.from.valueOf());
payload = payload.replace(/\$timeTo/g, options.range.to.valueOf());
payload = templateSrv.replace(payload, options.scopedVars);
......
......@@ -121,7 +121,7 @@ function (queryDef) {
}
};
ElasticQueryBuilder.prototype.build = function(target, adhocFilters) {
ElasticQueryBuilder.prototype.build = function(target, adhocFilters, queryString) {
// make sure query has defaults;
target.metrics = target.metrics || [{ type: 'count', id: '1' }];
target.dsType = 'elasticsearch';
......@@ -138,7 +138,7 @@ function (queryDef) {
{
"query_string": {
"analyze_wildcard": true,
"query": '$lucene_query'
"query": queryString,
}
}
]
......
......@@ -45,7 +45,7 @@ export default class InfluxDatasource {
query(options) {
var timeFilter = this.getTimeFilter(options);
var scopedVars = options.scopedVars ? _.cloneDeep(options.scopedVars) : {};
var scopedVars = options.scopedVars;
var targets = _.cloneDeep(options.targets);
var queryTargets = [];
var queryModel;
......@@ -56,8 +56,8 @@ export default class InfluxDatasource {
queryTargets.push(target);
// build query
scopedVars.interval = {value: target.interval || options.interval};
// backward compatability
scopedVars.interval = scopedVars.__interval;
queryModel = new InfluxQuery(target, this.templateSrv, scopedVars);
return queryModel.render(true);
......
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