sinch-rtc
Version:
RTC JavaScript/Web SDK
73 lines • 2.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.StatsBuffer = void 0;
const Errors_1 = require("../../utils/Errors");
/**
* Collection simplifying handling buffer of numeric values with maximum capacity.
* StatsBuffer keeps at most maxSize values. When a new value is prepended the oldest one is removed
* if size of the buffer exceeds the size limit.
*/
class StatsBuffer {
constructor(maxSize, samplesCountForWarning = 0, threshold = 0) {
this.maxSize = maxSize;
this.samplesCountForWarning = samplesCountForWarning;
this.threshold = threshold;
this.buffer = [];
this.bufferSum = 0;
if (samplesCountForWarning > maxSize) {
throw new Errors_1.ArgumentError("samplesCountForWarning cannot be greater than maxSize", "samplesCountForWarning");
}
}
/**
* True if buffer reached maximum capacity, false otherwise.
*/
get isFull() {
return this.buffer.length === this.maxSize;
}
/**
* True if all the values in the buffer are equal to 0.0, false otherwise.
*/
get allZeros() {
return (this.buffer.filter((value) => value === 0).length === this.maxSize);
}
/**
* True if `samplesCountForWarning` values kept in buffer are above threshold,false otherwise.
*/
get isAboveThreshold() {
if (this.buffer.length < this.maxSize) {
return false;
}
return (this.buffer.filter((value) => value > this.threshold).length >=
this.samplesCountForWarning);
}
/**
* Average value of the numbers in the buffer.
*/
get average() {
return this.bufferSum / this.buffer.length;
}
/**
* Standard deviation of the numbers in the buffer.
*/
get standardDeviation() {
const mean = this.average;
const sum = this.buffer.reduce((sum, value) => sum + Math.pow(value - mean, 2), 0);
return Math.sqrt(sum / this.buffer.length);
}
/**
* Adds a new value to the buffer.
* @param value - value to add.
*/
add(value) {
this.bufferSum += value;
this.buffer.unshift(value);
if (this.buffer.length > this.maxSize) {
const removedValue = this.buffer.pop();
if (removedValue !== undefined) {
this.bufferSum -= removedValue;
}
}
}
}
exports.StatsBuffer = StatsBuffer;
//# sourceMappingURL=StatsBuffer.js.map