import { isEmpty } from 'lodash' ; import { IAggConfigs } from '../aggs' ; import { tabifyGetColumns } from './get_columns' ; import { TabbedResponseWriterOptions , TabbedAggColumn , TabbedAggRow , TabbedTable } from './types' ; interface BufferColumn { id : string ; value : string | number ; } /**
 * Writer class that collects information about an aggregation response and
 * produces a table, or a series of tables.
 */ export class TabbedAggResponseWriter { columns : TabbedAggColumn [ ] ; rows : TabbedAggRow [ ] = [ ] ; bucketBuffer : BufferColumn [ ] = [ ] ; metricBuffer : BufferColumn [ ] = [ ] ; private readonly partialRows : boolean ; param { AggConfigs } aggs param { boolean } metricsAtAllLevels param { boolean } partialRows constructor ( aggs , { metricsAtAllLevels = false , partialRows = false } ) { this . partialRows = partialRows ; this . columns = tabifyGetColumns ( aggs . getResponseAggs ( ) , ! metricsAtAllLevels ) ; this . rows = [ ] ; } /**
     * Create a new row by reading the row buffer and bucketBuffer
     */ row ( ) { const rowBuffer = { } ; this . bucketBuffer . forEach ( ( bucket ) => { rowBuffer [ bucket . id ] = bucket . value ; } ) ; this . metricBuffer . forEach ( ( metric ) => { rowBuffer [ metric . id ] = metric . value ; } ) ; const isPartialRow = ! this . columns . every ( ( column ) => rowBuffer . hasOwnProperty ( column . id ) ) ; const removePartial = isPartialRow && ! this . partialRows ; if ( ! isEmpty ( rowBuffer ) && ! removePartial ) { this . rows . push ( rowBuffer ) ; } } response ( ) { return { columns : this . columns , rows : this . rows , } ; } }