@ -86,20 +86,71 @@ function (_, queryDef) {
}
} ;
ElasticResponse . prototype . processAggregationDocs = function ( esAgg , aggDef , target , docs , props ) {
var metric , y , i , bucket , metricName , doc ;
for ( i = 0 ; i < esAgg . buckets . length ; i ++ ) {
bucket = esAgg . buckets [ i ] ;
doc = _ . defaults ( { } , props ) ;
doc [ aggDef . field ] = bucket . key ;
for ( y = 0 ; y < target . metrics . length ; y ++ ) {
metric = target . metrics [ y ] ;
switch ( metric . type ) {
case "count" : {
metricName = this . _getMetricName ( metric . type ) ;
doc [ metricName ] = bucket . doc _count ;
break ;
}
case 'extended_stats' : {
for ( var statName in metric . meta ) {
if ( ! metric . meta [ statName ] ) {
continue ;
}
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 ;
metricName = this . _getMetricName ( statName ) ;
doc [ metricName ] = stats [ statName ] ;
}
break ;
}
default : {
metricName = this . _getMetricName ( metric . type ) ;
doc [ metricName ] = bucket [ metric . id ] . value ;
break ;
}
}
}
docs . push ( doc ) ;
}
} ;
// This is quite complex
// neeed to recurise down the nested buckets to build series
ElasticResponse . prototype . processBuckets = function ( aggs , target , seriesList , props ) {
ElasticResponse . prototype . processBuckets = function ( aggs , target , seriesList , docs , props , depth ) {
var bucket , aggDef , esAgg , aggId ;
var maxDepth = target . bucketAggs . length - 1 ;
for ( aggId in aggs ) {
aggDef = _ . findWhere ( target . bucketAggs , { id : aggId } ) ;
esAgg = aggs [ aggId ] ;
if ( ! aggDef ) {
continue ;
}
if ( aggDef . type === 'date_histogram' ) {
this . processMetrics ( esAgg , target , seriesList , props ) ;
if ( depth === maxDepth ) {
if ( aggDef . type === 'date_histogram' ) {
this . processMetrics ( esAgg , target , seriesList , props ) ;
} else {
this . processAggregationDocs ( esAgg , aggDef , target , docs , props ) ;
}
} else {
for ( var nameIndex in esAgg . buckets ) {
bucket = esAgg . buckets [ nameIndex ] ;
@ -109,7 +160,7 @@ function (_, queryDef) {
} else {
props [ "filter" ] = nameIndex ;
}
this . processBuckets ( bucket , target , seriesList , props ) ;
this . processBuckets ( bucket , target , seriesList , docs , props , depth + 1 ) ;
}
}
}
@ -217,13 +268,18 @@ function (_, queryDef) {
var aggregations = response . aggregations ;
var target = this . targets [ i ] ;
var tmpSeriesList = [ ] ;
var docs = [ ] ;
this . processBuckets ( aggregations , target , tmpSeriesList , { } ) ;
this . processBuckets ( aggregations , target , tmpSeriesList , docs , { } , 0 ) ;
this . nameSeries ( tmpSeriesList , target ) ;
for ( var y = 0 ; y < tmpSeriesList . length ; y ++ ) {
seriesList . push ( tmpSeriesList [ y ] ) ;
}
if ( seriesList . length === 0 && docs . length > 0 ) {
seriesList . push ( { target : 'docs' , type : 'docs' , datapoints : docs } ) ;
}
}
}