@splitsoftware/splitio-commons 1.6.2-rc.6 → 1.6.2-rc.7

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 (135) hide show
  1. package/CHANGES.txt +3 -0
  2. package/cjs/consent/sdkUserConsent.js +2 -2
  3. package/cjs/listeners/browser.js +2 -1
  4. package/cjs/logger/constants.js +2 -1
  5. package/cjs/sdkClient/sdkClient.js +3 -1
  6. package/cjs/sdkFactory/index.js +26 -5
  7. package/cjs/services/splitApi.js +20 -0
  8. package/cjs/storages/KeyBuilderSS.js +6 -0
  9. package/cjs/storages/inLocalStorage/index.js +4 -0
  10. package/cjs/storages/inMemory/ImpressionCountsCacheInMemory.js +12 -1
  11. package/cjs/storages/inMemory/InMemoryStorage.js +5 -1
  12. package/cjs/storages/inMemory/InMemoryStorageCS.js +5 -1
  13. package/cjs/storages/inMemory/uniqueKeysCacheInMemory.js +73 -0
  14. package/cjs/storages/inMemory/uniqueKeysCacheInMemoryCS.js +78 -0
  15. package/cjs/storages/inRedis/ImpressionCountsCacheInRedis.js +49 -0
  16. package/cjs/storages/inRedis/constants.js +4 -1
  17. package/cjs/storages/inRedis/index.js +15 -1
  18. package/cjs/storages/inRedis/uniqueKeysCacheInRedis.js +56 -0
  19. package/cjs/sync/submitters/submitterManager.js +3 -0
  20. package/cjs/sync/submitters/telemetrySubmitter.js +1 -0
  21. package/cjs/sync/submitters/uniqueKeysSubmitter.js +26 -0
  22. package/cjs/trackers/impressionsTracker.js +22 -41
  23. package/cjs/trackers/strategy/strategyDebug.js +25 -0
  24. package/cjs/trackers/strategy/strategyNone.js +29 -0
  25. package/cjs/trackers/strategy/strategyOptimized.js +35 -0
  26. package/cjs/trackers/uniqueKeysTracker.js +38 -0
  27. package/cjs/utils/constants/index.js +4 -2
  28. package/cjs/utils/settingsValidation/impressionsMode.js +2 -2
  29. package/cjs/utils/settingsValidation/index.js +4 -0
  30. package/esm/consent/sdkUserConsent.js +2 -2
  31. package/esm/listeners/browser.js +3 -2
  32. package/esm/logger/constants.js +1 -0
  33. package/esm/sdkClient/sdkClient.js +3 -1
  34. package/esm/sdkFactory/index.js +26 -5
  35. package/esm/services/splitApi.js +20 -0
  36. package/esm/storages/KeyBuilderSS.js +6 -0
  37. package/esm/storages/inLocalStorage/index.js +5 -1
  38. package/esm/storages/inMemory/ImpressionCountsCacheInMemory.js +12 -1
  39. package/esm/storages/inMemory/InMemoryStorage.js +6 -2
  40. package/esm/storages/inMemory/InMemoryStorageCS.js +6 -2
  41. package/esm/storages/inMemory/uniqueKeysCacheInMemory.js +70 -0
  42. package/esm/storages/inMemory/uniqueKeysCacheInMemoryCS.js +75 -0
  43. package/esm/storages/inRedis/ImpressionCountsCacheInRedis.js +46 -0
  44. package/esm/storages/inRedis/constants.js +3 -0
  45. package/esm/storages/inRedis/index.js +16 -2
  46. package/esm/storages/inRedis/uniqueKeysCacheInRedis.js +53 -0
  47. package/esm/sync/submitters/submitterManager.js +3 -0
  48. package/esm/sync/submitters/telemetrySubmitter.js +2 -1
  49. package/esm/sync/submitters/uniqueKeysSubmitter.js +22 -0
  50. package/esm/trackers/impressionsTracker.js +22 -41
  51. package/esm/trackers/strategy/strategyDebug.js +21 -0
  52. package/esm/trackers/strategy/strategyNone.js +25 -0
  53. package/esm/trackers/strategy/strategyOptimized.js +31 -0
  54. package/esm/trackers/uniqueKeysTracker.js +34 -0
  55. package/esm/utils/constants/index.js +2 -0
  56. package/esm/utils/settingsValidation/impressionsMode.js +3 -3
  57. package/esm/utils/settingsValidation/index.js +4 -0
  58. package/package.json +1 -1
  59. package/src/consent/sdkUserConsent.ts +2 -2
  60. package/src/listeners/browser.ts +3 -2
  61. package/src/logger/constants.ts +1 -0
  62. package/src/sdkClient/sdkClient.ts +3 -1
  63. package/src/sdkFactory/index.ts +29 -5
  64. package/src/sdkFactory/types.ts +7 -4
  65. package/src/services/splitApi.ts +22 -0
  66. package/src/services/types.ts +6 -0
  67. package/src/storages/AbstractSplitsCacheAsync.ts +0 -1
  68. package/src/storages/KeyBuilderSS.ts +8 -0
  69. package/src/storages/inLocalStorage/index.ts +4 -1
  70. package/src/storages/inMemory/ImpressionCountsCacheInMemory.ts +16 -1
  71. package/src/storages/inMemory/InMemoryStorage.ts +5 -2
  72. package/src/storages/inMemory/InMemoryStorageCS.ts +6 -2
  73. package/src/storages/inMemory/uniqueKeysCacheInMemory.ts +82 -0
  74. package/src/storages/inMemory/uniqueKeysCacheInMemoryCS.ts +88 -0
  75. package/src/storages/inRedis/ImpressionCountsCacheInRedis.ts +51 -0
  76. package/src/storages/inRedis/constants.ts +3 -0
  77. package/src/storages/inRedis/index.ts +12 -3
  78. package/src/storages/inRedis/uniqueKeysCacheInRedis.ts +63 -0
  79. package/src/storages/types.ts +30 -8
  80. package/src/sync/submitters/submitterManager.ts +2 -0
  81. package/src/sync/submitters/telemetrySubmitter.ts +4 -3
  82. package/src/sync/submitters/types.ts +20 -1
  83. package/src/sync/submitters/uniqueKeysSubmitter.ts +35 -0
  84. package/src/trackers/impressionsTracker.ts +27 -48
  85. package/src/trackers/strategy/strategyDebug.ts +28 -0
  86. package/src/trackers/strategy/strategyNone.ts +34 -0
  87. package/src/trackers/strategy/strategyOptimized.ts +42 -0
  88. package/src/trackers/types.ts +28 -0
  89. package/src/trackers/uniqueKeysTracker.ts +48 -0
  90. package/src/types.ts +5 -1
  91. package/src/utils/constants/index.ts +2 -0
  92. package/src/utils/settingsValidation/impressionsMode.ts +3 -3
  93. package/src/utils/settingsValidation/index.ts +5 -0
  94. package/types/logger/constants.d.ts +1 -0
  95. package/types/sdkClient/types.d.ts +18 -0
  96. package/types/sdkFactory/types.d.ts +4 -2
  97. package/types/services/types.d.ts +4 -0
  98. package/types/storages/KeyBuilderSS.d.ts +2 -0
  99. package/types/storages/inMemory/CountsCacheInMemory.d.ts +20 -0
  100. package/types/storages/inMemory/ImpressionCountsCacheInMemory.d.ts +5 -1
  101. package/types/storages/inMemory/LatenciesCacheInMemory.d.ts +20 -0
  102. package/types/storages/inMemory/uniqueKeysCacheInMemory.d.ts +35 -0
  103. package/types/storages/inMemory/uniqueKeysCacheInMemoryCS.d.ts +37 -0
  104. package/types/storages/inRedis/CountsCacheInRedis.d.ts +9 -0
  105. package/types/storages/inRedis/ImpressionCountsCacheInRedis.d.ts +14 -0
  106. package/types/storages/inRedis/LatenciesCacheInRedis.d.ts +9 -0
  107. package/types/storages/inRedis/constants.d.ts +3 -0
  108. package/types/storages/inRedis/uniqueKeysCacheInRedis.d.ts +15 -0
  109. package/types/storages/types.d.ts +18 -5
  110. package/types/sync/offline/LocalhostFromFile.d.ts +2 -0
  111. package/types/sync/offline/splitsParser/splitsParserFromFile.d.ts +2 -0
  112. package/types/sync/submitters/eventsSyncTask.d.ts +8 -0
  113. package/types/sync/submitters/impressionCountsSubmitterInRedis.d.ts +5 -0
  114. package/types/sync/submitters/impressionCountsSyncTask.d.ts +13 -0
  115. package/types/sync/submitters/impressionsSyncTask.d.ts +14 -0
  116. package/types/sync/submitters/metricsSyncTask.d.ts +12 -0
  117. package/types/sync/submitters/submitterSyncTask.d.ts +10 -0
  118. package/types/sync/submitters/types.d.ts +18 -1
  119. package/types/sync/submitters/uniqueKeysSubmitter.d.ts +5 -0
  120. package/types/sync/submitters/uniqueKeysSubmitterInRedis.d.ts +5 -0
  121. package/types/sync/syncTaskComposite.d.ts +5 -0
  122. package/types/trackers/filter/bloomFilter.d.ts +10 -0
  123. package/types/trackers/filter/dictionaryFilter.d.ts +8 -0
  124. package/types/trackers/filter/types.d.ts +5 -0
  125. package/types/trackers/impressionsTracker.d.ts +4 -6
  126. package/types/trackers/strategy/strategyDebug.d.ts +9 -0
  127. package/types/trackers/strategy/strategyNone.d.ts +10 -0
  128. package/types/trackers/strategy/strategyOptimized.d.ts +11 -0
  129. package/types/trackers/types.d.ts +23 -0
  130. package/types/trackers/uniqueKeysTracker.d.ts +13 -0
  131. package/types/types.d.ts +5 -1
  132. package/types/utils/constants/index.d.ts +2 -0
  133. package/types/utils/settingsValidation/index.d.ts +1 -0
  134. package/types/utils/timeTracker/index.d.ts +70 -0
  135. package/src/logger/.DS_Store +0 -0
@@ -0,0 +1,70 @@
1
+ import { setToArray, _Set } from '../../utils/lang/sets';
2
+ import { DEFAULT_CACHE_SIZE } from '../inRedis/constants';
3
+ var UniqueKeysCacheInMemory = /** @class */ (function () {
4
+ function UniqueKeysCacheInMemory(uniqueKeysQueueSize) {
5
+ if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = DEFAULT_CACHE_SIZE; }
6
+ this.uniqueTrackerSize = 0;
7
+ this.maxStorage = uniqueKeysQueueSize;
8
+ this.uniqueKeysTracker = {};
9
+ }
10
+ UniqueKeysCacheInMemory.prototype.setOnFullQueueCb = function (cb) {
11
+ this.onFullQueue = cb;
12
+ };
13
+ /**
14
+ * Store unique keys in sequential order
15
+ * key: string = feature name.
16
+ * value: Set<string> = set of unique keys.
17
+ */
18
+ UniqueKeysCacheInMemory.prototype.track = function (key, featureName) {
19
+ if (!this.uniqueKeysTracker[featureName])
20
+ this.uniqueKeysTracker[featureName] = new _Set();
21
+ var tracker = this.uniqueKeysTracker[featureName];
22
+ if (!tracker.has(key)) {
23
+ tracker.add(key);
24
+ this.uniqueTrackerSize++;
25
+ }
26
+ if (this.uniqueTrackerSize >= this.maxStorage && this.onFullQueue) {
27
+ this.uniqueTrackerSize = 0;
28
+ this.onFullQueue();
29
+ }
30
+ };
31
+ /**
32
+ * Clear the data stored on the cache.
33
+ */
34
+ UniqueKeysCacheInMemory.prototype.clear = function () {
35
+ this.uniqueKeysTracker = {};
36
+ };
37
+ /**
38
+ * Pop the collected data, used as payload for posting.
39
+ */
40
+ UniqueKeysCacheInMemory.prototype.pop = function () {
41
+ var data = this.uniqueKeysTracker;
42
+ this.uniqueKeysTracker = {};
43
+ return this.fromUniqueKeysCollector(data);
44
+ };
45
+ /**
46
+ * Check if the cache is empty.
47
+ */
48
+ UniqueKeysCacheInMemory.prototype.isEmpty = function () {
49
+ return Object.keys(this.uniqueKeysTracker).length === 0;
50
+ };
51
+ /**
52
+ * Converts `uniqueKeys` data from cache into request payload for SS.
53
+ */
54
+ UniqueKeysCacheInMemory.prototype.fromUniqueKeysCollector = function (uniqueKeys) {
55
+ var payload = [];
56
+ var featureNames = Object.keys(uniqueKeys);
57
+ for (var i = 0; i < featureNames.length; i++) {
58
+ var featureName = featureNames[i];
59
+ var featureKeys = setToArray(uniqueKeys[featureName]);
60
+ var uniqueKeysPayload = {
61
+ f: featureName,
62
+ ks: featureKeys
63
+ };
64
+ payload.push(uniqueKeysPayload);
65
+ }
66
+ return { keys: payload };
67
+ };
68
+ return UniqueKeysCacheInMemory;
69
+ }());
70
+ export { UniqueKeysCacheInMemory };
@@ -0,0 +1,75 @@
1
+ import { setToArray, _Set } from '../../utils/lang/sets';
2
+ import { DEFAULT_CACHE_SIZE } from '../inRedis/constants';
3
+ var UniqueKeysCacheInMemoryCS = /** @class */ (function () {
4
+ /**
5
+ *
6
+ * @param impressionsQueueSize number of queued impressions to call onFullQueueCb.
7
+ * Default value is 0, that means no maximum value, in case we want to avoid this being triggered.
8
+ */
9
+ function UniqueKeysCacheInMemoryCS(uniqueKeysQueueSize) {
10
+ if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = DEFAULT_CACHE_SIZE; }
11
+ this.uniqueTrackerSize = 0;
12
+ this.maxStorage = uniqueKeysQueueSize;
13
+ this.uniqueKeysTracker = {};
14
+ }
15
+ UniqueKeysCacheInMemoryCS.prototype.setOnFullQueueCb = function (cb) {
16
+ this.onFullQueue = cb;
17
+ };
18
+ /**
19
+ * Store unique keys in sequential order
20
+ * key: string = key.
21
+ * value: HashSet<string> = set of split names.
22
+ */
23
+ UniqueKeysCacheInMemoryCS.prototype.track = function (key, featureName) {
24
+ if (!this.uniqueKeysTracker[key])
25
+ this.uniqueKeysTracker[key] = new _Set();
26
+ var tracker = this.uniqueKeysTracker[key];
27
+ if (!tracker.has(featureName)) {
28
+ tracker.add(featureName);
29
+ this.uniqueTrackerSize++;
30
+ }
31
+ if (this.uniqueTrackerSize >= this.maxStorage && this.onFullQueue) {
32
+ this.uniqueTrackerSize = 0;
33
+ this.onFullQueue();
34
+ }
35
+ };
36
+ /**
37
+ * Clear the data stored on the cache.
38
+ */
39
+ UniqueKeysCacheInMemoryCS.prototype.clear = function () {
40
+ this.uniqueKeysTracker = {};
41
+ };
42
+ /**
43
+ * Pop the collected data, used as payload for posting.
44
+ */
45
+ UniqueKeysCacheInMemoryCS.prototype.pop = function () {
46
+ var data = this.uniqueKeysTracker;
47
+ this.uniqueKeysTracker = {};
48
+ return this.fromUniqueKeysCollector(data);
49
+ };
50
+ /**
51
+ * Check if the cache is empty.
52
+ */
53
+ UniqueKeysCacheInMemoryCS.prototype.isEmpty = function () {
54
+ return Object.keys(this.uniqueKeysTracker).length === 0;
55
+ };
56
+ /**
57
+ * Converts `uniqueKeys` data from cache into request payload.
58
+ */
59
+ UniqueKeysCacheInMemoryCS.prototype.fromUniqueKeysCollector = function (uniqueKeys) {
60
+ var payload = [];
61
+ var featureKeys = Object.keys(uniqueKeys);
62
+ for (var k = 0; k < featureKeys.length; k++) {
63
+ var featureKey = featureKeys[k];
64
+ var featureNames = setToArray(uniqueKeys[featureKey]);
65
+ var uniqueKeysPayload = {
66
+ k: featureKey,
67
+ fs: featureNames
68
+ };
69
+ payload.push(uniqueKeysPayload);
70
+ }
71
+ return { keys: payload };
72
+ };
73
+ return UniqueKeysCacheInMemoryCS;
74
+ }());
75
+ export { UniqueKeysCacheInMemoryCS };
@@ -0,0 +1,46 @@
1
+ import { __extends } from "tslib";
2
+ import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
3
+ import { LOG_PREFIX, REFRESH_RATE, TTL_REFRESH } from './constants';
4
+ var ImpressionCountsCacheInRedis = /** @class */ (function (_super) {
5
+ __extends(ImpressionCountsCacheInRedis, _super);
6
+ function ImpressionCountsCacheInRedis(log, key, redis, impressionCountsCacheSize, refreshRate) {
7
+ if (refreshRate === void 0) { refreshRate = REFRESH_RATE; }
8
+ var _this = _super.call(this, impressionCountsCacheSize) || this;
9
+ _this.log = log;
10
+ _this.key = key;
11
+ _this.redis = redis;
12
+ _this.refreshRate = refreshRate;
13
+ _this.onFullQueue = function () { _this.postImpressionCountsInRedis(); };
14
+ return _this;
15
+ }
16
+ ImpressionCountsCacheInRedis.prototype.postImpressionCountsInRedis = function () {
17
+ var _this = this;
18
+ var counts = this.pop();
19
+ if (!counts)
20
+ return false;
21
+ var keys = Object.keys(counts);
22
+ var pipeline = this.redis.pipeline();
23
+ keys.forEach(function (key) {
24
+ pipeline.hincrby(_this.key, key, counts[key]);
25
+ });
26
+ return pipeline.exec()
27
+ .then(function (data) {
28
+ // If this is the creation of the key on Redis, set the expiration for it in 3600 seconds.
29
+ if (data.length && data.length === keys.length) {
30
+ return _this.redis.expire(_this.key, TTL_REFRESH);
31
+ }
32
+ })
33
+ .catch(function (err) {
34
+ _this.log.error(LOG_PREFIX + "Error in impression counts pipeline: " + err + ".");
35
+ return false;
36
+ });
37
+ };
38
+ ImpressionCountsCacheInRedis.prototype.start = function () {
39
+ this.intervalId = setInterval(this.postImpressionCountsInRedis.bind(this), this.refreshRate);
40
+ };
41
+ ImpressionCountsCacheInRedis.prototype.stop = function () {
42
+ clearInterval(this.intervalId);
43
+ };
44
+ return ImpressionCountsCacheInRedis;
45
+ }(ImpressionCountsCacheInMemory));
46
+ export { ImpressionCountsCacheInRedis };
@@ -1 +1,4 @@
1
1
  export var LOG_PREFIX = 'storage:redis: ';
2
+ export var DEFAULT_CACHE_SIZE = 30000;
3
+ export var REFRESH_RATE = 300000; // 300.000 ms = start after 5 mins
4
+ export var TTL_REFRESH = 3600; // 1hr
@@ -5,8 +5,10 @@ import { SplitsCacheInRedis } from './SplitsCacheInRedis';
5
5
  import { SegmentsCacheInRedis } from './SegmentsCacheInRedis';
6
6
  import { ImpressionsCacheInRedis } from './ImpressionsCacheInRedis';
7
7
  import { EventsCacheInRedis } from './EventsCacheInRedis';
8
- import { STORAGE_REDIS } from '../../utils/constants';
8
+ import { DEBUG, NONE, STORAGE_REDIS } from '../../utils/constants';
9
9
  import { TelemetryCacheInRedis } from './TelemetryCacheInRedis';
10
+ import { UniqueKeysCacheInRedis } from './uniqueKeysCacheInRedis';
11
+ import { ImpressionCountsCacheInRedis } from './ImpressionCountsCacheInRedis';
10
12
  /**
11
13
  * InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.
12
14
  * @see {@link https://www.npmjs.com/package/ioredis}
@@ -15,13 +17,19 @@ export function InRedisStorage(options) {
15
17
  if (options === void 0) { options = {}; }
16
18
  var prefix = validatePrefix(options.prefix);
17
19
  function InRedisStorageFactory(_a) {
18
- var log = _a.log, metadata = _a.metadata, onReadyCb = _a.onReadyCb;
20
+ var log = _a.log, metadata = _a.metadata, onReadyCb = _a.onReadyCb, impressionsMode = _a.impressionsMode, impressionCountsQueueSize = _a.impressionCountsQueueSize, impressionCountsRefreshRate = _a.impressionCountsRefreshRate, uniqueKeysCacheSize = _a.uniqueKeysCacheSize, uniqueKeysRefreshRate = _a.uniqueKeysRefreshRate;
19
21
  var keys = new KeyBuilderSS(prefix, metadata);
20
22
  var redisClient = new RedisAdapter(log, options.options || {});
21
23
  var telemetry = new TelemetryCacheInRedis(log, keys, redisClient);
24
+ var impressionCountsCache = impressionsMode !== DEBUG ? new ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient, impressionCountsQueueSize, impressionCountsRefreshRate) : undefined;
25
+ var uniqueKeysCache = impressionsMode === NONE ? new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient, uniqueKeysCacheSize, uniqueKeysRefreshRate) : undefined;
22
26
  // subscription to Redis connect event in order to emit SDK_READY event on consumer mode
23
27
  redisClient.on('connect', function () {
24
28
  onReadyCb();
29
+ if (impressionCountsCache)
30
+ impressionCountsCache.start();
31
+ if (uniqueKeysCache)
32
+ uniqueKeysCache.start();
25
33
  // Synchronize config
26
34
  telemetry.recordConfig();
27
35
  });
@@ -29,12 +37,18 @@ export function InRedisStorage(options) {
29
37
  splits: new SplitsCacheInRedis(log, keys, redisClient),
30
38
  segments: new SegmentsCacheInRedis(log, keys, redisClient),
31
39
  impressions: new ImpressionsCacheInRedis(log, keys.buildImpressionsKey(), redisClient, metadata),
40
+ impressionCounts: impressionCountsCache,
32
41
  events: new EventsCacheInRedis(log, keys.buildEventsKey(), redisClient, metadata),
33
42
  telemetry: telemetry,
43
+ uniqueKeys: uniqueKeysCache,
34
44
  // When using REDIS we should:
35
45
  // 1- Disconnect from the storage
36
46
  destroy: function () {
37
47
  redisClient.disconnect();
48
+ if (impressionCountsCache)
49
+ impressionCountsCache.stop();
50
+ if (uniqueKeysCache)
51
+ uniqueKeysCache.stop();
38
52
  // @TODO check that caches works as expected when redisClient is disconnected
39
53
  }
40
54
  };
@@ -0,0 +1,53 @@
1
+ import { __extends } from "tslib";
2
+ import { UniqueKeysCacheInMemory } from '../inMemory/uniqueKeysCacheInMemory';
3
+ import { setToArray } from '../../utils/lang/sets';
4
+ import { DEFAULT_CACHE_SIZE, REFRESH_RATE, TTL_REFRESH } from './constants';
5
+ import { LOG_PREFIX } from './constants';
6
+ var UniqueKeysCacheInRedis = /** @class */ (function (_super) {
7
+ __extends(UniqueKeysCacheInRedis, _super);
8
+ function UniqueKeysCacheInRedis(log, key, redis, uniqueKeysQueueSize, refreshRate) {
9
+ if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = DEFAULT_CACHE_SIZE; }
10
+ if (refreshRate === void 0) { refreshRate = REFRESH_RATE; }
11
+ var _this = _super.call(this, uniqueKeysQueueSize) || this;
12
+ _this.log = log;
13
+ _this.key = key;
14
+ _this.redis = redis;
15
+ _this.refreshRate = refreshRate;
16
+ _this.onFullQueue = function () { _this.postUniqueKeysInRedis(); };
17
+ return _this;
18
+ }
19
+ UniqueKeysCacheInRedis.prototype.postUniqueKeysInRedis = function () {
20
+ var _this = this;
21
+ var pipeline = this.redis.pipeline();
22
+ var featureNames = Object.keys(this.uniqueKeysTracker);
23
+ for (var i = 0; i < featureNames.length; i++) {
24
+ var featureName = featureNames[i];
25
+ var featureKeys = setToArray(this.uniqueKeysTracker[featureName]);
26
+ var uniqueKeysPayload = {
27
+ f: featureName,
28
+ ks: featureKeys
29
+ };
30
+ pipeline.rpush(this.key, JSON.stringify(uniqueKeysPayload));
31
+ }
32
+ this.clear();
33
+ return pipeline.exec()
34
+ .then(function (data) {
35
+ // If this is the creation of the key on Redis, set the expiration for it in 3600 seconds.
36
+ if (data.length && data.length === featureNames.length) {
37
+ return _this.redis.expire(_this.key, TTL_REFRESH);
38
+ }
39
+ })
40
+ .catch(function (err) {
41
+ _this.log.error(LOG_PREFIX + "Error in uniqueKeys pipeline: " + err + ".");
42
+ return false;
43
+ });
44
+ };
45
+ UniqueKeysCacheInRedis.prototype.start = function () {
46
+ this.intervalId = setInterval(this.postUniqueKeysInRedis.bind(this), this.refreshRate);
47
+ };
48
+ UniqueKeysCacheInRedis.prototype.stop = function () {
49
+ clearInterval(this.intervalId);
50
+ };
51
+ return UniqueKeysCacheInRedis;
52
+ }(UniqueKeysCacheInMemory));
53
+ export { UniqueKeysCacheInRedis };
@@ -2,6 +2,7 @@ import { eventsSubmitterFactory } from './eventsSubmitter';
2
2
  import { impressionsSubmitterFactory } from './impressionsSubmitter';
3
3
  import { impressionCountsSubmitterFactory } from './impressionCountsSubmitter';
4
4
  import { telemetrySubmitterFactory } from './telemetrySubmitter';
5
+ import { uniqueKeysSubmitterFactory } from './uniqueKeysSubmitter';
5
6
  export function submitterManagerFactory(params) {
6
7
  var submitters = [
7
8
  impressionsSubmitterFactory(params),
@@ -11,6 +12,8 @@ export function submitterManagerFactory(params) {
11
12
  if (impressionCountsSubmitter)
12
13
  submitters.push(impressionCountsSubmitter);
13
14
  var telemetrySubmitter = telemetrySubmitterFactory(params);
15
+ if (params.uniqueKeysTracker)
16
+ submitters.push(uniqueKeysSubmitterFactory(params));
14
17
  return {
15
18
  // `onlyTelemetry` true if SDK is created with userConsent not GRANTED
16
19
  start: function (onlyTelemetry) {
@@ -1,6 +1,6 @@
1
1
  var _a, _b, _c;
2
2
  import { submitterFactory, firstPushWindowDecorator } from './submitter';
3
- import { QUEUED, DEDUPED, DROPPED, CONSUMER_MODE, CONSUMER_ENUM, STANDALONE_MODE, CONSUMER_PARTIAL_MODE, STANDALONE_ENUM, CONSUMER_PARTIAL_ENUM, OPTIMIZED, DEBUG, DEBUG_ENUM, OPTIMIZED_ENUM, CONSENT_GRANTED, CONSENT_DECLINED, CONSENT_UNKNOWN } from '../../utils/constants';
3
+ import { QUEUED, DEDUPED, DROPPED, CONSUMER_MODE, CONSUMER_ENUM, STANDALONE_MODE, CONSUMER_PARTIAL_MODE, STANDALONE_ENUM, CONSUMER_PARTIAL_ENUM, OPTIMIZED, DEBUG, NONE, DEBUG_ENUM, OPTIMIZED_ENUM, NONE_ENUM, CONSENT_GRANTED, CONSENT_DECLINED, CONSENT_UNKNOWN } from '../../utils/constants';
4
4
  import { SDK_READY, SDK_READY_FROM_CACHE } from '../../readiness/constants';
5
5
  import { base } from '../../utils/settingsValidation';
6
6
  import { usedKeysMap } from '../../utils/inputValidation/apiKey';
@@ -46,6 +46,7 @@ var OPERATION_MODE_MAP = (_a = {},
46
46
  var IMPRESSIONS_MODE_MAP = (_b = {},
47
47
  _b[OPTIMIZED] = OPTIMIZED_ENUM,
48
48
  _b[DEBUG] = DEBUG_ENUM,
49
+ _b[NONE] = NONE_ENUM,
49
50
  _b);
50
51
  var USER_CONSENT_MAP = (_c = {},
51
52
  _c[CONSENT_UNKNOWN] = 1,
@@ -0,0 +1,22 @@
1
+ import { SUBMITTERS_PUSH_FULL_QUEUE } from '../../logger/constants';
2
+ import { submitterFactory } from './submitter';
3
+ var DATA_NAME = 'uniqueKeys';
4
+ /**
5
+ * Submitter that periodically posts impression counts
6
+ */
7
+ export function uniqueKeysSubmitterFactory(params) {
8
+ var _a = params.settings, log = _a.log, uniqueKeysRefreshRate = _a.scheduler.uniqueKeysRefreshRate, key = _a.core.key, _b = params.splitApi, postUniqueKeysBulkCs = _b.postUniqueKeysBulkCs, postUniqueKeysBulkSs = _b.postUniqueKeysBulkSs, uniqueKeys = params.storage.uniqueKeys;
9
+ var isClientSide = key !== undefined;
10
+ var postUniqueKeysBulk = isClientSide ? postUniqueKeysBulkCs : postUniqueKeysBulkSs;
11
+ var syncTask = submitterFactory(log, postUniqueKeysBulk, uniqueKeys, uniqueKeysRefreshRate, 'unique keys');
12
+ // register unique keys submitter to be executed when uniqueKeys cache is full
13
+ uniqueKeys.setOnFullQueueCb(function () {
14
+ if (syncTask.isRunning()) {
15
+ log.info(SUBMITTERS_PUSH_FULL_QUEUE, [DATA_NAME]);
16
+ syncTask.execute();
17
+ }
18
+ // If submitter is stopped (e.g., user consent declined or unknown, or app state offline), we don't send the data.
19
+ // Data will be sent when submitter is resumed.
20
+ });
21
+ return syncTask;
22
+ }
@@ -1,6 +1,5 @@
1
1
  import { objectAssign } from '../utils/lang/objectAssign';
2
2
  import { thenable } from '../utils/promise/thenable';
3
- import { truncateTimeFrame } from '../utils/time';
4
3
  import { IMPRESSIONS_TRACKER_SUCCESS, ERROR_IMPRESSIONS_TRACKER, ERROR_IMPRESSIONS_LISTENER } from '../logger/constants';
5
4
  import { CONSENT_DECLINED, DEDUPED, QUEUED } from '../utils/constants';
6
5
  /**
@@ -10,52 +9,34 @@ import { CONSENT_DECLINED, DEDUPED, QUEUED } from '../utils/constants';
10
9
  * @param metadata runtime metadata (ip, hostname and version)
11
10
  * @param impressionListener optional impression listener
12
11
  * @param integrationsManager optional integrations manager
13
- * @param observer optional impression observer. If provided, previous time (pt property) is included in impression instances
14
- * @param countsCache optional cache to save impressions count. If provided, impressions will be deduped (OPTIMIZED mode)
12
+ * @param strategy strategy for impressions tracking.
15
13
  */
16
- export function impressionsTrackerFactory(settings, impressionsCache, integrationsManager,
17
- // if observer is provided, it implies `shouldAddPreviousTime` flag (i.e., if impressions previous time should be added or not)
18
- observer,
19
- // if countsCache is provided, it implies `isOptimized` flag (i.e., if impressions should be deduped or not)
20
- countsCache, telemetryCache) {
14
+ export function impressionsTrackerFactory(settings, impressionsCache, strategy, integrationsManager, telemetryCache) {
21
15
  var log = settings.log, impressionListener = settings.impressionListener, _a = settings.runtime, ip = _a.ip, hostname = _a.hostname, version = settings.version;
22
16
  return {
23
17
  track: function (impressions, attributes) {
24
18
  if (settings.userConsent === CONSENT_DECLINED)
25
19
  return;
26
20
  var impressionsCount = impressions.length;
27
- var impressionsToStore = []; // Track only the impressions that are going to be stored
28
- // Wraps impressions to store and adds previousTime if it corresponds
29
- impressions.forEach(function (impression) {
30
- if (observer) {
31
- // Adds previous time if it is enabled
32
- impression.pt = observer.testAndSet(impression);
21
+ var _a = strategy.process(impressions), impressionsToStore = _a.impressionsToStore, impressionsToListener = _a.impressionsToListener, deduped = _a.deduped;
22
+ var impressionsToListenerCount = impressionsToListener.length;
23
+ if (impressionsToStore.length > 0) {
24
+ var res = impressionsCache.track(impressionsToStore);
25
+ // If we're on an async storage, handle error and log it.
26
+ if (thenable(res)) {
27
+ res.then(function () {
28
+ log.info(IMPRESSIONS_TRACKER_SUCCESS, [impressionsCount]);
29
+ }).catch(function (err) {
30
+ log.error(ERROR_IMPRESSIONS_TRACKER, [impressionsCount, err]);
31
+ });
33
32
  }
34
- var now = Date.now();
35
- if (countsCache) {
36
- // Increments impression counter per featureName
37
- countsCache.track(impression.feature, now, 1);
38
- }
39
- // Checks if the impression should be added in queue to be sent
40
- if (!countsCache || !impression.pt || impression.pt < truncateTimeFrame(now)) {
41
- impressionsToStore.push(impression);
42
- }
43
- });
44
- var res = impressionsCache.track(impressionsToStore);
45
- // If we're on an async storage, handle error and log it.
46
- if (thenable(res)) {
47
- res.then(function () {
48
- log.info(IMPRESSIONS_TRACKER_SUCCESS, [impressionsCount]);
49
- }).catch(function (err) {
50
- log.error(ERROR_IMPRESSIONS_TRACKER, [impressionsCount, err]);
51
- });
52
- }
53
- else {
54
- // Record when impressionsCache is sync only (standalone mode)
55
- // @TODO we are not dropping impressions on full queue yet, so DROPPED stats are not recorded
56
- if (telemetryCache) {
57
- telemetryCache.recordImpressionStats(QUEUED, impressionsToStore.length);
58
- telemetryCache.recordImpressionStats(DEDUPED, impressions.length - impressionsToStore.length);
33
+ else {
34
+ // Record when impressionsCache is sync only (standalone mode)
35
+ // @TODO we are not dropping impressions on full queue yet, so DROPPED stats are not recorded
36
+ if (telemetryCache) {
37
+ telemetryCache.recordImpressionStats(QUEUED, impressionsToStore.length);
38
+ telemetryCache.recordImpressionStats(DEDUPED, deduped);
39
+ }
59
40
  }
60
41
  }
61
42
  // @TODO next block might be handled by the integration manager. In that case, the metadata object doesn't need to be passed in the constructor
@@ -63,7 +44,7 @@ countsCache, telemetryCache) {
63
44
  var _loop_1 = function (i) {
64
45
  var impressionData = {
65
46
  // copy of impression, to avoid unexpected behaviour if modified by integrations or impressionListener
66
- impression: objectAssign({}, impressions[i]),
47
+ impression: objectAssign({}, impressionsToListener[i]),
67
48
  attributes: attributes,
68
49
  ip: ip,
69
50
  hostname: hostname,
@@ -83,7 +64,7 @@ countsCache, telemetryCache) {
83
64
  }
84
65
  }, 0);
85
66
  };
86
- for (var i = 0; i < impressionsCount; i++) {
67
+ for (var i = 0; i < impressionsToListenerCount; i++) {
87
68
  _loop_1(i);
88
69
  }
89
70
  }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Debug strategy for impressions tracker. Wraps impressions to store and adds previousTime if it corresponds
3
+ *
4
+ * @param impressionsObserver impression observer. Previous time (pt property) is included in impression instances
5
+ * @returns IStrategyResult
6
+ */
7
+ export function strategyDebugFactory(impressionsObserver) {
8
+ return {
9
+ process: function (impressions) {
10
+ impressions.forEach(function (impression) {
11
+ // Adds previous time if it is enabled
12
+ impression.pt = impressionsObserver.testAndSet(impression);
13
+ });
14
+ return {
15
+ impressionsToStore: impressions,
16
+ impressionsToListener: impressions,
17
+ deduped: 0
18
+ };
19
+ }
20
+ };
21
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * None strategy for impressions tracker.
3
+ *
4
+ * @param impressionsCounter cache to save impressions count. impressions will be deduped (OPTIMIZED mode)
5
+ * @param uniqueKeysTracker unique keys tracker in charge of tracking the unique keys per split.
6
+ * @returns IStrategyResult
7
+ */
8
+ export function strategyNoneFactory(impressionsCounter, uniqueKeysTracker) {
9
+ return {
10
+ process: function (impressions) {
11
+ impressions.forEach(function (impression) {
12
+ var now = Date.now();
13
+ // Increments impression counter per featureName
14
+ impressionsCounter.track(impression.feature, now, 1);
15
+ // Keep track by unique key
16
+ uniqueKeysTracker.track(impression.keyName, impression.feature);
17
+ });
18
+ return {
19
+ impressionsToStore: [],
20
+ impressionsToListener: impressions,
21
+ deduped: 0
22
+ };
23
+ }
24
+ };
25
+ }
@@ -0,0 +1,31 @@
1
+ import { truncateTimeFrame } from '../../utils/time';
2
+ /**
3
+ * Optimized strategy for impressions tracker. Wraps impressions to store and adds previousTime if it corresponds
4
+ *
5
+ * @param impressionsObserver impression observer. previous time (pt property) is included in impression instances
6
+ * @param impressionsCounter cache to save impressions count. impressions will be deduped (OPTIMIZED mode)
7
+ * @returns IStrategyResult
8
+ */
9
+ export function strategyOptimizedFactory(impressionsObserver, impressionsCounter) {
10
+ return {
11
+ process: function (impressions) {
12
+ var impressionsToStore = [];
13
+ impressions.forEach(function (impression) {
14
+ impression.pt = impressionsObserver.testAndSet(impression);
15
+ var now = Date.now();
16
+ // Increments impression counter per featureName
17
+ if (impression.pt)
18
+ impressionsCounter.track(impression.feature, now, 1);
19
+ // Checks if the impression should be added in queue to be sent
20
+ if (!impression.pt || impression.pt < truncateTimeFrame(now)) {
21
+ impressionsToStore.push(impression);
22
+ }
23
+ });
24
+ return {
25
+ impressionsToStore: impressionsToStore,
26
+ impressionsToListener: impressions,
27
+ deduped: impressions.length - impressionsToStore.length
28
+ };
29
+ }
30
+ };
31
+ }
@@ -0,0 +1,34 @@
1
+ import { LOG_PREFIX_UNIQUE_KEYS_TRACKER } from '../logger/constants';
2
+ var noopFilterAdapter = {
3
+ add: function () { return true; },
4
+ contains: function () { return true; },
5
+ clear: function () { }
6
+ };
7
+ /**
8
+ * Trackes uniques keys
9
+ * Unique Keys Tracker will be in charge of checking if the MTK was already sent to the BE in the last period
10
+ * or schedule to be sent; if not it will be added in an internal cache and sent in the next post.
11
+ *
12
+ * @param log Logger instance
13
+ * @param uniqueKeysCache cache to save unique keys
14
+ * @param filterAdapter filter adapter
15
+ */
16
+ export function uniqueKeysTrackerFactory(log, uniqueKeysCache, filterAdapter) {
17
+ if (filterAdapter === void 0) { filterAdapter = noopFilterAdapter; }
18
+ var intervalId;
19
+ if (filterAdapter.refreshRate) {
20
+ intervalId = setInterval(filterAdapter.clear, filterAdapter.refreshRate);
21
+ }
22
+ return {
23
+ track: function (key, featureName) {
24
+ if (!filterAdapter.add(key, featureName)) {
25
+ log.debug(LOG_PREFIX_UNIQUE_KEYS_TRACKER + "The feature " + featureName + " and key " + key + " exist in the filter");
26
+ return;
27
+ }
28
+ uniqueKeysCache.track(key, featureName);
29
+ },
30
+ stop: function () {
31
+ clearInterval(intervalId);
32
+ }
33
+ };
34
+ }
@@ -13,6 +13,7 @@ export var SPLIT_EVENT = 'EVENT';
13
13
  // Impression collection modes
14
14
  export var DEBUG = 'DEBUG';
15
15
  export var OPTIMIZED = 'OPTIMIZED';
16
+ export var NONE = 'NONE';
16
17
  // SDK Modes
17
18
  export var LOCALHOST_MODE = 'localhost';
18
19
  export var STANDALONE_MODE = 'standalone';
@@ -37,6 +38,7 @@ export var CONSUMER_ENUM = 1;
37
38
  export var CONSUMER_PARTIAL_ENUM = 2;
38
39
  export var OPTIMIZED_ENUM = 0;
39
40
  export var DEBUG_ENUM = 1;
41
+ export var NONE_ENUM = 2;
40
42
  export var SPLITS = 'sp';
41
43
  export var IMPRESSIONS = 'im';
42
44
  export var IMPRESSIONS_COUNT = 'ic';
@@ -1,10 +1,10 @@
1
1
  import { ERROR_INVALID_CONFIG_PARAM } from '../../logger/constants';
2
- import { DEBUG, OPTIMIZED } from '../constants';
2
+ import { DEBUG, OPTIMIZED, NONE } from '../constants';
3
3
  import { stringToUpperCase } from '../lang';
4
4
  export function validImpressionsMode(log, impressionsMode) {
5
5
  impressionsMode = stringToUpperCase(impressionsMode);
6
- if ([DEBUG, OPTIMIZED].indexOf(impressionsMode) > -1)
6
+ if ([DEBUG, OPTIMIZED, NONE].indexOf(impressionsMode) > -1)
7
7
  return impressionsMode;
8
- log.error(ERROR_INVALID_CONFIG_PARAM, ['impressionsMode', [DEBUG, OPTIMIZED], OPTIMIZED]);
8
+ log.error(ERROR_INVALID_CONFIG_PARAM, ['impressionsMode', [DEBUG, OPTIMIZED, NONE], OPTIMIZED]);
9
9
  return OPTIMIZED;
10
10
  }
@@ -31,6 +31,8 @@ export var base = {
31
31
  telemetryRefreshRate: 3600,
32
32
  // publish evaluations each 300 sec (default value for OPTIMIZED impressions mode)
33
33
  impressionsRefreshRate: 300,
34
+ // publish unique Keys each 900 sec (15 min)
35
+ uniqueKeysRefreshRate: 900,
34
36
  // fetch offline changes each 15 sec
35
37
  offlineRefreshRate: 15,
36
38
  // publish events every 60 seconds after the first flush
@@ -109,6 +111,8 @@ export function settingsValidation(config, validationParams) {
109
111
  scheduler.segmentsRefreshRate = fromSecondsToMillis(scheduler.segmentsRefreshRate);
110
112
  scheduler.offlineRefreshRate = fromSecondsToMillis(scheduler.offlineRefreshRate);
111
113
  scheduler.eventsPushRate = fromSecondsToMillis(scheduler.eventsPushRate);
114
+ scheduler.uniqueKeysRefreshRate = fromSecondsToMillis(scheduler.uniqueKeysRefreshRate);
115
+ scheduler.impressionCountsRefreshRate = fromSecondsToMillis(scheduler.impressionCountsRefreshRate);
112
116
  scheduler.telemetryRefreshRate = fromSecondsToMillis(validateMinValue('telemetryRefreshRate', scheduler.telemetryRefreshRate, 60));
113
117
  // Default impressionsRefreshRate for DEBUG mode is 60 secs
114
118
  if (get(config, 'scheduler.impressionsRefreshRate') === undefined && withDefaults.sync.impressionsMode === DEBUG)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.6.2-rc.6",
3
+ "version": "1.6.2-rc.7",
4
4
  "description": "Split Javascript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -40,8 +40,8 @@ export function createUserConsentAPI(params: ISdkFactoryContext) {
40
40
 
41
41
  // @ts-ignore, clear method is present in storage for standalone and partial consumer mode
42
42
  if (events.clear) events.clear(); // @ts-ignore
43
- if (impressions.clear) impressions.clear();
44
- if (impressionCounts) impressionCounts.clear();
43
+ if (impressions.clear) impressions.clear();// @ts-ignore
44
+ if (impressionCounts && impressionCounts.clear) impressionCounts.clear();
45
45
  }
46
46
  } else {
47
47
  log.info(USER_CONSENT_NOT_UPDATED, [newConsentStatus]);