fix group by column

This commit is contained in:
Sven Klemm
2018-03-04 19:35:43 +01:00
parent e8c6341fed
commit 26e09b598c
2 changed files with 18 additions and 10 deletions

View File

@ -61,17 +61,17 @@ export default class PostgresQuery {
} }
addGroupBy(value) { addGroupBy(value) {
var stringParts = value.match(/^(\w+)\((.*)\)$/); var stringParts = value.match(/^(\w+)(\((.*)\))?$/);
var typePart = stringParts[1]; var typePart = stringParts[1];
var arg = stringParts[2]; var args = stringParts[3].split(",");
var partModel = queryPart.create({ type: typePart, params: [arg] }); var partModel = queryPart.create({ type: typePart, params: args });
var partCount = this.target.groupBy.length; var partCount = this.target.groupBy.length;
if (partCount === 0) { if (partCount === 0) {
this.target.groupBy.push(partModel.part); this.target.groupBy.push(partModel.part);
} else if (typePart === 'time') { } else if (typePart === 'time') {
this.target.groupBy.splice(0, 0, partModel.part); this.target.groupBy.splice(0, 0, partModel.part);
} else if (typePart === 'tag') { } else if (typePart === 'column') {
if (this.target.groupBy[partCount - 1].type === 'fill') { if (this.target.groupBy[partCount - 1].type === 'fill') {
this.target.groupBy.splice(partCount - 1, 0, partModel.part); this.target.groupBy.splice(partCount - 1, 0, partModel.part);
} else { } else {
@ -188,8 +188,16 @@ export default class PostgresQuery {
var query = 'SELECT '; var query = 'SELECT ';
if (this.hasGroupByTime()) { var timeGroup = this.hasGroupByTime();
query += '$__timeGroup(' + this.quoteIdentifier(target.timeColumn) + ',1m),';
if (timeGroup) {
var args;
if (timeGroup.params.length > 1 && timeGroup.params[1] !== "none") {
args = timeGroup.params.join(",");
} else {
args = timeGroup.params[0];
}
query += '$__timeGroup(' + this.quoteIdentifier(target.timeColumn) + ',' + args + '),';
} else { } else {
query += this.quoteIdentifier(target.timeColumn) + ' AS time,'; query += this.quoteIdentifier(target.timeColumn) + ' AS time,';
} }
@ -227,7 +235,7 @@ export default class PostgresQuery {
groupBySection += ', '; groupBySection += ', ';
} }
if (part.def.type === 'time') { if (part.def.type === 'time') {
groupBySection += 'time'; groupBySection += '1';
} else { } else {
groupBySection += part.render(''); groupBySection += part.render('');
} }
@ -237,7 +245,7 @@ export default class PostgresQuery {
query += ' GROUP BY ' + groupBySection; query += ' GROUP BY ' + groupBySection;
} }
query += ' ORDER BY time'; query += ' ORDER BY 1';
if (interpolate) { if (interpolate) {
query = this.templateSrv.replace(query, this.scopedVars, this.interpolateQueryStr); query = this.templateSrv.replace(query, this.scopedVars, this.interpolateQueryStr);

View File

@ -394,10 +394,10 @@ export class PostgresQueryCtrl extends QueryCtrl {
options.push(this.uiSegmentSrv.newSegment({ value: 'LIMIT' })); options.push(this.uiSegmentSrv.newSegment({ value: 'LIMIT' }));
} }
if (!this.queryModel.hasGroupByTime()) { if (!this.queryModel.hasGroupByTime()) {
options.push(this.uiSegmentSrv.newSegment({ value: 'time($interval)' })); options.push(this.uiSegmentSrv.newSegment({ type: 'time', value: 'time(1m,none)' }));
} }
for (let tag of tags) { for (let tag of tags) {
options.push(this.uiSegmentSrv.newSegment({ value: tag.text })); options.push(this.uiSegmentSrv.newSegment({ type: 'column', value: 'column(' + tag.text + ')' }));
} }
return options; return options;
}) })