import { APIMetric , Metric , MetricKey } from "./model" ; /**
 * Batcher joins metrics with matching properties.
 */ export class Batcher { private metrics = new Map < string , Metric > ( ) ; param metric public add ( metric ) { const key = this . getBatchKey ( metric ) ; const result = this . metrics . get ( key ) ; if ( result !== undefined ) { metric = result . union ( metric ) ; } this . metrics . set ( key , metric ) ; } /**
     * Convert batched metrics to a list of api compatible metrics
     */ public toAPIMetrics ( ) { return [ ... this . metrics . values ( ) ] . map ( ( metric ) => metric . toAPIMetrics ( ) ) . reduce ( ( prev , curr ) => prev . concat ( curr ) , [ ] ) ; } private getBatchKey ( metric ) { return JSON . stringify ( { host : metric . host , metricType : metric . metricType , name : metric . name , tags : [ ... metric . tags ] . sort ( ) , } ) ; } }