@splitsoftware/splitio-commons 1.6.2-rc.5 → 1.6.2-rc.8

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/CHANGES.txt +3 -0
  2. package/cjs/evaluator/index.js +10 -11
  3. package/cjs/integrations/ga/GaToSplit.js +8 -5
  4. package/cjs/sdkClient/sdkClient.js +3 -1
  5. package/cjs/sdkFactory/index.js +2 -2
  6. package/cjs/sdkManager/index.js +3 -11
  7. package/cjs/storages/AbstractSplitsCacheAsync.js +7 -9
  8. package/cjs/storages/AbstractSplitsCacheSync.js +7 -9
  9. package/cjs/storages/dataLoader.js +1 -1
  10. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +5 -6
  11. package/cjs/storages/inMemory/SplitsCacheInMemory.js +7 -10
  12. package/cjs/storages/inRedis/ImpressionCountsCacheInRedis.js +10 -6
  13. package/cjs/storages/inRedis/SplitsCacheInRedis.js +15 -9
  14. package/cjs/storages/inRedis/index.js +4 -3
  15. package/cjs/storages/inRedis/uniqueKeysCacheInRedis.js +11 -7
  16. package/cjs/storages/pluggable/SplitsCachePluggable.js +14 -9
  17. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
  18. package/cjs/sync/polling/updaters/splitChangesUpdater.js +1 -1
  19. package/cjs/trackers/strategy/strategyOptimized.js +2 -1
  20. package/cjs/trackers/telemetryTracker.js +6 -0
  21. package/cjs/trackers/uniqueKeysTracker.js +8 -1
  22. package/esm/evaluator/index.js +10 -11
  23. package/esm/integrations/ga/GaToSplit.js +8 -5
  24. package/esm/sdkClient/sdkClient.js +3 -1
  25. package/esm/sdkFactory/index.js +2 -2
  26. package/esm/sdkManager/index.js +3 -11
  27. package/esm/storages/AbstractSplitsCacheAsync.js +7 -9
  28. package/esm/storages/AbstractSplitsCacheSync.js +7 -9
  29. package/esm/storages/dataLoader.js +1 -1
  30. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +5 -6
  31. package/esm/storages/inMemory/SplitsCacheInMemory.js +7 -10
  32. package/esm/storages/inRedis/ImpressionCountsCacheInRedis.js +10 -6
  33. package/esm/storages/inRedis/SplitsCacheInRedis.js +15 -9
  34. package/esm/storages/inRedis/index.js +4 -3
  35. package/esm/storages/inRedis/uniqueKeysCacheInRedis.js +11 -7
  36. package/esm/storages/pluggable/SplitsCachePluggable.js +14 -9
  37. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
  38. package/esm/sync/polling/updaters/splitChangesUpdater.js +1 -1
  39. package/esm/trackers/strategy/strategyOptimized.js +2 -1
  40. package/esm/trackers/telemetryTracker.js +6 -0
  41. package/esm/trackers/uniqueKeysTracker.js +8 -1
  42. package/package.json +1 -1
  43. package/src/evaluator/index.ts +8 -9
  44. package/src/integrations/ga/GaToSplit.ts +9 -5
  45. package/src/integrations/types.ts +2 -1
  46. package/src/sdkClient/sdkClient.ts +3 -1
  47. package/src/sdkFactory/index.ts +2 -2
  48. package/src/sdkManager/index.ts +3 -12
  49. package/src/storages/AbstractSplitsCacheAsync.ts +12 -14
  50. package/src/storages/AbstractSplitsCacheSync.ts +14 -16
  51. package/src/storages/dataLoader.ts +1 -1
  52. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +8 -10
  53. package/src/storages/inMemory/SplitsCacheInMemory.ts +10 -14
  54. package/src/storages/inRedis/ImpressionCountsCacheInRedis.ts +10 -6
  55. package/src/storages/inRedis/SplitsCacheInRedis.ts +21 -17
  56. package/src/storages/inRedis/index.ts +5 -4
  57. package/src/storages/inRedis/uniqueKeysCacheInRedis.ts +11 -8
  58. package/src/storages/pluggable/SplitsCachePluggable.ts +20 -17
  59. package/src/storages/types.ts +13 -13
  60. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +5 -6
  61. package/src/sync/polling/updaters/splitChangesUpdater.ts +2 -2
  62. package/src/trackers/strategy/strategyOptimized.ts +1 -1
  63. package/src/trackers/telemetryTracker.ts +7 -2
  64. package/src/trackers/types.ts +6 -0
  65. package/src/trackers/uniqueKeysTracker.ts +13 -2
  66. package/types/integrations/types.d.ts +2 -1
  67. package/types/storages/AbstractSplitsCacheAsync.d.ts +6 -5
  68. package/types/storages/AbstractSplitsCacheSync.d.ts +5 -5
  69. package/types/storages/inLocalStorage/SplitsCacheInLocal.d.ts +3 -3
  70. package/types/storages/inMemory/SplitsCacheInMemory.d.ts +3 -2
  71. package/types/storages/inRedis/ImpressionCountsCacheInRedis.d.ts +5 -4
  72. package/types/storages/inRedis/SplitsCacheInRedis.d.ts +6 -5
  73. package/types/storages/inRedis/uniqueKeysCacheInRedis.d.ts +5 -4
  74. package/types/storages/pluggable/SplitsCachePluggable.d.ts +6 -5
  75. package/types/storages/types.d.ts +13 -13
  76. package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -1
  77. package/types/trackers/types.d.ts +6 -0
  78. package/types/trackers/uniqueKeysTracker.d.ts +1 -1
package/CHANGES.txt CHANGED
@@ -1,3 +1,6 @@
1
+ 1.6.2 (September XXX, 2022)
2
+ - Updated storage implementations to improve the performance of split evaluations (i.e., `getTreatment(s)` method calls) when using the default storage in memory.
3
+
1
4
  1.6.1 (July 22, 2022)
2
5
  - Updated GoogleAnalyticsToSplit integration to validate `autoRequire` config parameter and avoid some wrong warning logs when mapping GA hit fields to Split event properties.
3
6
 
@@ -37,45 +37,44 @@ function evaluateFeature(log, key, splitName, attributes, storage) {
37
37
  }
38
38
  exports.evaluateFeature = evaluateFeature;
39
39
  function evaluateFeatures(log, key, splitNames, attributes, storage) {
40
- var stringifiedSplits;
40
+ var parsedSplits;
41
41
  try {
42
- stringifiedSplits = storage.splits.getSplits(splitNames);
42
+ parsedSplits = storage.splits.getSplits(splitNames);
43
43
  }
44
44
  catch (e) {
45
45
  // Exception on sync `getSplits` storage. Not possible ATM with InMemory and InLocal storages.
46
46
  return treatmentsException(splitNames);
47
47
  }
48
- return ((0, thenable_1.thenable)(stringifiedSplits)) ?
49
- stringifiedSplits.then(function (splits) { return getEvaluations(log, splitNames, splits, key, attributes, storage); })
48
+ return (0, thenable_1.thenable)(parsedSplits) ?
49
+ parsedSplits.then(function (splits) { return getEvaluations(log, splitNames, splits, key, attributes, storage); })
50
50
  .catch(function () {
51
51
  // Exception on async `getSplits` storage. For example, when the storage is redis or
52
52
  // pluggable and there is a connection issue and we can't retrieve the split to be evaluated
53
53
  return treatmentsException(splitNames);
54
54
  }) :
55
- getEvaluations(log, splitNames, stringifiedSplits, key, attributes, storage);
55
+ getEvaluations(log, splitNames, parsedSplits, key, attributes, storage);
56
56
  }
57
57
  exports.evaluateFeatures = evaluateFeatures;
58
- function getEvaluation(log, stringifiedSplit, key, attributes, storage) {
58
+ function getEvaluation(log, splitJSON, key, attributes, storage) {
59
59
  var evaluation = {
60
60
  treatment: constants_1.CONTROL,
61
61
  label: LabelsConstants.SPLIT_NOT_FOUND,
62
62
  config: null
63
63
  };
64
- if (stringifiedSplit) {
65
- var splitJSON_1 = JSON.parse(stringifiedSplit);
66
- var split_1 = Engine_1.Engine.parse(log, splitJSON_1, storage);
64
+ if (splitJSON) {
65
+ var split_1 = Engine_1.Engine.parse(log, splitJSON, storage);
67
66
  evaluation = split_1.getTreatment(key, attributes, evaluateFeature);
68
67
  // If the storage is async and the evaluated split uses segment, evaluation is thenable
69
68
  if ((0, thenable_1.thenable)(evaluation)) {
70
69
  return evaluation.then(function (result) {
71
70
  result.changeNumber = split_1.getChangeNumber();
72
- result.config = splitJSON_1.configurations && splitJSON_1.configurations[result.treatment] || null;
71
+ result.config = splitJSON.configurations && splitJSON.configurations[result.treatment] || null;
73
72
  return result;
74
73
  });
75
74
  }
76
75
  else {
77
76
  evaluation.changeNumber = split_1.getChangeNumber(); // Always sync and optional
78
- evaluation.config = splitJSON_1.configurations && splitJSON_1.configurations[evaluation.treatment] || null;
77
+ evaluation.config = splitJSON.configurations && splitJSON.configurations[evaluation.treatment] || null;
79
78
  }
80
79
  }
81
80
  return evaluation;
@@ -16,7 +16,7 @@ var logNameMapper = 'ga-to-split:mapper';
16
16
  * @param log Logger instance.
17
17
  * @param autoRequire If true, log error when auto-require script is not detected
18
18
  */
19
- function providePlugin(window, pluginName, pluginConstructor, log, autoRequire) {
19
+ function providePlugin(window, pluginName, pluginConstructor, log, autoRequire, telemetryTracker) {
20
20
  // get reference to global command queue. Init it if not defined yet.
21
21
  var gaAlias = window.GoogleAnalyticsObject || 'ga';
22
22
  window[gaAlias] = window[gaAlias] || function () {
@@ -24,10 +24,13 @@ function providePlugin(window, pluginName, pluginConstructor, log, autoRequire)
24
24
  };
25
25
  // provides the plugin for use with analytics.js.
26
26
  window[gaAlias]('provide', pluginName, pluginConstructor);
27
- if (autoRequire && (!window[gaAlias].q || window[gaAlias].q.push === [].push)) {
28
- // Expecting spy on ga.q push method but not found
27
+ var hasAutoRequire = window[gaAlias].q && window[gaAlias].q.push !== [].push;
28
+ if (autoRequire && !hasAutoRequire) { // Expecting spy on ga.q push method but not found
29
29
  log.error(logPrefix + 'integration is configured to autorequire the splitTracker plugin, but the necessary script does not seem to have run. Please check the docs.');
30
30
  }
31
+ if (telemetryTracker && hasAutoRequire) {
32
+ telemetryTracker.addTag('integration:ga-autorequire');
33
+ }
31
34
  }
32
35
  // Default mapping: object used for building the default mapper from hits to Split events
33
36
  var defaultMapping = {
@@ -163,7 +166,7 @@ exports.fixEventTypeId = fixEventTypeId;
163
166
  * @param {object} log factory logger
164
167
  */
165
168
  function GaToSplit(sdkOptions, params) {
166
- var storage = params.storage, _a = params.settings, coreSettings = _a.core, log = _a.log;
169
+ var storage = params.storage, _a = params.settings, coreSettings = _a.core, log = _a.log, telemetryTracker = params.telemetryTracker;
167
170
  var defaultOptions = {
168
171
  prefix: exports.defaultPrefix,
169
172
  // We set default identities if key and TT are present in settings.core
@@ -249,6 +252,6 @@ function GaToSplit(sdkOptions, params) {
249
252
  return SplitTracker;
250
253
  }());
251
254
  // Register the plugin, even if config is invalid, since, if not provided, it will block `ga` command queue.
252
- providePlugin(window, 'splitTracker', SplitTracker, log, sdkOptions.autoRequire === true);
255
+ providePlugin(window, 'splitTracker', SplitTracker, log, sdkOptions.autoRequire === true, telemetryTracker);
253
256
  }
254
257
  exports.GaToSplit = GaToSplit;
@@ -9,7 +9,7 @@ var clientInputValidation_1 = require("./clientInputValidation");
9
9
  * Creates an Sdk client, i.e., a base client with status and destroy interface
10
10
  */
11
11
  function sdkClientFactory(params, isSharedClient) {
12
- var sdkReadinessManager = params.sdkReadinessManager, syncManager = params.syncManager, storage = params.storage, signalListener = params.signalListener, settings = params.settings, telemetryTracker = params.telemetryTracker;
12
+ var sdkReadinessManager = params.sdkReadinessManager, syncManager = params.syncManager, storage = params.storage, signalListener = params.signalListener, settings = params.settings, telemetryTracker = params.telemetryTracker, uniqueKeysTracker = params.uniqueKeysTracker;
13
13
  return (0, objectAssign_1.objectAssign)(
14
14
  // Proto-linkage of the readiness Event Emitter
15
15
  Object.create(sdkReadinessManager.sdkStatus),
@@ -31,6 +31,8 @@ function sdkClientFactory(params, isSharedClient) {
31
31
  // Release the API Key if it is the main client
32
32
  if (!isSharedClient)
33
33
  (0, apiKey_1.releaseApiKey)(settings.core.authorizationKey);
34
+ if (uniqueKeysTracker)
35
+ uniqueKeysTracker.stop();
34
36
  // Cleanup storage
35
37
  return storage.destroy();
36
38
  });
@@ -55,7 +55,8 @@ function sdkFactory(params) {
55
55
  };
56
56
  var storage = storageFactory(storageFactoryParams);
57
57
  // @TODO add support for dataloader: `if (params.dataLoader) params.dataLoader(storage);`
58
- var integrationsManager = integrationsManagerFactory && integrationsManagerFactory({ settings: settings, storage: storage });
58
+ var telemetryTracker = (0, telemetryTracker_1.telemetryTrackerFactory)(storage.telemetry, platform.now);
59
+ var integrationsManager = integrationsManagerFactory && integrationsManagerFactory({ settings: settings, storage: storage, telemetryTracker: telemetryTracker });
59
60
  var observer = impressionsObserverFactory();
60
61
  var uniqueKeysTracker = storageFactoryParams.impressionsMode === constants_3.NONE ? (0, uniqueKeysTracker_1.uniqueKeysTrackerFactory)(log, storage.uniqueKeys, filterAdapterFactory && filterAdapterFactory()) : undefined;
61
62
  var strategy;
@@ -71,7 +72,6 @@ function sdkFactory(params) {
71
72
  }
72
73
  var impressionsTracker = (0, impressionsTracker_1.impressionsTrackerFactory)(settings, storage.impressions, strategy, integrationsManager, storage.telemetry);
73
74
  var eventTracker = (0, eventTracker_1.eventTrackerFactory)(settings, storage.events, integrationsManager, storage.telemetry);
74
- var telemetryTracker = (0, telemetryTracker_1.telemetryTrackerFactory)(storage.telemetry, platform.now);
75
75
  // splitApi is used by SyncManager and Browser signal listener
76
76
  var splitApi = splitApiFactory && splitApiFactory(settings, platform, telemetryTracker);
77
77
  var ctx = { splitApi: splitApi, eventTracker: eventTracker, impressionsTracker: impressionsTracker, telemetryTracker: telemetryTracker, uniqueKeysTracker: uniqueKeysTracker, sdkReadinessManager: sdkReadinessManager, readiness: readiness, settings: settings, storage: storage, platform: platform };
@@ -15,15 +15,7 @@ function collectTreatments(splitObject) {
15
15
  // Then extract the treatments from the partitions
16
16
  return allTreatmentsCondition ? allTreatmentsCondition.partitions.map(function (v) { return v.treatment; }) : [];
17
17
  }
18
- function objectToView(json) {
19
- var splitObject;
20
- try {
21
- // @ts-expect-error
22
- splitObject = JSON.parse(json);
23
- }
24
- catch (e) {
25
- return null;
26
- }
18
+ function objectToView(splitObject) {
27
19
  if (!splitObject)
28
20
  return null;
29
21
  return {
@@ -35,9 +27,9 @@ function objectToView(json) {
35
27
  configs: splitObject.configurations || {}
36
28
  };
37
29
  }
38
- function objectsToViews(jsons) {
30
+ function objectsToViews(splitObjects) {
39
31
  var views = [];
40
- jsons.forEach(function (split) {
32
+ splitObjects.forEach(function (split) {
41
33
  var view = objectToView(split);
42
34
  if (view)
43
35
  views.push(view);
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AbstractSplitsCacheAsync = void 0;
4
+ var objectAssign_1 = require("../utils/lang/objectAssign");
4
5
  /**
5
6
  * This class provides a skeletal implementation of the ISplitsCacheAsync interface
6
7
  * to minimize the effort required to implement this interface.
@@ -34,15 +35,12 @@ var AbstractSplitsCacheAsync = /** @class */ (function () {
34
35
  AbstractSplitsCacheAsync.prototype.killLocally = function (name, defaultTreatment, changeNumber) {
35
36
  var _this = this;
36
37
  return this.getSplit(name).then(function (split) {
37
- if (split) {
38
- var parsedSplit = JSON.parse(split);
39
- if (!parsedSplit.changeNumber || parsedSplit.changeNumber < changeNumber) {
40
- parsedSplit.killed = true;
41
- parsedSplit.defaultTreatment = defaultTreatment;
42
- parsedSplit.changeNumber = changeNumber;
43
- var newSplit = JSON.stringify(parsedSplit);
44
- return _this.addSplit(name, newSplit);
45
- }
38
+ if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
39
+ var newSplit = (0, objectAssign_1.objectAssign)({}, split);
40
+ newSplit.killed = true;
41
+ newSplit.defaultTreatment = defaultTreatment;
42
+ newSplit.changeNumber = changeNumber;
43
+ return _this.addSplit(name, newSplit);
46
44
  }
47
45
  return false;
48
46
  }).catch(function () { return false; });
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.usesSegments = exports.AbstractSplitsCacheSync = void 0;
4
+ var objectAssign_1 = require("../utils/lang/objectAssign");
4
5
  /**
5
6
  * This class provides a skeletal implementation of the ISplitsCacheSync interface
6
7
  * to minimize the effort required to implement this interface.
@@ -47,15 +48,12 @@ var AbstractSplitsCacheSync = /** @class */ (function () {
47
48
  */
48
49
  AbstractSplitsCacheSync.prototype.killLocally = function (name, defaultTreatment, changeNumber) {
49
50
  var split = this.getSplit(name);
50
- if (split) {
51
- var parsedSplit = JSON.parse(split);
52
- if (!parsedSplit.changeNumber || parsedSplit.changeNumber < changeNumber) {
53
- parsedSplit.killed = true;
54
- parsedSplit.defaultTreatment = defaultTreatment;
55
- parsedSplit.changeNumber = changeNumber;
56
- var newSplit = JSON.stringify(parsedSplit);
57
- return this.addSplit(name, newSplit);
58
- }
51
+ if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
52
+ var newSplit = (0, objectAssign_1.objectAssign)({}, split);
53
+ newSplit.killed = true;
54
+ newSplit.defaultTreatment = defaultTreatment;
55
+ newSplit.changeNumber = changeNumber;
56
+ return this.addSplit(name, newSplit);
59
57
  }
60
58
  return false;
61
59
  };
@@ -35,7 +35,7 @@ function dataLoaderFactory(preloadedData) {
35
35
  storage.splits.clear();
36
36
  storage.splits.setChangeNumber(since);
37
37
  // splitsData in an object where the property is the split name and the pertaining value is a stringified json of its data
38
- storage.splits.addSplits(Object.keys(splitsData).map(function (splitName) { return [splitName, splitsData[splitName]]; }));
38
+ storage.splits.addSplits(Object.keys(splitsData).map(function (splitName) { return JSON.parse(splitsData[splitName]); }));
39
39
  // add mySegments data
40
40
  var mySegmentsData = preloadedData.mySegmentsData && preloadedData.mySegmentsData[userId];
41
41
  if (!mySegmentsData) {
@@ -96,9 +96,8 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
96
96
  var splitFromLocalStorage = localStorage.getItem(splitKey);
97
97
  var previousSplit = splitFromLocalStorage ? JSON.parse(splitFromLocalStorage) : null;
98
98
  this._decrementCounts(previousSplit);
99
- localStorage.setItem(splitKey, split);
100
- var parsedSplit = split ? JSON.parse(split) : null;
101
- this._incrementCounts(parsedSplit);
99
+ localStorage.setItem(splitKey, JSON.stringify(split));
100
+ this._incrementCounts(split);
102
101
  return true;
103
102
  }
104
103
  catch (e) {
@@ -110,8 +109,7 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
110
109
  try {
111
110
  var split = this.getSplit(name);
112
111
  localStorage.removeItem(this.keys.buildSplitKey(name));
113
- var parsedSplit = JSON.parse(split);
114
- this._decrementCounts(parsedSplit);
112
+ this._decrementCounts(split);
115
113
  return true;
116
114
  }
117
115
  catch (e) {
@@ -120,7 +118,8 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
120
118
  }
121
119
  };
122
120
  SplitsCacheInLocal.prototype.getSplit = function (name) {
123
- return localStorage.getItem(this.keys.buildSplitKey(name));
121
+ var item = localStorage.getItem(this.keys.buildSplitKey(name));
122
+ return item && JSON.parse(item);
124
123
  };
125
124
  SplitsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
126
125
  // when cache is ready but using a new split query, we must clear all split data
@@ -25,9 +25,8 @@ var SplitsCacheInMemory = /** @class */ (function (_super) {
25
25
  this.splitsWithSegmentsCount = 0;
26
26
  };
27
27
  SplitsCacheInMemory.prototype.addSplit = function (name, split) {
28
- var splitFromMemory = this.getSplit(name);
29
- if (splitFromMemory) { // We had this Split already
30
- var previousSplit = JSON.parse(splitFromMemory);
28
+ var previousSplit = this.getSplit(name);
29
+ if (previousSplit) { // We had this Split already
31
30
  if (previousSplit.trafficTypeName) {
32
31
  var previousTtName = previousSplit.trafficTypeName;
33
32
  this.ttCache[previousTtName]--;
@@ -38,19 +37,18 @@ var SplitsCacheInMemory = /** @class */ (function (_super) {
38
37
  this.splitsWithSegmentsCount--;
39
38
  }
40
39
  }
41
- var parsedSplit = JSON.parse(split);
42
- if (parsedSplit) {
40
+ if (split) {
43
41
  // Store the Split.
44
42
  this.splitsCache[name] = split;
45
43
  // Update TT cache
46
- var ttName = parsedSplit.trafficTypeName;
44
+ var ttName = split.trafficTypeName;
47
45
  if (ttName) { // safeguard
48
46
  if (!this.ttCache[ttName])
49
47
  this.ttCache[ttName] = 0;
50
48
  this.ttCache[ttName]++;
51
49
  }
52
50
  // Add to segments count for the new version of the Split
53
- if ((0, AbstractSplitsCacheSync_1.usesSegments)(parsedSplit))
51
+ if ((0, AbstractSplitsCacheSync_1.usesSegments)(split))
54
52
  this.splitsWithSegmentsCount++;
55
53
  return true;
56
54
  }
@@ -63,15 +61,14 @@ var SplitsCacheInMemory = /** @class */ (function (_super) {
63
61
  if (split) {
64
62
  // Delete the Split
65
63
  delete this.splitsCache[name];
66
- var parsedSplit = JSON.parse(split);
67
- var ttName = parsedSplit.trafficTypeName;
64
+ var ttName = split.trafficTypeName;
68
65
  if (ttName) { // safeguard
69
66
  this.ttCache[ttName]--; // Update tt cache
70
67
  if (!this.ttCache[ttName])
71
68
  delete this.ttCache[ttName];
72
69
  }
73
70
  // Update the segments count.
74
- if ((0, AbstractSplitsCacheSync_1.usesSegments)(parsedSplit))
71
+ if ((0, AbstractSplitsCacheSync_1.usesSegments)(split))
75
72
  this.splitsWithSegmentsCount--;
76
73
  return true;
77
74
  }
@@ -6,11 +6,13 @@ var ImpressionCountsCacheInMemory_1 = require("../inMemory/ImpressionCountsCache
6
6
  var constants_1 = require("./constants");
7
7
  var ImpressionCountsCacheInRedis = /** @class */ (function (_super) {
8
8
  (0, tslib_1.__extends)(ImpressionCountsCacheInRedis, _super);
9
- function ImpressionCountsCacheInRedis(log, key, redis, impressionCountsCacheSize) {
9
+ function ImpressionCountsCacheInRedis(log, key, redis, impressionCountsCacheSize, refreshRate) {
10
+ if (refreshRate === void 0) { refreshRate = constants_1.REFRESH_RATE; }
10
11
  var _this = _super.call(this, impressionCountsCacheSize) || this;
11
12
  _this.log = log;
12
13
  _this.key = key;
13
14
  _this.redis = redis;
15
+ _this.refreshRate = refreshRate;
14
16
  _this.onFullQueue = function () { _this.postImpressionCountsInRedis(); };
15
17
  return _this;
16
18
  }
@@ -18,6 +20,8 @@ var ImpressionCountsCacheInRedis = /** @class */ (function (_super) {
18
20
  var _this = this;
19
21
  var counts = this.pop();
20
22
  var keys = Object.keys(counts);
23
+ if (!keys)
24
+ return Promise.resolve(false);
21
25
  var pipeline = this.redis.pipeline();
22
26
  keys.forEach(function (key) {
23
27
  pipeline.hincrby(_this.key, key, counts[key]);
@@ -31,15 +35,15 @@ var ImpressionCountsCacheInRedis = /** @class */ (function (_super) {
31
35
  })
32
36
  .catch(function (err) {
33
37
  _this.log.error(constants_1.LOG_PREFIX + "Error in impression counts pipeline: " + err + ".");
34
- return false;
38
+ return Promise.resolve(false);
35
39
  });
36
40
  };
37
- ImpressionCountsCacheInRedis.prototype.start = function (refreshRate) {
38
- if (refreshRate === void 0) { refreshRate = constants_1.REFRESH_RATE; }
39
- this.handle = setInterval(this.postImpressionCountsInRedis.bind(this), refreshRate);
41
+ ImpressionCountsCacheInRedis.prototype.start = function () {
42
+ this.intervalId = setInterval(this.postImpressionCountsInRedis.bind(this), this.refreshRate);
40
43
  };
41
44
  ImpressionCountsCacheInRedis.prototype.stop = function () {
42
- clearInterval(this.handle);
45
+ clearInterval(this.intervalId);
46
+ return this.postImpressionCountsInRedis();
43
47
  };
44
48
  return ImpressionCountsCacheInRedis;
45
49
  }(ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory));
@@ -62,17 +62,17 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
62
62
  var splitKey = this.keys.buildSplitKey(name);
63
63
  return this.redis.get(splitKey).then(function (splitFromStorage) {
64
64
  // handling parsing errors
65
- var parsedPreviousSplit, parsedSplit;
65
+ var parsedPreviousSplit, newStringifiedSplit;
66
66
  try {
67
67
  parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
68
- parsedSplit = JSON.parse(split);
68
+ newStringifiedSplit = JSON.stringify(split);
69
69
  }
70
70
  catch (e) {
71
71
  throw new Error('Error parsing split definition: ' + e);
72
72
  }
73
73
  return Promise.all([
74
- _this.redis.set(splitKey, split),
75
- _this._incrementCounts(parsedSplit),
74
+ _this.redis.set(splitKey, newStringifiedSplit),
75
+ _this._incrementCounts(split),
76
76
  // If it's an update, we decrement the traffic type of the existing split,
77
77
  parsedPreviousSplit && _this._decrementCounts(parsedPreviousSplit)
78
78
  ]);
@@ -99,8 +99,7 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
99
99
  var _this = this;
100
100
  return this.getSplit(name).then(function (split) {
101
101
  if (split) {
102
- var parsedSplit = JSON.parse(split);
103
- _this._decrementCounts(parsedSplit);
102
+ _this._decrementCounts(split);
104
103
  }
105
104
  return _this.redis.del(_this.keys.buildSplitKey(name));
106
105
  });
@@ -123,7 +122,8 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
123
122
  this.log.error(constants_1.LOG_PREFIX + this.redisError);
124
123
  return Promise.reject(this.redisError);
125
124
  }
126
- return this.redis.get(this.keys.buildSplitKey(name));
125
+ return this.redis.get(this.keys.buildSplitKey(name))
126
+ .then(function (maybeSplit) { return maybeSplit && JSON.parse(maybeSplit); });
127
127
  };
128
128
  /**
129
129
  * Set till number.
@@ -158,7 +158,12 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
158
158
  */
159
159
  SplitsCacheInRedis.prototype.getAll = function () {
160
160
  var _this = this;
161
- return this.redis.keys(this.keys.searchPatternForSplitKeys()).then(function (listOfKeys) { return _this.redis.pipeline(listOfKeys.map(function (k) { return ['get', k]; })).exec(); }).then(processPipelineAnswer);
161
+ return this.redis.keys(this.keys.searchPatternForSplitKeys())
162
+ .then(function (listOfKeys) { return _this.redis.pipeline(listOfKeys.map(function (k) { return ['get', k]; })).exec(); })
163
+ .then(processPipelineAnswer)
164
+ .then(function (splitDefinitions) { return splitDefinitions.map(function (splitDefinition) {
165
+ return JSON.parse(splitDefinition);
166
+ }); });
162
167
  };
163
168
  /**
164
169
  * Get list of split names.
@@ -218,7 +223,8 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
218
223
  var keys = names.map(function (name) { return _this.keys.buildSplitKey(name); });
219
224
  return (_a = this.redis).mget.apply(_a, keys).then(function (splitDefinitions) {
220
225
  names.forEach(function (name, idx) {
221
- splits[name] = splitDefinitions[idx];
226
+ var split = splitDefinitions[idx];
227
+ splits[name] = split && JSON.parse(split);
222
228
  });
223
229
  return Promise.resolve(splits);
224
230
  })
@@ -47,11 +47,12 @@ function InRedisStorage(options) {
47
47
  // When using REDIS we should:
48
48
  // 1- Disconnect from the storage
49
49
  destroy: function () {
50
- redisClient.disconnect();
50
+ var promises = [];
51
51
  if (impressionCountsCache)
52
- impressionCountsCache.stop();
52
+ promises.push(impressionCountsCache.stop());
53
53
  if (uniqueKeysCache)
54
- uniqueKeysCache.stop();
54
+ promises.push(uniqueKeysCache.stop());
55
+ return Promise.all(promises).then(function () { redisClient.disconnect(); });
55
56
  // @TODO check that caches works as expected when redisClient is disconnected
56
57
  }
57
58
  };
@@ -8,19 +8,23 @@ var constants_1 = require("./constants");
8
8
  var constants_2 = require("./constants");
9
9
  var UniqueKeysCacheInRedis = /** @class */ (function (_super) {
10
10
  (0, tslib_1.__extends)(UniqueKeysCacheInRedis, _super);
11
- function UniqueKeysCacheInRedis(log, key, redis, uniqueKeysQueueSize) {
11
+ function UniqueKeysCacheInRedis(log, key, redis, uniqueKeysQueueSize, refreshRate) {
12
12
  if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = constants_1.DEFAULT_CACHE_SIZE; }
13
+ if (refreshRate === void 0) { refreshRate = constants_1.REFRESH_RATE; }
13
14
  var _this = _super.call(this, uniqueKeysQueueSize) || this;
14
15
  _this.log = log;
15
16
  _this.key = key;
16
17
  _this.redis = redis;
18
+ _this.refreshRate = refreshRate;
17
19
  _this.onFullQueue = function () { _this.postUniqueKeysInRedis(); };
18
20
  return _this;
19
21
  }
20
22
  UniqueKeysCacheInRedis.prototype.postUniqueKeysInRedis = function () {
21
23
  var _this = this;
22
- var pipeline = this.redis.pipeline();
23
24
  var featureNames = Object.keys(this.uniqueKeysTracker);
25
+ if (!featureNames)
26
+ return Promise.resolve(false);
27
+ var pipeline = this.redis.pipeline();
24
28
  for (var i = 0; i < featureNames.length; i++) {
25
29
  var featureName = featureNames[i];
26
30
  var featureKeys = (0, sets_1.setToArray)(this.uniqueKeysTracker[featureName]);
@@ -40,15 +44,15 @@ var UniqueKeysCacheInRedis = /** @class */ (function (_super) {
40
44
  })
41
45
  .catch(function (err) {
42
46
  _this.log.error(constants_2.LOG_PREFIX + "Error in uniqueKeys pipeline: " + err + ".");
43
- return false;
47
+ return Promise.resolve(false);
44
48
  });
45
49
  };
46
- UniqueKeysCacheInRedis.prototype.start = function (refreshRate) {
47
- if (refreshRate === void 0) { refreshRate = constants_1.REFRESH_RATE; }
48
- this.handle = setInterval(this.postUniqueKeysInRedis.bind(this), refreshRate);
50
+ UniqueKeysCacheInRedis.prototype.start = function () {
51
+ this.intervalId = setInterval(this.postUniqueKeysInRedis.bind(this), this.refreshRate);
49
52
  };
50
53
  UniqueKeysCacheInRedis.prototype.stop = function () {
51
- clearInterval(this.handle);
54
+ clearInterval(this.intervalId);
55
+ return this.postUniqueKeysInRedis();
52
56
  };
53
57
  return UniqueKeysCacheInRedis;
54
58
  }(uniqueKeysCacheInMemory_1.UniqueKeysCacheInMemory));
@@ -49,17 +49,17 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
49
49
  var splitKey = this.keys.buildSplitKey(name);
50
50
  return this.wrapper.get(splitKey).then(function (splitFromStorage) {
51
51
  // handling parsing error
52
- var parsedPreviousSplit, parsedSplit;
52
+ var parsedPreviousSplit, stringifiedNewSplit;
53
53
  try {
54
54
  parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
55
- parsedSplit = JSON.parse(split);
55
+ stringifiedNewSplit = JSON.stringify(split);
56
56
  }
57
57
  catch (e) {
58
58
  throw new Error('Error parsing split definition: ' + e);
59
59
  }
60
60
  return Promise.all([
61
- _this.wrapper.set(splitKey, split),
62
- _this._incrementCounts(parsedSplit),
61
+ _this.wrapper.set(splitKey, stringifiedNewSplit),
62
+ _this._incrementCounts(split),
63
63
  // If it's an update, we decrement the traffic type and segment count of the existing split,
64
64
  parsedPreviousSplit && _this._decrementCounts(parsedPreviousSplit)
65
65
  ]);
@@ -83,8 +83,7 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
83
83
  var _this = this;
84
84
  return this.getSplit(name).then(function (split) {
85
85
  if (split) {
86
- var parsedSplit = JSON.parse(split);
87
- _this._decrementCounts(parsedSplit);
86
+ _this._decrementCounts(split);
88
87
  }
89
88
  return _this.wrapper.del(_this.keys.buildSplitKey(name));
90
89
  });
@@ -104,7 +103,8 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
104
103
  * or rejected if wrapper operation fails.
105
104
  */
106
105
  SplitsCachePluggable.prototype.getSplit = function (name) {
107
- return this.wrapper.get(this.keys.buildSplitKey(name));
106
+ return this.wrapper.get(this.keys.buildSplitKey(name))
107
+ .then(function (maybeSplit) { return maybeSplit && JSON.parse(maybeSplit); });
108
108
  };
109
109
  /**
110
110
  * Get list of splits.
@@ -117,7 +117,8 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
117
117
  return this.wrapper.getMany(keys).then(function (splitDefinitions) {
118
118
  var splits = {};
119
119
  names.forEach(function (name, idx) {
120
- splits[name] = splitDefinitions[idx];
120
+ var split = splitDefinitions[idx];
121
+ splits[name] = split && JSON.parse(split);
121
122
  });
122
123
  return Promise.resolve(splits);
123
124
  });
@@ -129,7 +130,11 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
129
130
  */
130
131
  SplitsCachePluggable.prototype.getAll = function () {
131
132
  var _this = this;
132
- return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix()).then(function (listOfKeys) { return Promise.all(listOfKeys.map(_this.wrapper.get)); });
133
+ return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix())
134
+ .then(function (listOfKeys) { return _this.wrapper.getMany(listOfKeys); })
135
+ .then(function (splitDefinitions) { return splitDefinitions.map(function (splitDefinition) {
136
+ return JSON.parse(splitDefinition);
137
+ }); });
133
138
  };
134
139
  /**
135
140
  * Get list of split names.
@@ -27,8 +27,7 @@ function fromObjectUpdaterFactory(splitsParser, storage, readiness, settings) {
27
27
  log.debug(constants_3.SYNC_OFFLINE_DATA, [JSON.stringify(splitsMock)]);
28
28
  (0, lang_1.forOwn)(splitsMock, function (val, name) {
29
29
  splits.push([
30
- name,
31
- JSON.stringify({
30
+ name, {
32
31
  name: name,
33
32
  status: 'ACTIVE',
34
33
  killed: false,
@@ -37,7 +36,7 @@ function fromObjectUpdaterFactory(splitsParser, storage, readiness, settings) {
37
36
  conditions: val.conditions || [],
38
37
  configurations: val.configurations,
39
38
  trafficTypeName: val.trafficTypeName
40
- })
39
+ }
41
40
  ]);
42
41
  });
43
42
  return Promise.all([
@@ -41,7 +41,7 @@ function computeSplitsMutation(entries) {
41
41
  var segments = new sets_1._Set();
42
42
  var computed = entries.reduce(function (accum, split) {
43
43
  if (split.status === 'ACTIVE') {
44
- accum.added.push([split.name, JSON.stringify(split)]);
44
+ accum.added.push([split.name, split]);
45
45
  parseSegments(split).forEach(function (segmentName) {
46
46
  segments.add(segmentName);
47
47
  });
@@ -17,7 +17,8 @@ function strategyOptimizedFactory(impressionsObserver, impressionsCounter) {
17
17
  impression.pt = impressionsObserver.testAndSet(impression);
18
18
  var now = Date.now();
19
19
  // Increments impression counter per featureName
20
- impressionsCounter.track(impression.feature, now, 1);
20
+ if (impression.pt)
21
+ impressionsCounter.track(impression.feature, now, 1);
21
22
  // Checks if the impression should be added in queue to be sent
22
23
  if (!impression.pt || impression.pt < (0, time_1.truncateTimeFrame)(now)) {
23
24
  impressionsToStore.push(impression);
@@ -49,6 +49,11 @@ function telemetryTrackerFactory(telemetryCache, now) {
49
49
  if (e === constants_1.TOKEN_REFRESH)
50
50
  telemetryCache.recordTokenRefreshes();
51
51
  }
52
+ },
53
+ addTag: function (tag) {
54
+ // @ts-ignore
55
+ if (telemetryCache.addTag)
56
+ telemetryCache.addTag(tag);
52
57
  }
53
58
  };
54
59
  }
@@ -59,6 +64,7 @@ function telemetryTrackerFactory(telemetryCache, now) {
59
64
  trackHttp: noopTrack,
60
65
  sessionLength: function () { },
61
66
  streamingEvent: function () { },
67
+ addTag: function () { }
62
68
  };
63
69
  }
64
70
  }