@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
|
@@ -59,17 +59,17 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
|
|
|
59
59
|
var splitKey = this.keys.buildSplitKey(name);
|
|
60
60
|
return this.redis.get(splitKey).then(function (splitFromStorage) {
|
|
61
61
|
// handling parsing errors
|
|
62
|
-
var parsedPreviousSplit,
|
|
62
|
+
var parsedPreviousSplit, newStringifiedSplit;
|
|
63
63
|
try {
|
|
64
64
|
parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
|
|
65
|
-
|
|
65
|
+
newStringifiedSplit = JSON.stringify(split);
|
|
66
66
|
}
|
|
67
67
|
catch (e) {
|
|
68
68
|
throw new Error('Error parsing split definition: ' + e);
|
|
69
69
|
}
|
|
70
70
|
return Promise.all([
|
|
71
|
-
_this.redis.set(splitKey,
|
|
72
|
-
_this._incrementCounts(
|
|
71
|
+
_this.redis.set(splitKey, newStringifiedSplit),
|
|
72
|
+
_this._incrementCounts(split),
|
|
73
73
|
// If it's an update, we decrement the traffic type of the existing split,
|
|
74
74
|
parsedPreviousSplit && _this._decrementCounts(parsedPreviousSplit)
|
|
75
75
|
]);
|
|
@@ -96,8 +96,7 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
|
|
|
96
96
|
var _this = this;
|
|
97
97
|
return this.getSplit(name).then(function (split) {
|
|
98
98
|
if (split) {
|
|
99
|
-
|
|
100
|
-
_this._decrementCounts(parsedSplit);
|
|
99
|
+
_this._decrementCounts(split);
|
|
101
100
|
}
|
|
102
101
|
return _this.redis.del(_this.keys.buildSplitKey(name));
|
|
103
102
|
});
|
|
@@ -120,7 +119,8 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
|
|
|
120
119
|
this.log.error(LOG_PREFIX + this.redisError);
|
|
121
120
|
return Promise.reject(this.redisError);
|
|
122
121
|
}
|
|
123
|
-
return this.redis.get(this.keys.buildSplitKey(name))
|
|
122
|
+
return this.redis.get(this.keys.buildSplitKey(name))
|
|
123
|
+
.then(function (maybeSplit) { return maybeSplit && JSON.parse(maybeSplit); });
|
|
124
124
|
};
|
|
125
125
|
/**
|
|
126
126
|
* Set till number.
|
|
@@ -155,7 +155,12 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
|
|
|
155
155
|
*/
|
|
156
156
|
SplitsCacheInRedis.prototype.getAll = function () {
|
|
157
157
|
var _this = this;
|
|
158
|
-
return this.redis.keys(this.keys.searchPatternForSplitKeys())
|
|
158
|
+
return this.redis.keys(this.keys.searchPatternForSplitKeys())
|
|
159
|
+
.then(function (listOfKeys) { return _this.redis.pipeline(listOfKeys.map(function (k) { return ['get', k]; })).exec(); })
|
|
160
|
+
.then(processPipelineAnswer)
|
|
161
|
+
.then(function (splitDefinitions) { return splitDefinitions.map(function (splitDefinition) {
|
|
162
|
+
return JSON.parse(splitDefinition);
|
|
163
|
+
}); });
|
|
159
164
|
};
|
|
160
165
|
/**
|
|
161
166
|
* Get list of split names.
|
|
@@ -215,7 +220,8 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
|
|
|
215
220
|
var keys = names.map(function (name) { return _this.keys.buildSplitKey(name); });
|
|
216
221
|
return (_a = this.redis).mget.apply(_a, keys).then(function (splitDefinitions) {
|
|
217
222
|
names.forEach(function (name, idx) {
|
|
218
|
-
|
|
223
|
+
var split = splitDefinitions[idx];
|
|
224
|
+
splits[name] = split && JSON.parse(split);
|
|
219
225
|
});
|
|
220
226
|
return Promise.resolve(splits);
|
|
221
227
|
})
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { parseExceptionField, parseLatencyField, parseMetadata } from '../KeyBuilderSS';
|
|
1
2
|
import { findLatencyIndex } from '../findLatencyIndex';
|
|
2
3
|
import { getTelemetryConfigStats } from '../../sync/submitters/telemetrySubmitter';
|
|
3
4
|
import { CONSUMER_MODE, STORAGE_REDIS } from '../../utils/constants';
|
|
5
|
+
import { isNaNNumber, isString } from '../../utils/lang';
|
|
6
|
+
import { _Map } from '../../utils/lang/maps';
|
|
7
|
+
import { MAX_LATENCY_BUCKET_COUNT, newBuckets } from '../inMemory/TelemetryCacheInMemory';
|
|
4
8
|
var TelemetryCacheInRedis = /** @class */ (function () {
|
|
5
9
|
/**
|
|
6
10
|
* Create a Telemetry cache that uses Redis as storage.
|
|
@@ -28,6 +32,102 @@ var TelemetryCacheInRedis = /** @class */ (function () {
|
|
|
28
32
|
var value = JSON.stringify(getTelemetryConfigStats(CONSUMER_MODE, STORAGE_REDIS));
|
|
29
33
|
return this.redis.hset(key, field, value).catch(function () { });
|
|
30
34
|
};
|
|
35
|
+
/**
|
|
36
|
+
* Pop telemetry latencies.
|
|
37
|
+
* The returned promise rejects if redis operations fail.
|
|
38
|
+
*/
|
|
39
|
+
TelemetryCacheInRedis.prototype.popLatencies = function () {
|
|
40
|
+
var _this = this;
|
|
41
|
+
return this.redis.hgetall(this.keys.latencyPrefix).then(function (latencies) {
|
|
42
|
+
var result = new _Map();
|
|
43
|
+
Object.keys(latencies).forEach(function (field) {
|
|
44
|
+
var parsedField = parseLatencyField(field);
|
|
45
|
+
if (isString(parsedField)) {
|
|
46
|
+
_this.log.error("Ignoring invalid latency field: " + field + ": " + parsedField);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
var count = parseInt(latencies[field]);
|
|
50
|
+
if (isNaNNumber(count)) {
|
|
51
|
+
_this.log.error("Ignoring latency with invalid count: " + latencies[field]);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
var metadata = parsedField[0], method = parsedField[1], bucket = parsedField[2];
|
|
55
|
+
if (bucket >= MAX_LATENCY_BUCKET_COUNT) {
|
|
56
|
+
_this.log.error("Ignoring latency with invalid bucket: " + bucket);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (!result.has(metadata))
|
|
60
|
+
result.set(metadata, {
|
|
61
|
+
t: newBuckets(),
|
|
62
|
+
ts: newBuckets(),
|
|
63
|
+
tc: newBuckets(),
|
|
64
|
+
tcs: newBuckets(),
|
|
65
|
+
tr: newBuckets(),
|
|
66
|
+
});
|
|
67
|
+
result.get(metadata)[method][bucket] = count;
|
|
68
|
+
});
|
|
69
|
+
return _this.redis.del(_this.keys.latencyPrefix).then(function () { return result; });
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Pop telemetry exceptions.
|
|
74
|
+
* The returned promise rejects if redis operations fail.
|
|
75
|
+
*/
|
|
76
|
+
TelemetryCacheInRedis.prototype.popExceptions = function () {
|
|
77
|
+
var _this = this;
|
|
78
|
+
return this.redis.hgetall(this.keys.exceptionPrefix).then(function (exceptions) {
|
|
79
|
+
var result = new _Map();
|
|
80
|
+
Object.keys(exceptions).forEach(function (field) {
|
|
81
|
+
var parsedField = parseExceptionField(field);
|
|
82
|
+
if (isString(parsedField)) {
|
|
83
|
+
_this.log.error("Ignoring invalid exception field: " + field + ": " + parsedField);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
var count = parseInt(exceptions[field]);
|
|
87
|
+
if (isNaNNumber(count)) {
|
|
88
|
+
_this.log.error("Ignoring exception with invalid count: " + exceptions[field]);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
var metadata = parsedField[0], method = parsedField[1];
|
|
92
|
+
if (!result.has(metadata))
|
|
93
|
+
result.set(metadata, {
|
|
94
|
+
t: 0,
|
|
95
|
+
ts: 0,
|
|
96
|
+
tc: 0,
|
|
97
|
+
tcs: 0,
|
|
98
|
+
tr: 0,
|
|
99
|
+
});
|
|
100
|
+
result.get(metadata)[method] = count;
|
|
101
|
+
});
|
|
102
|
+
return _this.redis.del(_this.keys.exceptionPrefix).then(function () { return result; });
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Pop telemetry configs.
|
|
107
|
+
* The returned promise rejects if redis operations fail.
|
|
108
|
+
*/
|
|
109
|
+
TelemetryCacheInRedis.prototype.popConfigs = function () {
|
|
110
|
+
var _this = this;
|
|
111
|
+
return this.redis.hgetall(this.keys.initPrefix).then(function (configs) {
|
|
112
|
+
var result = new _Map();
|
|
113
|
+
Object.keys(configs).forEach(function (field) {
|
|
114
|
+
var parsedField = parseMetadata(field);
|
|
115
|
+
if (isString(parsedField)) {
|
|
116
|
+
_this.log.error("Ignoring invalid config field: " + field + ": " + parsedField);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
var metadata = parsedField[0];
|
|
120
|
+
try {
|
|
121
|
+
var config = JSON.parse(configs[field]);
|
|
122
|
+
result.set(metadata, config);
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
_this.log.error("Ignoring invalid config: " + configs[field]);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
return _this.redis.del(_this.keys.initPrefix).then(function () { return result; });
|
|
129
|
+
});
|
|
130
|
+
};
|
|
31
131
|
return TelemetryCacheInRedis;
|
|
32
132
|
}());
|
|
33
133
|
export { TelemetryCacheInRedis };
|
|
@@ -0,0 +1,68 @@
|
|
|
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 featureNames = Object.keys(this.uniqueKeysTracker);
|
|
22
|
+
if (!featureNames.length)
|
|
23
|
+
return Promise.resolve(false);
|
|
24
|
+
var pipeline = this.redis.pipeline();
|
|
25
|
+
for (var i = 0; i < featureNames.length; i++) {
|
|
26
|
+
var featureName = featureNames[i];
|
|
27
|
+
var featureKeys = setToArray(this.uniqueKeysTracker[featureName]);
|
|
28
|
+
var uniqueKeysPayload = {
|
|
29
|
+
f: featureName,
|
|
30
|
+
ks: featureKeys
|
|
31
|
+
};
|
|
32
|
+
pipeline.rpush(this.key, JSON.stringify(uniqueKeysPayload));
|
|
33
|
+
}
|
|
34
|
+
this.clear();
|
|
35
|
+
return pipeline.exec()
|
|
36
|
+
.then(function (data) {
|
|
37
|
+
// If this is the creation of the key on Redis, set the expiration for it in 3600 seconds.
|
|
38
|
+
if (data.length && data.length === featureNames.length) {
|
|
39
|
+
return _this.redis.expire(_this.key, TTL_REFRESH);
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
.catch(function (err) {
|
|
43
|
+
_this.log.error(LOG_PREFIX + "Error in uniqueKeys pipeline: " + err + ".");
|
|
44
|
+
return false;
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
UniqueKeysCacheInRedis.prototype.start = function () {
|
|
48
|
+
this.intervalId = setInterval(this.postUniqueKeysInRedis.bind(this), this.refreshRate);
|
|
49
|
+
};
|
|
50
|
+
UniqueKeysCacheInRedis.prototype.stop = function () {
|
|
51
|
+
clearInterval(this.intervalId);
|
|
52
|
+
return this.postUniqueKeysInRedis();
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Async consumer API, used by synchronizer.
|
|
56
|
+
* @param count number of items to pop from the queue. If not provided or equal 0, all items will be popped.
|
|
57
|
+
*/
|
|
58
|
+
UniqueKeysCacheInRedis.prototype.popNRaw = function (count) {
|
|
59
|
+
var _this = this;
|
|
60
|
+
if (count === void 0) { count = 0; }
|
|
61
|
+
return this.redis.lrange(this.key, 0, count - 1).then(function (uniqueKeyItems) {
|
|
62
|
+
return _this.redis.ltrim(_this.key, uniqueKeyItems.length, -1)
|
|
63
|
+
.then(function () { return uniqueKeyItems.map(function (uniqueKeyItem) { return JSON.parse(uniqueKeyItem); }); });
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
return UniqueKeysCacheInRedis;
|
|
67
|
+
}(UniqueKeysCacheInMemory));
|
|
68
|
+
export { UniqueKeysCacheInRedis };
|
|
@@ -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;
|
|
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) : undefined;
|
|
25
|
+
var uniqueKeysCache = impressionsMode === NONE ? new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient) : 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,19 @@ 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
|
+
var promises = [];
|
|
48
|
+
if (impressionCountsCache)
|
|
49
|
+
promises.push(impressionCountsCache.stop());
|
|
50
|
+
if (uniqueKeysCache)
|
|
51
|
+
promises.push(uniqueKeysCache.stop());
|
|
52
|
+
return Promise.all(promises).then(function () { redisClient.disconnect(); });
|
|
38
53
|
// @TODO check that caches works as expected when redisClient is disconnected
|
|
39
54
|
}
|
|
40
55
|
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { __extends } from "tslib";
|
|
2
|
+
import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
|
|
3
|
+
import { REFRESH_RATE } from '../inRedis/constants';
|
|
4
|
+
import { LOG_PREFIX } from './constants';
|
|
5
|
+
var ImpressionCountsCachePluggable = /** @class */ (function (_super) {
|
|
6
|
+
__extends(ImpressionCountsCachePluggable, _super);
|
|
7
|
+
function ImpressionCountsCachePluggable(log, key, wrapper, 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.wrapper = wrapper;
|
|
13
|
+
_this.refreshRate = refreshRate;
|
|
14
|
+
_this.onFullQueue = function () { _this.storeImpressionCounts(); };
|
|
15
|
+
return _this;
|
|
16
|
+
}
|
|
17
|
+
ImpressionCountsCachePluggable.prototype.storeImpressionCounts = 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 = keys.reduce(function (pipeline, key) {
|
|
24
|
+
return pipeline.then(function () { return _this.wrapper.incr(_this.key + "::" + key, counts[key]); });
|
|
25
|
+
}, Promise.resolve());
|
|
26
|
+
return pipeline.catch(function (err) {
|
|
27
|
+
_this.log.error(LOG_PREFIX + "Error in impression counts pipeline: " + err + ".");
|
|
28
|
+
return false;
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
ImpressionCountsCachePluggable.prototype.start = function () {
|
|
32
|
+
this.intervalId = setInterval(this.storeImpressionCounts.bind(this), this.refreshRate);
|
|
33
|
+
};
|
|
34
|
+
ImpressionCountsCachePluggable.prototype.stop = function () {
|
|
35
|
+
clearInterval(this.intervalId);
|
|
36
|
+
return this.storeImpressionCounts();
|
|
37
|
+
};
|
|
38
|
+
// Async consumer API, used by synchronizer
|
|
39
|
+
ImpressionCountsCachePluggable.prototype.getImpressionsCount = function () {
|
|
40
|
+
var _this = this;
|
|
41
|
+
return this.wrapper.getKeysByPrefix(this.key)
|
|
42
|
+
.then(function (keys) {
|
|
43
|
+
return keys.length ? Promise.all(keys.map(function (key) { return _this.wrapper.get(key); }))
|
|
44
|
+
.then(function (counts) {
|
|
45
|
+
keys.forEach(function (key) { return _this.wrapper.del(key).catch(function () { }); });
|
|
46
|
+
var impressionsCount = { pf: [] };
|
|
47
|
+
for (var i = 0; i < keys.length; i++) {
|
|
48
|
+
var key = keys[i];
|
|
49
|
+
var count = counts[i];
|
|
50
|
+
var keyFeatureNameAndTime = key.split('::');
|
|
51
|
+
if (keyFeatureNameAndTime.length !== 3) {
|
|
52
|
+
_this.log.error(LOG_PREFIX + "Error spliting key " + key);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
var timeFrame = parseInt(keyFeatureNameAndTime[2]);
|
|
56
|
+
if (isNaN(timeFrame)) {
|
|
57
|
+
_this.log.error(LOG_PREFIX + "Error parsing time frame " + keyFeatureNameAndTime[2]);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
var rawCount = parseInt(count);
|
|
62
|
+
if (isNaN(rawCount)) {
|
|
63
|
+
_this.log.error(LOG_PREFIX + "Error parsing raw count " + count);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
impressionsCount.pf.push({
|
|
67
|
+
f: keyFeatureNameAndTime[1],
|
|
68
|
+
m: timeFrame,
|
|
69
|
+
rc: rawCount,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return impressionsCount;
|
|
73
|
+
}) : undefined;
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
return ImpressionCountsCachePluggable;
|
|
77
|
+
}(ImpressionCountsCacheInMemory));
|
|
78
|
+
export { ImpressionCountsCachePluggable };
|
|
@@ -46,17 +46,17 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
|
|
|
46
46
|
var splitKey = this.keys.buildSplitKey(name);
|
|
47
47
|
return this.wrapper.get(splitKey).then(function (splitFromStorage) {
|
|
48
48
|
// handling parsing error
|
|
49
|
-
var parsedPreviousSplit,
|
|
49
|
+
var parsedPreviousSplit, stringifiedNewSplit;
|
|
50
50
|
try {
|
|
51
51
|
parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
|
|
52
|
-
|
|
52
|
+
stringifiedNewSplit = JSON.stringify(split);
|
|
53
53
|
}
|
|
54
54
|
catch (e) {
|
|
55
55
|
throw new Error('Error parsing split definition: ' + e);
|
|
56
56
|
}
|
|
57
57
|
return Promise.all([
|
|
58
|
-
_this.wrapper.set(splitKey,
|
|
59
|
-
_this._incrementCounts(
|
|
58
|
+
_this.wrapper.set(splitKey, stringifiedNewSplit),
|
|
59
|
+
_this._incrementCounts(split),
|
|
60
60
|
// If it's an update, we decrement the traffic type and segment count of the existing split,
|
|
61
61
|
parsedPreviousSplit && _this._decrementCounts(parsedPreviousSplit)
|
|
62
62
|
]);
|
|
@@ -80,8 +80,7 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
|
|
|
80
80
|
var _this = this;
|
|
81
81
|
return this.getSplit(name).then(function (split) {
|
|
82
82
|
if (split) {
|
|
83
|
-
|
|
84
|
-
_this._decrementCounts(parsedSplit);
|
|
83
|
+
_this._decrementCounts(split);
|
|
85
84
|
}
|
|
86
85
|
return _this.wrapper.del(_this.keys.buildSplitKey(name));
|
|
87
86
|
});
|
|
@@ -101,7 +100,8 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
|
|
|
101
100
|
* or rejected if wrapper operation fails.
|
|
102
101
|
*/
|
|
103
102
|
SplitsCachePluggable.prototype.getSplit = function (name) {
|
|
104
|
-
return this.wrapper.get(this.keys.buildSplitKey(name))
|
|
103
|
+
return this.wrapper.get(this.keys.buildSplitKey(name))
|
|
104
|
+
.then(function (maybeSplit) { return maybeSplit && JSON.parse(maybeSplit); });
|
|
105
105
|
};
|
|
106
106
|
/**
|
|
107
107
|
* Get list of splits.
|
|
@@ -114,7 +114,8 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
|
|
|
114
114
|
return this.wrapper.getMany(keys).then(function (splitDefinitions) {
|
|
115
115
|
var splits = {};
|
|
116
116
|
names.forEach(function (name, idx) {
|
|
117
|
-
|
|
117
|
+
var split = splitDefinitions[idx];
|
|
118
|
+
splits[name] = split && JSON.parse(split);
|
|
118
119
|
});
|
|
119
120
|
return Promise.resolve(splits);
|
|
120
121
|
});
|
|
@@ -126,7 +127,11 @@ var SplitsCachePluggable = /** @class */ (function (_super) {
|
|
|
126
127
|
*/
|
|
127
128
|
SplitsCachePluggable.prototype.getAll = function () {
|
|
128
129
|
var _this = this;
|
|
129
|
-
return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix())
|
|
130
|
+
return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix())
|
|
131
|
+
.then(function (listOfKeys) { return _this.wrapper.getMany(listOfKeys); })
|
|
132
|
+
.then(function (splitDefinitions) { return splitDefinitions.map(function (splitDefinition) {
|
|
133
|
+
return JSON.parse(splitDefinition);
|
|
134
|
+
}); });
|
|
130
135
|
};
|
|
131
136
|
/**
|
|
132
137
|
* Get list of split names.
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
+
import { parseExceptionField, parseLatencyField, parseMetadata } from '../KeyBuilderSS';
|
|
1
2
|
import { findLatencyIndex } from '../findLatencyIndex';
|
|
3
|
+
import { getTelemetryConfigStats } from '../../sync/submitters/telemetrySubmitter';
|
|
4
|
+
import { CONSUMER_MODE, STORAGE_PLUGGABLE } from '../../utils/constants';
|
|
5
|
+
import { isString, isNaNNumber } from '../../utils/lang';
|
|
6
|
+
import { _Map } from '../../utils/lang/maps';
|
|
7
|
+
import { MAX_LATENCY_BUCKET_COUNT, newBuckets } from '../inMemory/TelemetryCacheInMemory';
|
|
2
8
|
var TelemetryCachePluggable = /** @class */ (function () {
|
|
3
9
|
/**
|
|
4
10
|
* Create a Telemetry cache that uses a storage wrapper.
|
|
@@ -19,6 +25,126 @@ var TelemetryCachePluggable = /** @class */ (function () {
|
|
|
19
25
|
return this.wrapper.incr(this.keys.buildExceptionKey(method))
|
|
20
26
|
.catch(function () { });
|
|
21
27
|
};
|
|
28
|
+
TelemetryCachePluggable.prototype.recordConfig = function () {
|
|
29
|
+
var value = JSON.stringify(getTelemetryConfigStats(CONSUMER_MODE, STORAGE_PLUGGABLE));
|
|
30
|
+
return this.wrapper.set(this.keys.buildInitKey(), value).catch(function () { });
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Pop telemetry latencies.
|
|
34
|
+
* The returned promise rejects if wrapper operations fail.
|
|
35
|
+
*/
|
|
36
|
+
TelemetryCachePluggable.prototype.popLatencies = function () {
|
|
37
|
+
var _this = this;
|
|
38
|
+
return this.wrapper.getKeysByPrefix(this.keys.latencyPrefix).then(function (latencyKeys) {
|
|
39
|
+
return latencyKeys.length ?
|
|
40
|
+
_this.wrapper.getMany(latencyKeys).then(function (latencies) {
|
|
41
|
+
var result = new _Map();
|
|
42
|
+
for (var i = 0; i < latencyKeys.length; i++) {
|
|
43
|
+
var field = latencyKeys[i].split('::')[1];
|
|
44
|
+
var parsedField = parseLatencyField(field);
|
|
45
|
+
if (isString(parsedField)) {
|
|
46
|
+
_this.log.error("Ignoring invalid latency field: " + field + ": " + parsedField);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
var count = parseInt(latencies[i]);
|
|
51
|
+
if (isNaNNumber(count)) {
|
|
52
|
+
_this.log.error("Ignoring latency with invalid count: " + latencies[i]);
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
var metadata = parsedField[0], method = parsedField[1], bucket = parsedField[2];
|
|
56
|
+
if (bucket >= MAX_LATENCY_BUCKET_COUNT) {
|
|
57
|
+
_this.log.error("Ignoring latency with invalid bucket: " + bucket);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (!result.has(metadata))
|
|
61
|
+
result.set(metadata, {
|
|
62
|
+
t: newBuckets(),
|
|
63
|
+
ts: newBuckets(),
|
|
64
|
+
tc: newBuckets(),
|
|
65
|
+
tcs: newBuckets(),
|
|
66
|
+
tr: newBuckets(),
|
|
67
|
+
});
|
|
68
|
+
result.get(metadata)[method][bucket] = count;
|
|
69
|
+
}
|
|
70
|
+
return Promise.all(latencyKeys.map(function (latencyKey) { return _this.wrapper.del(latencyKey); })).then(function () { return result; });
|
|
71
|
+
}) :
|
|
72
|
+
// If latencyKeys is empty, return an empty map.
|
|
73
|
+
new _Map();
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Pop telemetry exceptions.
|
|
78
|
+
* The returned promise rejects if wrapper operations fail.
|
|
79
|
+
*/
|
|
80
|
+
TelemetryCachePluggable.prototype.popExceptions = function () {
|
|
81
|
+
var _this = this;
|
|
82
|
+
return this.wrapper.getKeysByPrefix(this.keys.exceptionPrefix).then(function (exceptionKeys) {
|
|
83
|
+
return exceptionKeys.length ?
|
|
84
|
+
_this.wrapper.getMany(exceptionKeys).then(function (exceptions) {
|
|
85
|
+
var result = new _Map();
|
|
86
|
+
for (var i = 0; i < exceptionKeys.length; i++) {
|
|
87
|
+
var field = exceptionKeys[i].split('::')[1];
|
|
88
|
+
var parsedField = parseExceptionField(field);
|
|
89
|
+
if (isString(parsedField)) {
|
|
90
|
+
_this.log.error("Ignoring invalid exception field: " + field + ": " + parsedField);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
var count = parseInt(exceptions[i]);
|
|
95
|
+
if (isNaNNumber(count)) {
|
|
96
|
+
_this.log.error("Ignoring exception with invalid count: " + exceptions[i]);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
var metadata = parsedField[0], method = parsedField[1];
|
|
100
|
+
if (!result.has(metadata))
|
|
101
|
+
result.set(metadata, {
|
|
102
|
+
t: 0,
|
|
103
|
+
ts: 0,
|
|
104
|
+
tc: 0,
|
|
105
|
+
tcs: 0,
|
|
106
|
+
tr: 0,
|
|
107
|
+
});
|
|
108
|
+
result.get(metadata)[method] = count;
|
|
109
|
+
}
|
|
110
|
+
return Promise.all(exceptionKeys.map(function (exceptionKey) { return _this.wrapper.del(exceptionKey); })).then(function () { return result; });
|
|
111
|
+
}) :
|
|
112
|
+
// If exceptionKeys is empty, return an empty map.
|
|
113
|
+
new _Map();
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Pop telemetry configs.
|
|
118
|
+
* The returned promise rejects if wrapper operations fail.
|
|
119
|
+
*/
|
|
120
|
+
TelemetryCachePluggable.prototype.popConfigs = function () {
|
|
121
|
+
var _this = this;
|
|
122
|
+
return this.wrapper.getKeysByPrefix(this.keys.initPrefix).then(function (configKeys) {
|
|
123
|
+
return configKeys.length ?
|
|
124
|
+
_this.wrapper.getMany(configKeys).then(function (configs) {
|
|
125
|
+
var result = new _Map();
|
|
126
|
+
for (var i = 0; i < configKeys.length; i++) {
|
|
127
|
+
var field = configKeys[i].split('::')[1];
|
|
128
|
+
var parsedField = parseMetadata(field);
|
|
129
|
+
if (isString(parsedField)) {
|
|
130
|
+
_this.log.error("Ignoring invalid config field: " + field + ": " + parsedField);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
var metadata = parsedField[0];
|
|
134
|
+
try { // @ts-ignore
|
|
135
|
+
var config = JSON.parse(configs[i]);
|
|
136
|
+
result.set(metadata, config);
|
|
137
|
+
}
|
|
138
|
+
catch (e) {
|
|
139
|
+
_this.log.error("Ignoring invalid config: " + configs[i]);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return Promise.all(configKeys.map(function (configKey) { return _this.wrapper.del(configKey); })).then(function () { return result; });
|
|
143
|
+
}) :
|
|
144
|
+
// If configKeys is empty, return an empty map.
|
|
145
|
+
new _Map();
|
|
146
|
+
});
|
|
147
|
+
};
|
|
22
148
|
return TelemetryCachePluggable;
|
|
23
149
|
}());
|
|
24
150
|
export { TelemetryCachePluggable };
|
|
@@ -0,0 +1,58 @@
|
|
|
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 } from '../inRedis/constants';
|
|
5
|
+
import { LOG_PREFIX } from './constants';
|
|
6
|
+
var UniqueKeysCachePluggable = /** @class */ (function (_super) {
|
|
7
|
+
__extends(UniqueKeysCachePluggable, _super);
|
|
8
|
+
function UniqueKeysCachePluggable(log, key, wrapper, 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.wrapper = wrapper;
|
|
15
|
+
_this.refreshRate = refreshRate;
|
|
16
|
+
_this.onFullQueue = function () { _this.storeUniqueKeys(); };
|
|
17
|
+
return _this;
|
|
18
|
+
}
|
|
19
|
+
UniqueKeysCachePluggable.prototype.storeUniqueKeys = function () {
|
|
20
|
+
var _this = this;
|
|
21
|
+
var featureNames = Object.keys(this.uniqueKeysTracker);
|
|
22
|
+
if (!featureNames.length)
|
|
23
|
+
return Promise.resolve(false);
|
|
24
|
+
var pipeline = featureNames.reduce(function (pipeline, featureName) {
|
|
25
|
+
var featureKeys = setToArray(_this.uniqueKeysTracker[featureName]);
|
|
26
|
+
var uniqueKeysPayload = {
|
|
27
|
+
f: featureName,
|
|
28
|
+
ks: featureKeys
|
|
29
|
+
};
|
|
30
|
+
return pipeline.then(function () { return _this.wrapper.pushItems(_this.key, [JSON.stringify(uniqueKeysPayload)]); });
|
|
31
|
+
}, Promise.resolve());
|
|
32
|
+
this.clear();
|
|
33
|
+
return pipeline.catch(function (err) {
|
|
34
|
+
_this.log.error(LOG_PREFIX + "Error in uniqueKeys pipeline: " + err + ".");
|
|
35
|
+
return false;
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
UniqueKeysCachePluggable.prototype.start = function () {
|
|
39
|
+
this.intervalId = setInterval(this.storeUniqueKeys.bind(this), this.refreshRate);
|
|
40
|
+
};
|
|
41
|
+
UniqueKeysCachePluggable.prototype.stop = function () {
|
|
42
|
+
clearInterval(this.intervalId);
|
|
43
|
+
return this.storeUniqueKeys();
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Async consumer API, used by synchronizer.
|
|
47
|
+
* @param count number of items to pop from the queue. If not provided or equal 0, all items will be popped.
|
|
48
|
+
*/
|
|
49
|
+
UniqueKeysCachePluggable.prototype.popNRaw = function (count) {
|
|
50
|
+
var _this = this;
|
|
51
|
+
if (count === void 0) { count = 0; }
|
|
52
|
+
return Promise.resolve(count || this.wrapper.getItemsCount(this.key))
|
|
53
|
+
.then(function (count) { return _this.wrapper.popItems(_this.key, count); })
|
|
54
|
+
.then(function (uniqueKeyItems) { return uniqueKeyItems.map(function (uniqueKeyItem) { return JSON.parse(uniqueKeyItem); }); });
|
|
55
|
+
};
|
|
56
|
+
return UniqueKeysCachePluggable;
|
|
57
|
+
}(UniqueKeysCacheInMemory));
|
|
58
|
+
export { UniqueKeysCachePluggable };
|