1717import * as defaultLogger from '../common/console-logger' ;
1818import * as loggerTypes from '../common/types' ;
1919
20+ import { BucketBoundaries } from './bucket-boundaries' ;
2021import { Recorder } from './recorder' ;
21- import { AggregationData , AggregationMetadata , AggregationType , Bucket , CountData , DistributionData , LastValueData , Measure , Measurement , MeasureType , SumData , Tags , View } from './types' ;
22+ import { AggregationData , AggregationMetadata , AggregationType , CountData , DistributionData , LastValueData , Measure , Measurement , MeasureType , SumData , Tags , View } from './types' ;
2223
2324const RECORD_SEPARATOR = String . fromCharCode ( 30 ) ;
2425const UNIT_SEPARATOR = String . fromCharCode ( 31 ) ;
@@ -55,7 +56,7 @@ export class BaseView implements View {
5556 /** The start time for this view */
5657 readonly startTime : number ;
5758 /** The bucket boundaries in a Distribution Aggregation */
58- private bucketBoundaries ?: number [ ] ;
59+ private bucketBoundaries : BucketBoundaries ;
5960 /**
6061 * The end time for this view - represents the last time a value was recorded
6162 */
@@ -91,7 +92,7 @@ export class BaseView implements View {
9192 this . columns = tagsKeys ;
9293 this . aggregation = aggregation ;
9394 this . startTime = Date . now ( ) ;
94- this . bucketBoundaries = bucketBoundaries ;
95+ this . bucketBoundaries = new BucketBoundaries ( bucketBoundaries ) ;
9596 }
9697
9798 /** Gets the view's tag keys */
@@ -156,7 +157,7 @@ export class BaseView implements View {
156157 */
157158 private createAggregationData ( tags : Tags ) : AggregationData {
158159 const aggregationMetadata = { tags, timestamp : Date . now ( ) } ;
159-
160+ const { buckets , bucketCounts } = this . bucketBoundaries ;
160161 switch ( this . aggregation ) {
161162 case AggregationType . DISTRIBUTION :
162163 return {
@@ -170,7 +171,8 @@ export class BaseView implements View {
170171 mean : null as number ,
171172 stdDeviation : null as number ,
172173 sumSquaredDeviations : null as number ,
173- buckets : this . createBuckets ( this . bucketBoundaries )
174+ buckets,
175+ bucketCounts
174176 } ;
175177 case AggregationType . SUM :
176178 return { ...aggregationMetadata , type : AggregationType . SUM , value : 0 } ;
@@ -185,64 +187,6 @@ export class BaseView implements View {
185187 }
186188 }
187189
188- /**
189- * Creates empty Buckets, given a list of bucket boundaries.
190- * @param bucketBoundaries a list with the bucket boundaries
191- */
192- private createBuckets ( bucketBoundaries : number [ ] ) : Bucket [ ] {
193- let negative = 0 ;
194- const result = bucketBoundaries . reduce ( ( accumulator , boundary , index ) => {
195- if ( boundary >= 0 ) {
196- const nextBoundary = bucketBoundaries [ index + 1 ] ;
197- this . validateBoundary ( boundary , nextBoundary ) ;
198- const len = bucketBoundaries . length - negative ;
199- const position = index - negative ;
200- const bucket = this . createBucket ( boundary , nextBoundary , position , len ) ;
201- accumulator . push ( bucket ) ;
202- } else {
203- negative ++ ;
204- }
205- return accumulator ;
206- } , [ ] ) ;
207- if ( negative ) {
208- this . logger . warn ( `Dropping ${
209- negative } negative bucket boundaries, the values must be strictly > 0.`) ;
210- }
211- return result ;
212- }
213-
214- /**
215- * Checks boundaries order and duplicates
216- * @param current Boundary
217- * @param next Next boundary
218- */
219- private validateBoundary ( current : number , next : number ) {
220- if ( next ) {
221- if ( current > next ) {
222- this . logger . error ( 'Bucket boundaries not sorted.' ) ;
223- }
224- if ( current === next ) {
225- this . logger . error ( 'Bucket boundaries not unique.' ) ;
226- }
227- }
228- }
229-
230- /**
231- * Creates empty bucket boundary.
232- * @param current Current boundary
233- * @param next Next boundary
234- * @param position Index of boundary
235- * @param max Maximum length of boundaries
236- */
237- private createBucket (
238- current : number , next : number , position : number , max : number ) : Bucket {
239- return {
240- count : 0 ,
241- lowBoundary : position ? current : - Infinity ,
242- highBoundary : ( position === max - 1 ) ? Infinity : next
243- } ;
244- }
245-
246190 /**
247191 * Returns a snapshot of an AggregationData for that tags/labels values.
248192 * @param tags The desired data's tags
0 commit comments