'use strict' ; import { Config } from '@Base/config' ; import { Histogram } from '@Monitoring/Histogram' ; import { Logger } from '@Tools/Logging' ; export class EventHistogram extends Histogram { _timeBase : number ; _numBuckets : number ; _bucketMilliseconds : number ; _lastBucket : number ; _totalHistogramMilliseconds : number ; _histogram : number [ ] ; constructor ( pNumberOfBuckets , pMillisecondsPerBucket ) { super ( ) ; this . _numBuckets = pNumberOfBuckets ; this . _bucketMilliseconds = pMillisecondsPerBucket ; this . _totalHistogramMilliseconds = this . _numBuckets * this . _bucketMilliseconds ; this . Zero ( ) ; this . _lastBucket = 0 ; this . _timeBase = Date . now ( ) . valueOf ( ) ; } ; Event ( pNumberOfEvents = 1 ) { const bucketTime = Date . now ( ) . valueOf ( ) - this . _timeBase ; let bucket = Math . floor ( bucketTime / this . _bucketMilliseconds ) ; while ( bucket !== this . _lastBucket ) { if ( this . _lastBucket === this . _numBuckets ) { this . _lastBucket = 0 ; bucket -= this . _numBuckets ; this . _timeBase += this . _totalHistogramMilliseconds ; } else { this . _lastBucket ++ ; } this . _histogram [ this . _lastBucket ] = 0 ; } ; this . _histogram [ this . _lastBucket ] += pNumberOfEvents ; } ; GetHistogram ( ) { const values = [ ] ; values . length = this . _numBuckets ; let idx = this . _lastBucket ; for ( let ii = 0 ; ii < this . _numBuckets ; ii ++ ) { if ( ++ idx >= this . _numBuckets ) { idx = 0 ; } ; values [ ii ] = this . _histogram [ idx ] ; } ; return { "buckets" : this . _numBuckets , "bucketMilliseconds" : this . _bucketMilliseconds , "totalMilliseconds" : this . _totalHistogramMilliseconds , "timeBase" : this . _timeBase - ( this . _bucketMilliseconds * ( this . _numBuckets - this . _lastBucket ) ) , "baseNumber" : Math . floor ( ( this . _timeBase / this . _bucketMilliseconds ) ) + this . _lastBucket + 1 , "type" : "accumulation" , "values" : values } ; } ; Zero ( ) { this . _histogram = [ ] ; this . _histogram . length = this . _numBuckets ; this . _histogram . fill ( 0 ) ; } ; } ;