Parcourir la source

feat(influxdb): minor fixes to new editor

Torkel Ödegaard il y a 10 ans
Parent
commit
b3d494d4c8

+ 18 - 4
public/app/plugins/datasource/influxdb/influx_query.ts

@@ -44,20 +44,34 @@ class InfluxQuery {
   }
 
   hasGroupByTime() {
-    return false;
+    return _.find(this.target.groupBy, (g: any) => g.type === 'time');
   }
 
   hasFill() {
-    return false;
+    return _.find(this.target.groupBy, (g: any) => g.type === 'fill');
   }
 
   addGroupBy(value) {
     var stringParts = value.match(/^(\w+)\((.*)\)$/);
     var typePart = stringParts[1];
     var arg = stringParts[2];
-    console.log(value, stringParts);
     var partModel = queryPart.create({type: typePart, params: [arg]});
-    this.target.groupBy.push(partModel.part);
+    var partCount = this.target.groupBy.length;
+
+    if (partCount === 0) {
+      this.target.groupBy.push(partModel.part);
+    } else if (typePart === 'time') {
+      this.target.groupBy.splice(0, 0, partModel.part);
+    } else if (typePart === 'tag') {
+      if (this.target.groupBy[partCount-1].type === 'fill') {
+        this.target.groupBy.splice(partCount-1, 0, partModel.part);
+      } else {
+        this.target.groupBy.push(partModel.part);
+      }
+    } else {
+      this.target.groupBy.push(partModel.part);
+    }
+
     this.updateProjection();
   }
 

+ 1 - 1
public/app/plugins/datasource/influxdb/query_part.ts

@@ -186,7 +186,7 @@ QueryPartDef.register({
 QueryPartDef.register({
   type: 'time',
   category: groupByTimeFunctions,
-  params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5min', '10m', '15m', '1h'] }],
+  params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'] }],
   defaultParams: ['$interval'],
   renderer: functionRenderer,
 });

+ 28 - 0
public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts

@@ -58,6 +58,34 @@ describe.only('InfluxQuery', function() {
     });
   });
 
+  describe('when adding group by part', function() {
+
+    it('should add tag before fill', function() {
+      var query = new InfluxQuery({
+        measurement: 'cpu',
+        groupBy: [{type: 'time'}, {type: 'fill'}]
+      });
+
+      query.addGroupBy('tag(host)');
+      expect(query.target.groupBy.length).to.be(3);
+      expect(query.target.groupBy[1].type).to.be('tag');
+      expect(query.target.groupBy[1].params[0]).to.be('host');
+      expect(query.target.groupBy[2].type).to.be('fill');
+    });
+
+    it('should add tag last if no fill', function() {
+      var query = new InfluxQuery({
+        measurement: 'cpu',
+        groupBy: []
+      });
+
+      query.addGroupBy('tag(host)');
+      expect(query.target.groupBy.length).to.be(1);
+      expect(query.target.groupBy[0].type).to.be('tag');
+    });
+
+  });
+
   describe('when adding select part', function() {
 
     it('should add mean after after field', function() {