@webex/internal-plugin-metrics 3.0.0-beta.24 → 3.0.0-beta.241

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js +66 -0
  2. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js.map +1 -0
  3. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js +446 -0
  4. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js.map +1 -0
  5. package/dist/call-diagnostic/call-diagnostic-metrics.js +704 -0
  6. package/dist/call-diagnostic/call-diagnostic-metrics.js.map +1 -0
  7. package/dist/call-diagnostic/call-diagnostic-metrics.util.js +281 -0
  8. package/dist/call-diagnostic/call-diagnostic-metrics.util.js.map +1 -0
  9. package/dist/call-diagnostic/config.js +582 -0
  10. package/dist/call-diagnostic/config.js.map +1 -0
  11. package/dist/config.js +20 -1
  12. package/dist/config.js.map +1 -1
  13. package/dist/index.js +30 -1
  14. package/dist/index.js.map +1 -1
  15. package/dist/metrics.js +30 -30
  16. package/dist/metrics.js.map +1 -1
  17. package/dist/metrics.types.js +7 -0
  18. package/dist/metrics.types.js.map +1 -0
  19. package/dist/new-metrics.js +323 -0
  20. package/dist/new-metrics.js.map +1 -0
  21. package/dist/types/batcher.d.ts +2 -0
  22. package/dist/types/call-diagnostic/call-diagnostic-metrics-batcher.d.ts +2 -0
  23. package/dist/types/call-diagnostic/call-diagnostic-metrics-latencies.d.ts +189 -0
  24. package/dist/types/call-diagnostic/call-diagnostic-metrics.d.ts +385 -0
  25. package/dist/types/call-diagnostic/call-diagnostic-metrics.util.d.ts +73 -0
  26. package/dist/types/call-diagnostic/config.d.ts +87 -0
  27. package/dist/types/client-metrics-batcher.d.ts +2 -0
  28. package/dist/types/config.d.ts +35 -0
  29. package/dist/types/index.d.ts +13 -0
  30. package/dist/types/metrics.d.ts +3 -0
  31. package/dist/types/metrics.types.d.ts +99 -0
  32. package/dist/types/new-metrics.d.ts +133 -0
  33. package/dist/types/utils.d.ts +6 -0
  34. package/dist/utils.js +27 -0
  35. package/dist/utils.js.map +1 -0
  36. package/package.json +13 -8
  37. package/src/call-diagnostic/call-diagnostic-metrics-batcher.ts +80 -0
  38. package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +403 -0
  39. package/src/call-diagnostic/call-diagnostic-metrics.ts +762 -0
  40. package/src/call-diagnostic/call-diagnostic-metrics.util.ts +291 -0
  41. package/src/call-diagnostic/config.ts +580 -0
  42. package/src/config.js +19 -0
  43. package/src/index.ts +43 -0
  44. package/src/metrics.js +25 -27
  45. package/src/metrics.types.ts +152 -0
  46. package/src/new-metrics.ts +314 -0
  47. package/src/utils.ts +17 -0
  48. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +398 -0
  49. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-latencies.ts +465 -0
  50. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.ts +1303 -0
  51. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.util.ts +479 -0
  52. package/test/unit/spec/metrics.js +65 -97
  53. package/test/unit/spec/new-metrics.ts +269 -0
  54. package/test/unit/spec/utils.ts +22 -0
  55. package/tsconfig.json +6 -0
  56. package/dist/call-diagnostic-events-batcher.js +0 -60
  57. package/dist/call-diagnostic-events-batcher.js.map +0 -1
  58. package/src/call-diagnostic-events-batcher.js +0 -62
  59. package/src/index.js +0 -17
  60. package/test/unit/spec/call-diagnostic-events-batcher.js +0 -195
@@ -0,0 +1,762 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ /* eslint-disable class-methods-use-this */
3
+ /* eslint-disable valid-jsdoc */
4
+ import {getOSNameInternal} from '@webex/internal-plugin-metrics';
5
+ import {BrowserDetection} from '@webex/common';
6
+ import uuid from 'uuid';
7
+ import {merge} from 'lodash';
8
+ import {StatelessWebexPlugin} from '@webex/webex-core';
9
+
10
+ import {
11
+ anonymizeIPAddress,
12
+ clearEmptyKeysRecursively,
13
+ isLocusServiceErrorCode,
14
+ prepareDiagnosticMetricItem,
15
+ userAgentToString,
16
+ extractVersionMetadata,
17
+ isMeetingInfoServiceError,
18
+ isBrowserMediaErrorName,
19
+ } from './call-diagnostic-metrics.util';
20
+ import {CLIENT_NAME} from '../config';
21
+ import {
22
+ Event,
23
+ ClientType,
24
+ SubClientType,
25
+ NetworkType,
26
+ EnvironmentType,
27
+ NewEnvironmentType,
28
+ ClientEvent,
29
+ SubmitClientEventOptions,
30
+ MediaQualityEvent,
31
+ SubmitMQEOptions,
32
+ SubmitMQEPayload,
33
+ ClientLaunchMethodType,
34
+ ClientEventError,
35
+ ClientEventPayload,
36
+ ClientInfo,
37
+ ClientEventPayloadError,
38
+ } from '../metrics.types';
39
+ import CallDiagnosticEventsBatcher from './call-diagnostic-metrics-batcher';
40
+ import {
41
+ CLIENT_ERROR_CODE_TO_ERROR_PAYLOAD,
42
+ CALL_DIAGNOSTIC_EVENT_FAILED_TO_SEND,
43
+ NEW_LOCUS_ERROR_CLIENT_CODE,
44
+ SERVICE_ERROR_CODES_TO_CLIENT_ERROR_CODES_MAP,
45
+ UNKNOWN_ERROR,
46
+ BROWSER_MEDIA_ERROR_NAME_TO_CLIENT_ERROR_CODES_MAP,
47
+ MEETING_INFO_LOOKUP_ERROR_CLIENT_CODE,
48
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
49
+ } from './config';
50
+ import {generateCommonErrorMetadata} from '../utils';
51
+
52
+ const {getOSVersion, getBrowserName, getBrowserVersion} = BrowserDetection();
53
+
54
+ type GetOriginOptions = {
55
+ clientType: ClientType;
56
+ subClientType: SubClientType;
57
+ networkType?: NetworkType;
58
+ clientLaunchMethod?: ClientLaunchMethodType;
59
+ environment?: EnvironmentType;
60
+ newEnvironment?: NewEnvironmentType;
61
+ };
62
+
63
+ type GetIdentifiersOptions = {
64
+ meeting?: any;
65
+ mediaConnections?: any[];
66
+ correlationId?: string;
67
+ };
68
+
69
+ /**
70
+ * @description Util class to handle Call Analyzer Metrics
71
+ * @export
72
+ * @class CallDiagnosticMetrics
73
+ */
74
+ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
75
+ // @ts-ignore
76
+ private callDiagnosticEventsBatcher: CallDiagnosticEventsBatcher;
77
+ private logger: any; // to avoid adding @ts-ignore everywhere
78
+
79
+ /**
80
+ * Constructor
81
+ * @param args
82
+ */
83
+ constructor(...args) {
84
+ super(...args);
85
+ // @ts-ignore
86
+ this.logger = this.webex.logger;
87
+ // @ts-ignore
88
+ this.callDiagnosticEventsBatcher = new CallDiagnosticEventsBatcher({}, {parent: this.webex});
89
+ }
90
+
91
+ /**
92
+ * Returns the login type of the current user
93
+ * @returns one of 'login-ci','unverified-guest', null
94
+ */
95
+ getCurLoginType() {
96
+ // @ts-ignore
97
+ if (this.webex.canAuthorize) {
98
+ // @ts-ignore
99
+ return this.webex.credentials.isUnverifiedGuest ? 'unverified-guest' : 'login-ci';
100
+ }
101
+
102
+ return null;
103
+ }
104
+
105
+ /**
106
+ * Returns if the meeting has converged architecture enabled
107
+ * @param options.meetingId
108
+ */
109
+ getIsConvergedArchitectureEnabled({meetingId}: {meetingId?: string}): boolean {
110
+ if (meetingId) {
111
+ // @ts-ignore
112
+ const meeting = this.webex.meetings.meetingCollection.get(meetingId);
113
+
114
+ return meeting?.meetingInfo?.enableConvergedArchitecture;
115
+ }
116
+
117
+ return undefined;
118
+ }
119
+
120
+ /**
121
+ * Get origin object for Call Diagnostic Event payload.
122
+ * @param options
123
+ * @param meetingId
124
+ * @returns
125
+ */
126
+ getOrigin(options: GetOriginOptions, meetingId?: string) {
127
+ const defaultClientType: ClientType =
128
+ // @ts-ignore
129
+ this.webex.meetings.config?.metrics?.clientType;
130
+ const defaultSubClientType: SubClientType =
131
+ // @ts-ignore
132
+ this.webex.meetings.config?.metrics?.subClientType;
133
+ // @ts-ignore
134
+ const providedClientVersion: string = this.webex.meetings.config?.metrics?.clientVersion;
135
+ // @ts-ignore
136
+ const defaultSDKClientVersion = `${CLIENT_NAME}/${this.webex.version}`;
137
+
138
+ let versionMetadata: Pick<ClientInfo, 'majorVersion' | 'minorVersion'> = {};
139
+
140
+ // sdk version split doesn't really make sense for now...
141
+ if (providedClientVersion) {
142
+ versionMetadata = extractVersionMetadata(providedClientVersion);
143
+ }
144
+
145
+ if (
146
+ (defaultClientType && defaultSubClientType) ||
147
+ (options.clientType && options.subClientType)
148
+ ) {
149
+ const origin: Event['origin'] = {
150
+ name: 'endpoint',
151
+ networkType: options?.networkType || 'unknown',
152
+ userAgent: userAgentToString({
153
+ // @ts-ignore
154
+ clientName: this.webex.meetings?.metrics?.clientName,
155
+ // @ts-ignore
156
+ webexVersion: this.webex.version,
157
+ }),
158
+ clientInfo: {
159
+ clientType: options?.clientType || defaultClientType,
160
+ clientVersion: providedClientVersion || defaultSDKClientVersion,
161
+ ...versionMetadata,
162
+ localNetworkPrefix:
163
+ // @ts-ignore
164
+ anonymizeIPAddress(this.webex.meetings.geoHintInfo?.clientAddress) || undefined,
165
+ osVersion: getOSVersion() || 'unknown',
166
+ subClientType: options?.subClientType || defaultSubClientType,
167
+ os: getOSNameInternal(),
168
+ browser: getBrowserName(),
169
+ browserVersion: getBrowserVersion(),
170
+ },
171
+ };
172
+
173
+ if (meetingId) {
174
+ // @ts-ignore
175
+ const meeting = this.webex.meetings.meetingCollection.get(meetingId);
176
+ if (meeting?.environment) {
177
+ origin.environment = meeting.environment;
178
+ }
179
+ }
180
+
181
+ if (options?.environment) {
182
+ origin.environment = options.environment;
183
+ }
184
+
185
+ if (options?.newEnvironment) {
186
+ origin.newEnvironment = options.newEnvironment;
187
+ }
188
+
189
+ if (options?.clientLaunchMethod) {
190
+ origin.clientInfo.clientLaunchMethod = options.clientLaunchMethod;
191
+ }
192
+
193
+ return origin;
194
+ }
195
+
196
+ throw new Error("ClientType and SubClientType can't be undefined");
197
+ }
198
+
199
+ /**
200
+ * Gather identifier details for call diagnostic payload.
201
+ * @throws Error if initialization fails.
202
+ * @param options
203
+ */
204
+ getIdentifiers(options: GetIdentifiersOptions) {
205
+ const {meeting, mediaConnections, correlationId} = options;
206
+ const identifiers: Event['event']['identifiers'] = {correlationId: 'unknown'};
207
+
208
+ if (meeting) {
209
+ identifiers.correlationId = meeting.correlationId;
210
+ }
211
+
212
+ if (correlationId) {
213
+ identifiers.correlationId = correlationId;
214
+ }
215
+ // @ts-ignore
216
+ if (this.webex.internal) {
217
+ // @ts-ignore
218
+ const {device} = this.webex.internal;
219
+ identifiers.userId = device.userId;
220
+ identifiers.deviceId = device.url;
221
+ identifiers.orgId = device.orgId;
222
+ // @ts-ignore
223
+ identifiers.locusUrl = this.webex.internal.services.get('locus');
224
+ }
225
+
226
+ if (meeting?.locusInfo?.fullState) {
227
+ identifiers.locusUrl = meeting.locusUrl;
228
+ identifiers.locusId = meeting.locusUrl && meeting.locusUrl.split('/').pop();
229
+ identifiers.locusStartTime =
230
+ meeting.locusInfo.fullState && meeting.locusInfo.fullState.lastActive;
231
+ }
232
+
233
+ if (mediaConnections) {
234
+ identifiers.mediaAgentAlias = mediaConnections?.[0]?.mediaAgentAlias;
235
+ identifiers.mediaAgentGroupId = mediaConnections?.[0]?.mediaAgentGroupId;
236
+ }
237
+
238
+ if (identifiers.correlationId === undefined) {
239
+ throw new Error('Identifiers initialization failed.');
240
+ }
241
+
242
+ return identifiers;
243
+ }
244
+
245
+ /**
246
+ * Create diagnostic event, which can hold client event, feature event or MQE event data.
247
+ * This just initiates the shared properties that are required for all the 3 event categories.
248
+ * @param eventData
249
+ * @param options
250
+ * @returns
251
+ */
252
+ prepareDiagnosticEvent(eventData: Event['event'], options: any) {
253
+ const {meetingId} = options;
254
+ const origin = this.getOrigin(options, meetingId);
255
+
256
+ const event: Event = {
257
+ eventId: uuid.v4(),
258
+ version: 1,
259
+ origin,
260
+ originTime: {
261
+ triggered: new Date().toISOString(),
262
+ // is overridden in prepareRequest batcher
263
+ sent: 'not_defined_yet',
264
+ },
265
+ // @ts-ignore
266
+ senderCountryCode: this.webex.meetings.geoHintInfo?.countryCode,
267
+ event: eventData,
268
+ };
269
+
270
+ // sanitize (remove empty properties, CA requires it)
271
+ // but we don't want to sanitize MQE as most of the times
272
+ // values will be 0, [] etc, and they are required.
273
+ if (eventData.name !== 'client.mediaquality.event') {
274
+ clearEmptyKeysRecursively(event);
275
+ }
276
+
277
+ return event;
278
+ }
279
+
280
+ /**
281
+ * TODO: NOT IMPLEMENTED
282
+ * Submit Feature Event
283
+ * @returns
284
+ */
285
+ public submitFeatureEvent() {
286
+ throw Error('Not implemented');
287
+ }
288
+
289
+ /**
290
+ * Submit Media Quality Event
291
+ * @param args - submit params
292
+ * @param arg.name - event key
293
+ * @param arg.payload - additional payload to be merge with the default payload
294
+ * @param arg.options - options
295
+ */
296
+ submitMQE({
297
+ name,
298
+ payload,
299
+ options,
300
+ }: {
301
+ name: MediaQualityEvent['name'];
302
+ payload: SubmitMQEPayload;
303
+ options: SubmitMQEOptions;
304
+ }) {
305
+ const {meetingId, mediaConnections} = options;
306
+
307
+ // events that will most likely happen in join phase
308
+ if (meetingId) {
309
+ // @ts-ignore
310
+ const meeting = this.webex.meetings.meetingCollection.get(meetingId);
311
+
312
+ if (!meeting) {
313
+ console.warn(
314
+ 'Attempt to send MQE but no meeting was found...',
315
+ `event: ${name}, meetingId: ${meetingId}`
316
+ );
317
+ // @ts-ignore
318
+ this.webex.internal.metrics.submitClientMetrics(CALL_DIAGNOSTIC_EVENT_FAILED_TO_SEND, {
319
+ fields: {
320
+ meetingId,
321
+ name,
322
+ },
323
+ });
324
+
325
+ return;
326
+ }
327
+
328
+ // merge identifiers
329
+ const identifiers = this.getIdentifiers({
330
+ meeting,
331
+ mediaConnections: meeting.mediaConnections || mediaConnections,
332
+ });
333
+
334
+ // create media quality event object
335
+ let clientEventObject: MediaQualityEvent['payload'] = {
336
+ name,
337
+ canProceed: true,
338
+ identifiers,
339
+ eventData: {
340
+ webClientDomain: window.location.hostname,
341
+ },
342
+ intervals: payload.intervals,
343
+ sourceMetadata: {
344
+ applicationSoftwareType: CLIENT_NAME,
345
+ // @ts-ignore
346
+ applicationSoftwareVersion: this.webex.version,
347
+ mediaEngineSoftwareType: getBrowserName() || 'browser',
348
+ mediaEngineSoftwareVersion: getOSVersion() || 'unknown',
349
+ startTime: new Date().toISOString(),
350
+ },
351
+ };
352
+
353
+ // merge any new properties, or override existing ones
354
+ clientEventObject = merge(clientEventObject, payload);
355
+
356
+ // append media quality event data to the call diagnostic event
357
+ const diagnosticEvent = this.prepareDiagnosticEvent(clientEventObject, options);
358
+ this.submitToCallDiagnostics(diagnosticEvent);
359
+ } else {
360
+ throw new Error(
361
+ 'Media quality events cant be sent outside the context of a meeting. Meeting id is required.'
362
+ );
363
+ }
364
+ }
365
+
366
+ /**
367
+ * Return Client Event payload by client error code
368
+ * @param arg - get error arg
369
+ * @param arg.clientErrorCode
370
+ * @param arg.serviceErrorCode
371
+ * @returns
372
+ */
373
+ public getErrorPayloadForClientErrorCode({
374
+ clientErrorCode,
375
+ serviceErrorCode,
376
+ serviceErrorName,
377
+ }: {
378
+ clientErrorCode: number;
379
+ serviceErrorCode: any;
380
+ serviceErrorName?: any;
381
+ }): ClientEventError {
382
+ let error: ClientEventError;
383
+
384
+ if (clientErrorCode) {
385
+ const partialParsedError = CLIENT_ERROR_CODE_TO_ERROR_PAYLOAD[clientErrorCode];
386
+
387
+ if (partialParsedError) {
388
+ error = merge(
389
+ {fatal: true, shownToUser: false, name: 'other', category: 'other'}, // default values
390
+ {errorCode: clientErrorCode},
391
+ serviceErrorName ? {errorData: {errorName: serviceErrorName}} : {},
392
+ {serviceErrorCode},
393
+ partialParsedError
394
+ );
395
+
396
+ return error;
397
+ }
398
+ }
399
+
400
+ return undefined;
401
+ }
402
+
403
+ /**
404
+ * Generate error payload for Client Event
405
+ * @param rawError
406
+ */
407
+ generateClientEventErrorPayload(rawError: any) {
408
+ if (rawError.name) {
409
+ if (isBrowserMediaErrorName(rawError.name)) {
410
+ return this.getErrorPayloadForClientErrorCode({
411
+ serviceErrorCode: undefined,
412
+ clientErrorCode: BROWSER_MEDIA_ERROR_NAME_TO_CLIENT_ERROR_CODES_MAP[rawError.name],
413
+ serviceErrorName: rawError.name,
414
+ });
415
+ }
416
+ }
417
+
418
+ const serviceErrorCode =
419
+ rawError?.error?.body?.errorCode ||
420
+ rawError?.body?.errorCode ||
421
+ rawError?.body?.code ||
422
+ rawError?.body?.reason?.reasonCode;
423
+
424
+ if (serviceErrorCode) {
425
+ const clientErrorCode = SERVICE_ERROR_CODES_TO_CLIENT_ERROR_CODES_MAP[serviceErrorCode];
426
+ if (clientErrorCode) {
427
+ return this.getErrorPayloadForClientErrorCode({clientErrorCode, serviceErrorCode});
428
+ }
429
+
430
+ // by default, if it is locus error, return new locus err
431
+ if (isLocusServiceErrorCode(serviceErrorCode)) {
432
+ return this.getErrorPayloadForClientErrorCode({
433
+ clientErrorCode: NEW_LOCUS_ERROR_CLIENT_CODE,
434
+ serviceErrorCode,
435
+ });
436
+ }
437
+ }
438
+
439
+ if (isMeetingInfoServiceError(rawError)) {
440
+ return this.getErrorPayloadForClientErrorCode({
441
+ clientErrorCode: MEETING_INFO_LOOKUP_ERROR_CLIENT_CODE,
442
+ serviceErrorCode,
443
+ });
444
+ }
445
+
446
+ // otherwise return unkown error
447
+ return this.getErrorPayloadForClientErrorCode({
448
+ clientErrorCode: UNKNOWN_ERROR,
449
+ serviceErrorCode: UNKNOWN_ERROR,
450
+ });
451
+ }
452
+
453
+ /**
454
+ * Create client event object for in meeting events
455
+ * @param arg - create args
456
+ * @param arg.event - event key
457
+ * @param arg.options - options
458
+ * @returns object
459
+ */
460
+ private createClientEventObjectInMeeting({
461
+ name,
462
+ options,
463
+ errors,
464
+ }: {
465
+ name: ClientEvent['name'];
466
+ options?: SubmitClientEventOptions;
467
+ errors?: ClientEventPayloadError;
468
+ }) {
469
+ this.logger.log(
470
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
471
+ 'CallDiagnosticMetrics: @createClientEventObjectInMeeting. Creating in meeting event object.',
472
+ `name: ${name}`
473
+ );
474
+ const {meetingId, mediaConnections} = options;
475
+
476
+ // @ts-ignore
477
+ const meeting = this.webex.meetings.meetingCollection.get(meetingId);
478
+
479
+ if (!meeting) {
480
+ console.warn(
481
+ 'Attempt to send client event but no meeting was found...',
482
+ `name: ${name}, meetingId: ${meetingId}`
483
+ );
484
+ // @ts-ignore
485
+ this.webex.internal.metrics.submitClientMetrics(CALL_DIAGNOSTIC_EVENT_FAILED_TO_SEND, {
486
+ fields: {
487
+ meetingId,
488
+ name,
489
+ },
490
+ });
491
+
492
+ return undefined;
493
+ }
494
+
495
+ // grab identifiers
496
+ const identifiers = this.getIdentifiers({
497
+ meeting,
498
+ mediaConnections: meeting?.mediaConnections || mediaConnections,
499
+ });
500
+
501
+ // create client event object
502
+ const clientEventObject: ClientEvent['payload'] = {
503
+ name,
504
+ canProceed: true,
505
+ identifiers,
506
+ errors,
507
+ eventData: {
508
+ webClientDomain: window.location.hostname,
509
+ },
510
+ userType: meeting.getCurUserType(),
511
+ loginType: this.getCurLoginType(),
512
+ isConvergedArchitectureEnabled: this.getIsConvergedArchitectureEnabled({
513
+ meetingId,
514
+ }),
515
+ };
516
+
517
+ return clientEventObject;
518
+ }
519
+
520
+ /**
521
+ * Create client event object for pre meeting events
522
+ * @param arg - create args
523
+ * @param arg.event - event key
524
+ * @param arg.options - payload
525
+ * @returns object
526
+ */
527
+ private createClientEventObjectPreMeeting({
528
+ name,
529
+ options,
530
+ errors,
531
+ }: {
532
+ name: ClientEvent['name'];
533
+ options?: SubmitClientEventOptions;
534
+ errors?: ClientEventPayloadError;
535
+ }) {
536
+ this.logger.log(
537
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
538
+ 'CallDiagnosticMetrics: @createClientEventObjectPreMeeting. Creating pre meeting event object.',
539
+ `name: ${name}`
540
+ );
541
+ const {correlationId} = options;
542
+
543
+ // grab identifiers
544
+ const identifiers = this.getIdentifiers({
545
+ correlationId,
546
+ });
547
+
548
+ // create client event object
549
+ const clientEventObject: ClientEvent['payload'] = {
550
+ name,
551
+ errors,
552
+ canProceed: true,
553
+ identifiers,
554
+ eventData: {
555
+ webClientDomain: window.location.hostname,
556
+ },
557
+ loginType: this.getCurLoginType(),
558
+ };
559
+
560
+ return clientEventObject;
561
+ }
562
+
563
+ /**
564
+ * Prepare Client Event CA event.
565
+ * @param arg - submit params
566
+ * @param arg.event - event key
567
+ * @param arg.payload - additional payload to be merged with default payload
568
+ * @param arg.options - payload
569
+ * @returns {any} options to be with fetch
570
+ * @throws
571
+ */
572
+ private prepareClientEvent({
573
+ name,
574
+ payload,
575
+ options,
576
+ }: {
577
+ name: ClientEvent['name'];
578
+ payload?: ClientEventPayload;
579
+ options?: SubmitClientEventOptions;
580
+ }) {
581
+ const {meetingId, correlationId, rawError} = options;
582
+ let clientEventObject: ClientEvent['payload'];
583
+
584
+ // check if we need to generate errors
585
+ const errors: ClientEventPayloadError = [];
586
+
587
+ if (rawError) {
588
+ this.logger.log(
589
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
590
+ 'CallDiagnosticMetrics: @prepareClientEvent. Error detected, attempting to map and attach it to the event...',
591
+ `name: ${name}`,
592
+ `rawError: ${generateCommonErrorMetadata(rawError)}`
593
+ );
594
+
595
+ const generatedError = this.generateClientEventErrorPayload(rawError);
596
+ if (generatedError) {
597
+ errors.push(generatedError);
598
+ }
599
+ this.logger.log(
600
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
601
+ 'CallDiagnosticMetrics: @prepareClientEvent. Generated errors:',
602
+ `generatedError: ${JSON.stringify(generatedError)}`
603
+ );
604
+ }
605
+
606
+ // events that will most likely happen in join phase
607
+ if (meetingId) {
608
+ clientEventObject = this.createClientEventObjectInMeeting({name, options, errors});
609
+ } else if (correlationId) {
610
+ // any pre join events or events that are outside the meeting.
611
+ clientEventObject = this.createClientEventObjectPreMeeting({name, options, errors});
612
+ } else {
613
+ throw new Error('Not implemented');
614
+ }
615
+
616
+ // merge any new properties, or override existing ones
617
+ clientEventObject = merge(clientEventObject, payload);
618
+
619
+ // append client event data to the call diagnostic event
620
+ const diagnosticEvent = this.prepareDiagnosticEvent(clientEventObject, options);
621
+
622
+ return diagnosticEvent;
623
+ }
624
+
625
+ /**
626
+ * Submit Client Event CA event.
627
+ * @param arg - submit params
628
+ * @param arg.event - event key
629
+ * @param arg.payload - additional payload to be merged with default payload
630
+ * @param arg.options - payload
631
+ * @throws
632
+ */
633
+ public submitClientEvent({
634
+ name,
635
+ payload,
636
+ options,
637
+ }: {
638
+ name: ClientEvent['name'];
639
+ payload?: ClientEventPayload;
640
+ options?: SubmitClientEventOptions;
641
+ }) {
642
+ this.logger.log(
643
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
644
+ 'CallDiagnosticMetrics: @submitClientEvent. Submit Client Event CA event.',
645
+ `name: ${name}`,
646
+ `payload: ${JSON.stringify(payload)}`,
647
+ `options: ${JSON.stringify(options)}`
648
+ );
649
+ const diagnosticEvent = this.prepareClientEvent({name, payload, options});
650
+
651
+ if (options?.preLoginId) {
652
+ return this.submitToCallDiagnosticsPreLogin(diagnosticEvent, options?.preLoginId);
653
+ }
654
+
655
+ return this.submitToCallDiagnostics(diagnosticEvent);
656
+ }
657
+
658
+ /**
659
+ * Prepare the event and send the request to metrics-a service.
660
+ * @param event
661
+ * @returns promise
662
+ */
663
+ submitToCallDiagnostics(event: Event): Promise<any> {
664
+ // build metrics-a event type
665
+ const finalEvent = {
666
+ eventPayload: event,
667
+ type: ['diagnostic-event'],
668
+ };
669
+
670
+ this.logger.log(
671
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
672
+ 'CallDiagnosticMetrics: @submitToCallDiagnostics. Preparing to send the request',
673
+ `finalEvent: ${JSON.stringify(finalEvent)}`
674
+ );
675
+
676
+ return this.callDiagnosticEventsBatcher.request(finalEvent);
677
+ }
678
+
679
+ /**
680
+ * Pre login events are not batched. We make the request directly.
681
+ * @param event
682
+ * @param preLoginId
683
+ * @returns
684
+ */
685
+ public submitToCallDiagnosticsPreLogin = (event: Event, preLoginId?: string): Promise<any> => {
686
+ // build metrics-a event type
687
+ // @ts-ignore
688
+ const diagnosticEvent = prepareDiagnosticMetricItem(this.webex, {
689
+ eventPayload: event,
690
+ type: ['diagnostic-event'],
691
+ });
692
+
693
+ // append sent timestamp
694
+ diagnosticEvent.eventPayload.originTime.sent = new Date().toISOString();
695
+
696
+ this.logger.log(
697
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
698
+ `CallDiagnosticMetrics: @submitToCallDiagnosticsPreLogin. Sending the request:`,
699
+ `diagnosticEvent: ${JSON.stringify(diagnosticEvent)}`
700
+ );
701
+
702
+ // @ts-ignore
703
+ return this.webex.internal.newMetrics.postPreLoginMetric(diagnosticEvent, preLoginId);
704
+ };
705
+
706
+ /**
707
+ * Builds a request options object to later be passed to fetch().
708
+ * @param arg - submit params
709
+ * @param arg.event - event key
710
+ * @param arg.payload - additional payload to be merged with default payload
711
+ * @param arg.options - client event options
712
+ * @returns {Promise<any>}
713
+ * @throws
714
+ */
715
+ public async buildClientEventFetchRequestOptions({
716
+ name,
717
+ payload,
718
+ options,
719
+ }: {
720
+ name: ClientEvent['name'];
721
+ payload?: ClientEventPayload;
722
+ options?: SubmitClientEventOptions;
723
+ }): Promise<any> {
724
+ this.logger.log(
725
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
726
+ 'CallDiagnosticMetrics: @buildClientEventFetchRequestOptions. Building request options object for fetch()...',
727
+ `name: ${name}`,
728
+ `payload: ${JSON.stringify(payload)}`,
729
+ `options: ${JSON.stringify(options)}`
730
+ );
731
+
732
+ const clientEvent = this.prepareClientEvent({name, payload, options});
733
+
734
+ // build metrics-a event type
735
+ // @ts-ignore
736
+ const diagnosticEvent = prepareDiagnosticMetricItem(this.webex, {
737
+ eventPayload: clientEvent,
738
+ type: ['diagnostic-event'],
739
+ });
740
+
741
+ const request = {
742
+ method: 'POST',
743
+ service: 'metrics',
744
+ resource: 'clientmetrics',
745
+ body: {
746
+ metrics: [diagnosticEvent],
747
+ },
748
+ headers: {},
749
+ };
750
+
751
+ if (options.preLoginId) {
752
+ request.headers = {
753
+ authorization: false,
754
+ 'x-prelogin-userid': options.preLoginId,
755
+ };
756
+ request.resource = 'clientmetrics-prelogin';
757
+ }
758
+
759
+ // @ts-ignore
760
+ return this.webex.prepareFetchOptions(request);
761
+ }
762
+ }