adds support for moving avg support in es queries

This commit is contained in:
Carl Bergquist 2015-12-08 12:04:42 +01:00
parent e194461420
commit 0731064375
2 changed files with 46 additions and 3 deletions

View File

@ -175,8 +175,28 @@ function () {
}
var aggField = {};
aggField[metric.type] = metricAgg;
nestedAggs.aggs[metric.id] = aggField;
if (metric.type === 'moving_avg') {
var pipeAgg = '';
for(var aggname in nestedAggs.aggs) {
var agg = nestedAggs.aggs[aggname];
for(prop in agg) {
if (agg.hasOwnProperty(prop) && agg[prop] !== null) {
if (metric.field === prop) {
pipeAgg = aggname;
break;
}
}
}
}
if (pipeAgg !== '') {
aggField[metric.type] = { buckets_path: pipeAgg };
nestedAggs.aggs[metric.id] = aggField;
}
} else {
aggField[metric.type] = metricAgg;
nestedAggs.aggs[metric.id] = aggField;
}
}
return query;
@ -217,5 +237,4 @@ function () {
};
return ElasticQueryBuilder;
});

View File

@ -155,4 +155,28 @@ describe('ElasticQueryBuilder', function() {
expect(query.size).to.be(500);
});
it('with moving average', function() {
var query = builder.build({
metrics: [
{
id: '1',
type: 'sum',
field: '@value',
},
{
id: '2',
type: 'moving_avg',
field: 'sum'
}
],
bucketAggs: [
{type: 'date_histogram', field: '@timestamp', id: '3'}
],
});
var firstLevel = query.aggs["3"];
expect(firstLevel.aggs["2"]).not.to.be(undefined);
expect(firstLevel.aggs["2"].moving_avg).not.to.be(undefined);
expect(firstLevel.aggs["2"].moving_avg.buckets_path).to.be("1");
});
});