@splitsoftware/splitio-commons 1.6.2-rc.1 → 1.6.2-rc.11
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.
- package/CHANGES.txt +4 -0
- package/cjs/consent/sdkUserConsent.js +2 -2
- package/cjs/evaluator/index.js +15 -16
- package/cjs/integrations/ga/GaToSplit.js +8 -5
- package/cjs/sdkClient/client.js +19 -7
- package/cjs/sdkClient/sdkClient.js +3 -1
- package/cjs/sdkFactory/index.js +15 -6
- package/cjs/sdkManager/index.js +3 -11
- package/cjs/services/splitApi.js +6 -6
- package/cjs/storages/AbstractSplitsCacheAsync.js +8 -10
- package/cjs/storages/AbstractSplitsCacheSync.js +8 -10
- package/cjs/storages/KeyBuilderSS.js +54 -9
- package/cjs/storages/dataLoader.js +1 -1
- package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +5 -7
- package/cjs/storages/inLocalStorage/index.js +5 -1
- package/cjs/storages/inMemory/ImpressionCountsCacheInMemory.js +12 -1
- package/cjs/storages/inMemory/InMemoryStorage.js +6 -2
- package/cjs/storages/inMemory/InMemoryStorageCS.js +6 -2
- package/cjs/storages/inMemory/SplitsCacheInMemory.js +7 -10
- package/cjs/storages/inMemory/TelemetryCacheInMemory.js +10 -5
- package/cjs/storages/inMemory/UniqueKeysCacheInMemory.js +72 -0
- package/cjs/storages/inMemory/UniqueKeysCacheInMemoryCS.js +76 -0
- package/cjs/storages/inRedis/EventsCacheInRedis.js +1 -1
- package/cjs/storages/inRedis/ImpressionCountsCacheInRedis.js +85 -0
- package/cjs/storages/inRedis/SplitsCacheInRedis.js +15 -9
- package/cjs/storages/inRedis/TelemetryCacheInRedis.js +100 -0
- package/cjs/storages/inRedis/UniqueKeysCacheInRedis.js +71 -0
- package/cjs/storages/inRedis/constants.js +4 -1
- package/cjs/storages/inRedis/index.js +17 -2
- package/cjs/storages/pluggable/ImpressionCountsCachePluggable.js +81 -0
- package/cjs/storages/pluggable/SplitsCachePluggable.js +14 -9
- package/cjs/storages/pluggable/TelemetryCachePluggable.js +126 -0
- package/cjs/storages/pluggable/UniqueKeysCachePluggable.js +61 -0
- package/cjs/storages/pluggable/index.js +46 -17
- package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
- package/cjs/sync/polling/updaters/splitChangesUpdater.js +1 -1
- package/cjs/sync/submitters/telemetrySubmitter.js +8 -4
- package/cjs/sync/submitters/uniqueKeysSubmitter.js +16 -59
- package/cjs/trackers/impressionsTracker.js +17 -15
- package/cjs/trackers/strategy/strategyNone.js +1 -1
- package/cjs/trackers/strategy/strategyOptimized.js +2 -1
- package/cjs/trackers/telemetryTracker.js +6 -0
- package/cjs/trackers/uniqueKeysTracker.js +11 -42
- package/cjs/utils/constants/index.js +3 -2
- package/cjs/utils/lang/maps.js +15 -7
- package/cjs/utils/redis/RedisMock.js +31 -0
- package/cjs/utils/settingsValidation/index.js +0 -3
- package/esm/consent/sdkUserConsent.js +2 -2
- package/esm/evaluator/index.js +15 -16
- package/esm/integrations/ga/GaToSplit.js +8 -5
- package/esm/sdkClient/client.js +19 -7
- package/esm/sdkClient/sdkClient.js +3 -1
- package/esm/sdkFactory/index.js +16 -7
- package/esm/sdkManager/index.js +3 -11
- package/esm/services/splitApi.js +6 -6
- package/esm/storages/AbstractSplitsCacheAsync.js +8 -10
- package/esm/storages/AbstractSplitsCacheSync.js +8 -10
- package/esm/storages/KeyBuilderSS.js +50 -8
- package/esm/storages/dataLoader.js +1 -1
- package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +5 -7
- package/esm/storages/inLocalStorage/index.js +6 -2
- package/esm/storages/inMemory/ImpressionCountsCacheInMemory.js +12 -1
- package/esm/storages/inMemory/InMemoryStorage.js +8 -4
- package/esm/storages/inMemory/InMemoryStorageCS.js +7 -3
- package/esm/storages/inMemory/SplitsCacheInMemory.js +7 -10
- package/esm/storages/inMemory/TelemetryCacheInMemory.js +9 -5
- package/esm/storages/inMemory/UniqueKeysCacheInMemory.js +68 -0
- package/esm/storages/inMemory/UniqueKeysCacheInMemoryCS.js +73 -0
- package/esm/storages/inRedis/EventsCacheInRedis.js +1 -1
- package/esm/storages/inRedis/ImpressionCountsCacheInRedis.js +82 -0
- package/esm/storages/inRedis/SplitsCacheInRedis.js +15 -9
- package/esm/storages/inRedis/TelemetryCacheInRedis.js +100 -0
- package/esm/storages/inRedis/UniqueKeysCacheInRedis.js +68 -0
- package/esm/storages/inRedis/constants.js +3 -0
- package/esm/storages/inRedis/index.js +18 -3
- package/esm/storages/pluggable/ImpressionCountsCachePluggable.js +78 -0
- package/esm/storages/pluggable/SplitsCachePluggable.js +14 -9
- package/esm/storages/pluggable/TelemetryCachePluggable.js +126 -0
- package/esm/storages/pluggable/UniqueKeysCachePluggable.js +58 -0
- package/esm/storages/pluggable/index.js +47 -18
- package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
- package/esm/sync/polling/updaters/splitChangesUpdater.js +1 -1
- package/esm/sync/submitters/telemetrySubmitter.js +9 -5
- package/esm/sync/submitters/uniqueKeysSubmitter.js +15 -56
- package/esm/trackers/impressionsTracker.js +17 -15
- package/esm/trackers/strategy/strategyNone.js +1 -1
- package/esm/trackers/strategy/strategyOptimized.js +2 -1
- package/esm/trackers/telemetryTracker.js +6 -0
- package/esm/trackers/uniqueKeysTracker.js +11 -42
- package/esm/utils/constants/index.js +1 -0
- package/esm/utils/lang/maps.js +15 -7
- package/esm/utils/redis/RedisMock.js +28 -0
- package/esm/utils/settingsValidation/index.js +0 -3
- package/package.json +1 -2
- package/src/consent/sdkUserConsent.ts +2 -2
- package/src/evaluator/index.ts +14 -15
- package/src/integrations/ga/GaToSplit.ts +9 -5
- package/src/integrations/types.ts +2 -1
- package/src/logger/.DS_Store +0 -0
- package/src/sdkClient/client.ts +21 -8
- package/src/sdkClient/sdkClient.ts +3 -1
- package/src/sdkFactory/index.ts +17 -7
- package/src/sdkManager/index.ts +3 -12
- package/src/services/splitApi.ts +6 -6
- package/src/services/types.ts +2 -2
- package/src/storages/AbstractSplitsCacheAsync.ts +13 -15
- package/src/storages/AbstractSplitsCacheSync.ts +15 -17
- package/src/storages/KeyBuilderSS.ts +61 -9
- package/src/storages/dataLoader.ts +1 -1
- package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +8 -11
- package/src/storages/inLocalStorage/index.ts +5 -2
- package/src/storages/inMemory/ImpressionCountsCacheInMemory.ts +16 -1
- package/src/storages/inMemory/InMemoryStorage.ts +7 -4
- package/src/storages/inMemory/InMemoryStorageCS.ts +6 -3
- package/src/storages/inMemory/SplitsCacheInMemory.ts +10 -14
- package/src/storages/inMemory/TelemetryCacheInMemory.ts +10 -6
- package/src/storages/inMemory/UniqueKeysCacheInMemory.ts +80 -0
- package/src/storages/inMemory/UniqueKeysCacheInMemoryCS.ts +86 -0
- package/src/storages/inRedis/EventsCacheInRedis.ts +1 -1
- package/src/storages/inRedis/ImpressionCountsCacheInRedis.ts +95 -0
- package/src/storages/inRedis/SplitsCacheInRedis.ts +21 -17
- package/src/storages/inRedis/TelemetryCacheInRedis.ts +122 -2
- package/src/storages/inRedis/UniqueKeysCacheInRedis.ts +77 -0
- package/src/storages/inRedis/constants.ts +3 -0
- package/src/storages/inRedis/index.ts +15 -5
- package/src/storages/pluggable/ImpressionCountsCachePluggable.ts +92 -0
- package/src/storages/pluggable/SplitsCachePluggable.ts +20 -17
- package/src/storages/pluggable/TelemetryCachePluggable.ts +147 -2
- package/src/storages/pluggable/UniqueKeysCachePluggable.ts +67 -0
- package/src/storages/pluggable/index.ts +51 -19
- package/src/storages/types.ts +38 -30
- package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +5 -6
- package/src/sync/polling/updaters/splitChangesUpdater.ts +2 -2
- package/src/sync/submitters/telemetrySubmitter.ts +15 -8
- package/src/sync/submitters/types.ts +26 -12
- package/src/sync/submitters/uniqueKeysSubmitter.ts +18 -61
- package/src/trackers/impressionsTracker.ts +16 -15
- package/src/trackers/strategy/strategyNone.ts +1 -1
- package/src/trackers/strategy/strategyOptimized.ts +1 -1
- package/src/trackers/telemetryTracker.ts +7 -2
- package/src/trackers/types.ts +9 -7
- package/src/trackers/uniqueKeysTracker.ts +15 -47
- package/src/types.ts +0 -1
- package/src/utils/constants/index.ts +1 -0
- package/src/utils/lang/maps.ts +20 -8
- package/src/utils/redis/RedisMock.ts +33 -0
- package/src/utils/settingsValidation/index.ts +1 -4
- package/types/integrations/types.d.ts +2 -1
- package/types/services/types.d.ts +2 -2
- package/types/storages/AbstractSplitsCacheAsync.d.ts +7 -6
- package/types/storages/AbstractSplitsCacheSync.d.ts +6 -6
- package/types/storages/KeyBuilderSS.d.ts +9 -2
- package/types/storages/inLocalStorage/SplitsCacheInLocal.d.ts +3 -4
- package/types/storages/inMemory/ImpressionCountsCacheInMemory.d.ts +5 -1
- package/types/storages/inMemory/SplitsCacheInMemory.d.ts +3 -2
- package/types/storages/inMemory/TelemetryCacheInMemory.d.ts +6 -3
- package/types/storages/inMemory/uniqueKeysCacheInMemory.d.ts +35 -0
- package/types/storages/inMemory/uniqueKeysCacheInMemoryCS.d.ts +35 -0
- package/types/storages/inRedis/EventsCacheInRedis.d.ts +1 -1
- package/types/storages/inRedis/ImpressionCountsCacheInRedis.d.ts +16 -0
- package/types/storages/inRedis/SplitsCacheInRedis.d.ts +6 -5
- package/types/storages/inRedis/TelemetryCacheInRedis.d.ts +16 -1
- package/types/storages/inRedis/constants.d.ts +3 -0
- package/types/storages/inRedis/uniqueKeysCacheInRedis.d.ts +21 -0
- package/types/storages/pluggable/ImpressionCountsCachePluggable.d.ts +16 -0
- package/types/storages/pluggable/SplitsCachePluggable.d.ts +6 -5
- package/types/storages/pluggable/TelemetryCachePluggable.d.ts +17 -1
- package/types/storages/pluggable/UniqueKeysCachePluggable.d.ts +20 -0
- package/types/storages/types.d.ts +35 -35
- package/types/sync/polling/updaters/splitChangesUpdater.d.ts +1 -1
- package/types/sync/submitters/telemetrySubmitter.d.ts +1 -1
- package/types/sync/submitters/types.d.ts +19 -12
- package/types/sync/submitters/uniqueKeysSubmitter.d.ts +0 -14
- package/types/trackers/types.d.ts +9 -11
- package/types/trackers/uniqueKeysTracker.d.ts +3 -3
- package/types/types.d.ts +0 -1
- package/types/utils/constants/index.d.ts +1 -0
- package/types/utils/lang/maps.d.ts +6 -2
- package/types/utils/redis/RedisMock.d.ts +4 -0
- package/types/utils/settingsValidation/index.d.ts +0 -1
- package/types/sdkClient/types.d.ts +0 -18
- package/types/storages/inMemory/CountsCacheInMemory.d.ts +0 -20
- package/types/storages/inMemory/LatenciesCacheInMemory.d.ts +0 -20
- package/types/storages/inRedis/CountsCacheInRedis.d.ts +0 -9
- package/types/storages/inRedis/LatenciesCacheInRedis.d.ts +0 -9
- package/types/sync/offline/LocalhostFromFile.d.ts +0 -2
- package/types/sync/offline/splitsParser/splitsParserFromFile.d.ts +0 -2
- package/types/sync/submitters/eventsSyncTask.d.ts +0 -8
- package/types/sync/submitters/impressionCountsSyncTask.d.ts +0 -13
- package/types/sync/submitters/impressionsSyncTask.d.ts +0 -14
- package/types/sync/submitters/metricsSyncTask.d.ts +0 -12
- package/types/sync/submitters/submitterSyncTask.d.ts +0 -10
- package/types/sync/syncTaskComposite.d.ts +0 -5
- package/types/trackers/filter/bloomFilter.d.ts +0 -10
- package/types/trackers/filter/dictionaryFilter.d.ts +0 -8
- package/types/trackers/filter/types.d.ts +0 -5
- package/types/utils/timeTracker/index.d.ts +0 -70
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { objectAssign } from '../utils/lang/objectAssign';
|
|
1
2
|
/**
|
|
2
3
|
* This class provides a skeletal implementation of the ISplitsCacheSync interface
|
|
3
4
|
* to minimize the effort required to implement this interface.
|
|
@@ -39,20 +40,17 @@ var AbstractSplitsCacheSync = /** @class */ (function () {
|
|
|
39
40
|
* @param {string} name
|
|
40
41
|
* @param {string} defaultTreatment
|
|
41
42
|
* @param {number} changeNumber
|
|
42
|
-
* @returns {
|
|
43
|
+
* @returns {boolean} `true` if the operation successed updating the split, or `false` if no split is updated,
|
|
43
44
|
* for instance, if the `changeNumber` is old, or if the split is not found (e.g., `/splitchanges` hasn't been fetched yet), or if the storage fails to apply the update.
|
|
44
45
|
*/
|
|
45
46
|
AbstractSplitsCacheSync.prototype.killLocally = function (name, defaultTreatment, changeNumber) {
|
|
46
47
|
var split = this.getSplit(name);
|
|
47
|
-
if (split) {
|
|
48
|
-
var
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
var newSplit = JSON.stringify(parsedSplit);
|
|
54
|
-
return this.addSplit(name, newSplit);
|
|
55
|
-
}
|
|
48
|
+
if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
|
|
49
|
+
var newSplit = objectAssign({}, split);
|
|
50
|
+
newSplit.killed = true;
|
|
51
|
+
newSplit.defaultTreatment = defaultTreatment;
|
|
52
|
+
newSplit.changeNumber = changeNumber;
|
|
53
|
+
return this.addSplit(name, newSplit);
|
|
56
54
|
}
|
|
57
55
|
return false;
|
|
58
56
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { __extends } from "tslib";
|
|
2
2
|
import { KeyBuilder } from './KeyBuilder';
|
|
3
|
-
|
|
3
|
+
import { MAX_LATENCY_BUCKET_COUNT } from './inMemory/TelemetryCacheInMemory';
|
|
4
|
+
var METHOD_NAMES = {
|
|
4
5
|
t: 'treatment',
|
|
5
6
|
ts: 'treatments',
|
|
6
7
|
tc: 'treatmentWithConfig',
|
|
@@ -11,7 +12,10 @@ var KeyBuilderSS = /** @class */ (function (_super) {
|
|
|
11
12
|
__extends(KeyBuilderSS, _super);
|
|
12
13
|
function KeyBuilderSS(prefix, metadata) {
|
|
13
14
|
var _this = _super.call(this, prefix) || this;
|
|
14
|
-
_this.
|
|
15
|
+
_this.latencyPrefix = _this.prefix + ".telemetry.latencies";
|
|
16
|
+
_this.exceptionPrefix = _this.prefix + ".telemetry.exceptions";
|
|
17
|
+
_this.initPrefix = _this.prefix + ".telemetry.init";
|
|
18
|
+
_this.versionablePrefix = metadata.s + "/" + metadata.n + "/" + metadata.i;
|
|
15
19
|
return _this;
|
|
16
20
|
}
|
|
17
21
|
KeyBuilderSS.prototype.buildRegisteredSegmentsKey = function () {
|
|
@@ -20,6 +24,12 @@ var KeyBuilderSS = /** @class */ (function (_super) {
|
|
|
20
24
|
KeyBuilderSS.prototype.buildImpressionsKey = function () {
|
|
21
25
|
return this.prefix + ".impressions";
|
|
22
26
|
};
|
|
27
|
+
KeyBuilderSS.prototype.buildImpressionsCountKey = function () {
|
|
28
|
+
return this.prefix + ".impressions.count";
|
|
29
|
+
};
|
|
30
|
+
KeyBuilderSS.prototype.buildUniqueKeysKey = function () {
|
|
31
|
+
return this.prefix + ".uniquekeys";
|
|
32
|
+
};
|
|
23
33
|
KeyBuilderSS.prototype.buildEventsKey = function () {
|
|
24
34
|
return this.prefix + ".events";
|
|
25
35
|
};
|
|
@@ -28,17 +38,49 @@ var KeyBuilderSS = /** @class */ (function (_super) {
|
|
|
28
38
|
};
|
|
29
39
|
/* Telemetry keys */
|
|
30
40
|
KeyBuilderSS.prototype.buildLatencyKey = function (method, bucket) {
|
|
31
|
-
return this.
|
|
41
|
+
return this.latencyPrefix + "::" + this.versionablePrefix + "/" + METHOD_NAMES[method] + "/" + bucket;
|
|
32
42
|
};
|
|
33
43
|
KeyBuilderSS.prototype.buildExceptionKey = function (method) {
|
|
34
|
-
return this.
|
|
44
|
+
return this.exceptionPrefix + "::" + this.versionablePrefix + "/" + METHOD_NAMES[method];
|
|
35
45
|
};
|
|
36
46
|
KeyBuilderSS.prototype.buildInitKey = function () {
|
|
37
|
-
return this.
|
|
38
|
-
};
|
|
39
|
-
KeyBuilderSS.prototype.buildVersionablePrefix = function () {
|
|
40
|
-
return this.metadata.s + "/" + this.metadata.n + "/" + this.metadata.i;
|
|
47
|
+
return this.initPrefix + "::" + this.versionablePrefix;
|
|
41
48
|
};
|
|
42
49
|
return KeyBuilderSS;
|
|
43
50
|
}(KeyBuilder));
|
|
44
51
|
export { KeyBuilderSS };
|
|
52
|
+
// Used by consumer methods of TelemetryCacheInRedis and TelemetryCachePluggable
|
|
53
|
+
var REVERSE_METHOD_NAMES = Object.keys(METHOD_NAMES).reduce(function (acc, key) {
|
|
54
|
+
acc[METHOD_NAMES[key]] = key;
|
|
55
|
+
return acc;
|
|
56
|
+
}, {});
|
|
57
|
+
export function parseMetadata(field) {
|
|
58
|
+
var parts = field.split('/');
|
|
59
|
+
if (parts.length !== 3)
|
|
60
|
+
return "invalid subsection count. Expected 3, got: " + parts.length;
|
|
61
|
+
var s = parts[0] /* metadata.s */, n = parts[1] /* metadata.n */, i = parts[2] /* metadata.i */;
|
|
62
|
+
return [JSON.stringify({ s: s, n: n, i: i })];
|
|
63
|
+
}
|
|
64
|
+
export function parseExceptionField(field) {
|
|
65
|
+
var parts = field.split('/');
|
|
66
|
+
if (parts.length !== 4)
|
|
67
|
+
return "invalid subsection count. Expected 4, got: " + parts.length;
|
|
68
|
+
var s = parts[0] /* metadata.s */, n = parts[1] /* metadata.n */, i = parts[2] /* metadata.i */, m = parts[3];
|
|
69
|
+
var method = REVERSE_METHOD_NAMES[m];
|
|
70
|
+
if (!method)
|
|
71
|
+
return "unknown method '" + m + "'";
|
|
72
|
+
return [JSON.stringify({ s: s, n: n, i: i }), method];
|
|
73
|
+
}
|
|
74
|
+
export function parseLatencyField(field) {
|
|
75
|
+
var parts = field.split('/');
|
|
76
|
+
if (parts.length !== 5)
|
|
77
|
+
return "invalid subsection count. Expected 5, got: " + parts.length;
|
|
78
|
+
var s = parts[0] /* metadata.s */, n = parts[1] /* metadata.n */, i = parts[2] /* metadata.i */, m = parts[3], b = parts[4];
|
|
79
|
+
var method = REVERSE_METHOD_NAMES[m];
|
|
80
|
+
if (!method)
|
|
81
|
+
return "unknown method '" + m + "'";
|
|
82
|
+
var bucket = parseInt(b);
|
|
83
|
+
if (isNaN(bucket) || bucket >= MAX_LATENCY_BUCKET_COUNT)
|
|
84
|
+
return "invalid bucket. Expected a number between 0 and " + (MAX_LATENCY_BUCKET_COUNT - 1) + ", got: " + b;
|
|
85
|
+
return [JSON.stringify({ s: s, n: n, i: i }), method, bucket];
|
|
86
|
+
}
|
|
@@ -32,7 +32,7 @@ export function dataLoaderFactory(preloadedData) {
|
|
|
32
32
|
storage.splits.clear();
|
|
33
33
|
storage.splits.setChangeNumber(since);
|
|
34
34
|
// splitsData in an object where the property is the split name and the pertaining value is a stringified json of its data
|
|
35
|
-
storage.splits.addSplits(Object.keys(splitsData).map(function (splitName) { return
|
|
35
|
+
storage.splits.addSplits(Object.keys(splitsData).map(function (splitName) { return JSON.parse(splitsData[splitName]); }));
|
|
36
36
|
// add mySegments data
|
|
37
37
|
var mySegmentsData = preloadedData.mySegmentsData && preloadedData.mySegmentsData[userId];
|
|
38
38
|
if (!mySegmentsData) {
|
|
@@ -93,9 +93,8 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
|
|
|
93
93
|
var splitFromLocalStorage = localStorage.getItem(splitKey);
|
|
94
94
|
var previousSplit = splitFromLocalStorage ? JSON.parse(splitFromLocalStorage) : null;
|
|
95
95
|
this._decrementCounts(previousSplit);
|
|
96
|
-
localStorage.setItem(splitKey, split);
|
|
97
|
-
|
|
98
|
-
this._incrementCounts(parsedSplit);
|
|
96
|
+
localStorage.setItem(splitKey, JSON.stringify(split));
|
|
97
|
+
this._incrementCounts(split);
|
|
99
98
|
return true;
|
|
100
99
|
}
|
|
101
100
|
catch (e) {
|
|
@@ -107,8 +106,7 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
|
|
|
107
106
|
try {
|
|
108
107
|
var split = this.getSplit(name);
|
|
109
108
|
localStorage.removeItem(this.keys.buildSplitKey(name));
|
|
110
|
-
|
|
111
|
-
this._decrementCounts(parsedSplit);
|
|
109
|
+
this._decrementCounts(split);
|
|
112
110
|
return true;
|
|
113
111
|
}
|
|
114
112
|
catch (e) {
|
|
@@ -117,7 +115,8 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
|
|
|
117
115
|
}
|
|
118
116
|
};
|
|
119
117
|
SplitsCacheInLocal.prototype.getSplit = function (name) {
|
|
120
|
-
|
|
118
|
+
var item = localStorage.getItem(this.keys.buildSplitKey(name));
|
|
119
|
+
return item && JSON.parse(item);
|
|
121
120
|
};
|
|
122
121
|
SplitsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
|
|
123
122
|
// when cache is ready but using a new split query, we must clear all split data
|
|
@@ -201,7 +200,6 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
|
|
|
201
200
|
};
|
|
202
201
|
/**
|
|
203
202
|
* Clean Splits cache if its `lastUpdated` timestamp is older than the given `expirationTimestamp`,
|
|
204
|
-
* Clean operation (clear) also updates `lastUpdated` timestamp with current time.
|
|
205
203
|
*
|
|
206
204
|
* @param {number | undefined} expirationTimestamp if the value is not a number, data will not be cleaned
|
|
207
205
|
*/
|
|
@@ -11,8 +11,9 @@ import { SplitsCacheInMemory } from '../inMemory/SplitsCacheInMemory';
|
|
|
11
11
|
import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
|
|
12
12
|
import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
|
|
13
13
|
import { LOG_PREFIX } from './constants';
|
|
14
|
-
import {
|
|
14
|
+
import { NONE, STORAGE_LOCALSTORAGE } from '../../utils/constants';
|
|
15
15
|
import { shouldRecordTelemetry, TelemetryCacheInMemory } from '../inMemory/TelemetryCacheInMemory';
|
|
16
|
+
import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS';
|
|
16
17
|
/**
|
|
17
18
|
* InLocal storage factory for standalone client-side SplitFactory
|
|
18
19
|
*/
|
|
@@ -34,13 +35,16 @@ export function InLocalStorage(options) {
|
|
|
34
35
|
impressions: new ImpressionsCacheInMemory(params.impressionsQueueSize),
|
|
35
36
|
impressionCounts: params.optimize ? new ImpressionCountsCacheInMemory() : undefined,
|
|
36
37
|
events: new EventsCacheInMemory(params.eventsQueueSize),
|
|
37
|
-
telemetry:
|
|
38
|
+
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory() : undefined,
|
|
39
|
+
uniqueKeys: params.impressionsMode === NONE ? new UniqueKeysCacheInMemoryCS() : undefined,
|
|
38
40
|
destroy: function () {
|
|
41
|
+
var _a;
|
|
39
42
|
this.splits = new SplitsCacheInMemory();
|
|
40
43
|
this.segments = new MySegmentsCacheInMemory();
|
|
41
44
|
this.impressions.clear();
|
|
42
45
|
this.impressionCounts && this.impressionCounts.clear();
|
|
43
46
|
this.events.clear();
|
|
47
|
+
(_a = this.uniqueKeys) === null || _a === void 0 ? void 0 : _a.clear();
|
|
44
48
|
},
|
|
45
49
|
// When using shared instanciation with MEMORY we reuse everything but segments (they are customer per key).
|
|
46
50
|
shared: function (matchingKey) {
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { truncateTimeFrame } from '../../utils/time';
|
|
2
|
+
import { DEFAULT_CACHE_SIZE } from '../inRedis/constants';
|
|
2
3
|
var ImpressionCountsCacheInMemory = /** @class */ (function () {
|
|
3
|
-
function ImpressionCountsCacheInMemory() {
|
|
4
|
+
function ImpressionCountsCacheInMemory(impressionCountsCacheSize) {
|
|
5
|
+
if (impressionCountsCacheSize === void 0) { impressionCountsCacheSize = DEFAULT_CACHE_SIZE; }
|
|
4
6
|
this.cache = {};
|
|
7
|
+
this.cacheSize = 0;
|
|
8
|
+
this.maxStorage = impressionCountsCacheSize;
|
|
5
9
|
}
|
|
6
10
|
/**
|
|
7
11
|
* Builds key to be stored in the cache with the featureName and the timeFrame truncated.
|
|
@@ -16,6 +20,13 @@ var ImpressionCountsCacheInMemory = /** @class */ (function () {
|
|
|
16
20
|
var key = this._makeKey(featureName, timeFrame);
|
|
17
21
|
var currentAmount = this.cache[key];
|
|
18
22
|
this.cache[key] = currentAmount ? currentAmount + amount : amount;
|
|
23
|
+
if (this.onFullQueue) {
|
|
24
|
+
this.cacheSize = this.cacheSize + amount;
|
|
25
|
+
if (this.cacheSize >= this.maxStorage) {
|
|
26
|
+
this.onFullQueue();
|
|
27
|
+
this.cacheSize = 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
19
30
|
};
|
|
20
31
|
/**
|
|
21
32
|
* Pop the collected data, used as payload for posting.
|
|
@@ -3,8 +3,9 @@ import { SegmentsCacheInMemory } from './SegmentsCacheInMemory';
|
|
|
3
3
|
import { ImpressionsCacheInMemory } from './ImpressionsCacheInMemory';
|
|
4
4
|
import { EventsCacheInMemory } from './EventsCacheInMemory';
|
|
5
5
|
import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
|
|
6
|
-
import {
|
|
7
|
-
import { TelemetryCacheInMemory } from './TelemetryCacheInMemory';
|
|
6
|
+
import { DEBUG, NONE, STORAGE_MEMORY } from '../../utils/constants';
|
|
7
|
+
import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
|
|
8
|
+
import { UniqueKeysCacheInMemory } from './UniqueKeysCacheInMemory';
|
|
8
9
|
/**
|
|
9
10
|
* InMemory storage factory for standalone server-side SplitFactory
|
|
10
11
|
*
|
|
@@ -15,16 +16,19 @@ export function InMemoryStorageFactory(params) {
|
|
|
15
16
|
splits: new SplitsCacheInMemory(),
|
|
16
17
|
segments: new SegmentsCacheInMemory(),
|
|
17
18
|
impressions: new ImpressionsCacheInMemory(params.impressionsQueueSize),
|
|
18
|
-
impressionCounts: params.
|
|
19
|
+
impressionCounts: params.impressionsMode !== DEBUG ? new ImpressionCountsCacheInMemory() : undefined,
|
|
19
20
|
events: new EventsCacheInMemory(params.eventsQueueSize),
|
|
20
|
-
telemetry: params
|
|
21
|
+
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory() : undefined,
|
|
22
|
+
uniqueKeys: params.impressionsMode === NONE ? new UniqueKeysCacheInMemory() : undefined,
|
|
21
23
|
// When using MEMORY we should clean all the caches to leave them empty
|
|
22
24
|
destroy: function () {
|
|
25
|
+
var _a;
|
|
23
26
|
this.splits.clear();
|
|
24
27
|
this.segments.clear();
|
|
25
28
|
this.impressions.clear();
|
|
26
29
|
this.impressionCounts && this.impressionCounts.clear();
|
|
27
30
|
this.events.clear();
|
|
31
|
+
(_a = this.uniqueKeys) === null || _a === void 0 ? void 0 : _a.clear();
|
|
28
32
|
}
|
|
29
33
|
};
|
|
30
34
|
}
|
|
@@ -3,8 +3,9 @@ import { MySegmentsCacheInMemory } from './MySegmentsCacheInMemory';
|
|
|
3
3
|
import { ImpressionsCacheInMemory } from './ImpressionsCacheInMemory';
|
|
4
4
|
import { EventsCacheInMemory } from './EventsCacheInMemory';
|
|
5
5
|
import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
|
|
6
|
-
import {
|
|
6
|
+
import { DEBUG, NONE, STORAGE_MEMORY } from '../../utils/constants';
|
|
7
7
|
import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
|
|
8
|
+
import { UniqueKeysCacheInMemoryCS } from './UniqueKeysCacheInMemoryCS';
|
|
8
9
|
/**
|
|
9
10
|
* InMemory storage factory for standalone client-side SplitFactory
|
|
10
11
|
*
|
|
@@ -15,16 +16,19 @@ export function InMemoryStorageCSFactory(params) {
|
|
|
15
16
|
splits: new SplitsCacheInMemory(),
|
|
16
17
|
segments: new MySegmentsCacheInMemory(),
|
|
17
18
|
impressions: new ImpressionsCacheInMemory(params.impressionsQueueSize),
|
|
18
|
-
impressionCounts: params.
|
|
19
|
+
impressionCounts: params.impressionsMode !== DEBUG ? new ImpressionCountsCacheInMemory() : undefined,
|
|
19
20
|
events: new EventsCacheInMemory(params.eventsQueueSize),
|
|
20
|
-
telemetry:
|
|
21
|
+
telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory() : undefined,
|
|
22
|
+
uniqueKeys: params.impressionsMode === NONE ? new UniqueKeysCacheInMemoryCS() : undefined,
|
|
21
23
|
// When using MEMORY we should clean all the caches to leave them empty
|
|
22
24
|
destroy: function () {
|
|
25
|
+
var _a;
|
|
23
26
|
this.splits.clear();
|
|
24
27
|
this.segments.clear();
|
|
25
28
|
this.impressions.clear();
|
|
26
29
|
this.impressionCounts && this.impressionCounts.clear();
|
|
27
30
|
this.events.clear();
|
|
31
|
+
(_a = this.uniqueKeys) === null || _a === void 0 ? void 0 : _a.clear();
|
|
28
32
|
},
|
|
29
33
|
// When using shared instanciation with MEMORY we reuse everything but segments (they are unique per key)
|
|
30
34
|
shared: function () {
|
|
@@ -22,9 +22,8 @@ var SplitsCacheInMemory = /** @class */ (function (_super) {
|
|
|
22
22
|
this.splitsWithSegmentsCount = 0;
|
|
23
23
|
};
|
|
24
24
|
SplitsCacheInMemory.prototype.addSplit = function (name, split) {
|
|
25
|
-
var
|
|
26
|
-
if (
|
|
27
|
-
var previousSplit = JSON.parse(splitFromMemory);
|
|
25
|
+
var previousSplit = this.getSplit(name);
|
|
26
|
+
if (previousSplit) { // We had this Split already
|
|
28
27
|
if (previousSplit.trafficTypeName) {
|
|
29
28
|
var previousTtName = previousSplit.trafficTypeName;
|
|
30
29
|
this.ttCache[previousTtName]--;
|
|
@@ -35,19 +34,18 @@ var SplitsCacheInMemory = /** @class */ (function (_super) {
|
|
|
35
34
|
this.splitsWithSegmentsCount--;
|
|
36
35
|
}
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
if (parsedSplit) {
|
|
37
|
+
if (split) {
|
|
40
38
|
// Store the Split.
|
|
41
39
|
this.splitsCache[name] = split;
|
|
42
40
|
// Update TT cache
|
|
43
|
-
var ttName =
|
|
41
|
+
var ttName = split.trafficTypeName;
|
|
44
42
|
if (ttName) { // safeguard
|
|
45
43
|
if (!this.ttCache[ttName])
|
|
46
44
|
this.ttCache[ttName] = 0;
|
|
47
45
|
this.ttCache[ttName]++;
|
|
48
46
|
}
|
|
49
47
|
// Add to segments count for the new version of the Split
|
|
50
|
-
if (usesSegments(
|
|
48
|
+
if (usesSegments(split))
|
|
51
49
|
this.splitsWithSegmentsCount++;
|
|
52
50
|
return true;
|
|
53
51
|
}
|
|
@@ -60,15 +58,14 @@ var SplitsCacheInMemory = /** @class */ (function (_super) {
|
|
|
60
58
|
if (split) {
|
|
61
59
|
// Delete the Split
|
|
62
60
|
delete this.splitsCache[name];
|
|
63
|
-
var
|
|
64
|
-
var ttName = parsedSplit.trafficTypeName;
|
|
61
|
+
var ttName = split.trafficTypeName;
|
|
65
62
|
if (ttName) { // safeguard
|
|
66
63
|
this.ttCache[ttName]--; // Update tt cache
|
|
67
64
|
if (!this.ttCache[ttName])
|
|
68
65
|
delete this.ttCache[ttName];
|
|
69
66
|
}
|
|
70
67
|
// Update the segments count.
|
|
71
|
-
if (usesSegments(
|
|
68
|
+
if (usesSegments(split))
|
|
72
69
|
this.splitsWithSegmentsCount--;
|
|
73
70
|
return true;
|
|
74
71
|
}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
import { LOCALHOST_MODE } from '../../utils/constants';
|
|
1
2
|
import { findLatencyIndex } from '../findLatencyIndex';
|
|
2
3
|
var MAX_STREAMING_EVENTS = 20;
|
|
3
4
|
var MAX_TAGS = 10;
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
export var MAX_LATENCY_BUCKET_COUNT = 23;
|
|
6
|
+
export function newBuckets() {
|
|
7
|
+
// MAX_LATENCY_BUCKET_COUNT (length) is 23
|
|
8
|
+
// Not using Array.fill for old browsers compatibility
|
|
6
9
|
return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
7
10
|
}
|
|
8
11
|
var ACCEPTANCE_RANGE = 0.001;
|
|
9
12
|
/**
|
|
10
|
-
*
|
|
13
|
+
* Record telemetry if mode is not localhost.
|
|
14
|
+
* All factory instances track telemetry on server-side, and 0.1% on client-side.
|
|
11
15
|
*/
|
|
12
|
-
export function shouldRecordTelemetry() {
|
|
13
|
-
return Math.random() <= ACCEPTANCE_RANGE;
|
|
16
|
+
export function shouldRecordTelemetry(params) {
|
|
17
|
+
return params.mode !== LOCALHOST_MODE && (params.matchingKey === undefined || Math.random() <= ACCEPTANCE_RANGE);
|
|
14
18
|
}
|
|
15
19
|
var TelemetryCacheInMemory = /** @class */ (function () {
|
|
16
20
|
function TelemetryCacheInMemory() {
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { setToArray, _Set } from '../../utils/lang/sets';
|
|
2
|
+
import { DEFAULT_CACHE_SIZE } from '../inRedis/constants';
|
|
3
|
+
/**
|
|
4
|
+
* Converts `uniqueKeys` data from cache into request payload for SS.
|
|
5
|
+
*/
|
|
6
|
+
export function fromUniqueKeysCollector(uniqueKeys) {
|
|
7
|
+
var payload = [];
|
|
8
|
+
var featureNames = Object.keys(uniqueKeys);
|
|
9
|
+
for (var i = 0; i < featureNames.length; i++) {
|
|
10
|
+
var featureName = featureNames[i];
|
|
11
|
+
var userKeys = setToArray(uniqueKeys[featureName]);
|
|
12
|
+
var uniqueKeysPayload = {
|
|
13
|
+
f: featureName,
|
|
14
|
+
ks: userKeys
|
|
15
|
+
};
|
|
16
|
+
payload.push(uniqueKeysPayload);
|
|
17
|
+
}
|
|
18
|
+
return { keys: payload };
|
|
19
|
+
}
|
|
20
|
+
var UniqueKeysCacheInMemory = /** @class */ (function () {
|
|
21
|
+
function UniqueKeysCacheInMemory(uniqueKeysQueueSize) {
|
|
22
|
+
if (uniqueKeysQueueSize === void 0) { uniqueKeysQueueSize = DEFAULT_CACHE_SIZE; }
|
|
23
|
+
this.uniqueTrackerSize = 0;
|
|
24
|
+
this.maxStorage = uniqueKeysQueueSize;
|
|
25
|
+
this.uniqueKeysTracker = {};
|
|
26
|
+
}
|
|
27
|
+
UniqueKeysCacheInMemory.prototype.setOnFullQueueCb = function (cb) {
|
|
28
|
+
this.onFullQueue = cb;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Store unique keys per feature.
|
|
32
|
+
*/
|
|
33
|
+
UniqueKeysCacheInMemory.prototype.track = function (userKey, featureName) {
|
|
34
|
+
if (!this.uniqueKeysTracker[featureName])
|
|
35
|
+
this.uniqueKeysTracker[featureName] = new _Set();
|
|
36
|
+
var tracker = this.uniqueKeysTracker[featureName];
|
|
37
|
+
if (!tracker.has(userKey)) {
|
|
38
|
+
tracker.add(userKey);
|
|
39
|
+
this.uniqueTrackerSize++;
|
|
40
|
+
}
|
|
41
|
+
if (this.uniqueTrackerSize >= this.maxStorage && this.onFullQueue) {
|
|
42
|
+
this.uniqueTrackerSize = 0;
|
|
43
|
+
this.onFullQueue();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Clear the data stored on the cache.
|
|
48
|
+
*/
|
|
49
|
+
UniqueKeysCacheInMemory.prototype.clear = function () {
|
|
50
|
+
this.uniqueKeysTracker = {};
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Pop the collected data, used as payload for posting.
|
|
54
|
+
*/
|
|
55
|
+
UniqueKeysCacheInMemory.prototype.pop = function () {
|
|
56
|
+
var data = this.uniqueKeysTracker;
|
|
57
|
+
this.uniqueKeysTracker = {};
|
|
58
|
+
return fromUniqueKeysCollector(data);
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Check if the cache is empty.
|
|
62
|
+
*/
|
|
63
|
+
UniqueKeysCacheInMemory.prototype.isEmpty = function () {
|
|
64
|
+
return Object.keys(this.uniqueKeysTracker).length === 0;
|
|
65
|
+
};
|
|
66
|
+
return UniqueKeysCacheInMemory;
|
|
67
|
+
}());
|
|
68
|
+
export { UniqueKeysCacheInMemory };
|
|
@@ -0,0 +1,73 @@
|
|
|
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 per feature.
|
|
20
|
+
*/
|
|
21
|
+
UniqueKeysCacheInMemoryCS.prototype.track = function (userKey, featureName) {
|
|
22
|
+
if (!this.uniqueKeysTracker[userKey])
|
|
23
|
+
this.uniqueKeysTracker[userKey] = new _Set();
|
|
24
|
+
var tracker = this.uniqueKeysTracker[userKey];
|
|
25
|
+
if (!tracker.has(featureName)) {
|
|
26
|
+
tracker.add(featureName);
|
|
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
|
+
UniqueKeysCacheInMemoryCS.prototype.clear = function () {
|
|
38
|
+
this.uniqueKeysTracker = {};
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Pop the collected data, used as payload for posting.
|
|
42
|
+
*/
|
|
43
|
+
UniqueKeysCacheInMemoryCS.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
|
+
UniqueKeysCacheInMemoryCS.prototype.isEmpty = function () {
|
|
52
|
+
return Object.keys(this.uniqueKeysTracker).length === 0;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Converts `uniqueKeys` data from cache into request payload.
|
|
56
|
+
*/
|
|
57
|
+
UniqueKeysCacheInMemoryCS.prototype.fromUniqueKeysCollector = function (uniqueKeys) {
|
|
58
|
+
var payload = [];
|
|
59
|
+
var userKeys = Object.keys(uniqueKeys);
|
|
60
|
+
for (var k = 0; k < userKeys.length; k++) {
|
|
61
|
+
var userKey = userKeys[k];
|
|
62
|
+
var featureNames = setToArray(uniqueKeys[userKey]);
|
|
63
|
+
var uniqueKeysPayload = {
|
|
64
|
+
k: userKey,
|
|
65
|
+
fs: featureNames
|
|
66
|
+
};
|
|
67
|
+
payload.push(uniqueKeysPayload);
|
|
68
|
+
}
|
|
69
|
+
return { keys: payload };
|
|
70
|
+
};
|
|
71
|
+
return UniqueKeysCacheInMemoryCS;
|
|
72
|
+
}());
|
|
73
|
+
export { UniqueKeysCacheInMemoryCS };
|
|
@@ -39,7 +39,7 @@ var EventsCacheInRedis = /** @class */ (function () {
|
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
41
|
* Pop the given number of events from the storage.
|
|
42
|
-
* The returned promise rejects if the
|
|
42
|
+
* The returned promise rejects if the redis operation fails.
|
|
43
43
|
*
|
|
44
44
|
* NOTE: this method doesn't take into account MAX_EVENT_SIZE or MAX_QUEUE_BYTE_SIZE limits.
|
|
45
45
|
* It is the submitter responsability to handle that.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { __extends } from "tslib";
|
|
2
|
+
import { forOwn } from '../../utils/lang';
|
|
3
|
+
import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
|
|
4
|
+
import { LOG_PREFIX, REFRESH_RATE, TTL_REFRESH } from './constants';
|
|
5
|
+
var ImpressionCountsCacheInRedis = /** @class */ (function (_super) {
|
|
6
|
+
__extends(ImpressionCountsCacheInRedis, _super);
|
|
7
|
+
function ImpressionCountsCacheInRedis(log, key, redis, impressionCountsCacheSize, refreshRate) {
|
|
8
|
+
if (refreshRate === void 0) { refreshRate = REFRESH_RATE; }
|
|
9
|
+
var _this = _super.call(this, impressionCountsCacheSize) || this;
|
|
10
|
+
_this.log = log;
|
|
11
|
+
_this.key = key;
|
|
12
|
+
_this.redis = redis;
|
|
13
|
+
_this.refreshRate = refreshRate;
|
|
14
|
+
_this.onFullQueue = function () { _this.postImpressionCountsInRedis(); };
|
|
15
|
+
return _this;
|
|
16
|
+
}
|
|
17
|
+
ImpressionCountsCacheInRedis.prototype.postImpressionCountsInRedis = function () {
|
|
18
|
+
var _this = this;
|
|
19
|
+
var counts = this.pop();
|
|
20
|
+
var keys = Object.keys(counts);
|
|
21
|
+
if (!keys.length)
|
|
22
|
+
return Promise.resolve(false);
|
|
23
|
+
var pipeline = this.redis.pipeline();
|
|
24
|
+
keys.forEach(function (key) {
|
|
25
|
+
pipeline.hincrby(_this.key, key, counts[key]);
|
|
26
|
+
});
|
|
27
|
+
return pipeline.exec()
|
|
28
|
+
.then(function (data) {
|
|
29
|
+
// If this is the creation of the key on Redis, set the expiration for it in 3600 seconds.
|
|
30
|
+
if (data.length && data.length === keys.length) {
|
|
31
|
+
return _this.redis.expire(_this.key, TTL_REFRESH);
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
.catch(function (err) {
|
|
35
|
+
_this.log.error(LOG_PREFIX + "Error in impression counts pipeline: " + err + ".");
|
|
36
|
+
return false;
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
ImpressionCountsCacheInRedis.prototype.start = function () {
|
|
40
|
+
this.intervalId = setInterval(this.postImpressionCountsInRedis.bind(this), this.refreshRate);
|
|
41
|
+
};
|
|
42
|
+
ImpressionCountsCacheInRedis.prototype.stop = function () {
|
|
43
|
+
clearInterval(this.intervalId);
|
|
44
|
+
return this.postImpressionCountsInRedis();
|
|
45
|
+
};
|
|
46
|
+
// Async consumer API, used by synchronizer
|
|
47
|
+
ImpressionCountsCacheInRedis.prototype.getImpressionsCount = function () {
|
|
48
|
+
var _this = this;
|
|
49
|
+
return this.redis.hgetall(this.key)
|
|
50
|
+
.then(function (counts) {
|
|
51
|
+
if (!Object.keys(counts).length)
|
|
52
|
+
return undefined;
|
|
53
|
+
_this.redis.del(_this.key).catch(function () { });
|
|
54
|
+
var impressionsCount = { pf: [] };
|
|
55
|
+
forOwn(counts, function (count, key) {
|
|
56
|
+
var nameAndTime = key.split('::');
|
|
57
|
+
if (nameAndTime.length !== 2) {
|
|
58
|
+
_this.log.error(LOG_PREFIX + "Error spliting key " + key);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
var timeFrame = parseInt(nameAndTime[1]);
|
|
62
|
+
if (isNaN(timeFrame)) {
|
|
63
|
+
_this.log.error(LOG_PREFIX + "Error parsing time frame " + nameAndTime[1]);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
var rawCount = parseInt(count);
|
|
67
|
+
if (isNaN(rawCount)) {
|
|
68
|
+
_this.log.error(LOG_PREFIX + "Error parsing raw count " + count);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
impressionsCount.pf.push({
|
|
72
|
+
f: nameAndTime[0],
|
|
73
|
+
m: timeFrame,
|
|
74
|
+
rc: rawCount,
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
return impressionsCount;
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
return ImpressionCountsCacheInRedis;
|
|
81
|
+
}(ImpressionCountsCacheInMemory));
|
|
82
|
+
export { ImpressionCountsCacheInRedis };
|