import moment from 'moment' ; import { pickBy } from 'lodash' ; import objectHash from 'object-hash' ; import { ApmFields } from '../apm_fields' ; import { createPicker } from './create_picker' ; export function aggregate ( events , fields ) { const picker = createPicker ( fields ) ; const metricsets = new Map < string , { key : ApmFields ; events : ApmFields [ ] ; } > ( ) ; function getMetricsetKey ( span ) { const timestamp = moment ( span [ '@timestamp' ] ) . valueOf ( ) ; return { '@timestamp' : timestamp - ( timestamp % ( 60 * 1000 ) ) , ... pickBy ( span , picker ) , } ; } for ( const event of events ) { const key = getMetricsetKey ( event ) ; const id = objectHash ( key ) ; let metricset = metricsets . get ( id ) ; if ( ! metricset ) { metricset = { key : { ... key , 'processor.event' : 'metric' , 'processor.name' : 'metric' } , events : [ ] , } ; metricsets . set ( id , metricset ) ; } metricset . events . push ( event ) ; } return Array . from ( metricsets . values ( ) ) ; }