import { get } from 'lodash' ; import { overwrite , bucketTransform } from '../../helpers' ; import { calculateAggRoot } from './calculate_agg_root' ; import type { TableRequestProcessorsFunction } from './types' ; export const metricBuckets = ( { req , panel } ) => ( next ) => async ( doc ) => { panel . series . forEach ( ( column ) => { const aggRoot = calculateAggRoot ( doc , column ) ; column . metrics . filter ( ( row ) => ! /_bucket$/ . test ( row . type ) && ! /^series/ . test ( row . type ) ) . forEach ( ( metric ) => { const fn = bucketTransform [ metric . type ] ; if ( fn ) { try { const intervalString = get ( doc , aggRoot . replace ( /\.aggs$/ , '.meta.intervalString' ) ) ; const bucket = fn ( metric , column . metrics , intervalString ) ; overwrite ( doc , `${ aggRoot }.timeseries.aggs.${ metric . id }` , bucket ) ; } catch ( e ) { } } } ) ; } ) ; return next ( doc ) ; } ;