@webex/internal-plugin-metrics 3.0.0-beta.26 → 3.0.0-beta.260

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 +65 -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 +728 -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 +620 -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 +333 -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 +391 -0
  25. package/dist/types/call-diagnostic/call-diagnostic-metrics.util.d.ts +73 -0
  26. package/dist/types/call-diagnostic/config.d.ts +91 -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 +139 -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 +82 -0
  38. package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +403 -0
  39. package/src/call-diagnostic/call-diagnostic-metrics.ts +784 -0
  40. package/src/call-diagnostic/call-diagnostic-metrics.util.ts +291 -0
  41. package/src/call-diagnostic/config.ts +618 -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 +317 -0
  47. package/src/utils.ts +17 -0
  48. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +394 -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 +1550 -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,784 @@
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?.config?.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
+ if (options?.rawError?.message) {
518
+ // @ts-ignore
519
+ clientEventObject.eventData.rawErrorMessage = options?.rawError?.message;
520
+ }
521
+
522
+ return clientEventObject;
523
+ }
524
+
525
+ /**
526
+ * Create client event object for pre meeting events
527
+ * @param arg - create args
528
+ * @param arg.event - event key
529
+ * @param arg.options - payload
530
+ * @returns object
531
+ */
532
+ private createClientEventObjectPreMeeting({
533
+ name,
534
+ options,
535
+ errors,
536
+ }: {
537
+ name: ClientEvent['name'];
538
+ options?: SubmitClientEventOptions;
539
+ errors?: ClientEventPayloadError;
540
+ }) {
541
+ this.logger.log(
542
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
543
+ 'CallDiagnosticMetrics: @createClientEventObjectPreMeeting. Creating pre meeting event object.',
544
+ `name: ${name}`
545
+ );
546
+ const {correlationId} = options;
547
+
548
+ // grab identifiers
549
+ const identifiers = this.getIdentifiers({
550
+ correlationId,
551
+ });
552
+
553
+ // create client event object
554
+ const clientEventObject: ClientEvent['payload'] = {
555
+ name,
556
+ errors,
557
+ canProceed: true,
558
+ identifiers,
559
+ eventData: {
560
+ webClientDomain: window.location.hostname,
561
+ },
562
+ loginType: this.getCurLoginType(),
563
+ };
564
+
565
+ if (options?.rawError?.message) {
566
+ // @ts-ignore
567
+ clientEventObject.eventData.rawErrorMessage = options?.rawError?.message;
568
+ }
569
+
570
+ return clientEventObject;
571
+ }
572
+
573
+ /**
574
+ * Prepare Client Event CA event.
575
+ * @param arg - submit params
576
+ * @param arg.event - event key
577
+ * @param arg.payload - additional payload to be merged with default payload
578
+ * @param arg.options - payload
579
+ * @returns {any} options to be with fetch
580
+ * @throws
581
+ */
582
+ private prepareClientEvent({
583
+ name,
584
+ payload,
585
+ options,
586
+ }: {
587
+ name: ClientEvent['name'];
588
+ payload?: ClientEventPayload;
589
+ options?: SubmitClientEventOptions;
590
+ }) {
591
+ const {meetingId, correlationId, rawError} = options;
592
+ let clientEventObject: ClientEvent['payload'];
593
+
594
+ // check if we need to generate errors
595
+ const errors: ClientEventPayloadError = [];
596
+
597
+ if (rawError) {
598
+ this.logger.log(
599
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
600
+ 'CallDiagnosticMetrics: @prepareClientEvent. Error detected, attempting to map and attach it to the event...',
601
+ `name: ${name}`,
602
+ `rawError: ${generateCommonErrorMetadata(rawError)}`
603
+ );
604
+
605
+ const generatedError = this.generateClientEventErrorPayload(rawError);
606
+ if (generatedError) {
607
+ errors.push(generatedError);
608
+ }
609
+ this.logger.log(
610
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
611
+ 'CallDiagnosticMetrics: @prepareClientEvent. Generated errors:',
612
+ `generatedError: ${JSON.stringify(generatedError)}`
613
+ );
614
+ }
615
+
616
+ // events that will most likely happen in join phase
617
+ if (meetingId) {
618
+ clientEventObject = this.createClientEventObjectInMeeting({name, options, errors});
619
+ } else if (correlationId) {
620
+ // any pre join events or events that are outside the meeting.
621
+ clientEventObject = this.createClientEventObjectPreMeeting({name, options, errors});
622
+ } else {
623
+ throw new Error('Not implemented');
624
+ }
625
+
626
+ // merge any new properties, or override existing ones
627
+ clientEventObject = merge(clientEventObject, payload);
628
+
629
+ // append client event data to the call diagnostic event
630
+ const diagnosticEvent = this.prepareDiagnosticEvent(clientEventObject, options);
631
+
632
+ return diagnosticEvent;
633
+ }
634
+
635
+ /**
636
+ * Submit Client Event CA event.
637
+ * @param arg - submit params
638
+ * @param arg.event - event key
639
+ * @param arg.payload - additional payload to be merged with default payload
640
+ * @param arg.options - payload
641
+ * @throws
642
+ */
643
+ public submitClientEvent({
644
+ name,
645
+ payload,
646
+ options,
647
+ }: {
648
+ name: ClientEvent['name'];
649
+ payload?: ClientEventPayload;
650
+ options?: SubmitClientEventOptions;
651
+ }) {
652
+ this.logger.log(
653
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
654
+ 'CallDiagnosticMetrics: @submitClientEvent. Submit Client Event CA event.',
655
+ `name: ${name}`,
656
+ `payload: ${JSON.stringify(payload)}`,
657
+ `options: ${JSON.stringify(options)}`
658
+ );
659
+ const diagnosticEvent = this.prepareClientEvent({name, payload, options});
660
+
661
+ if (options?.preLoginId) {
662
+ return this.submitToCallDiagnosticsPreLogin(diagnosticEvent, options?.preLoginId);
663
+ }
664
+
665
+ return this.submitToCallDiagnostics(diagnosticEvent);
666
+ }
667
+
668
+ /**
669
+ * Prepare the event and send the request to metrics-a service.
670
+ * @param event
671
+ * @returns promise
672
+ */
673
+ submitToCallDiagnostics(event: Event): Promise<any> {
674
+ // build metrics-a event type
675
+ const finalEvent = {
676
+ eventPayload: event,
677
+ type: ['diagnostic-event'],
678
+ };
679
+
680
+ this.logger.log(
681
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
682
+ 'CallDiagnosticMetrics: @submitToCallDiagnostics. Preparing to send the request',
683
+ `finalEvent: ${JSON.stringify(finalEvent)}`
684
+ );
685
+
686
+ return this.callDiagnosticEventsBatcher.request(finalEvent);
687
+ }
688
+
689
+ /**
690
+ * Pre login events are not batched. We make the request directly.
691
+ * @param event
692
+ * @param preLoginId
693
+ * @returns
694
+ */
695
+ public submitToCallDiagnosticsPreLogin = (event: Event, preLoginId?: string): Promise<any> => {
696
+ // build metrics-a event type
697
+ // @ts-ignore
698
+ const diagnosticEvent = prepareDiagnosticMetricItem(this.webex, {
699
+ eventPayload: event,
700
+ type: ['diagnostic-event'],
701
+ });
702
+
703
+ // append sent timestamp
704
+ diagnosticEvent.eventPayload.originTime.sent = new Date().toISOString();
705
+
706
+ this.logger.log(
707
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
708
+ `CallDiagnosticMetrics: @submitToCallDiagnosticsPreLogin. Sending the request:`,
709
+ `diagnosticEvent: ${JSON.stringify(diagnosticEvent)}`
710
+ );
711
+
712
+ // @ts-ignore
713
+ return this.webex.internal.newMetrics.postPreLoginMetric(diagnosticEvent, preLoginId);
714
+ };
715
+
716
+ /**
717
+ * Builds a request options object to later be passed to fetch().
718
+ * @param arg - submit params
719
+ * @param arg.event - event key
720
+ * @param arg.payload - additional payload to be merged with default payload
721
+ * @param arg.options - client event options
722
+ * @returns {Promise<any>}
723
+ * @throws
724
+ */
725
+ public async buildClientEventFetchRequestOptions({
726
+ name,
727
+ payload,
728
+ options,
729
+ }: {
730
+ name: ClientEvent['name'];
731
+ payload?: ClientEventPayload;
732
+ options?: SubmitClientEventOptions;
733
+ }): Promise<any> {
734
+ this.logger.log(
735
+ CALL_DIAGNOSTIC_LOG_IDENTIFIER,
736
+ 'CallDiagnosticMetrics: @buildClientEventFetchRequestOptions. Building request options object for fetch()...',
737
+ `name: ${name}`,
738
+ `payload: ${JSON.stringify(payload)}`,
739
+ `options: ${JSON.stringify(options)}`
740
+ );
741
+
742
+ const clientEvent = this.prepareClientEvent({name, payload, options});
743
+
744
+ // build metrics-a event type
745
+ // @ts-ignore
746
+ const diagnosticEvent = prepareDiagnosticMetricItem(this.webex, {
747
+ eventPayload: clientEvent,
748
+ type: ['diagnostic-event'],
749
+ });
750
+
751
+ const request = {
752
+ method: 'POST',
753
+ service: 'metrics',
754
+ resource: 'clientmetrics',
755
+ body: {
756
+ metrics: [diagnosticEvent],
757
+ },
758
+ headers: {},
759
+ };
760
+
761
+ if (options.preLoginId) {
762
+ request.headers = {
763
+ authorization: false,
764
+ 'x-prelogin-userid': options.preLoginId,
765
+ };
766
+ request.resource = 'clientmetrics-prelogin';
767
+ }
768
+
769
+ // @ts-ignore
770
+ return this.webex.prepareFetchOptions(request);
771
+ }
772
+
773
+ /**
774
+ * Returns true if the specified serviceErrorCode maps to an expected error.
775
+ * @param {number} serviceErrorCode the service error code
776
+ * @returns {boolean}
777
+ */
778
+ public isServiceErrorExpected(serviceErrorCode: number): boolean {
779
+ const clientErrorCode = SERVICE_ERROR_CODES_TO_CLIENT_ERROR_CODES_MAP[serviceErrorCode];
780
+ const clientErrorPayload = CLIENT_ERROR_CODE_TO_ERROR_PAYLOAD[clientErrorCode];
781
+
782
+ return clientErrorPayload?.category === 'expected';
783
+ }
784
+ }