Commit f231af07 by Clicky

Parse additional group by columns from a raw query

This needs some further polishing but now parse out additional group by
columns from a raw query, although it really only supports one
additional column.
parent 3da1c276
......@@ -17,7 +17,6 @@ function (angular, _, kbn) {
this.username = datasource.username;
this.password = datasource.password;
this.name = datasource.name;
this.templateSettings = {
interpolate : /\[\[([\s\S]+?)\]\]/g,
};
......@@ -33,7 +32,8 @@ function (angular, _, kbn) {
}
var timeFilter = getTimeFilter(options);
var additionalGroups = [];
if (target.rawQuery) {
query = target.query;
query = query.replace(";", "");
......@@ -42,6 +42,17 @@ function (angular, _, kbn) {
var whereIndex = lowerCaseQueryElements.indexOf("where");
var groupByIndex = lowerCaseQueryElements.indexOf("group");
var orderIndex = lowerCaseQueryElements.indexOf("order");
var afterGroup = _.rest(lowerCaseQueryElements, groupByIndex);
for (var i = 0; i < afterGroup.length; i++) {
var el = afterGroup[i];
if (el === "order") break;
if ( /,$/.test(el) &&
_.size(afterGroup) > i &&
! /^time\(/.test(afterGroup[i + 1])) {
additionalGroups.push(queryElements[groupByIndex + i + 1]);
}
}
if (whereIndex !== -1) {
queryElements.splice(whereIndex+1, 0, timeFilter, "and");
......@@ -88,7 +99,7 @@ function (angular, _, kbn) {
target.query = query;
}
return this.doInfluxRequest(query, target.alias).then(handleInfluxQueryResponse);
return this.doInfluxRequest(query, target.alias).then(handleInfluxQueryResponse(additionalGroups));
}, this);
......@@ -157,38 +168,43 @@ function (angular, _, kbn) {
return deferred.promise;
};
function handleInfluxQueryResponse(data) {
var output = [];
_.each(data, function(series) {
var timeCol = series.columns.indexOf('time');
var groupByColumn = series.columns.indexOf('host');
_.each(series.columns, function(column, index) {
if (column === "time" || column === "sequence_number" || column === "host") {
return;
}
function handleInfluxQueryResponse(additionalGroup) {
return function(data) {
var output = [];
var target = data.alias || series.name + "." + column;
var datapoints = _.groupBy(series.points, function (point) {
if (groupByColumn == -1 ) return null;
else return point[groupByColumn];
});
datapoints = _.map(_.pairs(datapoints), function(values) {
return [values[0], _.map(values[1], function (point) { return [point[index], point[timeCol]]; }) ];
_.each(data, function(series) {
var timeCol = series.columns.indexOf('time');
var groupCols = _.map(additionalGroup, function(col) {
return series.columns.indexOf(col);
});
_.each(datapoints, function(values) {
if (values[0] == null) {
output.push({ target: target, datapoints: values[1]});
} else {
output.push({ target: values[0] + "-" + target, datapoints: values[1] });
var groupByColumn = _.find(groupCols, function(col) { return col > -1; });
_.each(series.columns, function(column, index) {
if (column === "time" || column === "sequence_number" || _.contains(additionalGroup, column)) {
return;
}
var target = data.alias || series.name + "." + column;
var datapoints = _.groupBy(series.points, function (point) {
if (groupByColumn == undefined) return null;
else return point[groupByColumn];
});
datapoints = _.map(_.pairs(datapoints), function(values) {
return [values[0], _.map(values[1], function (point) { return [point[index], point[timeCol]]; }) ];
});
_.each(datapoints, function(values) {
if (values[0] == null) {
output.push({ target: target, datapoints: values[1]});
} else {
output.push({ target: values[0] + "-" + target, datapoints: values[1] });
}
});
});
});
});
return output;
return output;
}
}
function getTimeFilter(options) {
......
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