@webex/internal-plugin-metrics 3.0.0-beta.30 → 3.0.0-beta.300

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