Commit 35cc3837 by Torkel Ödegaard

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

parent 572a80d1
define([ define([
"lodash" "lodash",
"./queryDef"
], ],
function (_) { function (_, queryDef) {
'use strict'; 'use strict';
function ElasticResponse(targets, response) { function ElasticResponse(targets, response) {
...@@ -87,7 +88,7 @@ function (_) { ...@@ -87,7 +88,7 @@ function (_) {
break; break;
} }
default: { 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++) { for (i = 0; i < esAgg.buckets.length; i++) {
bucket = esAgg.buckets[i]; bucket = esAgg.buckets[i];
value = bucket[metric.id].value; value = bucket[metric.id].value;
...@@ -100,35 +101,62 @@ function (_) { ...@@ -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) { if (target.alias) {
var regex = /\{\{([\s\S]+?)\}\}/g; var regex = /\{\{([\s\S]+?)\}\}/g;
return target.alias.replace(regex, function(match, g1, g2) { return target.alias.replace(regex, function(match, g1, g2) {
var group = g1 || g2; var group = g1 || g2;
if (props[group]) { return props[group]; } if (group.indexOf('term ') === 0) { return series.props[group.substring(5)]; }
if (group === 'metric') { return metric; } if (series.props[group]) { return series.props[group]; }
if (group === 'metric') { return metricName; }
if (group === 'field') { return series.field; }
return match; return match;
}); });
} }
var propKeys = _.keys(props); if (series.field) {
metricName += ' ' + series.field;
}
var propKeys = _.keys(series.props);
if (propKeys.length === 0) { if (propKeys.length === 0) {
return metric; return metricName;
} }
var name = ''; var name = '';
for (var propName in props) { for (var propName in series.props) {
name += props[propName] + ' '; name += series.props[propName] + ' ';
} }
if (metricTypeCount === 1) { if (metricTypeCount === 1) {
return name.trim(); 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() { ElasticResponse.prototype.getTimeSeries = function() {
...@@ -145,13 +173,10 @@ function (_) { ...@@ -145,13 +173,10 @@ function (_) {
var tmpSeriesList = []; var tmpSeriesList = [];
this.processBuckets(aggregations, target, tmpSeriesList, 0, {}); this.processBuckets(aggregations, target, tmpSeriesList, 0, {});
this.nameSeries(tmpSeriesList, target);
var metricTypeCount = _.uniq(_.pluck(tmpSeriesList, 'metric')).length;
for (var y = 0; y < tmpSeriesList.length; y++) { for (var y = 0; y < tmpSeriesList.length; y++) {
var series= tmpSeriesList[y]; seriesList.push(tmpSeriesList[y]);
series.target = this._getSeriesName(series.props, series.metric, target, metricTypeCount);
seriesList.push(series);
} }
} }
......
...@@ -48,6 +48,11 @@ function (angular, _, queryDef) { ...@@ -48,6 +48,11 @@ function (angular, _, queryDef) {
return memo; return memo;
}, []); }, []);
$scope.settingsLinkText = 'Stats: ' + stats.join(', '); $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;
}
} }
} }
}; };
......
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
Alias Alias
</li> </li>
<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> </li>
</ul> </ul>
<div class="clearfix"></div> <div class="clearfix"></div>
......
...@@ -19,11 +19,10 @@ ...@@ -19,11 +19,10 @@
<div class="grafana-info-box span6" ng-if="editorHelpIndex === 1"> <div class="grafana-info-box span6" ng-if="editorHelpIndex === 1">
<h5>Alias patterns</h5> <h5>Alias patterns</h5>
<ul> <ul ng-non-bindable>
<li>$m = replaced with measurement name</li> <li>{{term fieldname}} = replaced with value of term group by</li>
<li>$measurement = replaced with measurement name</li> <li>{{metric}} = replaced with metric name (ex. Average, Min, Max)</li>
<li>$tag_hostname = replaced with the value of the hostname tag</li> <li>{{field}} = replaced with the metric field name</li>
<li>You can also use [[tag_hostname]] pattern replacement syntax</li>
</ul> </ul>
</div> </div>
......
...@@ -7,10 +7,10 @@ function (_) { ...@@ -7,10 +7,10 @@ function (_) {
return { return {
metricAggTypes: [ metricAggTypes: [
{text: "Count", value: 'count' }, {text: "Count", value: 'count' },
{text: "Average of", value: 'avg' }, {text: "Average", value: 'avg' },
{text: "Sum of", value: 'sum' }, {text: "Sum", value: 'sum' },
{text: "Max of", value: 'max' }, {text: "Max", value: 'max' },
{text: "Min of", value: 'min' }, {text: "Min", value: 'min' },
{text: "Extended Stats", value: 'extended_stats' }, {text: "Extended Stats", value: 'extended_stats' },
{text: "Percentiles", value: 'percentiles' }, {text: "Percentiles", value: 'percentiles' },
{text: "Unique Count", value: "cardinality" } {text: "Unique Count", value: "cardinality" }
......
...@@ -40,7 +40,7 @@ define([ ...@@ -40,7 +40,7 @@ define([
it('should return 1 series', function() { it('should return 1 series', function() {
expect(result.data.length).to.be(1); 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.length).to.be(2);
expect(result.data[0].datapoints[0][0]).to.be(10); expect(result.data[0].datapoints[0][0]).to.be(10);
expect(result.data[0].datapoints[0][1]).to.be(1000); expect(result.data[0].datapoints[0][1]).to.be(1000);
...@@ -87,7 +87,7 @@ define([ ...@@ -87,7 +87,7 @@ define([
expect(result.data[0].datapoints[0][0]).to.be(10); expect(result.data[0].datapoints[0][0]).to.be(10);
expect(result.data[0].datapoints[0][1]).to.be(1000); 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[0][0]).to.be(88);
expect(result.data[1].datapoints[1][0]).to.be(99); expect(result.data[1].datapoints[1][0]).to.be(99);
}); });
...@@ -191,10 +191,10 @@ define([ ...@@ -191,10 +191,10 @@ define([
it('should return 2 series', function() { it('should return 2 series', function() {
expect(result.data.length).to.be(4); expect(result.data.length).to.be(4);
expect(result.data[0].datapoints.length).to.be(2); expect(result.data[0].datapoints.length).to.be(2);
expect(result.data[0].target).to.be('server1 count'); expect(result.data[0].target).to.be('server1 Count');
expect(result.data[1].target).to.be('server1 avg @value'); expect(result.data[1].target).to.be('server1 Average @value');
expect(result.data[2].target).to.be('server2 count'); expect(result.data[2].target).to.be('server2 Count');
expect(result.data[3].target).to.be('server2 avg @value'); expect(result.data[3].target).to.be('server2 Average @value');
}); });
}); });
...@@ -288,8 +288,8 @@ define([ ...@@ -288,8 +288,8 @@ define([
it('should return 4 series', function() { it('should return 4 series', function() {
expect(result.data.length).to.be(4); expect(result.data.length).to.be(4);
expect(result.data[0].datapoints.length).to.be(1); expect(result.data[0].datapoints.length).to.be(1);
expect(result.data[0].target).to.be('server1 max'); expect(result.data[0].target).to.be('server1 Max');
expect(result.data[1].target).to.be('server1 std_deviation_bounds_upper'); 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[0].datapoints[0][0]).to.be(10.2);
expect(result.data[1].datapoints[0][0]).to.be(3); expect(result.data[1].datapoints[0][0]).to.be(3);
...@@ -303,7 +303,7 @@ define([ ...@@ -303,7 +303,7 @@ define([
targets = [{ targets = [{
refId: 'A', refId: 'A',
metrics: [{type: 'count', id: '1'}], metrics: [{type: 'count', id: '1'}],
alias: '{{@host}} {{metric}} and!', alias: '{{term @host}} {{metric}} and!',
bucketAggs: [ bucketAggs: [
{type: 'terms', field: '@host', id: '2'}, {type: 'terms', field: '@host', id: '2'},
{type: 'date_histogram', field: '@timestamp', id: '3'} {type: 'date_histogram', field: '@timestamp', id: '3'}
...@@ -346,8 +346,8 @@ define([ ...@@ -346,8 +346,8 @@ define([
it('should return 2 series', function() { it('should return 2 series', function() {
expect(result.data.length).to.be(2); expect(result.data.length).to.be(2);
expect(result.data[0].datapoints.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[0].target).to.be('server1 Count and!');
expect(result.data[1].target).to.be('server2 count and!'); expect(result.data[1].target).to.be('server2 Count and!');
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment