Просмотр исходного кода

feat(elasticsearch): more work on alias pattern, #1034

Torkel Ödegaard 10 лет назад
Родитель
Сommit
35cc3837a0

+ 41 - 16
public/app/plugins/datasource/elasticsearch/elasticResponse.js

@@ -1,7 +1,8 @@
 define([
-  "lodash"
+  "lodash",
+  "./queryDef"
 ],
-function (_) {
+function (_, queryDef) {
   'use strict';
 
   function ElasticResponse(targets, response) {
@@ -87,7 +88,7 @@ function (_) {
           break;
         }
         default: {
-          newSeries = { datapoints: [], metric: metric.type + ' ' + metric.field, props: props};
+          newSeries = { datapoints: [], metric: metric.type, field: metric.field, props: props};
           for (i = 0; i < esAgg.buckets.length; i++) {
             bucket = esAgg.buckets[i];
             value = bucket[metric.id].value;
@@ -100,35 +101,62 @@ function (_) {
     }
   };
 
-  ElasticResponse.prototype._getSeriesName = function(props, metric, target, metricTypeCount) {
+  ElasticResponse.prototype._getMetricName = function(metric) {
+    var metricDef = _.findWhere(queryDef.metricAggTypes, {value: metric});
+    if (!metricDef)  {
+      metricDef = _.findWhere(queryDef.extendedStats, {value: metric});
+    }
+
+    return metricDef ? metricDef.text : metric;
+  };
+
+  ElasticResponse.prototype._getSeriesName = function(series, target, metricTypeCount) {
+    var metricName = this._getMetricName(series.metric);
+
     if (target.alias) {
       var regex = /\{\{([\s\S]+?)\}\}/g;
 
       return target.alias.replace(regex, function(match, g1, g2) {
         var group = g1 || g2;
 
-        if (props[group]) { return props[group]; }
-        if (group === 'metric') { return metric; }
+        if (group.indexOf('term ') === 0) { return series.props[group.substring(5)]; }
+        if (series.props[group]) { return series.props[group]; }
+        if (group === 'metric') { return metricName; }
+        if (group === 'field') { return series.field; }
 
         return match;
       });
     }
 
-    var propKeys = _.keys(props);
+    if (series.field) {
+      metricName += ' ' + series.field;
+    }
+
+    var propKeys = _.keys(series.props);
     if (propKeys.length === 0) {
-      return metric;
+      return metricName;
     }
 
     var name = '';
-    for (var propName in props) {
-      name += props[propName] + ' ';
+    for (var propName in series.props) {
+      name += series.props[propName] + ' ';
     }
 
     if (metricTypeCount === 1) {
       return name.trim();
     }
 
-    return name.trim() + ' ' + metric;
+    return name.trim() + ' ' + metricName;
+  };
+
+  ElasticResponse.prototype.nameSeries = function(seriesList, target) {
+    var metricTypeCount = _.uniq(_.pluck(seriesList, 'metric')).length;
+    var fieldNameCount = _.uniq(_.pluck(seriesList, 'field')).length;
+
+    for (var i = 0; i < seriesList.length; i++) {
+      var series = seriesList[i];
+      series.target = this._getSeriesName(series, target, metricTypeCount, fieldNameCount);
+    }
   };
 
   ElasticResponse.prototype.getTimeSeries = function() {
@@ -145,13 +173,10 @@ function (_) {
       var tmpSeriesList = [];
 
       this.processBuckets(aggregations, target, tmpSeriesList, 0, {});
-
-      var metricTypeCount = _.uniq(_.pluck(tmpSeriesList, 'metric')).length;
+      this.nameSeries(tmpSeriesList, target);
 
       for (var y = 0; y < tmpSeriesList.length; y++) {
-        var series= tmpSeriesList[y];
-        series.target = this._getSeriesName(series.props, series.metric, target, metricTypeCount);
-        seriesList.push(series);
+        seriesList.push(tmpSeriesList[y]);
       }
     }
 

+ 5 - 0
public/app/plugins/datasource/elasticsearch/metricAgg.js

@@ -48,6 +48,11 @@ function (angular, _, queryDef) {
             return memo;
           }, []);
           $scope.settingsLinkText = 'Stats: ' + stats.join(', ');
+
+          if (stats.length === 0)  {
+            $scope.agg.meta.std_deviation_bounds_lower = true;
+            $scope.agg.meta.std_deviation_bounds_upper = true;
+          }
         }
       }
     };

+ 1 - 1
public/app/plugins/datasource/elasticsearch/partials/query.editor.html

@@ -51,7 +51,7 @@
 			Alias
 		</li>
 		<li>
-			<input type="text" class="tight-form-input" style="width: 245px;" ng-model="target.alias" spellcheck='false' placeholder="alias patterns (empty = auto)" ng-blur="get_data()">
+			<input type="text" class="tight-form-input" style="width: 260px;" ng-model="target.alias" spellcheck='false' placeholder="alias patterns (empty = auto)" ng-blur="get_data()">
 		</li>
 	</ul>
 	<div class="clearfix"></div>

+ 4 - 5
public/app/plugins/datasource/elasticsearch/partials/query.options.html

@@ -19,11 +19,10 @@
 
 		<div class="grafana-info-box span6" ng-if="editorHelpIndex === 1">
 			<h5>Alias patterns</h5>
-			<ul>
-				<li>$m = replaced with measurement name</li>
-				<li>$measurement = replaced with measurement name</li>
-				<li>$tag_hostname = replaced with the value of the hostname tag</li>
-				<li>You can also use [[tag_hostname]] pattern replacement syntax</li>
+			<ul ng-non-bindable>
+				<li>{{term fieldname}} = replaced with value of term group by</li>
+				<li>{{metric}} = replaced with metric name (ex. Average, Min, Max)</li>
+				<li>{{field}} = replaced with the metric field name</li>
 			</ul>
 		</div>
 

+ 4 - 4
public/app/plugins/datasource/elasticsearch/queryDef.js

@@ -7,10 +7,10 @@ function (_) {
   return {
     metricAggTypes: [
       {text: "Count",   value: 'count' },
-      {text: "Average of",  value: 'avg' },
-      {text: "Sum of",  value: 'sum' },
-      {text: "Max of",  value: 'max' },
-      {text: "Min of",  value: 'min' },
+      {text: "Average",  value: 'avg' },
+      {text: "Sum",  value: 'sum' },
+      {text: "Max",  value: 'max' },
+      {text: "Min",  value: 'min' },
       {text: "Extended Stats",  value: 'extended_stats' },
       {text: "Percentiles",  value: 'percentiles' },
       {text: "Unique Count", value: "cardinality" }

+ 11 - 11
public/test/specs/elasticsearch-response-specs.js

@@ -40,7 +40,7 @@ define([
 
       it('should return 1 series', function() {
         expect(result.data.length).to.be(1);
-        expect(result.data[0].target).to.be('count');
+        expect(result.data[0].target).to.be('Count');
         expect(result.data[0].datapoints.length).to.be(2);
         expect(result.data[0].datapoints[0][0]).to.be(10);
         expect(result.data[0].datapoints[0][1]).to.be(1000);
@@ -87,7 +87,7 @@ define([
         expect(result.data[0].datapoints[0][0]).to.be(10);
         expect(result.data[0].datapoints[0][1]).to.be(1000);
 
-        expect(result.data[1].target).to.be("avg value");
+        expect(result.data[1].target).to.be("Average value");
         expect(result.data[1].datapoints[0][0]).to.be(88);
         expect(result.data[1].datapoints[1][0]).to.be(99);
       });
@@ -191,10 +191,10 @@ define([
       it('should return 2 series', function() {
         expect(result.data.length).to.be(4);
         expect(result.data[0].datapoints.length).to.be(2);
-        expect(result.data[0].target).to.be('server1 count');
-        expect(result.data[1].target).to.be('server1 avg @value');
-        expect(result.data[2].target).to.be('server2 count');
-        expect(result.data[3].target).to.be('server2 avg @value');
+        expect(result.data[0].target).to.be('server1 Count');
+        expect(result.data[1].target).to.be('server1 Average @value');
+        expect(result.data[2].target).to.be('server2 Count');
+        expect(result.data[3].target).to.be('server2 Average @value');
       });
     });
 
@@ -288,8 +288,8 @@ define([
       it('should return 4 series', function() {
         expect(result.data.length).to.be(4);
         expect(result.data[0].datapoints.length).to.be(1);
-        expect(result.data[0].target).to.be('server1 max');
-        expect(result.data[1].target).to.be('server1 std_deviation_bounds_upper');
+        expect(result.data[0].target).to.be('server1 Max');
+        expect(result.data[1].target).to.be('server1 Std Dev Upper');
 
         expect(result.data[0].datapoints[0][0]).to.be(10.2);
         expect(result.data[1].datapoints[0][0]).to.be(3);
@@ -303,7 +303,7 @@ define([
         targets = [{
           refId: 'A',
           metrics: [{type: 'count', id: '1'}],
-          alias: '{{@host}} {{metric}} and!',
+          alias: '{{term @host}} {{metric}} and!',
           bucketAggs: [
             {type: 'terms', field: '@host', id: '2'},
             {type: 'date_histogram', field: '@timestamp', id: '3'}
@@ -346,8 +346,8 @@ define([
       it('should return 2 series', function() {
         expect(result.data.length).to.be(2);
         expect(result.data[0].datapoints.length).to.be(2);
-        expect(result.data[0].target).to.be('server1 count and!');
-        expect(result.data[1].target).to.be('server2 count and!');
+        expect(result.data[0].target).to.be('server1 Count and!');
+        expect(result.data[1].target).to.be('server2 Count and!');
       });
     });