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;
}
}
|