Jelajahi Sumber

fix group by column

Sven Klemm 7 tahun lalu
induk
melakukan
26e09b598c

+ 16 - 8
public/app/plugins/datasource/postgres/postgres_query.ts

@@ -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 partModel = queryPart.create({ type: typePart, params: [arg] });
+    var args = stringParts[3].split(",");
+    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()) {
-      query += '$__timeGroup(' + this.quoteIdentifier(target.timeColumn) + ',1m),';
+    var timeGroup = this.hasGroupByTime();
+
+    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);

+ 2 - 2
public/app/plugins/datasource/postgres/query_ctrl.ts

@@ -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;
       })
       })