Kaynağa Gözat

feat(influxdb): support for policy selection in query editor, closes #2018

Torkel Ödegaard 10 yıl önce
ebeveyn
işleme
510a21195a

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ### New Features ###
 * **Playlists**: Playlists can now be persisted and started from urls, closes [#3655](https://github.com/grafana/grafana/pull/3655)
 * **Metadata**: Settings panel now shows dashboard metadata, closes [#3304](https://github.com/grafana/grafana/issues/3304)
+* **InfluxDB**: Support for policy selection in query editor, closes [#2018](https://github.com/grafana/grafana/issues/2018)
 
 ### Breaking changes
 **Plugin API**: Both datasource and panel plugin api (and plugin.json schema) as been updated, requiring a minor update to plugins. See [plugin api](https://github.com/grafana/grafana/blob/master/public/app/plugins/plugin_api.md) for more info.

+ 8 - 7
public/app/plugins/datasource/influxdb/datasource.js

@@ -117,14 +117,15 @@ function (angular, _, dateMath, InfluxSeries, InfluxQuery) {
         if (!influxResults.series) {
           return [];
         }
-        var series = influxResults.series[0];
-
-        if (query.indexOf('SHOW MEASUREMENTS') === 0) {
-          return _.map(series.values, function(value) { return { text: value[0], expandable: true }; });
-        }
 
-        var flattenedValues = _.flatten(series.values);
-        return _.map(flattenedValues, function(value) { return { text: value, expandable: true }; });
+        var series = influxResults.series[0];
+        return _.map(series.values, function(value) {
+          if (_.isArray(value)) {
+            return { text: value[0] };
+          } else {
+            return { text: value };
+          }
+        });
       });
     };
 

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

@@ -150,6 +150,23 @@ export default class InfluxQuery {
     return str + '"' + tag.key + '" ' + operator + ' ' + value;
   }
 
+  getMeasurementAndPolicy() {
+    var policy = this.target.policy
+    var measurement = this.target.measurement;
+
+    if (!measurement.match('^/.*/')) {
+      measurement = '"' + measurement+ '"';
+    }
+
+    if (policy !== 'default') {
+      policy = '"' + this.target.policy + '".';
+    } else {
+      policy = "";
+    }
+
+    return policy + measurement;
+  }
+
   render() {
     var target = this.target;
 
@@ -177,12 +194,7 @@ export default class InfluxQuery {
       query += selectText;
     }
 
-    var measurement = target.measurement;
-    if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) {
-      measurement = '"' + measurement+ '"';
-    }
-
-    query += ' FROM ' + measurement + ' WHERE ';
+    query += ' FROM ' + this.getMeasurementAndPolicy() + ' WHERE ';
     var conditions = _.map(target.tags, (tag, index) => {
       return this.renderTagCondition(tag, index);
     });

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

@@ -15,6 +15,18 @@ describe('InfluxQuery', function() {
     });
   });
 
+  describe('render series with policy only', function() {
+    it('should generate correct query', function() {
+      var query = new InfluxQuery({
+        measurement: 'cpu',
+        policy: '5m_avg'
+      });
+
+      var queryText = query.render();
+      expect(queryText).to.be('SELECT mean("value") FROM "5m_avg"."cpu" WHERE $timeFilter GROUP BY time($interval) fill(null)');
+    });
+  });
+
   describe('render series with math and alias', function() {
     it('should generate correct query', function() {
       var query = new InfluxQuery({