Skip to content

Commit

Permalink
feat(elasticsearch): metric response handling and processsing now sup…
Browse files Browse the repository at this point in the history
…ports alias patterns, {{term field name}} and {{metric}} now works, #1034
  • Loading branch information
torkelo committed Sep 7, 2015
1 parent 2aa695f commit 572a80d
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 97 deletions.
125 changes: 74 additions & 51 deletions public/app/plugins/datasource/elasticsearch/elasticResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ function (_) {

// This is quite complex
// neeed to recurise down the nested buckets to build series
ElasticResponse.prototype.processBuckets = function(aggs, target, series, level) {
var value, metric, i, y, bucket, aggDef, esAgg, nestedSeries;
ElasticResponse.prototype.processBuckets = function(aggs, target, seriesList, level, props) {
var value, metric, i, y, bucket, aggDef, esAgg, newSeries;

aggDef = target.bucketAggs[level];
esAgg = aggs[aggDef.id];

if (level < target.bucketAggs.length - 1) {
for (i = 0; i < esAgg.buckets.length; i++) {
bucket = esAgg.buckets[i];
nestedSeries = {prop: {key: bucket.key, field: aggDef.field}, series: []};
series.push(nestedSeries);
this.processBuckets(bucket, target, nestedSeries.series, level+1);
props = _.clone(props);
props[aggDef.field] = bucket.key;
this.processBuckets(bucket, target, seriesList, level+1, props);
}
return;
}
Expand All @@ -32,54 +32,86 @@ function (_) {

switch(metric.type) {
case 'count': {
var countSeries = { datapoints: [], metric: 'count'};
newSeries = { datapoints: [], metric: 'count', props: props};
for (i = 0; i < esAgg.buckets.length; i++) {
bucket = esAgg.buckets[i];
value = bucket.doc_count;
countSeries.datapoints.push([value, bucket.key]);
newSeries.datapoints.push([value, bucket.key]);
}
series.push(countSeries);
seriesList.push(newSeries);
break;
}
case 'percentiles': {
// for (i = 0; i < esAgg.buckets.length; i++) {
// bucket = esAgg.buckets[i];
// var values = bucket[metric.id].values;
// for (var prop in values) {
// addMetricPoint(seriesName + ' ' + prop, values[prop], bucket.key);
// }
// }
if (esAgg.buckets.length === 0) {
break;
}

var firstBucket = esAgg.buckets[0];
var percentiles = firstBucket[metric.id].values;

for (var percentileName in percentiles) {
newSeries = {datapoints: [], metric: 'p' + percentileName, props: props};

for (i = 0; i < esAgg.buckets.length; i++) {
bucket = esAgg.buckets[i];
var values = bucket[metric.id].values;
newSeries.datapoints.push([values[percentileName], bucket.key]);
}
seriesList.push(newSeries);
}

break;
}
case 'extended_stats': {
// var stats = bucket[metric.id];
// stats.std_deviation_bounds_upper = stats.std_deviation_bounds.upper;
// stats.std_deviation_bounds_lower = stats.std_deviation_bounds.lower;
//
// for (var statName in metric.meta) {
// if (metric.meta[statName]) {
// addMetricPoint(seriesName + ' ' + statName, stats[statName], bucket.key);
// }
// }
for (var statName in metric.meta) {
if (!metric.meta[statName]) {
continue;
}

newSeries = {datapoints: [], metric: statName, props: props};

for (i = 0; i < esAgg.buckets.length; i++) {
bucket = esAgg.buckets[i];
var stats = bucket[metric.id];

// add stats that are in nested obj to top level obj
stats.std_deviation_bounds_upper = stats.std_deviation_bounds.upper;
stats.std_deviation_bounds_lower = stats.std_deviation_bounds.lower;

newSeries.datapoints.push([stats[statName], bucket.key]);
}

seriesList.push(newSeries);
}

break;
}
default: {
var newSeries = { datapoints: [], metric: metric.type + ' ' + metric.field };
newSeries = { datapoints: [], metric: metric.type + ' ' + metric.field, props: props};
for (i = 0; i < esAgg.buckets.length; i++) {
bucket = esAgg.buckets[i];
value = bucket[metric.id].value;
newSeries.datapoints.push([value, bucket.key]);
}
series.push(newSeries);
seriesList.push(newSeries);
break;
}
}
}
};

ElasticResponse.prototype._getSeriesName = function(props, metric, alias) {
if (alias) {
return alias;
ElasticResponse.prototype._getSeriesName = function(props, metric, target, metricTypeCount) {
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; }

return match;
});
}

var propKeys = _.keys(props);
Expand All @@ -92,31 +124,15 @@ function (_) {
name += props[propName] + ' ';
}

if (propKeys.length === 1) {
if (metricTypeCount === 1) {
return name.trim();
}

return name.trim() + ' ' + metric;
};

ElasticResponse.prototype._collectSeriesFromTree = function(seriesTree, props, seriesList, alias) {
console.log('props: ', props);

for (var i = 0; i < seriesTree.length; i++) {
var series = seriesTree[i];
if (series.datapoints) {
series.target = this._getSeriesName(props, series.metric, alias);
seriesList.push(series);
} else {
props = _.clone(props);
props[series.prop.field] = series.prop.key;
this._collectSeriesFromTree(series.series, props, seriesList);
}
}
};

ElasticResponse.prototype.getTimeSeries = function() {
var series = [];
var seriesList = [];

for (var i = 0; i < this.response.responses.length; i++) {
var response = this.response.responses[i];
Expand All @@ -126,13 +142,20 @@ function (_) {

var aggregations = response.aggregations;
var target = this.targets[i];
var seriesTree = [];
var tmpSeriesList = [];

this.processBuckets(aggregations, target, tmpSeriesList, 0, {});

this.processBuckets(aggregations, target, seriesTree, 0, '');
this._collectSeriesFromTree(seriesTree, {}, series, '');
var metricTypeCount = _.uniq(_.pluck(tmpSeriesList, 'metric')).length;

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

return { data: series };
return { data: seriesList };
};

return ElasticResponse;
Expand Down
1 change: 0 additions & 1 deletion public/app/plugins/datasource/elasticsearch/metricAgg.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function (angular, _, queryDef) {
$scope.validateModel = function() {
$scope.isFirst = $scope.index === 0;
$scope.isSingle = metricAggs.length === 1;

$scope.settingsLinkText = '';

if (!$scope.agg.field) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@
<li>
<input type="text" class="tight-form-input" style="width: 345px;" ng-model="target.query" spellcheck='false' placeholder="Lucence query" ng-blur="get_data()">
</li>
<li class="tight-form-item query-keyword">
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()">
</li>
</ul>

<div class="clearfix"></div>

<div style="padding: 10px" ng-if="target.rawQuery">
Expand All @@ -57,7 +62,6 @@
</div>

<div ng-hide="target.rawQuery">

<div ng-repeat="agg in target.metrics">
<elastic-metric-agg
target="target" index="$index"
Expand Down
20 changes: 0 additions & 20 deletions public/app/plugins/datasource/influxdb/influxSeries.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,5 @@ function (_) {
return list;
};

p.createNameForSeries = function(seriesName, groupByColValue) {
var regex = /\$(\w+)/g;
var segments = seriesName.split('.');

return this.alias.replace(regex, function(match, group) {
if (group === 's') {
return seriesName;
}
else if (group === 'g') {
return groupByColValue;
}
var index = parseInt(group);
if (_.isNumber(index) && index < segments.length) {
return segments[index];
}
return match;
});

};

return InfluxSeries;
});
Loading

0 comments on commit 572a80d

Please sign in to comment.