@webex/internal-plugin-metrics 3.5.0 → 3.6.0

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 (55) hide show
  1. package/dist/business-metrics.js +119 -9
  2. package/dist/business-metrics.js.map +1 -1
  3. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js +1 -1
  4. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js.map +1 -1
  5. package/dist/call-diagnostic/call-diagnostic-metrics.js +18 -7
  6. package/dist/call-diagnostic/call-diagnostic-metrics.js.map +1 -1
  7. package/dist/call-diagnostic/call-diagnostic-metrics.util.js +7 -2
  8. package/dist/call-diagnostic/call-diagnostic-metrics.util.js.map +1 -1
  9. package/dist/call-diagnostic/config.js +13 -3
  10. package/dist/call-diagnostic/config.js.map +1 -1
  11. package/dist/call-diagnostic-events-batcher.js +59 -0
  12. package/dist/call-diagnostic-events-batcher.js.map +1 -0
  13. package/dist/index.js +7 -0
  14. package/dist/index.js.map +1 -1
  15. package/dist/metrics.js +1 -1
  16. package/dist/metrics.types.js.map +1 -1
  17. package/dist/new-metrics.js +5 -2
  18. package/dist/new-metrics.js.map +1 -1
  19. package/dist/rtcMetrics/constants.js +11 -0
  20. package/dist/rtcMetrics/constants.js.map +1 -0
  21. package/dist/rtcMetrics/index.js +223 -0
  22. package/dist/rtcMetrics/index.js.map +1 -0
  23. package/dist/types/business-metrics.d.ts +36 -4
  24. package/dist/types/call-diagnostic/call-diagnostic-metrics.d.ts +6 -1
  25. package/dist/types/call-diagnostic/config.d.ts +3 -0
  26. package/dist/types/index.d.ts +2 -1
  27. package/dist/types/metrics.types.d.ts +17 -2
  28. package/dist/types/new-metrics.d.ts +5 -3
  29. package/dist/types/rtcMetrics/constants.d.ts +4 -0
  30. package/dist/types/rtcMetrics/index.d.ts +80 -0
  31. package/package.json +11 -11
  32. package/src/business-metrics.ts +97 -5
  33. package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +1 -1
  34. package/src/call-diagnostic/call-diagnostic-metrics.ts +17 -7
  35. package/src/call-diagnostic/call-diagnostic-metrics.util.ts +11 -5
  36. package/src/call-diagnostic/config.ts +12 -0
  37. package/src/index.ts +2 -0
  38. package/src/metrics.types.ts +95 -23
  39. package/src/new-metrics.ts +12 -2
  40. package/src/rtcMetrics/constants.ts +3 -0
  41. package/src/rtcMetrics/index.ts +205 -0
  42. package/test/unit/spec/business/business-metrics.ts +69 -2
  43. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +2 -1
  44. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-latencies.ts +4 -6
  45. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.ts +406 -11
  46. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.util.ts +7 -3
  47. package/test/unit/spec/new-metrics.ts +18 -3
  48. package/test/unit/spec/prelogin-metrics-batcher.ts +3 -1
  49. package/test/unit/spec/rtcMetrics/index.ts +196 -0
  50. package/dist/behavioral/behavioral-metrics.js +0 -199
  51. package/dist/behavioral/behavioral-metrics.js.map +0 -1
  52. package/dist/behavioral/config.js +0 -11
  53. package/dist/behavioral/config.js.map +0 -1
  54. package/dist/types/behavioral/behavioral-metrics.d.ts +0 -63
  55. package/dist/types/behavioral/config.d.ts +0 -1
@@ -1,5 +1,5 @@
1
1
  import GenericMetrics from './generic-metrics';
2
- import {EventPayload} from './metrics.types';
2
+ import {EventPayload, Table} from './metrics.types';
3
3
 
4
4
  /**
5
5
  * @description Util class to handle Buisness Metrics
@@ -8,23 +8,115 @@ import {EventPayload} from './metrics.types';
8
8
  */
9
9
  export default class BusinessMetrics extends GenericMetrics {
10
10
  /**
11
- * Submit a buisness metric to our metrics endpoint.
11
+ * unfortunately, the pinot team does not allow changes to the schema of wbxapp_callend_metrics
12
+ * so we have to shim this layer specifically for this
13
+ * https://confluence-eng-gpk2.cisco.com/conf/display/WAP/Table+wbxapp_callend_metrics
14
+ * @param {EventPayload} payload payload of the metric
15
+ * @returns {Promise<any>}
16
+ */
17
+ private submitCallEndEvent({payload}: {payload: EventPayload}) {
18
+ const event = {
19
+ type: ['business'],
20
+ eventPayload: {
21
+ key: 'callEnd',
22
+ client_timestamp: new Date().toISOString(),
23
+ appType: 'Web Client',
24
+ value: {
25
+ ...payload,
26
+ },
27
+ },
28
+ };
29
+
30
+ return this.submitEvent({
31
+ kind: 'buisness-events:wbxapp_callend_metrics -> ',
32
+ name: 'wbxapp_callend_metrics',
33
+ event,
34
+ });
35
+ }
36
+
37
+ /**
38
+ * Submit a buisness metric to our metrics endpoint, going to the default business_ucf table
39
+ * all event payload keys are converted into a hex string value
40
+ * unfortunately, the pinot team does not allow changes to the schema of business_metrics
41
+ * so we have to shim this layer specifically for this
42
+ * https://confluence-eng-gpk2.cisco.com/conf/display/WAP/Table%3A+business_metrics
43
+ * @param {string} name of the metric
44
+ * @param {EventPayload} payload payload of the metric
45
+ * @returns {Promise<any>}
46
+ */
47
+ private submitBusinessMetricsEvent({name, payload}: {name: string; payload: EventPayload}) {
48
+ const event = {
49
+ type: ['business'],
50
+ eventPayload: {
51
+ key: name,
52
+ client_timestamp: new Date().toISOString(),
53
+ appType: 'Web Client',
54
+ value: {
55
+ ...this.getContext(),
56
+ ...this.getBrowserDetails(),
57
+ ...payload,
58
+ },
59
+ },
60
+ };
61
+
62
+ return this.submitEvent({kind: 'buisness-events:business_metrics -> ', name, event});
63
+ }
64
+
65
+ /**
66
+ * Submit a buisness metric to our metrics endpoint, going to the default business_ucf table
67
+ * all event payload keys are converted into a hex string value
68
+ * https://confluence-eng-gpk2.cisco.com/conf/display/WAP/Business+metrics++-%3E+ROMA
12
69
  * @param {string} name of the metric
13
70
  * @param {EventPayload} user payload of the metric
14
71
  * @returns {Promise<any>}
15
72
  */
16
- public submitBusinessEvent({name, payload}: {name: string; payload: EventPayload}) {
73
+ private submitDefaultEvent({name, payload}: {name: string; payload: EventPayload}) {
17
74
  const event = {
18
75
  type: ['business'],
19
76
  eventPayload: {
20
77
  key: name,
21
- client_timestamp: Date.now(),
78
+ appType: 'Web Client',
79
+ client_timestamp: new Date().toISOString(),
22
80
  context: this.getContext(),
23
81
  browserDetails: this.getBrowserDetails(),
24
82
  value: payload,
25
83
  },
26
84
  };
27
85
 
28
- this.submitEvent({kind: 'buisness-events -> ', name, event});
86
+ return this.submitEvent({kind: 'buisness-events:default -> ', name, event});
87
+ }
88
+
89
+ /**
90
+ * Submit a buisness metric to our metrics endpoint.
91
+ * routes to the correct table with the correct schema payload by table
92
+ * https://confluence-eng-gpk2.cisco.com/conf/display/WAP/Business+metrics++-%3E+ROMA
93
+ * @param {string} name of the metric, ignored if going to wbxapp_callend_metrics
94
+ * @param {EventPayload} payload user payload of the metric
95
+ * @param {Table} table optional - to submit the metric to and adapt the sent schema
96
+ * @returns {Promise<any>}
97
+ */
98
+ public submitBusinessEvent({
99
+ name,
100
+ payload,
101
+ table,
102
+ }: {
103
+ name: string;
104
+ payload: EventPayload;
105
+ table?: Table;
106
+ }): Promise<void> {
107
+ if (!table) {
108
+ table = 'default';
109
+ }
110
+ switch (table) {
111
+ case 'wbxapp_callend_metrics':
112
+ return this.submitCallEndEvent({payload});
113
+ case 'business_metrics':
114
+ return this.submitBusinessMetricsEvent({name, payload});
115
+ case 'business_ucf':
116
+ return this.submitDefaultEvent({name, payload});
117
+ case 'default':
118
+ default:
119
+ return this.submitDefaultEvent({name, payload});
120
+ }
29
121
  }
30
122
  }
@@ -49,7 +49,7 @@ export default class CallDiagnosticLatencies extends WebexPlugin {
49
49
  private getMeeting() {
50
50
  if (this.meetingId) {
51
51
  // @ts-ignore
52
- return this.webex.meetings.meetingCollection.get(this.meetingId);
52
+ return this.webex.meetings.getBasicMeetingInformation(this.meetingId);
53
53
  }
54
54
 
55
55
  return undefined;
@@ -139,7 +139,7 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
139
139
  getIsConvergedArchitectureEnabled({meetingId}: {meetingId?: string}): boolean {
140
140
  if (meetingId) {
141
141
  // @ts-ignore
142
- const meeting = this.webex.meetings.meetingCollection.get(meetingId);
142
+ const meeting = this.webex.meetings.getBasicMeetingInformation(meetingId);
143
143
 
144
144
  return meeting?.meetingInfo?.enableConvergedArchitecture;
145
145
  }
@@ -245,7 +245,7 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
245
245
 
246
246
  if (meetingId) {
247
247
  // @ts-ignore
248
- const meeting = this.webex.meetings.meetingCollection.get(meetingId);
248
+ const meeting = this.webex.meetings.getBasicMeetingInformation(meetingId);
249
249
  if (meeting?.environment) {
250
250
  origin.environment = meeting.environment;
251
251
  }
@@ -289,11 +289,18 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
289
289
  sessionCorrelationId,
290
290
  } = options;
291
291
  const identifiers: Event['event']['identifiers'] = {
292
- correlationId: 'unknown',
292
+ correlationId: 'unknown', // concerned with setting this to unknown. This will fail diagnostic events parsing because it's not a uuid pattern
293
293
  };
294
294
 
295
295
  if (meeting) {
296
296
  identifiers.correlationId = meeting.correlationId;
297
+ if (meeting.sessionCorrelationId) {
298
+ identifiers.sessionCorrelationId = meeting.sessionCorrelationId;
299
+ }
300
+ }
301
+
302
+ if (sessionCorrelationId) {
303
+ identifiers.sessionCorrelationId = sessionCorrelationId;
297
304
  }
298
305
 
299
306
  if (sessionCorrelationId) {
@@ -304,6 +311,8 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
304
311
  identifiers.correlationId = correlationId;
305
312
  }
306
313
 
314
+ // TODO: should we use patterns.uuid to validate correlationId and session correlation id? they will fail the diagnostic events validation pipeline if improperly formatted
315
+
307
316
  if (this.device) {
308
317
  const {device} = this;
309
318
  const {installationId} = device?.config || {};
@@ -425,7 +434,7 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
425
434
  // events that will most likely happen in join phase
426
435
  if (meetingId) {
427
436
  // @ts-ignore
428
- const meeting = this.webex.meetings.meetingCollection.get(meetingId);
437
+ const meeting = this.webex.meetings.getBasicMeetingInformation(meetingId);
429
438
 
430
439
  if (!meeting) {
431
440
  console.warn(
@@ -626,10 +635,11 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
626
635
  });
627
636
  }
628
637
 
629
- // otherwise return unkown error
638
+ // otherwise return unkown error but passing serviceErrorCode and serviceErrorName so that we know the issue
630
639
  return this.getErrorPayloadForClientErrorCode({
631
640
  clientErrorCode: UNKNOWN_ERROR,
632
- serviceErrorCode: UNKNOWN_ERROR,
641
+ serviceErrorCode: serviceErrorCode || UNKNOWN_ERROR,
642
+ serviceErrorName: rawError?.name,
633
643
  payloadOverrides: rawError.payloadOverrides,
634
644
  rawErrorMessage,
635
645
  httpStatusCode,
@@ -661,7 +671,7 @@ export default class CallDiagnosticMetrics extends StatelessWebexPlugin {
661
671
  } = options;
662
672
 
663
673
  // @ts-ignore
664
- const meeting = this.webex.meetings.meetingCollection.get(meetingId);
674
+ const meeting = this.webex.meetings.getBasicMeetingInformation(meetingId);
665
675
 
666
676
  if (!meeting) {
667
677
  console.warn(
@@ -231,13 +231,19 @@ export const getBuildType = (
231
231
  * @returns {Object} prepared item
232
232
  */
233
233
  export const prepareDiagnosticMetricItem = (webex: any, item: any) => {
234
+ const buildType = getBuildType(
235
+ webex,
236
+ item.eventPayload?.event?.eventData?.webClientDomain,
237
+ item.eventPayload?.event?.eventData?.markAsTestEvent
238
+ );
239
+
240
+ // Set upgradeChannel to 'gold' if buildType is 'prod', otherwise to the buildType value
241
+ const upgradeChannel = buildType === 'prod' ? 'gold' : buildType;
242
+
234
243
  const origin: Partial<Event['origin']> = {
235
- buildType: getBuildType(
236
- webex,
237
- item.eventPayload?.event?.eventData?.webClientDomain,
238
- item.eventPayload?.event?.eventData?.markAsTestEvent
239
- ),
244
+ buildType,
240
245
  networkType: 'unknown',
246
+ upgradeChannel,
241
247
  };
242
248
 
243
249
  // check event names and append latencies?
@@ -129,6 +129,7 @@ export const ERROR_DESCRIPTIONS = {
129
129
  ICE_AND_REACHABILITY_FAILED: 'ICEAndReachabilityFailed',
130
130
  SDP_OFFER_CREATION_ERROR: 'SdpOfferCreationError',
131
131
  SDP_OFFER_CREATION_ERROR_MISSING_CODEC: 'SdpOfferCreationErrorMissingCodec',
132
+ WDM_RESTRICTED_REGION: 'WdmRestrictedRegion',
132
133
  };
133
134
 
134
135
  export const SERVICE_ERROR_CODES_TO_CLIENT_ERROR_CODES_MAP = {
@@ -288,6 +289,12 @@ export const SERVICE_ERROR_CODES_TO_CLIENT_ERROR_CODES_MAP = {
288
289
  100005: 4103, // Depracated because of an issue in the UCF Clients
289
290
  // If both email-hash and domain-hash are null or undefined.
290
291
  100004: 4103,
292
+
293
+ // ---- WDM ----
294
+ // WDM_BLOCKED_ACCESS_BY_COUNTRY_CODE_BANNED_COUNTRY_ERROR_CODE
295
+ 4404002: 13000,
296
+ // WDM_BLOCKED_ACCESS_BY_COUNTRY_CODE_RESTRICTED_COUNTRY_ERROR_CODE
297
+ 4404003: 13000,
291
298
  };
292
299
 
293
300
  export const CLIENT_ERROR_CODE_TO_ERROR_PAYLOAD: Record<number, Partial<ClientEventError>> = {
@@ -687,6 +694,11 @@ export const CLIENT_ERROR_CODE_TO_ERROR_PAYLOAD: Record<number, Partial<ClientEv
687
694
  category: 'expected',
688
695
  fatal: true,
689
696
  },
697
+ 13000: {
698
+ errorDescription: ERROR_DESCRIPTIONS.WDM_RESTRICTED_REGION,
699
+ category: 'expected',
700
+ fatal: true,
701
+ },
690
702
  };
691
703
 
692
704
  export const CALL_DIAGNOSTIC_EVENT_FAILED_TO_SEND = 'js_sdk_call_diagnostic_event_failed_to_send';
package/src/index.ts CHANGED
@@ -25,6 +25,7 @@ import CallDiagnosticLatencies from './call-diagnostic/call-diagnostic-metrics-l
25
25
  import BehavioralMetrics from './behavioral-metrics';
26
26
  import OperationalMetrics from './operational-metrics';
27
27
  import BusinessMetrics from './business-metrics';
28
+ import RtcMetrics from './rtcMetrics';
28
29
 
29
30
  registerInternalPlugin('metrics', Metrics, {
30
31
  config,
@@ -47,6 +48,7 @@ export {
47
48
  BehavioralMetrics,
48
49
  OperationalMetrics,
49
50
  BusinessMetrics,
51
+ RtcMetrics,
50
52
  };
51
53
  export type {
52
54
  ClientEvent,
@@ -22,39 +22,93 @@ export type BrowserLaunchMethodType = NonNullable<
22
22
 
23
23
  export type MetricEventProduct = 'webex' | 'wxcc_desktop';
24
24
 
25
- export type MetricEventAgent = 'user' | 'browser' | 'system' | 'sdk' | 'redux' | 'service';
25
+ export type MetricEventAgent = 'user' | 'browser' | 'system' | 'sdk' | 'redux' | 'service' | 'api';
26
26
 
27
27
  export type MetricEventVerb =
28
+ | 'abort'
29
+ | 'accept'
30
+ | 'activate'
31
+ | 'apply'
32
+ | 'answer'
33
+ | 'authorize'
34
+ | 'build'
35
+ | 'cancel'
36
+ | 'change'
37
+ | 'click'
38
+ | 'close'
39
+ | 'complete'
40
+ | 'connect'
28
41
  | 'create'
29
- | 'get'
42
+ | 'deactivate'
43
+ | 'decrypt'
44
+ | 'delete'
45
+ | 'deliver'
46
+ | 'destroy'
47
+ | 'disable'
48
+ | 'disconnect'
49
+ | 'dismiss'
50
+ | 'display'
51
+ | 'download'
52
+ | 'edit'
53
+ | 'enable'
54
+ | 'encrypt'
55
+ | 'end'
56
+ | 'expire'
57
+ | 'fail'
30
58
  | 'fetch'
31
- | 'update'
59
+ | 'fire'
60
+ | 'generate'
61
+ | 'get'
62
+ | 'hide'
63
+ | 'hover'
64
+ | 'ignore'
65
+ | 'initialize'
66
+ | 'initiate'
67
+ | 'invalidate'
68
+ | 'join'
32
69
  | 'list'
33
- | 'delete'
70
+ | 'load'
71
+ | 'login'
72
+ | 'logout'
73
+ | 'notify'
74
+ | 'offer'
75
+ | 'open'
76
+ | 'press'
77
+ | 'receive'
78
+ | 'refer'
79
+ | 'refresh'
80
+ | 'register'
81
+ | 'release'
82
+ | 'reload'
83
+ | 'reject'
84
+ | 'request'
85
+ | 'reset'
86
+ | 'resize'
87
+ | 'respond'
88
+ | 'retry'
89
+ | 'revoke'
90
+ | 'save'
91
+ | 'search'
34
92
  | 'select'
35
- | 'view'
93
+ | 'send'
36
94
  | 'set'
95
+ | 'sign'
96
+ | 'start'
97
+ | 'submit'
98
+ | 'switch'
99
+ | 'sync'
37
100
  | 'toggle'
38
- | 'load'
39
- | 'reload'
40
- | 'click'
41
- | 'hover'
42
- | 'register'
101
+ | 'transfer'
43
102
  | 'unregister'
44
- | 'enable'
45
- | 'disable'
103
+ | 'update'
104
+ | 'upload'
46
105
  | 'use'
47
- | 'complete'
48
- | 'submit'
49
- | 'apply'
50
- | 'cancel'
51
- | 'abort'
52
- | 'sync'
53
- | 'login'
54
- | 'logout'
55
- | 'answer'
56
- | 'activate'
57
- | 'deactivate';
106
+ | 'validate'
107
+ | 'view'
108
+ | 'visit'
109
+ | 'wait'
110
+ | 'warn'
111
+ | 'exit';
58
112
 
59
113
  export type SubmitClientEventOptions = {
60
114
  meetingId?: string;
@@ -115,6 +169,8 @@ export interface DeviceContext {
115
169
 
116
170
  export type MetricType = 'behavioral' | 'operational' | 'business';
117
171
 
172
+ export type Table = 'wbxapp_callend_metrics' | 'business_metrics' | 'business_ucf' | 'default';
173
+
118
174
  type InternalEventPayload = string | number | boolean;
119
175
  export type EventPayload = Record<string, InternalEventPayload>;
120
176
  export type BehavioralEventPayload = EventPayload; // for compatibilty, can be remove after wxcc-desktop did change their imports.
@@ -242,3 +298,19 @@ export type PreComputedLatencies =
242
298
  | 'internal.call.init.join.req'
243
299
  | 'internal.other.app.api.time'
244
300
  | 'internal.api.fetch.intelligence.models';
301
+
302
+ export interface IdType {
303
+ meetingId?: string;
304
+ callId?: string;
305
+ }
306
+
307
+ export interface IMetricsAttributes {
308
+ type: string;
309
+ version: string;
310
+ userId: string;
311
+ correlationId: string;
312
+ connectionId: string;
313
+ data: any[];
314
+ meetingId?: string;
315
+ callId?: string;
316
+ }
@@ -21,6 +21,7 @@ import {
21
21
  MediaQualityEvent,
22
22
  InternalEvent,
23
23
  SubmitClientEventOptions,
24
+ Table,
24
25
  } from './metrics.types';
25
26
  import CallDiagnosticLatencies from './call-diagnostic/call-diagnostic-metrics-latencies';
26
27
  import {setMetricTimings} from './call-diagnostic/call-diagnostic-metrics.util';
@@ -28,6 +29,7 @@ import {generateCommonErrorMetadata} from './utils';
28
29
 
29
30
  /**
30
31
  * Metrics plugin to centralize all types of metrics.
32
+ * https://confluence-eng-gpk2.cisco.com/conf/pages/viewpage.action?pageId=231011379
31
33
  * @class
32
34
  */
33
35
  class Metrics extends WebexPlugin {
@@ -201,7 +203,15 @@ class Metrics extends WebexPlugin {
201
203
  * Buisness event
202
204
  * @param args
203
205
  */
204
- submitBusinessEvent({name, payload}: {name: string; payload: EventPayload}) {
206
+ submitBusinessEvent({
207
+ name,
208
+ payload,
209
+ table,
210
+ }: {
211
+ name: string;
212
+ payload: EventPayload;
213
+ table?: Table;
214
+ }) {
205
215
  if (!this.isReady) {
206
216
  // @ts-ignore
207
217
  this.webex.logger.log(
@@ -213,7 +223,7 @@ class Metrics extends WebexPlugin {
213
223
 
214
224
  this.lazyBuildBusinessMetrics();
215
225
 
216
- return this.businessMetrics.submitBusinessEvent({name, payload});
226
+ return this.businessMetrics.submitBusinessEvent({name, payload, table});
217
227
  }
218
228
 
219
229
  /**
@@ -0,0 +1,3 @@
1
+ const RTC_METRICS = {APP_ID: 'FFB51ED5-4319-4C55-8303-B1F2FCCDE231'};
2
+
3
+ export {RTC_METRICS as default};
@@ -0,0 +1,205 @@
1
+ /* eslint-disable class-methods-use-this */
2
+ import uuid from 'uuid';
3
+ import * as CallDiagnosticUtils from '../call-diagnostic/call-diagnostic-metrics.util';
4
+ import RTC_METRICS from './constants';
5
+ import {IdType, IMetricsAttributes} from '../metrics.types';
6
+
7
+ const parseJsonPayload = (payload: any[]): any | null => {
8
+ try {
9
+ if (payload && payload[0]) {
10
+ return JSON.parse(payload[0]);
11
+ }
12
+
13
+ return null;
14
+ } catch (_) {
15
+ return null;
16
+ }
17
+ };
18
+
19
+ /**
20
+ * Rtc Metrics
21
+ */
22
+ export default class RtcMetrics {
23
+ /**
24
+ * Array of MetricData items to be sent to the metrics service.
25
+ */
26
+ metricsQueue = [];
27
+
28
+ intervalId: number;
29
+
30
+ webex: any;
31
+
32
+ meetingId?: string;
33
+
34
+ callId?: string;
35
+
36
+ correlationId: string;
37
+
38
+ connectionId: string;
39
+
40
+ shouldSendMetricsOnNextStatsReport: boolean;
41
+
42
+ /**
43
+ * Initialize the interval.
44
+ *
45
+ * @param {object} webex - The main `webex` object.
46
+ * @param {IdType} Ids - Meeting or Calling id.
47
+ * @param {string} correlationId - The correlation id.
48
+ */
49
+ constructor(webex, {meetingId, callId}: IdType, correlationId) {
50
+ // `window` is used to prevent typescript from returning a NodeJS.Timer.
51
+ this.intervalId = window.setInterval(this.sendMetricsInQueue.bind(this), 30 * 1000);
52
+ this.meetingId = meetingId;
53
+ this.callId = callId;
54
+ this.webex = webex;
55
+ this.correlationId = correlationId;
56
+ this.resetConnection();
57
+ }
58
+
59
+ /**
60
+ * Updates the call identifier with the provided value.
61
+ *
62
+ * @param {string} callId - The new call identifier to set.
63
+ * @returns {void}
64
+ */
65
+ public updateCallId(callId: string) {
66
+ this.callId = callId;
67
+ }
68
+
69
+ /**
70
+ * Check to see if the metrics queue has any items.
71
+ *
72
+ * @returns {void}
73
+ */
74
+ public sendMetricsInQueue() {
75
+ if (this.metricsQueue.length) {
76
+ this.sendMetrics();
77
+ this.metricsQueue = [];
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Forces sending metrics when we get the next stats-report
83
+ *
84
+ * This is useful for cases when something important happens that affects the media connection,
85
+ * for example when we move from lobby into the meeting.
86
+ *
87
+ * @returns {void}
88
+ */
89
+ public sendNextMetrics() {
90
+ this.shouldSendMetricsOnNextStatsReport = true;
91
+ }
92
+
93
+ /**
94
+ * Add metrics items to the metrics queue.
95
+ *
96
+ * @param {object} data - An object with a payload array of metrics items.
97
+ *
98
+ * @returns {void}
99
+ */
100
+ addMetrics(data) {
101
+ if (data.payload.length) {
102
+ if (data.name === 'stats-report') {
103
+ data.payload = data.payload.map(this.anonymizeIp);
104
+ }
105
+
106
+ this.metricsQueue.push(data);
107
+
108
+ if (this.shouldSendMetricsOnNextStatsReport && data.name === 'stats-report') {
109
+ // this is the first useful set of data (WCME gives it to us after 5s), send it out immediately
110
+ // in case the user is unhappy and closes the browser early
111
+ this.sendMetricsInQueue();
112
+ this.shouldSendMetricsOnNextStatsReport = false;
113
+ }
114
+
115
+ try {
116
+ // If a connection fails, send the rest of the metrics in queue and get a new connection id.
117
+ const parsedPayload = parseJsonPayload(data.payload);
118
+ if (
119
+ data.name === 'onconnectionstatechange' &&
120
+ parsedPayload &&
121
+ parsedPayload.value === 'failed'
122
+ ) {
123
+ this.sendMetricsInQueue();
124
+ this.resetConnection();
125
+ }
126
+ } catch (e) {
127
+ console.error(e);
128
+ }
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Clear the metrics interval.
134
+ *
135
+ * @returns {void}
136
+ */
137
+ closeMetrics() {
138
+ this.sendMetricsInQueue();
139
+ clearInterval(this.intervalId);
140
+ }
141
+
142
+ /**
143
+ * Anonymize IP addresses.
144
+ *
145
+ * @param {array} stats - An RTCStatsReport organized into an array of strings.
146
+ * @returns {string}
147
+ */
148
+ anonymizeIp(stats: string): string {
149
+ const data = JSON.parse(stats);
150
+ // on local and remote candidates, anonymize the last 4 bits.
151
+ if (data.type === 'local-candidate' || data.type === 'remote-candidate') {
152
+ data.ip = CallDiagnosticUtils.anonymizeIPAddress(data.ip) || undefined;
153
+ data.address = CallDiagnosticUtils.anonymizeIPAddress(data.address) || undefined;
154
+ data.relatedAddress =
155
+ CallDiagnosticUtils.anonymizeIPAddress(data.relatedAddress) || undefined;
156
+ }
157
+
158
+ return JSON.stringify(data);
159
+ }
160
+
161
+ /**
162
+ * Set a new connection id.
163
+ *
164
+ * @returns {void}
165
+ */
166
+ private resetConnection() {
167
+ this.connectionId = uuid.v4();
168
+ this.shouldSendMetricsOnNextStatsReport = true;
169
+ }
170
+
171
+ /**
172
+ * Send metrics to the metrics service.
173
+ *
174
+ * @returns {void}
175
+ */
176
+ private sendMetrics() {
177
+ const metricsAttributes: IMetricsAttributes = {
178
+ type: 'webrtc',
179
+ version: '1.1.0',
180
+ userId: this.webex.internal.device.userId,
181
+ correlationId: this.correlationId,
182
+ connectionId: this.connectionId,
183
+ data: this.metricsQueue,
184
+ };
185
+
186
+ if (this.meetingId) {
187
+ metricsAttributes.meetingId = this.meetingId;
188
+ } else if (this.callId) {
189
+ metricsAttributes.callId = this.callId;
190
+ }
191
+
192
+ this.webex.request({
193
+ method: 'POST',
194
+ service: 'unifiedTelemetry',
195
+ resource: 'metric/v2',
196
+ headers: {
197
+ type: 'webrtcMedia',
198
+ appId: RTC_METRICS.APP_ID,
199
+ },
200
+ body: {
201
+ metrics: [metricsAttributes],
202
+ },
203
+ });
204
+ }
205
+ }