Skip to content

Commit

Permalink
feat: keep last N processed stats
Browse files Browse the repository at this point in the history
  • Loading branch information
desher committed Dec 23, 2024
1 parent ccd5837 commit be1df2e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
33 changes: 26 additions & 7 deletions src/detectors/BaseIssueDetector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IssueDetector, IssueDetectorResult, WebRTCStatsParsed } from '../types';
import { scheduleTask } from '../utils/tasks';
import { CLEANUP_PREV_STATS_TTL_MS } from '../utils/constants';
import { CLEANUP_PREV_STATS_TTL_MS, MAX_PARSED_STATS_STORAGE_SIZE } from '../utils/constants';

export interface PrevStatsCleanupPayload {
connectionId: string;
Expand All @@ -9,16 +9,19 @@ export interface PrevStatsCleanupPayload {

export interface BaseIssueDetectorParams {
statsCleanupTtlMs?: number;
maxParsedStatsStorageSize?: number;
}

abstract class BaseIssueDetector implements IssueDetector {
readonly #lastProcessedStats: Map<string, WebRTCStatsParsed | undefined>;
readonly #parsedStatsStorage: Map<string, WebRTCStatsParsed[]> = new Map();

readonly #statsCleanupDelayMs: number;

readonly #maxParsedStatsStorageSize: number;

constructor(params: BaseIssueDetectorParams = {}) {
this.#lastProcessedStats = new Map();
this.#statsCleanupDelayMs = params.statsCleanupTtlMs ?? CLEANUP_PREV_STATS_TTL_MS;
this.#maxParsedStatsStorageSize = params.maxParsedStatsStorageSize ?? MAX_PARSED_STATS_STORAGE_SIZE;
}

abstract performDetection(data: WebRTCStatsParsed): IssueDetectorResult;
Expand All @@ -36,7 +39,7 @@ abstract class BaseIssueDetector implements IssueDetector {
protected performPrevStatsCleanup(payload: PrevStatsCleanupPayload): void {
const { connectionId, cleanupCallback } = payload;

if (!this.#lastProcessedStats.has(connectionId)) {
if (!this.#parsedStatsStorage.has(connectionId)) {
return;
}

Expand All @@ -54,15 +57,31 @@ abstract class BaseIssueDetector implements IssueDetector {
}

protected setLastProcessedStats(connectionId: string, parsedStats: WebRTCStatsParsed): void {
this.#lastProcessedStats.set(connectionId, parsedStats);
if (!connectionId || parsedStats.connection.id !== connectionId) {
return;
}

const connectionStats = this.#parsedStatsStorage.get(connectionId) ?? [];
connectionStats.push(parsedStats);

if (connectionStats.length > this.#maxParsedStatsStorageSize) {
connectionStats.shift();
}

this.#parsedStatsStorage.set(connectionId, connectionStats);
}

protected getLastProcessedStats(connectionId: string): WebRTCStatsParsed | undefined {
return this.#lastProcessedStats.get(connectionId);
const connectionStats = this.#parsedStatsStorage.get(connectionId);
return connectionStats?.[connectionStats.length - 1];
}

protected getAllLastProcessedStats(connectionId: string): WebRTCStatsParsed[] {
return this.#parsedStatsStorage.get(connectionId) ?? [];
}

private deleteLastProcessedStats(connectionId: string): void {
this.#lastProcessedStats.delete(connectionId);
this.#parsedStatsStorage.delete(connectionId);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// eslint-disable-next-line import/prefer-default-export
export const CLEANUP_PREV_STATS_TTL_MS = 35_000;

export const MAX_PARSED_STATS_STORAGE_SIZE = 5;

0 comments on commit be1df2e

Please sign in to comment.