import { get , forEach , isEmpty } from 'lodash' ; import { overwrite } from '../../helpers' ; import type { TableRequestProcessorsFunction } from './types' ; const isEmptyFilter = ( filter ) => filter && Boolean ( filter . match_all ) && isEmpty ( filter . match_all ) ; const hasSiblingPipelineAggregation = ( aggs = { } ) => Object . keys ( aggs ) . length > 1 ; export const normalizeQuery = ( ) => { return ( ) => ( doc ) => { const series = get ( doc , 'aggs.pivot.aggs' ) as Array < { aggs : Record < string , unknown > ; } > ; const normalizedSeries = { } ; forEach ( series , ( value , seriesId ) => { const filter = get ( value , `filter` ) ; if ( isEmptyFilter ( filter ) && ! hasSiblingPipelineAggregation ( value . aggs ) ) { const agg = get ( value , 'aggs.timeseries' ) ; const meta = { ... get ( value , 'meta' ) , seriesId , } ; overwrite ( normalizedSeries , `${ seriesId }` , agg ) ; overwrite ( normalizedSeries , `${ seriesId }.meta` , { ... meta , normalized : true , } ) ; } else { overwrite ( normalizedSeries , `${ seriesId }` , value ) ; } } ) ; overwrite ( doc , 'aggs.pivot.aggs' , normalizedSeries ) ; return doc ; } ; } ;