@webex/internal-plugin-metrics 3.0.0-bnr.5 → 3.0.0-next.2

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 (78) hide show
  1. package/.eslintrc.js +6 -0
  2. package/babel.config.js +3 -0
  3. package/dist/batcher.js +41 -3
  4. package/dist/batcher.js.map +1 -1
  5. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js +64 -0
  6. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js.map +1 -0
  7. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js +474 -0
  8. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js.map +1 -0
  9. package/dist/call-diagnostic/call-diagnostic-metrics.js +850 -0
  10. package/dist/call-diagnostic/call-diagnostic-metrics.js.map +1 -0
  11. package/dist/call-diagnostic/call-diagnostic-metrics.util.js +349 -0
  12. package/dist/call-diagnostic/call-diagnostic-metrics.util.js.map +1 -0
  13. package/dist/call-diagnostic/config.js +609 -0
  14. package/dist/call-diagnostic/config.js.map +1 -0
  15. package/dist/client-metrics-batcher.js +3 -3
  16. package/dist/client-metrics-batcher.js.map +1 -1
  17. package/dist/config.js +6 -9
  18. package/dist/config.js.map +1 -1
  19. package/dist/index.js +35 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/metrics.js +28 -22
  22. package/dist/metrics.js.map +1 -1
  23. package/dist/metrics.types.js +7 -0
  24. package/dist/metrics.types.js.map +1 -0
  25. package/dist/new-metrics.js +302 -0
  26. package/dist/new-metrics.js.map +1 -0
  27. package/dist/prelogin-metrics-batcher.js +81 -0
  28. package/dist/prelogin-metrics-batcher.js.map +1 -0
  29. package/dist/types/batcher.d.ts +5 -0
  30. package/dist/types/call-diagnostic/call-diagnostic-metrics-latencies.d.ts +204 -0
  31. package/dist/types/call-diagnostic/call-diagnostic-metrics.d.ts +427 -0
  32. package/dist/types/call-diagnostic/call-diagnostic-metrics.util.d.ts +103 -0
  33. package/dist/types/call-diagnostic/config.d.ts +178 -0
  34. package/dist/types/config.d.ts +18 -0
  35. package/dist/types/index.d.ts +15 -3
  36. package/dist/types/metrics.d.ts +1 -0
  37. package/dist/types/metrics.types.d.ts +105 -0
  38. package/dist/types/new-metrics.d.ts +131 -0
  39. package/dist/types/prelogin-metrics-batcher.d.ts +2 -0
  40. package/dist/types/utils.d.ts +6 -0
  41. package/dist/utils.js +26 -0
  42. package/dist/utils.js.map +1 -0
  43. package/jest.config.js +3 -0
  44. package/package.json +34 -10
  45. package/process +1 -0
  46. package/src/batcher.js +38 -0
  47. package/src/call-diagnostic/call-diagnostic-metrics-batcher.ts +72 -0
  48. package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +435 -0
  49. package/src/call-diagnostic/call-diagnostic-metrics.ts +913 -0
  50. package/src/call-diagnostic/call-diagnostic-metrics.util.ts +392 -0
  51. package/src/call-diagnostic/config.ts +685 -0
  52. package/src/client-metrics-batcher.js +1 -0
  53. package/src/config.js +1 -0
  54. package/src/index.ts +54 -0
  55. package/src/metrics.js +20 -16
  56. package/src/metrics.types.ts +168 -0
  57. package/src/new-metrics.ts +278 -0
  58. package/src/prelogin-metrics-batcher.ts +95 -0
  59. package/src/utils.ts +17 -0
  60. package/test/unit/spec/batcher.js +2 -0
  61. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +458 -0
  62. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-latencies.ts +520 -0
  63. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.ts +2297 -0
  64. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.util.ts +628 -0
  65. package/test/unit/spec/client-metrics-batcher.js +2 -0
  66. package/test/unit/spec/metrics.js +76 -95
  67. package/test/unit/spec/new-metrics.ts +233 -0
  68. package/test/unit/spec/prelogin-metrics-batcher.ts +250 -0
  69. package/test/unit/spec/utils.ts +22 -0
  70. package/tsconfig.json +6 -0
  71. package/dist/call-diagnostic-events-batcher.js +0 -60
  72. package/dist/call-diagnostic-events-batcher.js.map +0 -1
  73. package/dist/internal-plugin-metrics.d.ts +0 -21
  74. package/dist/tsdoc-metadata.json +0 -11
  75. package/src/call-diagnostic-events-batcher.js +0 -62
  76. package/src/index.js +0 -17
  77. package/test/unit/spec/call-diagnostic-events-batcher.js +0 -195
  78. package/dist/types/{call-diagnostic-events-batcher.d.ts → call-diagnostic/call-diagnostic-metrics-batcher.d.ts} +1 -1
@@ -0,0 +1,392 @@
1
+ /* eslint-disable valid-jsdoc */
2
+ import anonymize from 'ip-anonymize';
3
+ import util from 'util';
4
+
5
+ import {BrowserDetection} from '@webex/common';
6
+ import {WebexHttpError} from '@webex/webex-core';
7
+ import {isEmpty, merge} from 'lodash';
8
+ import {
9
+ ClientEvent,
10
+ Event,
11
+ MediaQualityEventAudioSetupDelayPayload,
12
+ MediaQualityEventVideoSetupDelayPayload,
13
+ MetricEventNames,
14
+ } from '../metrics.types';
15
+ import {
16
+ BROWSER_MEDIA_ERROR_NAME_TO_CLIENT_ERROR_CODES_MAP,
17
+ DTLS_HANDSHAKE_FAILED_CLIENT_CODE,
18
+ ICE_FAILED_WITHOUT_TURN_TLS_CLIENT_CODE,
19
+ ICE_FAILED_WITH_TURN_TLS_CLIENT_CODE,
20
+ ICE_FAILURE_CLIENT_CODE,
21
+ MISSING_ROAP_ANSWER_CLIENT_CODE,
22
+ WBX_APP_API_URL,
23
+ ERROR_DESCRIPTIONS,
24
+ } from './config';
25
+
26
+ const {getOSName, getOSVersion, getBrowserName, getBrowserVersion} = BrowserDetection();
27
+
28
+ export const anonymizeIPAddress = (localIp) => anonymize(localIp, 28, 96);
29
+
30
+ /**
31
+ * Returns a formated string of the user agent.
32
+ *
33
+ * @returns {string} formatted user agent information
34
+ */
35
+ export const userAgentToString = ({clientName, webexVersion}) => {
36
+ let userAgentOption;
37
+ let browserInfo;
38
+ const clientInfo = util.format('client=%s', `${clientName}`);
39
+
40
+ if (
41
+ ['chrome', 'firefox', 'msie', 'msedge', 'safari'].indexOf(getBrowserName().toLowerCase()) !== -1
42
+ ) {
43
+ browserInfo = util.format(
44
+ 'browser=%s',
45
+ `${getBrowserName().toLowerCase()}/${getBrowserVersion().split('.')[0]}`
46
+ );
47
+ }
48
+ const osInfo = util.format('os=%s', `${getOSName()}/${getOSVersion().split('.')[0]}`);
49
+
50
+ if (browserInfo) {
51
+ userAgentOption = `(${browserInfo}`;
52
+ }
53
+ if (osInfo) {
54
+ userAgentOption = userAgentOption
55
+ ? `${userAgentOption}; ${clientInfo}; ${osInfo}`
56
+ : `${clientInfo}; (${osInfo}`;
57
+ }
58
+ if (userAgentOption) {
59
+ userAgentOption += ')';
60
+
61
+ return util.format(
62
+ 'webex-js-sdk/%s %s',
63
+ `${process.env.NODE_ENV}-${webexVersion}`,
64
+ userAgentOption
65
+ );
66
+ }
67
+
68
+ return util.format('webex-js-sdk/%s', `${process.env.NODE_ENV}-${webexVersion}`);
69
+ };
70
+
71
+ /**
72
+ * Iterates object recursively and removes any
73
+ * property that returns isEmpty for it's associated value
74
+ * isEmpty = implementation from Lodash.
75
+ *
76
+ * It modifies the object in place (mutable)
77
+ *
78
+ * @param obj - input
79
+ * @returns
80
+ */
81
+ export const clearEmptyKeysRecursively = (obj: any) => {
82
+ // Check if the object is empty
83
+ if (Object.keys(obj).length === 0) {
84
+ return;
85
+ }
86
+
87
+ Object.keys(obj).forEach((key) => {
88
+ if (
89
+ (typeof obj[key] === 'object' || typeof obj[key] === 'string' || Array.isArray(obj[key])) &&
90
+ isEmpty(obj[key])
91
+ ) {
92
+ delete obj[key];
93
+ }
94
+ if (Array.isArray(obj[key])) {
95
+ obj[key] = [...obj[key].filter((x) => !!x)];
96
+ }
97
+ if (typeof obj[key] === 'object') {
98
+ clearEmptyKeysRecursively(obj[key]);
99
+ }
100
+ });
101
+ };
102
+
103
+ /**
104
+ * Locus error codes start with 2. The next three digits are the
105
+ * HTTP status code related to the error code (like 400, 403, 502, etc.)
106
+ * The remaining three digits are just an increasing integer.
107
+ * If it is 7 digits and starts with a 2, it is locus.
108
+ *
109
+ * @param errorCode
110
+ * @returns {boolean}
111
+ */
112
+ export const isLocusServiceErrorCode = (errorCode: string | number) => {
113
+ const code = `${errorCode}`;
114
+
115
+ if (code.length === 7 && code.charAt(0) === '2') {
116
+ return true;
117
+ }
118
+
119
+ return false;
120
+ };
121
+
122
+ /**
123
+ * MeetingInfo errors sometimes has body.data.meetingInfo object
124
+ * MeetingInfo errors come with a wbxappapi url
125
+ *
126
+ * @param {Object} rawError
127
+ * @returns {boolean}
128
+ */
129
+ export const isMeetingInfoServiceError = (rawError: any) => {
130
+ if (rawError.body?.data?.meetingInfo || rawError.body?.url?.includes(WBX_APP_API_URL)) {
131
+ return true;
132
+ }
133
+
134
+ return false;
135
+ };
136
+
137
+ /**
138
+ * Returns true if the raw error is a network related error
139
+ *
140
+ * @param {Object} rawError
141
+ * @returns {boolean}
142
+ */
143
+ export const isNetworkError = (rawError: any) => {
144
+ if (rawError instanceof WebexHttpError.NetworkOrCORSError) {
145
+ return true;
146
+ }
147
+
148
+ return false;
149
+ };
150
+
151
+ /**
152
+ * Returns true if the error is an unauthorized error
153
+ *
154
+ * @param {Object} rawError
155
+ * @returns {boolean}
156
+ */
157
+ export const isUnauthorizedError = (rawError: any) => {
158
+ if (rawError instanceof WebexHttpError.Unauthorized) {
159
+ return true;
160
+ }
161
+
162
+ return false;
163
+ };
164
+
165
+ /**
166
+ * Returns true if the error is an SdpOfferCreation error
167
+ *
168
+ * @param {Object} rawError
169
+ * @returns {boolean}
170
+ */
171
+ export const isSdpOfferCreationError = (rawError: any) => {
172
+ // would LIKE to do rawError instanceof Errors.SdpOfferCreationError
173
+ // but including internal-media-core in plugin-metrics breaks meetings and metrics unit tests
174
+ if (rawError.name === ERROR_DESCRIPTIONS.SDP_OFFER_CREATION_ERROR) {
175
+ return true;
176
+ }
177
+
178
+ return false;
179
+ };
180
+
181
+ /**
182
+ * MDN Media Devices getUserMedia() method returns a name if it errs
183
+ * Documentation can be found here: https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
184
+ *
185
+ * @param errorCode
186
+ * @returns
187
+ */
188
+ export const isBrowserMediaErrorName = (errorName: any) => {
189
+ if (BROWSER_MEDIA_ERROR_NAME_TO_CLIENT_ERROR_CODES_MAP[errorName]) {
190
+ return true;
191
+ }
192
+
193
+ return false;
194
+ };
195
+
196
+ /**
197
+ * @param webClientDomain
198
+ * @returns
199
+ */
200
+ export const getBuildType = (
201
+ webClientDomain,
202
+ markAsTestEvent = false
203
+ ): Event['origin']['buildType'] => {
204
+ // used temporary to test pre join in production without creating noise data, SPARK-468456
205
+ if (markAsTestEvent) {
206
+ return 'test';
207
+ }
208
+
209
+ if (
210
+ webClientDomain?.includes('localhost') ||
211
+ webClientDomain?.includes('127.0.0.1') ||
212
+ process.env.NODE_ENV !== 'production'
213
+ ) {
214
+ return 'test';
215
+ }
216
+
217
+ return 'prod';
218
+ };
219
+
220
+ /**
221
+ * Prepare metric item for submission.
222
+ * @param {Object} webex sdk instance
223
+ * @param {Object} item
224
+ * @returns {Object} prepared item
225
+ */
226
+ export const prepareDiagnosticMetricItem = (webex: any, item: any) => {
227
+ const origin: Partial<Event['origin']> = {
228
+ buildType: exports.getBuildType(
229
+ item.eventPayload?.event?.eventData?.webClientDomain,
230
+ item.eventPayload?.event?.eventData?.markAsTestEvent
231
+ ),
232
+ networkType: 'unknown',
233
+ };
234
+
235
+ // check event names and append latencies?
236
+ const eventName = item.eventPayload?.event?.name as MetricEventNames;
237
+ const joinTimes: ClientEvent['payload']['joinTimes'] = {};
238
+ const audioSetupDelay: MediaQualityEventAudioSetupDelayPayload = {};
239
+ const videoSetupDelay: MediaQualityEventVideoSetupDelayPayload = {};
240
+
241
+ const cdl = webex.internal.newMetrics.callDiagnosticLatencies;
242
+
243
+ switch (eventName) {
244
+ case 'client.webexapp.launched':
245
+ joinTimes.downloadTime = cdl.getDownloadTimeJMT();
246
+ break;
247
+ case 'client.interstitial-window.launched':
248
+ joinTimes.meetingInfoReqResp = cdl.getMeetingInfoReqResp();
249
+ joinTimes.clickToInterstitial = cdl.getClickToInterstitial();
250
+ break;
251
+
252
+ case 'client.call.initiated':
253
+ joinTimes.meetingInfoReqResp = cdl.getMeetingInfoReqResp();
254
+ joinTimes.showInterstitialTime = cdl.getShowInterstitialTime();
255
+ joinTimes.registerWDMDeviceJMT = cdl.getRegisterWDMDeviceJMT();
256
+ break;
257
+
258
+ case 'client.locus.join.response':
259
+ joinTimes.meetingInfoReqResp = cdl.getMeetingInfoReqResp();
260
+ joinTimes.callInitJoinReq = cdl.getCallInitJoinReq();
261
+ joinTimes.joinReqResp = cdl.getJoinReqResp();
262
+ joinTimes.joinReqSentReceived = cdl.getJoinRespSentReceived();
263
+ joinTimes.pageJmt = cdl.getPageJMT();
264
+ joinTimes.clickToInterstitial = cdl.getClickToInterstitial();
265
+ joinTimes.interstitialToJoinOK = cdl.getInterstitialToJoinOK();
266
+ joinTimes.totalJmt = cdl.getTotalJMT();
267
+ joinTimes.clientJmt = cdl.getClientJMT();
268
+ joinTimes.downloadTime = cdl.getDownloadTimeJMT();
269
+ break;
270
+
271
+ case 'client.ice.end':
272
+ joinTimes.ICESetupTime = cdl.getICESetupTime();
273
+ joinTimes.audioICESetupTime = cdl.getAudioICESetupTime();
274
+ joinTimes.videoICESetupTime = cdl.getVideoICESetupTime();
275
+ joinTimes.shareICESetupTime = cdl.getShareICESetupTime();
276
+ break;
277
+
278
+ case 'client.media.rx.start':
279
+ joinTimes.localSDPGenRemoteSDPRecv = cdl.getLocalSDPGenRemoteSDPRecv();
280
+ break;
281
+
282
+ case 'client.media-engine.ready':
283
+ joinTimes.totalMediaJMT = cdl.getTotalMediaJMT();
284
+ joinTimes.interstitialToMediaOKJMT = cdl.getInterstitialToMediaOKJMT();
285
+ joinTimes.callInitMediaEngineReady = cdl.getCallInitMediaEngineReady();
286
+ joinTimes.stayLobbyTime = cdl.getStayLobbyTime();
287
+ break;
288
+
289
+ case 'client.mediaquality.event':
290
+ audioSetupDelay.joinRespRxStart = cdl.getAudioJoinRespRxStart();
291
+ audioSetupDelay.joinRespTxStart = cdl.getAudioJoinRespTxStart();
292
+ videoSetupDelay.joinRespRxStart = cdl.getVideoJoinRespRxStart();
293
+ videoSetupDelay.joinRespTxStart = cdl.getVideoJoinRespTxStart();
294
+ }
295
+
296
+ if (!isEmpty(joinTimes)) {
297
+ item.eventPayload.event = merge(item.eventPayload.event, {joinTimes});
298
+ }
299
+
300
+ if (!isEmpty(audioSetupDelay)) {
301
+ item.eventPayload.event = merge(item.eventPayload.event, {audioSetupDelay});
302
+ }
303
+
304
+ if (!isEmpty(videoSetupDelay)) {
305
+ item.eventPayload.event = merge(item.eventPayload.event, {videoSetupDelay});
306
+ }
307
+
308
+ item.eventPayload.origin = Object.assign(origin, item.eventPayload.origin);
309
+
310
+ // @ts-ignore
311
+ webex.logger.log(
312
+ `CallDiagnosticLatencies,prepareDiagnosticMetricItem: ${JSON.stringify({
313
+ latencies: Object.fromEntries(cdl.latencyTimestamps),
314
+ event: item,
315
+ })}`
316
+ );
317
+
318
+ return item;
319
+ };
320
+
321
+ /**
322
+ * Sets the originTime value(s) before the request/fetch.
323
+ * This function is only useful if you are about to submit a metrics
324
+ * request using pre-built fetch options;
325
+ *
326
+ * @param {any} options
327
+ * @returns {any} the updated options object
328
+ */
329
+ export const setMetricTimings = (options) => {
330
+ if (options.body && options.json) {
331
+ const body = JSON.parse(options.body);
332
+
333
+ const now = new Date().toISOString();
334
+ body.metrics?.forEach((metric) => {
335
+ if (metric.eventPayload) {
336
+ // The event will effectively be triggered and sent at the same time.
337
+ // The existing triggered time is from when the options were built.
338
+ metric.eventPayload.originTime = {
339
+ triggered: now,
340
+ sent: now,
341
+ };
342
+ }
343
+ });
344
+ options.body = JSON.stringify(body);
345
+ }
346
+
347
+ return options;
348
+ };
349
+
350
+ export const extractVersionMetadata = (version: string) => {
351
+ // extract major and minor version
352
+ const [majorVersion, minorVersion] = version.split('.');
353
+
354
+ return {
355
+ majorVersion: parseInt(majorVersion, 10),
356
+ minorVersion: parseInt(minorVersion, 10),
357
+ };
358
+ };
359
+
360
+ /**
361
+ * Generates client error codes for specific ice failures
362
+ * that happen when trying to add media in a meeting.
363
+ */
364
+ export const generateClientErrorCodeForIceFailure = ({
365
+ signalingState,
366
+ iceConnectionState,
367
+ turnServerUsed,
368
+ }: {
369
+ signalingState: RTCPeerConnection['signalingState'];
370
+ iceConnectionState: RTCPeerConnection['iceConnectionState'];
371
+ turnServerUsed: boolean;
372
+ }) => {
373
+ let errorCode = ICE_FAILURE_CLIENT_CODE; // default;
374
+
375
+ if (signalingState === 'have-local-offer') {
376
+ errorCode = MISSING_ROAP_ANSWER_CLIENT_CODE;
377
+ }
378
+
379
+ if (signalingState === 'stable' && iceConnectionState === 'connected') {
380
+ errorCode = DTLS_HANDSHAKE_FAILED_CLIENT_CODE;
381
+ }
382
+
383
+ if (signalingState !== 'have-local-offer' && iceConnectionState !== 'connected') {
384
+ if (turnServerUsed) {
385
+ errorCode = ICE_FAILED_WITH_TURN_TLS_CLIENT_CODE;
386
+ } else {
387
+ errorCode = ICE_FAILED_WITHOUT_TURN_TLS_CLIENT_CODE;
388
+ }
389
+ }
390
+
391
+ return errorCode;
392
+ };