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) { ...@@ -17,7 +17,6 @@ function (angular, _, kbn) {
this.username = datasource.username; this.username = datasource.username;
this.password = datasource.password; this.password = datasource.password;
this.name = datasource.name; this.name = datasource.name;
this.templateSettings = { this.templateSettings = {
interpolate : /\[\[([\s\S]+?)\]\]/g, interpolate : /\[\[([\s\S]+?)\]\]/g,
}; };
...@@ -33,7 +32,8 @@ function (angular, _, kbn) { ...@@ -33,7 +32,8 @@ function (angular, _, kbn) {
} }
var timeFilter = getTimeFilter(options); var timeFilter = getTimeFilter(options);
var additionalGroups = [];
if (target.rawQuery) { if (target.rawQuery) {
query = target.query; query = target.query;
query = query.replace(";", ""); query = query.replace(";", "");
...@@ -42,6 +42,17 @@ function (angular, _, kbn) { ...@@ -42,6 +42,17 @@ function (angular, _, kbn) {
var whereIndex = lowerCaseQueryElements.indexOf("where"); var whereIndex = lowerCaseQueryElements.indexOf("where");
var groupByIndex = lowerCaseQueryElements.indexOf("group"); var groupByIndex = lowerCaseQueryElements.indexOf("group");
var orderIndex = lowerCaseQueryElements.indexOf("order"); 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) { if (whereIndex !== -1) {
queryElements.splice(whereIndex+1, 0, timeFilter, "and"); queryElements.splice(whereIndex+1, 0, timeFilter, "and");
...@@ -88,7 +99,7 @@ function (angular, _, kbn) { ...@@ -88,7 +99,7 @@ function (angular, _, kbn) {
target.query = query; target.query = query;
} }
return this.doInfluxRequest(query, target.alias).then(handleInfluxQueryResponse); return this.doInfluxRequest(query, target.alias).then(handleInfluxQueryResponse(additionalGroups));
}, this); }, this);
...@@ -157,38 +168,43 @@ function (angular, _, kbn) { ...@@ -157,38 +168,43 @@ function (angular, _, kbn) {
return deferred.promise; return deferred.promise;
}; };
function handleInfluxQueryResponse(data) { function handleInfluxQueryResponse(additionalGroup) {
var output = []; return function(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;
}
var target = data.alias || series.name + "." + column; _.each(data, function(series) {
var datapoints = _.groupBy(series.points, function (point) { var timeCol = series.columns.indexOf('time');
if (groupByColumn == -1 ) return null; var groupCols = _.map(additionalGroup, function(col) {
else return point[groupByColumn]; return series.columns.indexOf(col);
});
datapoints = _.map(_.pairs(datapoints), function(values) {
return [values[0], _.map(values[1], function (point) { return [point[index], point[timeCol]]; }) ];
}); });
var groupByColumn = _.find(groupCols, function(col) { return col > -1; });
_.each(datapoints, function(values) {
if (values[0] == null) { _.each(series.columns, function(column, index) {
output.push({ target: target, datapoints: values[1]}); if (column === "time" || column === "sequence_number" || _.contains(additionalGroup, column)) {
} else { return;
output.push({ target: values[0] + "-" + target, datapoints: values[1] });
} }
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) { 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