diff --git a/packages/hms-video-store/src/analytics/AnalyticsEventFactory.ts b/packages/hms-video-store/src/analytics/AnalyticsEventFactory.ts index 8a70bf8259..bd8373bd7f 100644 --- a/packages/hms-video-store/src/analytics/AnalyticsEventFactory.ts +++ b/packages/hms-video-store/src/analytics/AnalyticsEventFactory.ts @@ -236,8 +236,8 @@ export default class AnalyticsEventFactory { }); } - static getKrispUsage(sessionID: string) { - const duration = pluginUsageTracker.getPluginUsage('HMSKrispPlugin', sessionID); + static getKrispUsage() { + const duration = pluginUsageTracker.getPluginUsage('HMSKrispPlugin'); return new AnalyticsEvent({ name: 'krisp.usage', level: AnalyticsEventLevel.INFO, diff --git a/packages/hms-video-store/src/common/PluginUsageTracker.ts b/packages/hms-video-store/src/common/PluginUsageTracker.ts index 7d07825a0f..8d57f31020 100644 --- a/packages/hms-video-store/src/common/PluginUsageTracker.ts +++ b/packages/hms-video-store/src/common/PluginUsageTracker.ts @@ -1,47 +1,44 @@ import AnalyticsEvent from '../analytics/AnalyticsEvent'; -class PluginUsageTracker { +export class PluginUsageTracker { private pluginUsage: Map = new Map(); private pluginLastAddedAt: Map = new Map(); - getPluginUsage = (name: string, sessionID: string) => { - const pluginKey = `${sessionID}-${name}`; - - if (!this.pluginUsage.has(pluginKey)) { - this.pluginUsage.set(pluginKey, 0); + getPluginUsage = (name: string) => { + if (!this.pluginUsage.has(name)) { + this.pluginUsage.set(name, 0); } - if (this.pluginLastAddedAt.has(pluginKey)) { - const lastAddedAt = this.pluginLastAddedAt.get(pluginKey) || 0; + if (this.pluginLastAddedAt.has(name)) { + const lastAddedAt = this.pluginLastAddedAt.get(name) || 0; const extraDuration = lastAddedAt ? Date.now() - lastAddedAt : 0; - this.pluginUsage.set(pluginKey, (this.pluginUsage.get(pluginKey) || 0) + extraDuration); - this.pluginLastAddedAt.delete(pluginKey); + this.pluginUsage.set(name, (this.pluginUsage.get(name) || 0) + extraDuration); + this.pluginLastAddedAt.delete(name); } - return this.pluginUsage.get(pluginKey); + return this.pluginUsage.get(name); }; // eslint-disable-next-line complexity - updatePluginUsageData = (event: AnalyticsEvent, sessionID: string) => { + updatePluginUsageData = (event: AnalyticsEvent) => { const name = event.properties?.plugin_name || ''; - const pluginKey = `${sessionID}-${name}`; + switch (event.name) { // Sent on leave, after krisp usage is sent case 'transport.leave': { - this.cleanup(sessionID); + this.cleanup(); return; } case 'mediaPlugin.toggled.on': case 'mediaPlugin.added': { const addedAt = event.properties.added_at || Date.now(); - this.pluginLastAddedAt.set(pluginKey, addedAt); + this.pluginLastAddedAt.set(name, addedAt); break; } case 'mediaPlugin.toggled.off': case 'mediaPlugin.stats': { - if (this.pluginLastAddedAt.has(pluginKey)) { - const duration = - event.properties.duration || (Date.now() - (this.pluginLastAddedAt.get(pluginKey) || 0)) / 1000; - this.pluginUsage.set(pluginKey, (this.pluginUsage.get(pluginKey) || 0) + Math.max(duration, 0) * 1000); - this.pluginLastAddedAt.delete(pluginKey); + if (this.pluginLastAddedAt.has(name)) { + const duration = event.properties.duration || (Date.now() - (this.pluginLastAddedAt.get(name) || 0)) / 1000; + this.pluginUsage.set(name, (this.pluginUsage.get(name) || 0) + Math.max(duration, 0) * 1000); + this.pluginLastAddedAt.delete(name); } break; } @@ -49,14 +46,10 @@ class PluginUsageTracker { } }; - private cleanup = (sessionID: string) => { + private cleanup = () => { for (const key of this.pluginUsage.keys()) { - if (sessionID.length && key.includes(sessionID)) { - this.pluginUsage.delete(key); - this.pluginLastAddedAt.delete(key); - } + this.pluginUsage.delete(key); + this.pluginLastAddedAt.delete(key); } }; } - -export const pluginUsageTracker = new PluginUsageTracker(); diff --git a/packages/hms-video-store/src/plugins/audio/HMSAudioPlugin.ts b/packages/hms-video-store/src/plugins/audio/HMSAudioPlugin.ts index 66008701d1..dc95c38d2c 100644 --- a/packages/hms-video-store/src/plugins/audio/HMSAudioPlugin.ts +++ b/packages/hms-video-store/src/plugins/audio/HMSAudioPlugin.ts @@ -29,7 +29,7 @@ export interface HMSAudioPlugin { * variables, loading ML models etc. This can be used by a plugin to ensure it's prepared at the time * processAudio is called. */ - init(sessionID?: string): Promise | void; + init(): Promise | void; /** * The name is meant to uniquely specify a plugin instance. This will be used to track number of plugins diff --git a/packages/hms-video-store/src/plugins/video/HMSMediaStreamPlugin.ts b/packages/hms-video-store/src/plugins/video/HMSMediaStreamPlugin.ts index c17f4cf132..aed35847ae 100644 --- a/packages/hms-video-store/src/plugins/video/HMSMediaStreamPlugin.ts +++ b/packages/hms-video-store/src/plugins/video/HMSMediaStreamPlugin.ts @@ -5,7 +5,7 @@ export interface HMSMediaStreamPlugin { */ getName(): string; - apply(stream: MediaStream, sessionID?: string): MediaStream; + apply(stream: MediaStream): MediaStream; stop(): void; } diff --git a/packages/hms-video-store/src/plugins/video/HMSVideoPlugin.ts b/packages/hms-video-store/src/plugins/video/HMSVideoPlugin.ts index b431cf2b39..2df07b7a52 100644 --- a/packages/hms-video-store/src/plugins/video/HMSVideoPlugin.ts +++ b/packages/hms-video-store/src/plugins/video/HMSVideoPlugin.ts @@ -29,7 +29,7 @@ export interface HMSVideoPlugin { * variables, loading ML models etc. This can be used by a plugin to ensure it's prepared at the time * processVideoFrame is called. */ - init(sessionID?: string): Promise; + init(): Promise; /** * @see HMSVideoPluginType diff --git a/packages/hms-video-store/src/sdk/index.ts b/packages/hms-video-store/src/sdk/index.ts index 5d8de0e8ea..de3851486d 100644 --- a/packages/hms-video-store/src/sdk/index.ts +++ b/packages/hms-video-store/src/sdk/index.ts @@ -572,8 +572,7 @@ export class HMSSdk implements HMSInterface { throw error; } HMSLogger.timeEnd(`join-room-${roomId}`); - const sessionID = this.store.getRoom()?.sessionId || ''; - this.eventBus.analytics.subscribe(e => pluginUsageTracker.updatePluginUsageData(e, sessionID)); + this.eventBus.analytics.subscribe(pluginUsageTracker.updatePluginUsageData); } private stringifyMetadata(config: HMSConfig) {