@webex/internal-plugin-metrics 3.0.0-beta.4 → 3.0.0-beta.400

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 (74) hide show
  1. package/README.md +1 -3
  2. package/dist/batcher.js +42 -22
  3. package/dist/batcher.js.map +1 -1
  4. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js +65 -0
  5. package/dist/call-diagnostic/call-diagnostic-metrics-batcher.js.map +1 -0
  6. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js +508 -0
  7. package/dist/call-diagnostic/call-diagnostic-metrics-latencies.js.map +1 -0
  8. package/dist/call-diagnostic/call-diagnostic-metrics.js +860 -0
  9. package/dist/call-diagnostic/call-diagnostic-metrics.js.map +1 -0
  10. package/dist/call-diagnostic/call-diagnostic-metrics.util.js +367 -0
  11. package/dist/call-diagnostic/call-diagnostic-metrics.util.js.map +1 -0
  12. package/dist/call-diagnostic/config.js +627 -0
  13. package/dist/call-diagnostic/config.js.map +1 -0
  14. package/dist/client-metrics-batcher.js +3 -8
  15. package/dist/client-metrics-batcher.js.map +1 -1
  16. package/dist/config.js +23 -6
  17. package/dist/config.js.map +1 -1
  18. package/dist/index.js +46 -10
  19. package/dist/index.js.map +1 -1
  20. package/dist/metrics.js +47 -80
  21. package/dist/metrics.js.map +1 -1
  22. package/dist/metrics.types.js +7 -0
  23. package/dist/metrics.types.js.map +1 -0
  24. package/dist/new-metrics.js +300 -0
  25. package/dist/new-metrics.js.map +1 -0
  26. package/dist/prelogin-metrics-batcher.js +82 -0
  27. package/dist/prelogin-metrics-batcher.js.map +1 -0
  28. package/dist/types/batcher.d.ts +7 -0
  29. package/dist/types/call-diagnostic/call-diagnostic-metrics-batcher.d.ts +2 -0
  30. package/dist/types/call-diagnostic/call-diagnostic-metrics-latencies.d.ts +218 -0
  31. package/dist/types/call-diagnostic/call-diagnostic-metrics.d.ts +421 -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/client-metrics-batcher.d.ts +2 -0
  35. package/dist/types/config.d.ts +36 -0
  36. package/dist/types/index.d.ts +15 -0
  37. package/dist/types/metrics.d.ts +3 -0
  38. package/dist/types/metrics.types.d.ts +105 -0
  39. package/dist/types/new-metrics.d.ts +131 -0
  40. package/dist/types/prelogin-metrics-batcher.d.ts +2 -0
  41. package/dist/types/utils.d.ts +6 -0
  42. package/dist/utils.js +27 -0
  43. package/dist/utils.js.map +1 -0
  44. package/package.json +16 -8
  45. package/src/batcher.js +71 -26
  46. package/src/call-diagnostic/call-diagnostic-metrics-batcher.ts +72 -0
  47. package/src/call-diagnostic/call-diagnostic-metrics-latencies.ts +467 -0
  48. package/src/call-diagnostic/call-diagnostic-metrics.ts +919 -0
  49. package/src/call-diagnostic/call-diagnostic-metrics.util.ts +395 -0
  50. package/src/call-diagnostic/config.ts +685 -0
  51. package/src/client-metrics-batcher.js +4 -4
  52. package/src/config.js +26 -5
  53. package/src/index.ts +56 -0
  54. package/src/metrics.js +47 -58
  55. package/src/metrics.types.ts +170 -0
  56. package/src/new-metrics.ts +278 -0
  57. package/src/prelogin-metrics-batcher.ts +95 -0
  58. package/src/utils.ts +17 -0
  59. package/test/unit/spec/batcher.js +28 -15
  60. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-batcher.ts +457 -0
  61. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics-latencies.ts +657 -0
  62. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.ts +2303 -0
  63. package/test/unit/spec/call-diagnostic/call-diagnostic-metrics.util.ts +634 -0
  64. package/test/unit/spec/client-metrics-batcher.js +28 -15
  65. package/test/unit/spec/metrics.js +94 -116
  66. package/test/unit/spec/new-metrics.ts +231 -0
  67. package/test/unit/spec/prelogin-metrics-batcher.ts +250 -0
  68. package/test/unit/spec/utils.ts +22 -0
  69. package/tsconfig.json +6 -0
  70. package/dist/call-diagnostic-events-batcher.js +0 -70
  71. package/dist/call-diagnostic-events-batcher.js.map +0 -1
  72. package/src/call-diagnostic-events-batcher.js +0 -62
  73. package/src/index.js +0 -17
  74. package/test/unit/spec/call-diagnostic-events-batcher.js +0 -180
@@ -0,0 +1,250 @@
1
+ /*!
2
+ * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
+ */
4
+
5
+ import {assert} from '@webex/test-helper-chai';
6
+ import {config, Utils} from '@webex/internal-plugin-metrics';
7
+ import {CallDiagnosticUtils} from '@webex/internal-plugin-metrics';
8
+ import MockWebex from '@webex/test-helper-mock-webex';
9
+ import sinon from 'sinon';
10
+ import FakeTimers from '@sinonjs/fake-timers';
11
+ import {NewMetrics} from '@webex/internal-plugin-metrics';
12
+ import {uniqueId} from 'lodash';
13
+
14
+ const flushPromises = () => new Promise(setImmediate);
15
+
16
+ describe('internal-plugin-metrics', () => {
17
+ describe('PreLoginMetricsBatcher', () => {
18
+ let webex;
19
+ let clock;
20
+ let now;
21
+
22
+ const preLoginId = 'my_prelogin_id';
23
+
24
+ beforeEach(() => {
25
+ now = new Date();
26
+ clock = FakeTimers.install({now});
27
+
28
+ //@ts-ignore
29
+ webex = new MockWebex({
30
+ children: {
31
+ newMetrics: NewMetrics,
32
+ },
33
+ });
34
+
35
+ webex.request = (options) =>
36
+ Promise.resolve({body: {items: []}, waitForServiceTimeout: 15, options});
37
+
38
+ sinon.spy(webex, 'request');
39
+ webex.emit('ready');
40
+ webex.config.metrics = config.metrics;
41
+ });
42
+
43
+ afterEach(() => {
44
+ sinon.restore();
45
+ clock.uninstall();
46
+ });
47
+
48
+ describe('#request()', () => {
49
+ it('when the request completes successfully, clears the queue', async () => {
50
+ const promise =
51
+ webex.internal.newMetrics.callDiagnosticMetrics.submitToCallDiagnosticsPreLogin(
52
+ //@ts-ignore
53
+ {event: {name: 'client.interstitial-window.launched'}},
54
+ preLoginId
55
+ );
56
+ await flushPromises();
57
+ clock.tick(config.metrics.batcherWait);
58
+
59
+ await promise;
60
+
61
+ const webexRequestArgs = webex.request.args[0][0];
62
+
63
+ const dateAfterBatcherWait = new Date(now.getTime() + config.metrics.batcherWait);
64
+ //@ts-ignore
65
+ assert.calledOnce(webex.request);
66
+
67
+ // matching because the request includes a symbol key: value pair and sinon cannot handle to compare it..
68
+ assert.match(webexRequestArgs, {
69
+ body: {
70
+ metrics: [
71
+ {
72
+ eventPayload: {
73
+ event: {
74
+ joinTimes: {
75
+ meetingInfoReqResp: undefined,
76
+ clickToInterstitial: undefined,
77
+ },
78
+ name: 'client.interstitial-window.launched',
79
+ },
80
+ origin: {
81
+ buildType: 'test',
82
+ networkType: 'unknown',
83
+ },
84
+ originTime: {
85
+ sent: dateAfterBatcherWait.toISOString(),
86
+ },
87
+ },
88
+ type: ['diagnostic-event'],
89
+ },
90
+ ],
91
+ },
92
+ headers: {
93
+ authorization: false,
94
+ 'x-prelogin-userid': preLoginId,
95
+ },
96
+ method: 'POST',
97
+ resource: 'clientmetrics-prelogin',
98
+ service: 'metrics',
99
+ waitForServiceTimeout: 30,
100
+ });
101
+ assert.lengthOf(
102
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.queue,
103
+ 0
104
+ );
105
+ });
106
+
107
+ it('when the request fails, does not clear the queue', async () => {
108
+ webex.request = sinon.stub().rejects(new Error('my_error'));
109
+
110
+ webex.logger.error = sinon.stub();
111
+ webex.logger.log = sinon.stub();
112
+ sinon.stub(Utils, 'generateCommonErrorMetadata').returns('formattedError');
113
+
114
+ const promise =
115
+ webex.internal.newMetrics.callDiagnosticMetrics.submitToCallDiagnosticsPreLogin(
116
+ {
117
+ event: 'my.event',
118
+ },
119
+ preLoginId
120
+ );
121
+
122
+ await flushPromises();
123
+ clock.tick(config.metrics.batcherWait);
124
+
125
+ let error;
126
+
127
+ // catch the expected error and store it
128
+ try {
129
+ await promise;
130
+ } catch (err) {
131
+ error = err;
132
+ }
133
+
134
+ // This is horrific, but stubbing lodash is proving difficult
135
+ const expectedBatchId = parseInt(uniqueId()) - 1;
136
+
137
+ assert.equal(error.message, 'my_error');
138
+ assert.calledOnceWithExactly(
139
+ webex.logger.error,
140
+ 'Pre Login Metrics -->',
141
+ `PreLoginMetricsBatcher: @submitHttpRequest#prelogin-ca-batch-${expectedBatchId}. Request failed:`,
142
+ `error: formattedError`
143
+ );
144
+ assert.lengthOf(
145
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.queue,
146
+ 0
147
+ );
148
+ });
149
+
150
+ it('fails if preLoinId is not set', async () => {
151
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.preLoginId = undefined;
152
+
153
+ const promise =
154
+ webex.internal.newMetrics.callDiagnosticMetrics.submitToCallDiagnosticsPreLogin(
155
+ {
156
+ event: 'my.event',
157
+ },
158
+ undefined
159
+ );
160
+
161
+ await flushPromises();
162
+ clock.tick(config.metrics.batcherWait);
163
+
164
+ let error;
165
+
166
+ // catch the expected error and store it
167
+ try {
168
+ await promise;
169
+ } catch (err) {
170
+ error = err;
171
+ }
172
+
173
+ assert.equal(error.message, 'PreLoginId is not set.');
174
+ });
175
+ });
176
+
177
+ describe('prepareItem', () => {
178
+ it('calls prepareDiagnosticMetricItem correctly', async () => {
179
+ // avoid setting .sent timestamp
180
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.prepareRequest = (q) =>
181
+ Promise.resolve(q);
182
+
183
+ const prepareItemSpy = sinon.spy(
184
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher,
185
+ 'prepareItem'
186
+ );
187
+ const prepareDiagnosticMetricItemSpy = sinon.spy(
188
+ CallDiagnosticUtils,
189
+ 'prepareDiagnosticMetricItem'
190
+ );
191
+
192
+ const promise =
193
+ webex.internal.newMetrics.callDiagnosticMetrics.submitToCallDiagnosticsPreLogin(
194
+ {
195
+ event: 'my.event',
196
+ },
197
+ preLoginId
198
+ );
199
+
200
+ await flushPromises();
201
+
202
+ clock.tick(config.metrics.batcherWait);
203
+
204
+ await promise;
205
+
206
+ const calls = prepareItemSpy.getCalls()[0];
207
+
208
+ // item also gets assigned a delay property but the key is a Symbol and haven't been able to test that..
209
+ assert.deepEqual(calls.args[0].eventPayload, {
210
+ event: 'my.event',
211
+ origin: {buildType: 'test', networkType: 'unknown'},
212
+ });
213
+
214
+ assert.deepEqual(calls.args[0].type, ['diagnostic-event']);
215
+
216
+ const prepareDiagnosticMetricItemCalls = prepareDiagnosticMetricItemSpy.getCalls();
217
+
218
+ // second argument (item) also gets assigned a delay property but the key is a Symbol and haven't been able to test that..
219
+ assert.deepEqual(prepareDiagnosticMetricItemCalls[0].args[0], webex);
220
+ assert.deepEqual(prepareDiagnosticMetricItemCalls[0].args[1].eventPayload, {
221
+ event: 'my.event',
222
+ origin: {
223
+ buildType: 'test',
224
+ networkType: 'unknown',
225
+ },
226
+ });
227
+ assert.deepEqual(prepareDiagnosticMetricItemCalls[0].args[1].type, ['diagnostic-event']);
228
+ });
229
+ });
230
+
231
+ describe('savePreLoginId', () => {
232
+ it('saves the preLoginId', () => {
233
+ const preLoginId = 'my_prelogin_id';
234
+
235
+ assert.isUndefined(
236
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.preLoginId
237
+ );
238
+
239
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.savePreLoginId(
240
+ preLoginId
241
+ );
242
+
243
+ assert.equal(
244
+ webex.internal.newMetrics.callDiagnosticMetrics.preLoginMetricsBatcher.preLoginId,
245
+ preLoginId
246
+ );
247
+ });
248
+ });
249
+ });
250
+ });
@@ -0,0 +1,22 @@
1
+ import {assert} from '@webex/test-helper-chai';
2
+ import {Utils} from '@webex/internal-plugin-metrics';
3
+
4
+ describe('internal-plugin-metrics', () => {
5
+ describe('generateCommonErrorMetadata', () => {
6
+ it('should return JSON stringified error object', () => {
7
+ const error = new Error('test error');
8
+ const result = Utils.generateCommonErrorMetadata(error);
9
+ assert.deepEqual(result, JSON.stringify({
10
+ message: 'test error',
11
+ name: 'Error',
12
+ stack: error.stack
13
+ }))
14
+ });
15
+
16
+ it('should return error if not instanceof Error', () => {
17
+ const error = 'test error';
18
+ const result = Utils.generateCommonErrorMetadata(error);
19
+ assert.deepEqual(result, 'test error')
20
+ });
21
+ });
22
+ })
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "include": [
4
+ "src"
5
+ ],
6
+ }
@@ -1,70 +0,0 @@
1
- "use strict";
2
-
3
- var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
4
-
5
- var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
6
-
7
- _Object$defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
-
11
- exports.default = void 0;
12
-
13
- var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));
14
-
15
- var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));
16
-
17
- var _batcher = _interopRequireDefault(require("./batcher"));
18
-
19
- /*!
20
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
21
- */
22
- var CallDiagnosticEventsBatcher = _batcher.default.extend({
23
- namespace: 'Metrics',
24
-
25
- /**
26
- * @param {string} webClientDomain
27
- * @returns {string}
28
- */
29
- getBuildType: function getBuildType(webClientDomain) {
30
- if (webClientDomain !== null && webClientDomain !== void 0 && webClientDomain.includes('teams.webex.com') || webClientDomain !== null && webClientDomain !== void 0 && webClientDomain.includes('localhost') || webClientDomain !== null && webClientDomain !== void 0 && webClientDomain.includes('127.0.0.1') || process.env.NODE_ENV !== 'production') {
31
- return 'test';
32
- }
33
-
34
- return process.env.NODE_ENV === 'production' ? 'prod' : 'test';
35
- },
36
- prepareItem: function prepareItem(item) {
37
- var _item$event, _item$event$eventData;
38
-
39
- // networkType should be a enum value: `wifi`, `ethernet`, `cellular`, or `unknown`.
40
- // Browsers cannot provide such information right now. However, it is a required field.
41
- var origin = {
42
- buildType: this.getBuildType((_item$event = item.event) === null || _item$event === void 0 ? void 0 : (_item$event$eventData = _item$event.eventData) === null || _item$event$eventData === void 0 ? void 0 : _item$event$eventData.webClientDomain),
43
- networkType: 'unknown'
44
- };
45
- item.eventPayload.origin = (0, _assign.default)(origin, item.eventPayload.origin);
46
- return _promise.default.resolve(item);
47
- },
48
- prepareRequest: function prepareRequest(queue) {
49
- // Add sent timestamp
50
- queue.forEach(function (item) {
51
- item.eventPayload.originTime = item.eventPayload.originTime || {};
52
- item.eventPayload.originTime.sent = new Date().toISOString();
53
- });
54
- return _promise.default.resolve(queue);
55
- },
56
- submitHttpRequest: function submitHttpRequest(payload) {
57
- return this.webex.request({
58
- method: 'POST',
59
- service: 'metrics',
60
- resource: 'clientmetrics',
61
- body: {
62
- metrics: payload
63
- }
64
- });
65
- }
66
- });
67
-
68
- var _default = CallDiagnosticEventsBatcher;
69
- exports.default = _default;
70
- //# sourceMappingURL=call-diagnostic-events-batcher.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["CallDiagnosticEventsBatcher","Batcher","extend","namespace","getBuildType","webClientDomain","includes","process","env","NODE_ENV","prepareItem","item","origin","buildType","event","eventData","networkType","eventPayload","resolve","prepareRequest","queue","forEach","originTime","sent","Date","toISOString","submitHttpRequest","payload","webex","request","method","service","resource","body","metrics"],"sources":["call-diagnostic-events-batcher.js"],"sourcesContent":["/*!\n * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.\n */\n\nimport Batcher from './batcher';\n\nconst CallDiagnosticEventsBatcher = Batcher.extend({\n namespace: 'Metrics',\n\n /**\n * @param {string} webClientDomain\n * @returns {string}\n */\n getBuildType(webClientDomain) {\n if (\n webClientDomain?.includes('teams.webex.com') ||\n webClientDomain?.includes('localhost') ||\n webClientDomain?.includes('127.0.0.1') ||\n process.env.NODE_ENV !== 'production'\n ) {\n return 'test';\n }\n\n return process.env.NODE_ENV === 'production' ? 'prod' : 'test';\n },\n\n prepareItem(item) {\n // networkType should be a enum value: `wifi`, `ethernet`, `cellular`, or `unknown`.\n // Browsers cannot provide such information right now. However, it is a required field.\n const origin = {\n buildType: this.getBuildType(item.event?.eventData?.webClientDomain),\n networkType: 'unknown'\n };\n\n item.eventPayload.origin = Object.assign(origin, item.eventPayload.origin);\n\n return Promise.resolve(item);\n },\n\n prepareRequest(queue) {\n // Add sent timestamp\n queue.forEach((item) => {\n item.eventPayload.originTime = item.eventPayload.originTime || {};\n item.eventPayload.originTime.sent = new Date().toISOString();\n });\n\n return Promise.resolve(queue);\n },\n\n submitHttpRequest(payload) {\n return this.webex.request({\n method: 'POST',\n service: 'metrics',\n resource: 'clientmetrics',\n body: {\n metrics: payload\n }\n });\n }\n});\n\nexport default CallDiagnosticEventsBatcher;\n"],"mappings":";;;;;;;;;;;;;;;;AAIA;;AAJA;AACA;AACA;AAIA,IAAMA,2BAA2B,GAAGC,gBAAA,CAAQC,MAAR,CAAe;EACjDC,SAAS,EAAE,SADsC;;EAGjD;AACF;AACA;AACA;EACEC,YAPiD,wBAOpCC,eAPoC,EAOnB;IAC5B,IACEA,eAAe,SAAf,IAAAA,eAAe,WAAf,IAAAA,eAAe,CAAEC,QAAjB,CAA0B,iBAA1B,KACAD,eADA,aACAA,eADA,eACAA,eAAe,CAAEC,QAAjB,CAA0B,WAA1B,CADA,IAEAD,eAFA,aAEAA,eAFA,eAEAA,eAAe,CAAEC,QAAjB,CAA0B,WAA1B,CAFA,IAGAC,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAJ3B,EAKE;MACA,OAAO,MAAP;IACD;;IAED,OAAOF,OAAO,CAACC,GAAR,CAAYC,QAAZ,KAAyB,YAAzB,GAAwC,MAAxC,GAAiD,MAAxD;EACD,CAlBgD;EAoBjDC,WApBiD,uBAoBrCC,IApBqC,EAoB/B;IAAA;;IAChB;IACA;IACA,IAAMC,MAAM,GAAG;MACbC,SAAS,EAAE,KAAKT,YAAL,gBAAkBO,IAAI,CAACG,KAAvB,yEAAkB,YAAYC,SAA9B,0DAAkB,sBAAuBV,eAAzC,CADE;MAEbW,WAAW,EAAE;IAFA,CAAf;IAKAL,IAAI,CAACM,YAAL,CAAkBL,MAAlB,GAA2B,qBAAcA,MAAd,EAAsBD,IAAI,CAACM,YAAL,CAAkBL,MAAxC,CAA3B;IAEA,OAAO,iBAAQM,OAAR,CAAgBP,IAAhB,CAAP;EACD,CA/BgD;EAiCjDQ,cAjCiD,0BAiClCC,KAjCkC,EAiC3B;IACpB;IACAA,KAAK,CAACC,OAAN,CAAc,UAACV,IAAD,EAAU;MACtBA,IAAI,CAACM,YAAL,CAAkBK,UAAlB,GAA+BX,IAAI,CAACM,YAAL,CAAkBK,UAAlB,IAAgC,EAA/D;MACAX,IAAI,CAACM,YAAL,CAAkBK,UAAlB,CAA6BC,IAA7B,GAAoC,IAAIC,IAAJ,GAAWC,WAAX,EAApC;IACD,CAHD;IAKA,OAAO,iBAAQP,OAAR,CAAgBE,KAAhB,CAAP;EACD,CAzCgD;EA2CjDM,iBA3CiD,6BA2C/BC,OA3C+B,EA2CtB;IACzB,OAAO,KAAKC,KAAL,CAAWC,OAAX,CAAmB;MACxBC,MAAM,EAAE,MADgB;MAExBC,OAAO,EAAE,SAFe;MAGxBC,QAAQ,EAAE,eAHc;MAIxBC,IAAI,EAAE;QACJC,OAAO,EAAEP;MADL;IAJkB,CAAnB,CAAP;EAQD;AApDgD,CAAf,CAApC;;eAuDe3B,2B"}
@@ -1,62 +0,0 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import Batcher from './batcher';
6
-
7
- const CallDiagnosticEventsBatcher = Batcher.extend({
8
- namespace: 'Metrics',
9
-
10
- /**
11
- * @param {string} webClientDomain
12
- * @returns {string}
13
- */
14
- getBuildType(webClientDomain) {
15
- if (
16
- webClientDomain?.includes('teams.webex.com') ||
17
- webClientDomain?.includes('localhost') ||
18
- webClientDomain?.includes('127.0.0.1') ||
19
- process.env.NODE_ENV !== 'production'
20
- ) {
21
- return 'test';
22
- }
23
-
24
- return process.env.NODE_ENV === 'production' ? 'prod' : 'test';
25
- },
26
-
27
- prepareItem(item) {
28
- // networkType should be a enum value: `wifi`, `ethernet`, `cellular`, or `unknown`.
29
- // Browsers cannot provide such information right now. However, it is a required field.
30
- const origin = {
31
- buildType: this.getBuildType(item.event?.eventData?.webClientDomain),
32
- networkType: 'unknown'
33
- };
34
-
35
- item.eventPayload.origin = Object.assign(origin, item.eventPayload.origin);
36
-
37
- return Promise.resolve(item);
38
- },
39
-
40
- prepareRequest(queue) {
41
- // Add sent timestamp
42
- queue.forEach((item) => {
43
- item.eventPayload.originTime = item.eventPayload.originTime || {};
44
- item.eventPayload.originTime.sent = new Date().toISOString();
45
- });
46
-
47
- return Promise.resolve(queue);
48
- },
49
-
50
- submitHttpRequest(payload) {
51
- return this.webex.request({
52
- method: 'POST',
53
- service: 'metrics',
54
- resource: 'clientmetrics',
55
- body: {
56
- metrics: payload
57
- }
58
- });
59
- }
60
- });
61
-
62
- export default CallDiagnosticEventsBatcher;
package/src/index.js DELETED
@@ -1,17 +0,0 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import '@webex/internal-plugin-device';
6
-
7
- import {registerInternalPlugin} from '@webex/webex-core';
8
-
9
- import Metrics from './metrics';
10
- import config from './config';
11
-
12
- registerInternalPlugin('metrics', Metrics, {
13
- config
14
- });
15
-
16
- export {default} from './metrics';
17
- export {config};
@@ -1,180 +0,0 @@
1
- /*!
2
- * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file.
3
- */
4
-
5
- import {assert} from '@webex/test-helper-chai';
6
- import FakeTimers from '@sinonjs/fake-timers';
7
- import Metrics, {config} from '@webex/internal-plugin-metrics';
8
- import MockWebex from '@webex/test-helper-mock-webex';
9
- import sinon from 'sinon';
10
- import {WebexHttpError} from '@webex/webex-core';
11
-
12
- function promiseTick(count) {
13
- let promise = Promise.resolve();
14
-
15
- while (count > 1) {
16
- promise = promise.then(() => promiseTick(1));
17
- count -= 1;
18
- }
19
-
20
- return promise;
21
- }
22
-
23
- describe('plugin-metrics', () => {
24
- describe('callDiagnosticEventsBatcher', () => {
25
- let webex;
26
-
27
- beforeEach(() => {
28
- webex = new MockWebex({
29
- children: {
30
- metrics: Metrics
31
- }
32
- });
33
-
34
- webex.config.metrics = config.metrics;
35
-
36
- webex.request = function (options) {
37
- return Promise.resolve({
38
- statusCode: 204,
39
- body: undefined,
40
- options
41
- });
42
- };
43
- sinon.spy(webex, 'request');
44
- });
45
-
46
- let clock;
47
-
48
- beforeEach(() => {
49
- clock = FakeTimers.install();
50
- });
51
-
52
- afterEach(() => {
53
- clock.uninstall();
54
- });
55
-
56
- describe('#request()', () => {
57
- describe('when the request completes successfully', () => {
58
- it('clears the queue', () => {
59
- clock.uninstall();
60
-
61
- return webex.internal.metrics.callDiagnosticEventsBatcher.request({
62
- type: 'diagnostic-event',
63
- eventPayload: {
64
- originTime: {
65
- triggered: 'mock triggered timestamp'
66
- }
67
- }
68
- })
69
- .then(() => {
70
- assert.calledOnce(webex.request);
71
- assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0);
72
- });
73
- });
74
- });
75
-
76
- describe('when the request fails due to network disconnect', () => {
77
- it('reenqueues the payload', () => {
78
- // sinon appears to have gap in its api where stub.onCall(n) doesn't
79
- // accept a function, so the following is more verbose than one might
80
- // desire
81
- webex.request = function () {
82
- // noop
83
- };
84
- let count = 0;
85
-
86
- sinon.stub(webex, 'request').callsFake((options) => {
87
- options.headers = {
88
- trackingid: count
89
- };
90
-
91
- count += 1;
92
- if (count < 9) {
93
- return Promise.reject(new WebexHttpError.NetworkOrCORSError({
94
- statusCode: 0,
95
- options
96
- }));
97
- }
98
-
99
- return Promise.resolve({
100
- statusCode: 204,
101
- body: undefined,
102
- options
103
- });
104
- });
105
-
106
- const promise = webex.internal.metrics.callDiagnosticEventsBatcher.request({
107
- type: 'diagnostic-event',
108
- eventPayload: {
109
- originTime: {
110
- triggered: 'mock triggered timestamp'
111
- }
112
- }
113
- });
114
-
115
- return promiseTick(50)
116
- .then(() => assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 1))
117
- .then(() => clock.tick(config.metrics.batcherWait))
118
- .then(() => assert.calledOnce(webex.request))
119
-
120
- .then(() => promiseTick(50))
121
- .then(() => clock.tick(1000))
122
- .then(() => promiseTick(50))
123
- .then(() => clock.tick(config.metrics.batcherWait))
124
- .then(() => assert.calledTwice(webex.request))
125
-
126
- .then(() => promiseTick(50))
127
- .then(() => clock.tick(2000))
128
- .then(() => promiseTick(50))
129
- .then(() => clock.tick(config.metrics.batcherWait))
130
- .then(() => assert.calledThrice(webex.request))
131
-
132
- .then(() => promiseTick(50))
133
- .then(() => clock.tick(4000))
134
- .then(() => promiseTick(50))
135
- .then(() => clock.tick(config.metrics.batcherWait))
136
- .then(() => assert.callCount(webex.request, 4))
137
-
138
- .then(() => promiseTick(50))
139
- .then(() => clock.tick(8000))
140
- .then(() => promiseTick(50))
141
- .then(() => clock.tick(config.metrics.batcherWait))
142
- .then(() => assert.callCount(webex.request, 5))
143
-
144
- .then(() => promiseTick(50))
145
- .then(() => clock.tick(16000))
146
- .then(() => promiseTick(50))
147
- .then(() => clock.tick(config.metrics.batcherWait))
148
- .then(() => assert.callCount(webex.request, 6))
149
-
150
- .then(() => promiseTick(50))
151
- .then(() => clock.tick(32000))
152
- .then(() => promiseTick(50))
153
- .then(() => clock.tick(config.metrics.batcherWait))
154
- .then(() => assert.callCount(webex.request, 7))
155
-
156
- .then(() => promiseTick(50))
157
- .then(() => clock.tick(32000))
158
- .then(() => promiseTick(50))
159
- .then(() => clock.tick(config.metrics.batcherWait))
160
- .then(() => assert.callCount(webex.request, 8))
161
-
162
- .then(() => promiseTick(50))
163
- .then(() => clock.tick(32000))
164
- .then(() => promiseTick(50))
165
- .then(() => clock.tick(config.metrics.batcherWait))
166
- .then(() => assert.callCount(webex.request, 9))
167
-
168
- .then(() => promiseTick(50))
169
- .then(() => assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0))
170
- .then(() => promise)
171
- .then(() => {
172
- assert.lengthOf(webex.request.args[1][0].body.metrics, 1, 'Reenqueuing the metric once did not increase the number of metrics to be submitted');
173
- assert.lengthOf(webex.request.args[2][0].body.metrics, 1, 'Reenqueuing the metric twice did not increase the number of metrics to be submitted');
174
- assert.lengthOf(webex.internal.metrics.callDiagnosticEventsBatcher.queue, 0);
175
- });
176
- });
177
- });
178
- });
179
- });
180
- });