Commit c193208c by Adam Heinz

Rewrite query builder to allow for multiple time series.

parent 923f9345
......@@ -295,46 +295,29 @@ function (angular, _, config, kbn, moment, ElasticQueryBuilder) {
};
ElasticDatasource.prototype.query = function(options) {
var self = this;
var allQueries = options.targets.map(function(target) {
if (target.hide) { return []; }
var queryBuilder = new ElasticQueryBuilder(target);
var query = queryBuilder.build();
console.log(target);
query = query.replace(/\$interval/g, target.interval || options.interval);
query = query.replace(/\$rangeFrom/g, options.range.from);
query = query.replace(/\$rangeTo/g, options.range.to);
query = query.replace(/\$maxDataPoints/g, options.maxDataPoints);
query = query.replace(/\$keyField/g, target.keyField);
query = query.replace(/\$valueField/g, target.valueField);
query = query.replace(/\$termKey/g, target.termKey);
query = query.replace(/\$termValue/g, target.termValue);
query = templateSrv.replace(query, options.scopedVars);
return query;
}).join("\n");
console.log(allQueries);
return this._post('/_search?search_type=count', allQueries).then(function(results) {
if (!results || !results.facets) {
return { data: [] };
}
return { data: self._getTimeSeries(results.facets) };
});
var queryBuilder = new ElasticQueryBuilder;
var query = queryBuilder.build(options.targets);
query = query.replace(/\$interval/g, options.interval);
query = query.replace(/\$rangeFrom/g, options.range.from);
query = query.replace(/\$rangeTo/g, options.range.to);
query = query.replace(/\$maxDataPoints/g, options.maxDataPoints);
query = templateSrv.replace(query, options.scopedVars);
return this._post('/_search?search_type=count', query).then(this._getTimeSeries);
};
ElasticDatasource.prototype._getTimeSeries = function(facets) {
var self = this;
var targets = ['metric'];
var data = targets.map(function(target) {
var datapoints = facets[target].entries.map(self._getDatapoint);
return { target: target, datapoints: datapoints };
});
return data;
};
ElasticDatasource.prototype._getDatapoint = function(entry) {
return [entry.mean, entry.time];
ElasticDatasource.prototype._getTimeSeries = function(results) {
var data = [];
if (results && results.facets) {
for (var target in results.facets) {
if (results.facets.hasOwnProperty(target) && results.facets[target].entries) {
var datapoints = results.facets[target].entries.map(function(entry) {
return [entry.mean, entry.time];
});
data.push({ target: target, datapoints: datapoints });
}
}
}
return { data: data };
};
return ElasticDatasource;
......
......@@ -3,51 +3,51 @@ define([
function () {
'use strict';
function ElasticQueryBuilder(target) {
this.target = target;
function ElasticQueryBuilder() {
}
ElasticQueryBuilder.prototype.build = function() {
var target = this.target;
ElasticQueryBuilder.prototype.build = function(targets) {
var query = {
"facets": {
"metric": {
"facets": {},
"size": "$maxDataPoints"
};
var self = this;
targets.forEach(function(target) {
if (!target.hide) {
query["facets"][target.termKey + "_" + target.termValue] = {
"date_histogram": {
"interval": "$interval",
"key_field": "$keyField",
"interval": target.interval || "$interval",
"key_field": target.keyField,
"min_doc_count": 0,
"value_field": "$valueField"
}
}
},
"query": {
"filtered": {
"filter": {
"value_field": target.valueField
},
"facet_filter": {
"and": [
{
"range": {
"@timestamp": {
"gte": "$rangeFrom",
"lte": "$rangeTo"
}
}
},
{
"term": {
"$termKey": "$termValue",
}
}
self._buildRangeFilter(target),
self._buildTermFilter(target)
]
}
}
},
"size": "$maxDataPoints",
"sort": "@timestamp"
};
};
}
});
query = JSON.stringify(query);
target.query = query;
return query;
};
ElasticQueryBuilder.prototype._buildRangeFilter = function(target) {
var filter = {"range":{}};
filter["range"][target.keyField] = {
"gte": "$rangeFrom",
"lte": "$rangeTo"
};
return filter;
};
ElasticQueryBuilder.prototype._buildTermFilter = function(target) {
var filter = {"term":{}};
filter["term"][target.termKey] = target.termValue;
return filter;
};
return ElasticQueryBuilder;
});
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