@splitsoftware/splitio-commons 1.6.2-rc.0 → 1.6.2-rc.10

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 (265) hide show
  1. package/CHANGES.txt +4 -0
  2. package/cjs/consent/sdkUserConsent.js +2 -2
  3. package/cjs/evaluator/index.js +15 -16
  4. package/cjs/integrations/ga/GaToSplit.js +8 -5
  5. package/cjs/listeners/browser.js +2 -1
  6. package/cjs/logger/constants.js +2 -1
  7. package/cjs/sdkClient/client.js +19 -7
  8. package/cjs/sdkClient/sdkClient.js +3 -1
  9. package/cjs/sdkFactory/index.js +24 -7
  10. package/cjs/sdkManager/index.js +3 -11
  11. package/cjs/services/splitApi.js +24 -4
  12. package/cjs/storages/AbstractSplitsCacheAsync.js +8 -10
  13. package/cjs/storages/AbstractSplitsCacheSync.js +8 -10
  14. package/cjs/storages/KeyBuilderSS.js +54 -9
  15. package/cjs/storages/dataLoader.js +1 -1
  16. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +5 -7
  17. package/cjs/storages/inLocalStorage/index.js +5 -1
  18. package/cjs/storages/inMemory/ImpressionCountsCacheInMemory.js +12 -1
  19. package/cjs/storages/inMemory/InMemoryStorage.js +6 -2
  20. package/cjs/storages/inMemory/InMemoryStorageCS.js +6 -2
  21. package/cjs/storages/inMemory/SplitsCacheInMemory.js +7 -10
  22. package/cjs/storages/inMemory/TelemetryCacheInMemory.js +10 -5
  23. package/cjs/storages/inMemory/UniqueKeysCacheInMemory.js +73 -0
  24. package/cjs/storages/inMemory/UniqueKeysCacheInMemoryCS.js +78 -0
  25. package/cjs/storages/inRedis/EventsCacheInRedis.js +1 -1
  26. package/cjs/storages/inRedis/ImpressionCountsCacheInRedis.js +50 -0
  27. package/cjs/storages/inRedis/SplitsCacheInRedis.js +15 -9
  28. package/cjs/storages/inRedis/TelemetryCacheInRedis.js +100 -0
  29. package/cjs/storages/inRedis/UniqueKeysCacheInRedis.js +59 -0
  30. package/cjs/storages/inRedis/constants.js +4 -1
  31. package/cjs/storages/inRedis/index.js +17 -2
  32. package/cjs/storages/pluggable/ImpressionCountsCachePluggable.js +43 -0
  33. package/cjs/storages/pluggable/SplitsCachePluggable.js +14 -9
  34. package/cjs/storages/pluggable/TelemetryCachePluggable.js +126 -0
  35. package/cjs/storages/pluggable/UniqueKeysCachePluggable.js +50 -0
  36. package/cjs/storages/pluggable/index.js +42 -17
  37. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
  38. package/cjs/sync/polling/updaters/splitChangesUpdater.js +1 -1
  39. package/cjs/sync/submitters/submitterManager.js +3 -0
  40. package/cjs/sync/submitters/telemetrySubmitter.js +8 -4
  41. package/cjs/sync/submitters/uniqueKeysSubmitter.js +27 -0
  42. package/cjs/trackers/impressionsTracker.js +22 -41
  43. package/cjs/trackers/strategy/strategyDebug.js +25 -0
  44. package/cjs/trackers/strategy/strategyNone.js +29 -0
  45. package/cjs/trackers/strategy/strategyOptimized.js +35 -0
  46. package/cjs/trackers/telemetryTracker.js +6 -0
  47. package/cjs/trackers/uniqueKeysTracker.js +38 -0
  48. package/cjs/utils/constants/index.js +4 -2
  49. package/cjs/utils/lang/maps.js +15 -7
  50. package/cjs/utils/redis/RedisMock.js +31 -0
  51. package/cjs/utils/settingsValidation/impressionsMode.js +2 -2
  52. package/esm/consent/sdkUserConsent.js +2 -2
  53. package/esm/evaluator/index.js +15 -16
  54. package/esm/integrations/ga/GaToSplit.js +8 -5
  55. package/esm/listeners/browser.js +3 -2
  56. package/esm/logger/constants.js +1 -0
  57. package/esm/sdkClient/client.js +19 -7
  58. package/esm/sdkClient/sdkClient.js +3 -1
  59. package/esm/sdkFactory/index.js +24 -7
  60. package/esm/sdkManager/index.js +3 -11
  61. package/esm/services/splitApi.js +24 -4
  62. package/esm/storages/AbstractSplitsCacheAsync.js +8 -10
  63. package/esm/storages/AbstractSplitsCacheSync.js +8 -10
  64. package/esm/storages/KeyBuilderSS.js +50 -8
  65. package/esm/storages/dataLoader.js +1 -1
  66. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +5 -7
  67. package/esm/storages/inLocalStorage/index.js +6 -2
  68. package/esm/storages/inMemory/ImpressionCountsCacheInMemory.js +12 -1
  69. package/esm/storages/inMemory/InMemoryStorage.js +8 -4
  70. package/esm/storages/inMemory/InMemoryStorageCS.js +7 -3
  71. package/esm/storages/inMemory/SplitsCacheInMemory.js +7 -10
  72. package/esm/storages/inMemory/TelemetryCacheInMemory.js +9 -5
  73. package/esm/storages/inMemory/UniqueKeysCacheInMemory.js +70 -0
  74. package/esm/storages/inMemory/UniqueKeysCacheInMemoryCS.js +75 -0
  75. package/esm/storages/inRedis/EventsCacheInRedis.js +1 -1
  76. package/esm/storages/inRedis/ImpressionCountsCacheInRedis.js +47 -0
  77. package/esm/storages/inRedis/SplitsCacheInRedis.js +15 -9
  78. package/esm/storages/inRedis/TelemetryCacheInRedis.js +100 -0
  79. package/esm/storages/inRedis/UniqueKeysCacheInRedis.js +56 -0
  80. package/esm/storages/inRedis/constants.js +3 -0
  81. package/esm/storages/inRedis/index.js +18 -3
  82. package/esm/storages/pluggable/ImpressionCountsCachePluggable.js +40 -0
  83. package/esm/storages/pluggable/SplitsCachePluggable.js +14 -9
  84. package/esm/storages/pluggable/TelemetryCachePluggable.js +126 -0
  85. package/esm/storages/pluggable/UniqueKeysCachePluggable.js +47 -0
  86. package/esm/storages/pluggable/index.js +43 -18
  87. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
  88. package/esm/sync/polling/updaters/splitChangesUpdater.js +1 -1
  89. package/esm/sync/submitters/submitterManager.js +3 -0
  90. package/esm/sync/submitters/telemetrySubmitter.js +9 -5
  91. package/esm/sync/submitters/uniqueKeysSubmitter.js +23 -0
  92. package/esm/trackers/impressionsTracker.js +22 -41
  93. package/esm/trackers/strategy/strategyDebug.js +21 -0
  94. package/esm/trackers/strategy/strategyNone.js +25 -0
  95. package/esm/trackers/strategy/strategyOptimized.js +31 -0
  96. package/esm/trackers/telemetryTracker.js +6 -0
  97. package/esm/trackers/uniqueKeysTracker.js +34 -0
  98. package/esm/utils/constants/index.js +2 -0
  99. package/esm/utils/lang/maps.js +15 -7
  100. package/esm/utils/redis/RedisMock.js +28 -0
  101. package/esm/utils/settingsValidation/impressionsMode.js +3 -3
  102. package/package.json +1 -2
  103. package/src/consent/sdkUserConsent.ts +2 -2
  104. package/src/evaluator/index.ts +14 -15
  105. package/src/integrations/ga/GaToSplit.ts +9 -5
  106. package/src/integrations/types.ts +2 -1
  107. package/src/listeners/browser.ts +3 -2
  108. package/src/logger/constants.ts +1 -0
  109. package/src/sdkClient/client.ts +21 -8
  110. package/src/sdkClient/sdkClient.ts +3 -1
  111. package/src/sdkFactory/index.ts +28 -8
  112. package/src/sdkFactory/types.ts +7 -4
  113. package/src/sdkManager/index.ts +3 -12
  114. package/src/services/splitApi.ts +26 -4
  115. package/src/services/types.ts +8 -2
  116. package/src/storages/AbstractSplitsCacheAsync.ts +13 -15
  117. package/src/storages/AbstractSplitsCacheSync.ts +15 -17
  118. package/src/storages/KeyBuilderSS.ts +61 -9
  119. package/src/storages/dataLoader.ts +1 -1
  120. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +8 -11
  121. package/src/storages/inLocalStorage/index.ts +5 -2
  122. package/src/storages/inMemory/ImpressionCountsCacheInMemory.ts +16 -1
  123. package/src/storages/inMemory/InMemoryStorage.ts +7 -4
  124. package/src/storages/inMemory/InMemoryStorageCS.ts +6 -3
  125. package/src/storages/inMemory/SplitsCacheInMemory.ts +10 -14
  126. package/src/storages/inMemory/TelemetryCacheInMemory.ts +10 -6
  127. package/src/storages/inMemory/UniqueKeysCacheInMemory.ts +82 -0
  128. package/src/storages/inMemory/UniqueKeysCacheInMemoryCS.ts +88 -0
  129. package/src/storages/inRedis/EventsCacheInRedis.ts +1 -1
  130. package/src/storages/inRedis/ImpressionCountsCacheInRedis.ts +53 -0
  131. package/src/storages/inRedis/SplitsCacheInRedis.ts +21 -17
  132. package/src/storages/inRedis/TelemetryCacheInRedis.ts +122 -2
  133. package/src/storages/inRedis/UniqueKeysCacheInRedis.ts +65 -0
  134. package/src/storages/inRedis/constants.ts +3 -0
  135. package/src/storages/inRedis/index.ts +15 -5
  136. package/src/storages/pluggable/ImpressionCountsCachePluggable.ts +47 -0
  137. package/src/storages/pluggable/SplitsCachePluggable.ts +20 -17
  138. package/src/storages/pluggable/TelemetryCachePluggable.ts +147 -2
  139. package/src/storages/pluggable/UniqueKeysCachePluggable.ts +56 -0
  140. package/src/storages/pluggable/index.ts +44 -19
  141. package/src/storages/types.ts +50 -29
  142. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +5 -6
  143. package/src/sync/polling/updaters/splitChangesUpdater.ts +2 -2
  144. package/src/sync/submitters/submitterManager.ts +2 -0
  145. package/src/sync/submitters/telemetrySubmitter.ts +15 -8
  146. package/src/sync/submitters/types.ts +38 -7
  147. package/src/sync/submitters/uniqueKeysSubmitter.ts +36 -0
  148. package/src/trackers/impressionsTracker.ts +27 -48
  149. package/src/trackers/strategy/strategyDebug.ts +28 -0
  150. package/src/trackers/strategy/strategyNone.ts +34 -0
  151. package/src/trackers/strategy/strategyOptimized.ts +42 -0
  152. package/src/trackers/telemetryTracker.ts +7 -2
  153. package/src/trackers/types.ts +32 -0
  154. package/src/trackers/uniqueKeysTracker.ts +48 -0
  155. package/src/types.ts +1 -1
  156. package/src/utils/constants/index.ts +2 -0
  157. package/src/utils/lang/maps.ts +20 -8
  158. package/src/utils/redis/RedisMock.ts +33 -0
  159. package/src/utils/settingsValidation/impressionsMode.ts +3 -3
  160. package/src/utils/settingsValidation/index.ts +1 -0
  161. package/types/integrations/types.d.ts +2 -1
  162. package/types/logger/browser/{debugLogger.d.ts → DebugLogger.d.ts} +0 -0
  163. package/types/logger/browser/{errorLogger.d.ts → ErrorLogger.d.ts} +0 -0
  164. package/types/logger/browser/{infoLogger.d.ts → InfoLogger.d.ts} +0 -0
  165. package/types/logger/browser/{warnLogger.d.ts → WarnLogger.d.ts} +0 -0
  166. package/types/logger/constants.d.ts +1 -0
  167. package/types/sdkFactory/types.d.ts +4 -2
  168. package/types/services/types.d.ts +6 -2
  169. package/types/storages/AbstractSplitsCacheAsync.d.ts +7 -6
  170. package/types/storages/AbstractSplitsCacheSync.d.ts +6 -6
  171. package/types/storages/KeyBuilderSS.d.ts +9 -2
  172. package/types/storages/inLocalStorage/SplitsCacheInLocal.d.ts +3 -4
  173. package/types/storages/inMemory/ImpressionCountsCacheInMemory.d.ts +5 -1
  174. package/types/storages/inMemory/SplitsCacheInMemory.d.ts +3 -2
  175. package/types/storages/inMemory/TelemetryCacheInMemory.d.ts +6 -3
  176. package/types/storages/inMemory/uniqueKeysCacheInMemory.d.ts +35 -0
  177. package/types/storages/inMemory/uniqueKeysCacheInMemoryCS.d.ts +37 -0
  178. package/types/storages/inRedis/EventsCacheInRedis.d.ts +1 -1
  179. package/types/storages/inRedis/ImpressionCountsCacheInRedis.d.ts +14 -0
  180. package/types/storages/inRedis/SplitsCacheInRedis.d.ts +6 -5
  181. package/types/storages/inRedis/TelemetryCacheInRedis.d.ts +16 -1
  182. package/types/storages/inRedis/constants.d.ts +3 -0
  183. package/types/storages/inRedis/uniqueKeysCacheInRedis.d.ts +15 -0
  184. package/types/storages/pluggable/ImpressionCountsCachePluggable.d.ts +14 -0
  185. package/types/storages/pluggable/SplitsCachePluggable.d.ts +6 -5
  186. package/types/storages/pluggable/TelemetryCachePluggable.d.ts +17 -1
  187. package/types/storages/pluggable/UniqueKeysCachePluggable.d.ts +14 -0
  188. package/types/storages/types.d.ts +38 -25
  189. package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -1
  190. package/types/sync/submitters/telemetrySubmitter.d.ts +1 -1
  191. package/types/sync/submitters/types.d.ts +30 -7
  192. package/types/sync/submitters/uniqueKeysSubmitter.d.ts +0 -8
  193. package/types/trackers/impressionsTracker.d.ts +4 -6
  194. package/types/trackers/types.d.ts +27 -0
  195. package/types/trackers/uniqueKeysTracker.d.ts +3 -3
  196. package/types/types.d.ts +1 -1
  197. package/types/utils/constants/index.d.ts +2 -0
  198. package/types/utils/lang/maps.d.ts +6 -2
  199. package/types/utils/redis/RedisMock.d.ts +4 -0
  200. package/src/splitio.d.ts +0 -1602
  201. package/types/integrations/ga/GaToSplitPlugin.d.ts +0 -3
  202. package/types/integrations/ga/SplitToGaPlugin.d.ts +0 -4
  203. package/types/integrations/ga/autoRequire.d.ts +0 -4
  204. package/types/logger/codes.d.ts +0 -2
  205. package/types/logger/codesConstants.d.ts +0 -117
  206. package/types/logger/codesConstantsBrowser.d.ts +0 -2
  207. package/types/logger/codesConstantsNode.d.ts +0 -14
  208. package/types/logger/codesDebug.d.ts +0 -1
  209. package/types/logger/codesDebugBrowser.d.ts +0 -1
  210. package/types/logger/codesDebugNode.d.ts +0 -1
  211. package/types/logger/codesError.d.ts +0 -1
  212. package/types/logger/codesErrorNode.d.ts +0 -1
  213. package/types/logger/codesInfo.d.ts +0 -1
  214. package/types/logger/codesWarn.d.ts +0 -1
  215. package/types/logger/codesWarnNode.d.ts +0 -1
  216. package/types/logger/debugLogger.d.ts +0 -2
  217. package/types/logger/errorLogger.d.ts +0 -2
  218. package/types/logger/infoLogger.d.ts +0 -2
  219. package/types/logger/messages/debugBrowser.d.ts +0 -1
  220. package/types/logger/messages/debugNode.d.ts +0 -1
  221. package/types/logger/messages/errorNode.d.ts +0 -1
  222. package/types/logger/messages/warnNode.d.ts +0 -1
  223. package/types/logger/noopLogger.d.ts +0 -2
  224. package/types/logger/warnLogger.d.ts +0 -2
  225. package/types/sdkClient/types.d.ts +0 -18
  226. package/types/sdkFactory/userConsentProps.d.ts +0 -6
  227. package/types/sdkManager/sdkManagerMethod.d.ts +0 -6
  228. package/types/storages/getRegisteredSegments.d.ts +0 -10
  229. package/types/storages/inMemory/CountsCacheInMemory.d.ts +0 -20
  230. package/types/storages/inMemory/LatenciesCacheInMemory.d.ts +0 -20
  231. package/types/storages/inMemory/index.d.ts +0 -10
  232. package/types/storages/inRedis/CountsCacheInRedis.d.ts +0 -9
  233. package/types/storages/inRedis/LatenciesCacheInRedis.d.ts +0 -9
  234. package/types/storages/parseSegments.d.ts +0 -6
  235. package/types/sync/offline/LocalhostFromFile.d.ts +0 -2
  236. package/types/sync/offline/splitsParser/splitsParserFromFile.d.ts +0 -2
  237. package/types/sync/polling/syncTasks/splitsSyncTask.copy.d.ts +0 -35
  238. package/types/sync/polling/syncTasks/splitsSyncTask.morelikeoriginal.d.ts +0 -35
  239. package/types/sync/streaming/AuthClient/indexV1.d.ts +0 -12
  240. package/types/sync/streaming/AuthClient/indexV2.d.ts +0 -8
  241. package/types/sync/streaming/pushManagerCS.d.ts +0 -1
  242. package/types/sync/streaming/pushManagerNoUsers.d.ts +0 -13
  243. package/types/sync/streaming/pushManagerSS.d.ts +0 -1
  244. package/types/sync/submitters/eventsSyncTask.d.ts +0 -8
  245. package/types/sync/submitters/impressionCountsSyncTask.d.ts +0 -13
  246. package/types/sync/submitters/impressionsSyncTask.d.ts +0 -14
  247. package/types/sync/submitters/metricsSyncTask.d.ts +0 -12
  248. package/types/sync/submitters/submitterSyncTask.d.ts +0 -10
  249. package/types/sync/submitters/telemetrySyncTask.d.ts +0 -0
  250. package/types/sync/syncManagerFromFile.d.ts +0 -2
  251. package/types/sync/syncManagerFromObject.d.ts +0 -2
  252. package/types/sync/syncManagerOffline.d.ts +0 -9
  253. package/types/sync/syncTaskComposite.d.ts +0 -5
  254. package/types/trackers/telemetryRecorder.d.ts +0 -0
  255. package/types/utils/EventEmitter.d.ts +0 -4
  256. package/types/utils/consent.d.ts +0 -2
  257. package/types/utils/lang/errors.d.ts +0 -10
  258. package/types/utils/murmur3/commons.d.ts +0 -12
  259. package/types/utils/settingsValidation/buildMetadata.d.ts +0 -3
  260. package/types/utils/settingsValidation/localhost/index.d.ts +0 -9
  261. package/types/utils/settingsValidation/logger.d.ts +0 -11
  262. package/types/utils/settingsValidation/runtime/browser.d.ts +0 -2
  263. package/types/utils/settingsValidation/runtime/node.d.ts +0 -2
  264. package/types/utils/settingsValidation/userConsent.d.ts +0 -5
  265. package/types/utils/timeTracker/index.d.ts +0 -1
@@ -16,6 +16,7 @@ var InMemoryStorageCS_1 = require("../inMemory/InMemoryStorageCS");
16
16
  var constants_1 = require("./constants");
17
17
  var constants_2 = require("../../utils/constants");
18
18
  var TelemetryCacheInMemory_1 = require("../inMemory/TelemetryCacheInMemory");
19
+ var UniqueKeysCacheInMemoryCS_1 = require("../inMemory/UniqueKeysCacheInMemoryCS");
19
20
  /**
20
21
  * InLocal storage factory for standalone client-side SplitFactory
21
22
  */
@@ -37,13 +38,16 @@ function InLocalStorage(options) {
37
38
  impressions: new ImpressionsCacheInMemory_1.ImpressionsCacheInMemory(params.impressionsQueueSize),
38
39
  impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory() : undefined,
39
40
  events: new EventsCacheInMemory_1.EventsCacheInMemory(params.eventsQueueSize),
40
- telemetry: params.mode !== constants_2.LOCALHOST_MODE && (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)() ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory() : undefined,
41
+ telemetry: (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)(params) ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory() : undefined,
42
+ uniqueKeys: params.impressionsMode === constants_2.NONE ? new UniqueKeysCacheInMemoryCS_1.UniqueKeysCacheInMemoryCS() : undefined,
41
43
  destroy: function () {
44
+ var _a;
42
45
  this.splits = new SplitsCacheInMemory_1.SplitsCacheInMemory();
43
46
  this.segments = new MySegmentsCacheInMemory_1.MySegmentsCacheInMemory();
44
47
  this.impressions.clear();
45
48
  this.impressionCounts && this.impressionCounts.clear();
46
49
  this.events.clear();
50
+ (_a = this.uniqueKeys) === null || _a === void 0 ? void 0 : _a.clear();
47
51
  },
48
52
  // When using shared instanciation with MEMORY we reuse everything but segments (they are customer per key).
49
53
  shared: function (matchingKey) {
@@ -2,9 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImpressionCountsCacheInMemory = void 0;
4
4
  var time_1 = require("../../utils/time");
5
+ var constants_1 = require("../inRedis/constants");
5
6
  var ImpressionCountsCacheInMemory = /** @class */ (function () {
6
- function ImpressionCountsCacheInMemory() {
7
+ function ImpressionCountsCacheInMemory(impressionCountsCacheSize) {
8
+ if (impressionCountsCacheSize === void 0) { impressionCountsCacheSize = constants_1.DEFAULT_CACHE_SIZE; }
7
9
  this.cache = {};
10
+ this.cacheSize = 0;
11
+ this.maxStorage = impressionCountsCacheSize;
8
12
  }
9
13
  /**
10
14
  * Builds key to be stored in the cache with the featureName and the timeFrame truncated.
@@ -19,6 +23,13 @@ var ImpressionCountsCacheInMemory = /** @class */ (function () {
19
23
  var key = this._makeKey(featureName, timeFrame);
20
24
  var currentAmount = this.cache[key];
21
25
  this.cache[key] = currentAmount ? currentAmount + amount : amount;
26
+ if (this.onFullQueue) {
27
+ this.cacheSize = this.cacheSize + amount;
28
+ if (this.cacheSize >= this.maxStorage) {
29
+ this.onFullQueue();
30
+ this.cacheSize = 0;
31
+ }
32
+ }
22
33
  };
23
34
  /**
24
35
  * Pop the collected data, used as payload for posting.
@@ -8,6 +8,7 @@ var EventsCacheInMemory_1 = require("./EventsCacheInMemory");
8
8
  var ImpressionCountsCacheInMemory_1 = require("./ImpressionCountsCacheInMemory");
9
9
  var constants_1 = require("../../utils/constants");
10
10
  var TelemetryCacheInMemory_1 = require("./TelemetryCacheInMemory");
11
+ var UniqueKeysCacheInMemory_1 = require("./UniqueKeysCacheInMemory");
11
12
  /**
12
13
  * InMemory storage factory for standalone server-side SplitFactory
13
14
  *
@@ -18,16 +19,19 @@ function InMemoryStorageFactory(params) {
18
19
  splits: new SplitsCacheInMemory_1.SplitsCacheInMemory(),
19
20
  segments: new SegmentsCacheInMemory_1.SegmentsCacheInMemory(),
20
21
  impressions: new ImpressionsCacheInMemory_1.ImpressionsCacheInMemory(params.impressionsQueueSize),
21
- impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory() : undefined,
22
+ impressionCounts: params.impressionsMode !== constants_1.DEBUG ? new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory() : undefined,
22
23
  events: new EventsCacheInMemory_1.EventsCacheInMemory(params.eventsQueueSize),
23
- telemetry: params.mode !== constants_1.LOCALHOST_MODE ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory() : undefined,
24
+ telemetry: (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)(params) ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory() : undefined,
25
+ uniqueKeys: params.impressionsMode === constants_1.NONE ? new UniqueKeysCacheInMemory_1.UniqueKeysCacheInMemory() : undefined,
24
26
  // When using MEMORY we should clean all the caches to leave them empty
25
27
  destroy: function () {
28
+ var _a;
26
29
  this.splits.clear();
27
30
  this.segments.clear();
28
31
  this.impressions.clear();
29
32
  this.impressionCounts && this.impressionCounts.clear();
30
33
  this.events.clear();
34
+ (_a = this.uniqueKeys) === null || _a === void 0 ? void 0 : _a.clear();
31
35
  }
32
36
  };
33
37
  }
@@ -8,6 +8,7 @@ var EventsCacheInMemory_1 = require("./EventsCacheInMemory");
8
8
  var ImpressionCountsCacheInMemory_1 = require("./ImpressionCountsCacheInMemory");
9
9
  var constants_1 = require("../../utils/constants");
10
10
  var TelemetryCacheInMemory_1 = require("./TelemetryCacheInMemory");
11
+ var UniqueKeysCacheInMemoryCS_1 = require("./UniqueKeysCacheInMemoryCS");
11
12
  /**
12
13
  * InMemory storage factory for standalone client-side SplitFactory
13
14
  *
@@ -18,16 +19,19 @@ function InMemoryStorageCSFactory(params) {
18
19
  splits: new SplitsCacheInMemory_1.SplitsCacheInMemory(),
19
20
  segments: new MySegmentsCacheInMemory_1.MySegmentsCacheInMemory(),
20
21
  impressions: new ImpressionsCacheInMemory_1.ImpressionsCacheInMemory(params.impressionsQueueSize),
21
- impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory() : undefined,
22
+ impressionCounts: params.impressionsMode !== constants_1.DEBUG ? new ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory() : undefined,
22
23
  events: new EventsCacheInMemory_1.EventsCacheInMemory(params.eventsQueueSize),
23
- telemetry: params.mode !== constants_1.LOCALHOST_MODE && (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)() ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory() : undefined,
24
+ telemetry: (0, TelemetryCacheInMemory_1.shouldRecordTelemetry)(params) ? new TelemetryCacheInMemory_1.TelemetryCacheInMemory() : undefined,
25
+ uniqueKeys: params.impressionsMode === constants_1.NONE ? new UniqueKeysCacheInMemoryCS_1.UniqueKeysCacheInMemoryCS() : undefined,
24
26
  // When using MEMORY we should clean all the caches to leave them empty
25
27
  destroy: function () {
28
+ var _a;
26
29
  this.splits.clear();
27
30
  this.segments.clear();
28
31
  this.impressions.clear();
29
32
  this.impressionCounts && this.impressionCounts.clear();
30
33
  this.events.clear();
34
+ (_a = this.uniqueKeys) === null || _a === void 0 ? void 0 : _a.clear();
31
35
  },
32
36
  // When using shared instanciation with MEMORY we reuse everything but segments (they are unique per key)
33
37
  shared: function () {
@@ -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
  }
@@ -1,19 +1,24 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TelemetryCacheInMemory = exports.shouldRecordTelemetry = void 0;
3
+ exports.TelemetryCacheInMemory = exports.shouldRecordTelemetry = exports.newBuckets = exports.MAX_LATENCY_BUCKET_COUNT = void 0;
4
+ var constants_1 = require("../../utils/constants");
4
5
  var findLatencyIndex_1 = require("../findLatencyIndex");
5
6
  var MAX_STREAMING_EVENTS = 20;
6
7
  var MAX_TAGS = 10;
8
+ exports.MAX_LATENCY_BUCKET_COUNT = 23;
7
9
  function newBuckets() {
8
- // MAX_LATENCY_BUCKET_COUNT (length) is 23;
10
+ // MAX_LATENCY_BUCKET_COUNT (length) is 23
11
+ // Not using Array.fill for old browsers compatibility
9
12
  return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
10
13
  }
14
+ exports.newBuckets = newBuckets;
11
15
  var ACCEPTANCE_RANGE = 0.001;
12
16
  /**
13
- * Used on client-side. 0.1% of instances will track telemetry
17
+ * Record telemetry if mode is not localhost.
18
+ * All factory instances track telemetry on server-side, and 0.1% on client-side.
14
19
  */
15
- function shouldRecordTelemetry() {
16
- return Math.random() <= ACCEPTANCE_RANGE;
20
+ function shouldRecordTelemetry(params) {
21
+ return params.mode !== constants_1.LOCALHOST_MODE && (params.matchingKey === undefined || Math.random() <= ACCEPTANCE_RANGE);
17
22
  }
18
23
  exports.shouldRecordTelemetry = shouldRecordTelemetry;
19
24
  var TelemetryCacheInMemory = /** @class */ (function () {
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UniqueKeysCacheInMemory = void 0;
4
+ var sets_1 = require("../../utils/lang/sets");
5
+ var constants_1 = require("../inRedis/constants");
6
+ var UniqueKeysCacheInMemory = /** @class */ (function () {
7
+ function UniqueKeysCacheInMemory(uniqueKeysQueueSize) {
8
+ if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = constants_1.DEFAULT_CACHE_SIZE; }
9
+ this.uniqueTrackerSize = 0;
10
+ this.maxStorage = uniqueKeysQueueSize;
11
+ this.uniqueKeysTracker = {};
12
+ }
13
+ UniqueKeysCacheInMemory.prototype.setOnFullQueueCb = function (cb) {
14
+ this.onFullQueue = cb;
15
+ };
16
+ /**
17
+ * Store unique keys in sequential order
18
+ * key: string = feature name.
19
+ * value: Set<string> = set of unique keys.
20
+ */
21
+ UniqueKeysCacheInMemory.prototype.track = function (key, featureName) {
22
+ if (!this.uniqueKeysTracker[featureName])
23
+ this.uniqueKeysTracker[featureName] = new sets_1._Set();
24
+ var tracker = this.uniqueKeysTracker[featureName];
25
+ if (!tracker.has(key)) {
26
+ tracker.add(key);
27
+ this.uniqueTrackerSize++;
28
+ }
29
+ if (this.uniqueTrackerSize >= this.maxStorage && this.onFullQueue) {
30
+ this.uniqueTrackerSize = 0;
31
+ this.onFullQueue();
32
+ }
33
+ };
34
+ /**
35
+ * Clear the data stored on the cache.
36
+ */
37
+ UniqueKeysCacheInMemory.prototype.clear = function () {
38
+ this.uniqueKeysTracker = {};
39
+ };
40
+ /**
41
+ * Pop the collected data, used as payload for posting.
42
+ */
43
+ UniqueKeysCacheInMemory.prototype.pop = function () {
44
+ var data = this.uniqueKeysTracker;
45
+ this.uniqueKeysTracker = {};
46
+ return this.fromUniqueKeysCollector(data);
47
+ };
48
+ /**
49
+ * Check if the cache is empty.
50
+ */
51
+ UniqueKeysCacheInMemory.prototype.isEmpty = function () {
52
+ return Object.keys(this.uniqueKeysTracker).length === 0;
53
+ };
54
+ /**
55
+ * Converts `uniqueKeys` data from cache into request payload for SS.
56
+ */
57
+ UniqueKeysCacheInMemory.prototype.fromUniqueKeysCollector = function (uniqueKeys) {
58
+ var payload = [];
59
+ var featureNames = Object.keys(uniqueKeys);
60
+ for (var i = 0; i < featureNames.length; i++) {
61
+ var featureName = featureNames[i];
62
+ var featureKeys = (0, sets_1.setToArray)(uniqueKeys[featureName]);
63
+ var uniqueKeysPayload = {
64
+ f: featureName,
65
+ ks: featureKeys
66
+ };
67
+ payload.push(uniqueKeysPayload);
68
+ }
69
+ return { keys: payload };
70
+ };
71
+ return UniqueKeysCacheInMemory;
72
+ }());
73
+ exports.UniqueKeysCacheInMemory = UniqueKeysCacheInMemory;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UniqueKeysCacheInMemoryCS = void 0;
4
+ var sets_1 = require("../../utils/lang/sets");
5
+ var constants_1 = require("../inRedis/constants");
6
+ var UniqueKeysCacheInMemoryCS = /** @class */ (function () {
7
+ /**
8
+ *
9
+ * @param impressionsQueueSize number of queued impressions to call onFullQueueCb.
10
+ * Default value is 0, that means no maximum value, in case we want to avoid this being triggered.
11
+ */
12
+ function UniqueKeysCacheInMemoryCS(uniqueKeysQueueSize) {
13
+ if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = constants_1.DEFAULT_CACHE_SIZE; }
14
+ this.uniqueTrackerSize = 0;
15
+ this.maxStorage = uniqueKeysQueueSize;
16
+ this.uniqueKeysTracker = {};
17
+ }
18
+ UniqueKeysCacheInMemoryCS.prototype.setOnFullQueueCb = function (cb) {
19
+ this.onFullQueue = cb;
20
+ };
21
+ /**
22
+ * Store unique keys in sequential order
23
+ * key: string = key.
24
+ * value: HashSet<string> = set of split names.
25
+ */
26
+ UniqueKeysCacheInMemoryCS.prototype.track = function (key, featureName) {
27
+ if (!this.uniqueKeysTracker[key])
28
+ this.uniqueKeysTracker[key] = new sets_1._Set();
29
+ var tracker = this.uniqueKeysTracker[key];
30
+ if (!tracker.has(featureName)) {
31
+ tracker.add(featureName);
32
+ this.uniqueTrackerSize++;
33
+ }
34
+ if (this.uniqueTrackerSize >= this.maxStorage && this.onFullQueue) {
35
+ this.uniqueTrackerSize = 0;
36
+ this.onFullQueue();
37
+ }
38
+ };
39
+ /**
40
+ * Clear the data stored on the cache.
41
+ */
42
+ UniqueKeysCacheInMemoryCS.prototype.clear = function () {
43
+ this.uniqueKeysTracker = {};
44
+ };
45
+ /**
46
+ * Pop the collected data, used as payload for posting.
47
+ */
48
+ UniqueKeysCacheInMemoryCS.prototype.pop = function () {
49
+ var data = this.uniqueKeysTracker;
50
+ this.uniqueKeysTracker = {};
51
+ return this.fromUniqueKeysCollector(data);
52
+ };
53
+ /**
54
+ * Check if the cache is empty.
55
+ */
56
+ UniqueKeysCacheInMemoryCS.prototype.isEmpty = function () {
57
+ return Object.keys(this.uniqueKeysTracker).length === 0;
58
+ };
59
+ /**
60
+ * Converts `uniqueKeys` data from cache into request payload.
61
+ */
62
+ UniqueKeysCacheInMemoryCS.prototype.fromUniqueKeysCollector = function (uniqueKeys) {
63
+ var payload = [];
64
+ var featureKeys = Object.keys(uniqueKeys);
65
+ for (var k = 0; k < featureKeys.length; k++) {
66
+ var featureKey = featureKeys[k];
67
+ var featureNames = (0, sets_1.setToArray)(uniqueKeys[featureKey]);
68
+ var uniqueKeysPayload = {
69
+ k: featureKey,
70
+ fs: featureNames
71
+ };
72
+ payload.push(uniqueKeysPayload);
73
+ }
74
+ return { keys: payload };
75
+ };
76
+ return UniqueKeysCacheInMemoryCS;
77
+ }());
78
+ exports.UniqueKeysCacheInMemoryCS = UniqueKeysCacheInMemoryCS;
@@ -42,7 +42,7 @@ var EventsCacheInRedis = /** @class */ (function () {
42
42
  };
43
43
  /**
44
44
  * Pop the given number of events from the storage.
45
- * The returned promise rejects if the wrapper operation fails.
45
+ * The returned promise rejects if the redis operation fails.
46
46
  *
47
47
  * NOTE: this method doesn't take into account MAX_EVENT_SIZE or MAX_QUEUE_BYTE_SIZE limits.
48
48
  * It is the submitter responsability to handle that.
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ImpressionCountsCacheInRedis = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var ImpressionCountsCacheInMemory_1 = require("../inMemory/ImpressionCountsCacheInMemory");
6
+ var constants_1 = require("./constants");
7
+ var ImpressionCountsCacheInRedis = /** @class */ (function (_super) {
8
+ (0, tslib_1.__extends)(ImpressionCountsCacheInRedis, _super);
9
+ function ImpressionCountsCacheInRedis(log, key, redis, impressionCountsCacheSize, refreshRate) {
10
+ if (refreshRate === void 0) { refreshRate = constants_1.REFRESH_RATE; }
11
+ var _this = _super.call(this, impressionCountsCacheSize) || this;
12
+ _this.log = log;
13
+ _this.key = key;
14
+ _this.redis = redis;
15
+ _this.refreshRate = refreshRate;
16
+ _this.onFullQueue = function () { _this.postImpressionCountsInRedis(); };
17
+ return _this;
18
+ }
19
+ ImpressionCountsCacheInRedis.prototype.postImpressionCountsInRedis = function () {
20
+ var _this = this;
21
+ var counts = this.pop();
22
+ var keys = Object.keys(counts);
23
+ if (!keys.length)
24
+ return Promise.resolve(false);
25
+ var pipeline = this.redis.pipeline();
26
+ keys.forEach(function (key) {
27
+ pipeline.hincrby(_this.key, key, counts[key]);
28
+ });
29
+ return pipeline.exec()
30
+ .then(function (data) {
31
+ // If this is the creation of the key on Redis, set the expiration for it in 3600 seconds.
32
+ if (data.length && data.length === keys.length) {
33
+ return _this.redis.expire(_this.key, constants_1.TTL_REFRESH);
34
+ }
35
+ })
36
+ .catch(function (err) {
37
+ _this.log.error(constants_1.LOG_PREFIX + "Error in impression counts pipeline: " + err + ".");
38
+ return false;
39
+ });
40
+ };
41
+ ImpressionCountsCacheInRedis.prototype.start = function () {
42
+ this.intervalId = setInterval(this.postImpressionCountsInRedis.bind(this), this.refreshRate);
43
+ };
44
+ ImpressionCountsCacheInRedis.prototype.stop = function () {
45
+ clearInterval(this.intervalId);
46
+ return this.postImpressionCountsInRedis();
47
+ };
48
+ return ImpressionCountsCacheInRedis;
49
+ }(ImpressionCountsCacheInMemory_1.ImpressionCountsCacheInMemory));
50
+ exports.ImpressionCountsCacheInRedis = ImpressionCountsCacheInRedis;
@@ -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
  })
@@ -1,9 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TelemetryCacheInRedis = void 0;
4
+ var KeyBuilderSS_1 = require("../KeyBuilderSS");
4
5
  var findLatencyIndex_1 = require("../findLatencyIndex");
5
6
  var telemetrySubmitter_1 = require("../../sync/submitters/telemetrySubmitter");
6
7
  var constants_1 = require("../../utils/constants");
8
+ var lang_1 = require("../../utils/lang");
9
+ var maps_1 = require("../../utils/lang/maps");
10
+ var TelemetryCacheInMemory_1 = require("../inMemory/TelemetryCacheInMemory");
7
11
  var TelemetryCacheInRedis = /** @class */ (function () {
8
12
  /**
9
13
  * Create a Telemetry cache that uses Redis as storage.
@@ -31,6 +35,102 @@ var TelemetryCacheInRedis = /** @class */ (function () {
31
35
  var value = JSON.stringify((0, telemetrySubmitter_1.getTelemetryConfigStats)(constants_1.CONSUMER_MODE, constants_1.STORAGE_REDIS));
32
36
  return this.redis.hset(key, field, value).catch(function () { });
33
37
  };
38
+ /**
39
+ * Pop telemetry latencies.
40
+ * The returned promise rejects if redis operations fail.
41
+ */
42
+ TelemetryCacheInRedis.prototype.popLatencies = function () {
43
+ var _this = this;
44
+ return this.redis.hgetall(this.keys.latencyPrefix).then(function (latencies) {
45
+ var result = new maps_1._Map();
46
+ Object.keys(latencies).forEach(function (field) {
47
+ var parsedField = (0, KeyBuilderSS_1.parseLatencyField)(field);
48
+ if ((0, lang_1.isString)(parsedField)) {
49
+ _this.log.error("Ignoring invalid latency field: " + field + ": " + parsedField);
50
+ return;
51
+ }
52
+ var count = parseInt(latencies[field]);
53
+ if ((0, lang_1.isNaNNumber)(count)) {
54
+ _this.log.error("Ignoring latency with invalid count: " + latencies[field]);
55
+ return;
56
+ }
57
+ var metadata = parsedField[0], method = parsedField[1], bucket = parsedField[2];
58
+ if (bucket >= TelemetryCacheInMemory_1.MAX_LATENCY_BUCKET_COUNT) {
59
+ _this.log.error("Ignoring latency with invalid bucket: " + bucket);
60
+ return;
61
+ }
62
+ if (!result.has(metadata))
63
+ result.set(metadata, {
64
+ t: (0, TelemetryCacheInMemory_1.newBuckets)(),
65
+ ts: (0, TelemetryCacheInMemory_1.newBuckets)(),
66
+ tc: (0, TelemetryCacheInMemory_1.newBuckets)(),
67
+ tcs: (0, TelemetryCacheInMemory_1.newBuckets)(),
68
+ tr: (0, TelemetryCacheInMemory_1.newBuckets)(),
69
+ });
70
+ result.get(metadata)[method][bucket] = count;
71
+ });
72
+ return _this.redis.del(_this.keys.latencyPrefix).then(function () { return result; });
73
+ });
74
+ };
75
+ /**
76
+ * Pop telemetry exceptions.
77
+ * The returned promise rejects if redis operations fail.
78
+ */
79
+ TelemetryCacheInRedis.prototype.popExceptions = function () {
80
+ var _this = this;
81
+ return this.redis.hgetall(this.keys.exceptionPrefix).then(function (exceptions) {
82
+ var result = new maps_1._Map();
83
+ Object.keys(exceptions).forEach(function (field) {
84
+ var parsedField = (0, KeyBuilderSS_1.parseExceptionField)(field);
85
+ if ((0, lang_1.isString)(parsedField)) {
86
+ _this.log.error("Ignoring invalid exception field: " + field + ": " + parsedField);
87
+ return;
88
+ }
89
+ var count = parseInt(exceptions[field]);
90
+ if ((0, lang_1.isNaNNumber)(count)) {
91
+ _this.log.error("Ignoring exception with invalid count: " + exceptions[field]);
92
+ return;
93
+ }
94
+ var metadata = parsedField[0], method = parsedField[1];
95
+ if (!result.has(metadata))
96
+ result.set(metadata, {
97
+ t: 0,
98
+ ts: 0,
99
+ tc: 0,
100
+ tcs: 0,
101
+ tr: 0,
102
+ });
103
+ result.get(metadata)[method] = count;
104
+ });
105
+ return _this.redis.del(_this.keys.exceptionPrefix).then(function () { return result; });
106
+ });
107
+ };
108
+ /**
109
+ * Pop telemetry configs.
110
+ * The returned promise rejects if redis operations fail.
111
+ */
112
+ TelemetryCacheInRedis.prototype.popConfigs = function () {
113
+ var _this = this;
114
+ return this.redis.hgetall(this.keys.initPrefix).then(function (configs) {
115
+ var result = new maps_1._Map();
116
+ Object.keys(configs).forEach(function (field) {
117
+ var parsedField = (0, KeyBuilderSS_1.parseMetadata)(field);
118
+ if ((0, lang_1.isString)(parsedField)) {
119
+ _this.log.error("Ignoring invalid config field: " + field + ": " + parsedField);
120
+ return;
121
+ }
122
+ var metadata = parsedField[0];
123
+ try {
124
+ var config = JSON.parse(configs[field]);
125
+ result.set(metadata, config);
126
+ }
127
+ catch (e) {
128
+ _this.log.error("Ignoring invalid config: " + configs[field]);
129
+ }
130
+ });
131
+ return _this.redis.del(_this.keys.initPrefix).then(function () { return result; });
132
+ });
133
+ };
34
134
  return TelemetryCacheInRedis;
35
135
  }());
36
136
  exports.TelemetryCacheInRedis = TelemetryCacheInRedis;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UniqueKeysCacheInRedis = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var UniqueKeysCacheInMemory_1 = require("../inMemory/UniqueKeysCacheInMemory");
6
+ var sets_1 = require("../../utils/lang/sets");
7
+ var constants_1 = require("./constants");
8
+ var constants_2 = require("./constants");
9
+ var UniqueKeysCacheInRedis = /** @class */ (function (_super) {
10
+ (0, tslib_1.__extends)(UniqueKeysCacheInRedis, _super);
11
+ function UniqueKeysCacheInRedis(log, key, redis, uniqueKeysQueueSize, refreshRate) {
12
+ if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = constants_1.DEFAULT_CACHE_SIZE; }
13
+ if (refreshRate === void 0) { refreshRate = constants_1.REFRESH_RATE; }
14
+ var _this = _super.call(this, uniqueKeysQueueSize) || this;
15
+ _this.log = log;
16
+ _this.key = key;
17
+ _this.redis = redis;
18
+ _this.refreshRate = refreshRate;
19
+ _this.onFullQueue = function () { _this.postUniqueKeysInRedis(); };
20
+ return _this;
21
+ }
22
+ UniqueKeysCacheInRedis.prototype.postUniqueKeysInRedis = function () {
23
+ var _this = this;
24
+ var featureNames = Object.keys(this.uniqueKeysTracker);
25
+ if (!featureNames.length)
26
+ return Promise.resolve(false);
27
+ var pipeline = this.redis.pipeline();
28
+ for (var i = 0; i < featureNames.length; i++) {
29
+ var featureName = featureNames[i];
30
+ var featureKeys = (0, sets_1.setToArray)(this.uniqueKeysTracker[featureName]);
31
+ var uniqueKeysPayload = {
32
+ f: featureName,
33
+ ks: featureKeys
34
+ };
35
+ pipeline.rpush(this.key, JSON.stringify(uniqueKeysPayload));
36
+ }
37
+ this.clear();
38
+ return pipeline.exec()
39
+ .then(function (data) {
40
+ // If this is the creation of the key on Redis, set the expiration for it in 3600 seconds.
41
+ if (data.length && data.length === featureNames.length) {
42
+ return _this.redis.expire(_this.key, constants_1.TTL_REFRESH);
43
+ }
44
+ })
45
+ .catch(function (err) {
46
+ _this.log.error(constants_2.LOG_PREFIX + "Error in uniqueKeys pipeline: " + err + ".");
47
+ return false;
48
+ });
49
+ };
50
+ UniqueKeysCacheInRedis.prototype.start = function () {
51
+ this.intervalId = setInterval(this.postUniqueKeysInRedis.bind(this), this.refreshRate);
52
+ };
53
+ UniqueKeysCacheInRedis.prototype.stop = function () {
54
+ clearInterval(this.intervalId);
55
+ return this.postUniqueKeysInRedis();
56
+ };
57
+ return UniqueKeysCacheInRedis;
58
+ }(UniqueKeysCacheInMemory_1.UniqueKeysCacheInMemory));
59
+ exports.UniqueKeysCacheInRedis = UniqueKeysCacheInRedis;