Code coverage report for core/src/apm/local/histogram.ts

Statements: 90% (18 / 20)      Branches: 71.43% (5 / 7)      Functions: 83.33% (5 / 6)      Lines: 89.47% (17 / 19)      Ignored: none     

All files » core/src/apm/local/ » histogram.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 591         1     1   6 6         1 5         5 5 5         8                   6         6         6 6 6     6        
import {getTimeFactory, TimeFactory} from '../../time';
import {TimeMeasurement} from '../../time/primitives';
import {Tags} from '../../util';
import {Histogram} from '../histogram';
import {Resettable} from '../resettable';
import {ReadOnlyValue, Value} from './metric';
 
 
export class HistogramValue<T> extends ReadOnlyValue<T> {
 
  constructor(name: string, value: T, readonly time: TimeMeasurement, ...tags: Tags[]) {
    super(name, value, ...tags);
  }
}
 
 
export class LocalHistogram<T> implements TimeFactory, Histogram<T>, Resettable {
  protected _values: HistogramValue<T>[] = [];
  private readonly timeFactory: TimeFactory;
  readonly tags: Tags;
 
 
  constructor(readonly name: string, readonly limit = 100, timeFactory?: TimeFactory, tags: Tags = {}) {
    this.timeFactory = timeFactory || getTimeFactory();
    this.tags = { ...tags };
  }
 
 
  get values(): HistogramValue<T>[] {
    return [...this._values];
  }
 
 
  reset(): void {
    this._values = [];
  }
 
 
  now(): TimeMeasurement {
    return this.timeFactory.now();
  }
 
 
  set(value: T, tags: Tags = {}): Value<T> {
    const val = new HistogramValue(
      this.name,
      value,
      this.now(), this.tags, tags,
    );
    this._values.push(val);
    const start = this._values.length - this.limit;
    Iif (start > 0) {
      this._values = this._values.slice(start);
    }
    return val;
  }
 
}