11import { IssueDetector , IssueDetectorResult , WebRTCStatsParsed } from '../types' ;
22import { scheduleTask } from '../utils/tasks' ;
3- import { CLEANUP_PREV_STATS_TTL_MS } from '../utils/constants' ;
3+ import { CLEANUP_PREV_STATS_TTL_MS , MAX_PARSED_STATS_STORAGE_SIZE } from '../utils/constants' ;
44
55export interface PrevStatsCleanupPayload {
66 connectionId : string ;
@@ -9,16 +9,19 @@ export interface PrevStatsCleanupPayload {
99
1010export interface BaseIssueDetectorParams {
1111 statsCleanupTtlMs ?: number ;
12+ maxParsedStatsStorageSize ?: number ;
1213}
1314
1415abstract class BaseIssueDetector implements IssueDetector {
15- readonly #lastProcessedStats : Map < string , WebRTCStatsParsed | undefined > ;
16+ readonly #parsedStatsStorage : Map < string , WebRTCStatsParsed [ ] > = new Map ( ) ;
1617
1718 readonly #statsCleanupDelayMs: number ;
1819
20+ readonly #maxParsedStatsStorageSize: number ;
21+
1922 constructor ( params : BaseIssueDetectorParams = { } ) {
20- this . #lastProcessedStats = new Map ( ) ;
2123 this . #statsCleanupDelayMs = params . statsCleanupTtlMs ?? CLEANUP_PREV_STATS_TTL_MS ;
24+ this . #maxParsedStatsStorageSize = params . maxParsedStatsStorageSize ?? MAX_PARSED_STATS_STORAGE_SIZE ;
2225 }
2326
2427 abstract performDetection ( data : WebRTCStatsParsed ) : IssueDetectorResult ;
@@ -36,7 +39,7 @@ abstract class BaseIssueDetector implements IssueDetector {
3639 protected performPrevStatsCleanup ( payload : PrevStatsCleanupPayload ) : void {
3740 const { connectionId, cleanupCallback } = payload ;
3841
39- if ( ! this . #lastProcessedStats . has ( connectionId ) ) {
42+ if ( ! this . #parsedStatsStorage . has ( connectionId ) ) {
4043 return ;
4144 }
4245
@@ -54,15 +57,31 @@ abstract class BaseIssueDetector implements IssueDetector {
5457 }
5558
5659 protected setLastProcessedStats ( connectionId : string , parsedStats : WebRTCStatsParsed ) : void {
57- this . #lastProcessedStats. set ( connectionId , parsedStats ) ;
60+ if ( ! connectionId || parsedStats . connection . id !== connectionId ) {
61+ return ;
62+ }
63+
64+ const connectionStats = this . #parsedStatsStorage. get ( connectionId ) ?? [ ] ;
65+ connectionStats . push ( parsedStats ) ;
66+
67+ if ( connectionStats . length > this . #maxParsedStatsStorageSize) {
68+ connectionStats . shift ( ) ;
69+ }
70+
71+ this . #parsedStatsStorage. set ( connectionId , connectionStats ) ;
5872 }
5973
6074 protected getLastProcessedStats ( connectionId : string ) : WebRTCStatsParsed | undefined {
61- return this . #lastProcessedStats. get ( connectionId ) ;
75+ const connectionStats = this . #parsedStatsStorage. get ( connectionId ) ;
76+ return connectionStats ?. [ connectionStats . length - 1 ] ;
77+ }
78+
79+ protected getAllLastProcessedStats ( connectionId : string ) : WebRTCStatsParsed [ ] {
80+ return this . #parsedStatsStorage. get ( connectionId ) ?? [ ] ;
6281 }
6382
6483 private deleteLastProcessedStats ( connectionId : string ) : void {
65- this . #lastProcessedStats . delete ( connectionId ) ;
84+ this . #parsedStatsStorage . delete ( connectionId ) ;
6685 }
6786}
6887
0 commit comments