Skip to content

Commit 7877b46

Browse files
committed
[STREAM-151] Use a proper communicationId
1 parent 2433470 commit 7877b46

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/stats-aggregator.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import StatsGatherer, { GetStatsEvent, StatsEvent } from "webrtc-stats-gatherer";
22
import { v4 as uuidv4 } from "uuid";
33

4-
import GenesysCloudWebrtSdk, { IExtendedMediaSession } from ".";
4+
import GenesysCloudWebrtSdk, { IConversationParticipantFromEvent, IExtendedMediaSession } from ".";
55
import { requestApi } from "./utils";
66

77
interface ISentStats {
@@ -28,6 +28,8 @@ export class StatsAggregator {
2828
}
2929

3030
private handleStatsUpdate (stats: StatsEvent) {
31+
32+
3133
// We want the time to be as close to the cretion of the stats as possible, even
3234
// though we may throw this away in some cases.
3335
const dateCreated = new Date();
@@ -102,8 +104,22 @@ export class StatsAggregator {
102104
}
103105

104106
private sendStats (rtpStats: ISentStats, dateCreated: Date) {
107+
// Not sure if this is where I want to do this yet, but I'm sketching
108+
const pcParticipant = this.session.pcParticipant;
109+
console.log('Hjon: This is the pcParticpant: ', pcParticipant);
110+
if (!pcParticipant) {
111+
console.warn('Hjon: no pcParticipant');
112+
return;
113+
}
114+
const calls = pcParticipant['calls'] ?? [];
115+
if (calls.length === 0) {
116+
console.warn('Hjon: I\'m wrong, no calls exist on this pcParticipant');
117+
return;
118+
}
119+
const participant = pcParticipant as unknown as IConversationParticipantFromEvent;
120+
const communicationId = participant.calls[0].id;
121+
105122
const conversationId = this.session.conversationId;
106-
const communicationId = "hjon-test-data";
107123

108124
const {
109125
originAppId,

test/unit/stats-aggregator.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import GenesysCloudWebrtSdk, { IExtendedMediaSession, utils } from "../../src";
22
import { StatsAggregator } from "../../src/stats-aggregator"
3-
import { requestApiWithRetry } from "../../src/utils";
43
import { MockSession, SimpleMockSdk } from "../test-utils";
54

65
describe('StatsAggregator', () => {
@@ -160,6 +159,10 @@ describe('StatsAggregator', () => {
160159
});
161160

162161
describe('sendStats', () => {
162+
afterEach(() => {
163+
jest.restoreAllMocks();
164+
});
165+
163166
it('should call requestApi', () => {
164167
const requestApiSpy = jest.spyOn(utils, 'requestApi').mockResolvedValue(null);
165168
const mockSession = new MockSession() as unknown as IExtendedMediaSession;
@@ -171,6 +174,10 @@ describe('StatsAggregator', () => {
171174
averageJitter: 0.1,
172175
estimatedAverageMos: 5
173176
}
177+
const participant = {
178+
calls: [{ id: "testCall" }]
179+
};
180+
mockSession.pcParticipant = participant as any;
174181

175182
statsAggregator['sendStats'](rtpStats, new Date());
176183

@@ -185,5 +192,49 @@ describe('StatsAggregator', () => {
185192
// This should actually check it
186193
expect(statsArgument['dateCreated']).toBeTruthy();
187194
});
195+
196+
it('should not call requestApi if there is no participant', () => {
197+
const requestApiSpy = jest.spyOn(utils, 'requestApi').mockResolvedValue(null);
198+
const mockSession = new MockSession() as unknown as IExtendedMediaSession;
199+
const sdk = new SimpleMockSdk() as unknown as GenesysCloudWebrtSdk;
200+
const statsAggregator = new StatsAggregator(mockSession, sdk);
201+
const rtpStats = {
202+
packetsReceived: 5,
203+
packetsSent: 5,
204+
averageJitter: 0.1,
205+
estimatedAverageMos: 5
206+
}
207+
208+
statsAggregator['sendStats'](rtpStats, new Date());
209+
210+
expect(requestApiSpy).not.toHaveBeenCalled();
211+
});
212+
213+
it('should not call requestApi if there are no calls for the participant', () => {
214+
const requestApiSpy = jest.spyOn(utils, 'requestApi').mockResolvedValue(null);
215+
const mockSession = new MockSession() as unknown as IExtendedMediaSession;
216+
const sdk = new SimpleMockSdk() as unknown as GenesysCloudWebrtSdk;
217+
const statsAggregator = new StatsAggregator(mockSession, sdk);
218+
const rtpStats = {
219+
packetsReceived: 5,
220+
packetsSent: 5,
221+
averageJitter: 0.1,
222+
estimatedAverageMos: 5
223+
}
224+
225+
const participantA = {
226+
calls: []
227+
};
228+
mockSession.pcParticipant = participantA as any;
229+
statsAggregator['sendStats'](rtpStats, new Date());
230+
231+
expect(requestApiSpy).not.toHaveBeenCalled();
232+
233+
const participantB = {};
234+
mockSession.pcParticipant = participantB as any;
235+
statsAggregator['sendStats'](rtpStats, new Date());
236+
237+
expect(requestApiSpy).not.toHaveBeenCalled();
238+
});
188239
});
189240
});

0 commit comments

Comments
 (0)