Skip to content

Commit

Permalink
fix: remove session id dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
KaustubhKumar05 committed Mar 19, 2024
1 parent 98babbc commit 0c7911e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
47 changes: 20 additions & 27 deletions packages/hms-video-store/src/common/PluginUsageTracker.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,55 @@
import AnalyticsEvent from '../analytics/AnalyticsEvent';

class PluginUsageTracker {
export class PluginUsageTracker {
private pluginUsage: Map<string, number> = new Map<string, number>();
private pluginLastAddedAt: Map<string, number> = new Map<string, number>();

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

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();
Original file line number Diff line number Diff line change
Expand Up @@ -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> | void;
init(): Promise<void> | void;

/**
* The name is meant to uniquely specify a plugin instance. This will be used to track number of plugins
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface HMSMediaStreamPlugin {
*/
getName(): string;

apply(stream: MediaStream, sessionID?: string): MediaStream;
apply(stream: MediaStream): MediaStream;

stop(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
init(): Promise<void>;

/**
* @see HMSVideoPluginType
Expand Down
3 changes: 1 addition & 2 deletions packages/hms-video-store/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0c7911e

Please sign in to comment.