@microsoft/applicationinsights-common 3.0.0-beta.2303-02 → 3.0.0-beta.2303-03
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/browser/applicationinsights-common.integrity.json +9 -9
- package/browser/applicationinsights-common.js +116 -33
- package/browser/applicationinsights-common.js.map +1 -1
- package/browser/applicationinsights-common.min.js +2 -2
- package/browser/applicationinsights-common.min.js.map +1 -1
- package/dist/applicationinsights-common.api.json +57 -9
- package/dist/applicationinsights-common.api.md +5 -5
- package/dist/applicationinsights-common.d.ts +16 -3
- package/dist/applicationinsights-common.js +116 -33
- package/dist/applicationinsights-common.js.map +1 -1
- package/dist/applicationinsights-common.min.js +2 -2
- package/dist/applicationinsights-common.min.js.map +1 -1
- package/dist/applicationinsights-common.rollup.d.ts +16 -3
- package/dist-esm/ConnectionStringParser.js +1 -1
- package/dist-esm/Constants.js +1 -1
- package/dist-esm/DomHelperFuncs.js +1 -1
- package/dist-esm/Enums.js +1 -1
- package/dist-esm/HelperFuncs.js +1 -1
- package/dist-esm/Interfaces/ConnectionString.js +1 -1
- package/dist-esm/Interfaces/Context/IApplication.js +1 -1
- package/dist-esm/Interfaces/Context/IDevice.js +1 -1
- package/dist-esm/Interfaces/Context/IInternal.js +1 -1
- package/dist-esm/Interfaces/Context/ILocation.js +1 -1
- package/dist-esm/Interfaces/Context/IOperatingSystem.js +1 -1
- package/dist-esm/Interfaces/Context/ISample.js +1 -1
- package/dist-esm/Interfaces/Context/ISession.js +1 -1
- package/dist-esm/Interfaces/Context/ITelemetryTrace.js +1 -1
- package/dist-esm/Interfaces/Context/IUser.js +1 -1
- package/dist-esm/Interfaces/Context/IWeb.js +1 -1
- package/dist-esm/Interfaces/Contracts/AvailabilityData.js +1 -1
- package/dist-esm/Interfaces/Contracts/ContextTagKeys.js +1 -1
- package/dist-esm/Interfaces/Contracts/DataPointType.js +1 -1
- package/dist-esm/Interfaces/Contracts/DependencyKind.js +1 -1
- package/dist-esm/Interfaces/Contracts/DependencySourceType.js +1 -1
- package/dist-esm/Interfaces/Contracts/IBase.js +1 -1
- package/dist-esm/Interfaces/Contracts/IData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IDataPoint.js +1 -1
- package/dist-esm/Interfaces/Contracts/IDomain.js +1 -1
- package/dist-esm/Interfaces/Contracts/IEnvelope.js +1 -1
- package/dist-esm/Interfaces/Contracts/IEventData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IExceptionData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IExceptionDetails.js +1 -1
- package/dist-esm/Interfaces/Contracts/IMessageData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IMetricData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IPageViewData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IPageViewPerfData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IRemoteDependencyData.js +1 -1
- package/dist-esm/Interfaces/Contracts/IStackFrame.js +1 -1
- package/dist-esm/Interfaces/Contracts/RequestData.js +1 -1
- package/dist-esm/Interfaces/Contracts/SeverityLevel.js +1 -1
- package/dist-esm/Interfaces/IAppInsights.js +1 -1
- package/dist-esm/Interfaces/IChannelControlsAI.js +1 -1
- package/dist-esm/Interfaces/IConfig.js +1 -1
- package/dist-esm/Interfaces/ICorrelationConfig.js +1 -1
- package/dist-esm/Interfaces/IDependencyTelemetry.js +1 -1
- package/dist-esm/Interfaces/IEventTelemetry.js +1 -1
- package/dist-esm/Interfaces/IExceptionTelemetry.js +1 -1
- package/dist-esm/Interfaces/IMetricTelemetry.js +1 -1
- package/dist-esm/Interfaces/IPageViewPerformanceTelemetry.js +1 -1
- package/dist-esm/Interfaces/IPageViewTelemetry.js +1 -1
- package/dist-esm/Interfaces/IPartC.js +1 -1
- package/dist-esm/Interfaces/IPropertiesPlugin.js +1 -1
- package/dist-esm/Interfaces/IRequestContext.js +1 -1
- package/dist-esm/Interfaces/ITelemetryContext.js +1 -1
- package/dist-esm/Interfaces/IThrottleMgr.js +1 -1
- package/dist-esm/Interfaces/ITraceTelemetry.js +1 -1
- package/dist-esm/Interfaces/PartAExtensions.js +1 -1
- package/dist-esm/Interfaces/Telemetry/IEnvelope.js +1 -1
- package/dist-esm/Interfaces/Telemetry/ISerializable.js +1 -1
- package/dist-esm/RequestResponseHeaders.js +1 -1
- package/dist-esm/StorageHelperFuncs.js +1 -1
- package/dist-esm/Telemetry/Common/Data.js +1 -1
- package/dist-esm/Telemetry/Common/DataPoint.js +1 -1
- package/dist-esm/Telemetry/Common/DataSanitizer.js +1 -1
- package/dist-esm/Telemetry/Common/Envelope.js +1 -1
- package/dist-esm/Telemetry/Event.js +1 -1
- package/dist-esm/Telemetry/Exception.js +1 -1
- package/dist-esm/Telemetry/Metric.js +1 -1
- package/dist-esm/Telemetry/PageView.js +1 -1
- package/dist-esm/Telemetry/PageViewPerformance.js +1 -1
- package/dist-esm/Telemetry/RemoteDependencyData.js +1 -1
- package/dist-esm/Telemetry/Trace.js +1 -1
- package/dist-esm/TelemetryItemCreator.js +1 -1
- package/dist-esm/ThrottleMgr.js +54 -34
- package/dist-esm/ThrottleMgr.js.map +1 -1
- package/dist-esm/UrlHelperFuncs.js +1 -1
- package/dist-esm/Util.js +1 -1
- package/dist-esm/__DynamicConstants.js +1 -1
- package/dist-esm/applicationinsights-common.js +1 -1
- package/package.json +2 -2
- package/src/Enums.ts +10 -0
- package/src/Interfaces/IThrottleMgr.ts +4 -2
- package/src/ThrottleMgr.ts +61 -33
- package/src/applicationinsights-common.ts +1 -1
- package/types/Enums.d.ts +9 -0
- package/types/Interfaces/IThrottleMgr.d.ts +3 -1
- package/types/ThrottleMgr.d.ts +3 -2
- package/types/applicationinsights-common.d.ts +1 -1
package/dist-esm/ThrottleMgr.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Application Insights JavaScript SDK - Common, 3.0.0-beta.2303-
|
|
2
|
+
* Application Insights JavaScript SDK - Common, 3.0.0-beta.2303-03
|
|
3
3
|
* Copyright (c) Microsoft and contributors. All rights reserved.
|
|
4
4
|
*/
|
|
5
|
-
import { _throwInternal, arrForEach, arrIndexOf, isNotNullOrUndefined, isNullOrUndefined, randomValue, safeGetLogger, strTrim } from "@microsoft/applicationinsights-core-js";
|
|
5
|
+
import { _throwInternal, arrForEach, arrIndexOf, createDynamicConfig, createUnloadHookContainer, isNotNullOrUndefined, isNullOrUndefined, onConfigChange, randomValue, safeGetLogger, strTrim } from "@microsoft/applicationinsights-core-js";
|
|
6
6
|
import { utlCanUseLocalStorage, utlGetLocalStorage, utlSetLocalStorage } from "./StorageHelperFuncs";
|
|
7
7
|
import { _DYN_COUNT, _DYN_DATE, _DYN_DAYS_OF_MONTH, _DYN_DISABLED, _DYN_GET_UTCDATE, _DYN_INTERVAL, _DYN_LENGTH, _DYN_MESSAGE, _DYN_PRE_TRIGGER_DATE, _DYN_STRINGIFY } from "./__DynamicConstants";
|
|
8
8
|
var THROTTLE_STORAGE_PREFIX = "appInsightsThrottle";
|
|
9
9
|
var ThrottleMgr = /** @class */ (function () {
|
|
10
|
-
function ThrottleMgr(
|
|
10
|
+
function ThrottleMgr(config, core, namePrefix, unloadHookContainer) {
|
|
11
11
|
var _self = this;
|
|
12
12
|
var _canUseLocalStorage;
|
|
13
13
|
var _logger;
|
|
@@ -20,6 +20,10 @@ var ThrottleMgr = /** @class */ (function () {
|
|
|
20
20
|
var _isReady = false;
|
|
21
21
|
var _isSpecificDaysGiven = false;
|
|
22
22
|
_initConfig();
|
|
23
|
+
// Special internal method to allow the unit tests and DebugPlugin to hook embedded objects
|
|
24
|
+
_self["_getDbgPlgTargets"] = function () {
|
|
25
|
+
return [_queue];
|
|
26
|
+
};
|
|
23
27
|
_self.getConfig = function () {
|
|
24
28
|
return _config;
|
|
25
29
|
};
|
|
@@ -56,8 +60,10 @@ var ThrottleMgr = /** @class */ (function () {
|
|
|
56
60
|
_self.flush = function () {
|
|
57
61
|
try {
|
|
58
62
|
if (_isReady && _queue[_DYN_LENGTH /* @min:%2elength */] > 0) {
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
var items = _queue.slice(0);
|
|
64
|
+
_queue = [];
|
|
65
|
+
arrForEach(items, function (item) {
|
|
66
|
+
_flushMessage(item.msgID, item[_DYN_MESSAGE /* @min:%2emessage */], item.severity, false);
|
|
61
67
|
});
|
|
62
68
|
return true;
|
|
63
69
|
}
|
|
@@ -78,6 +84,9 @@ var ThrottleMgr = /** @class */ (function () {
|
|
|
78
84
|
return _self.flush();
|
|
79
85
|
};
|
|
80
86
|
_self.sendMessage = function (msgID, message, severity) {
|
|
87
|
+
return _flushMessage(msgID, message, severity, true);
|
|
88
|
+
};
|
|
89
|
+
function _flushMessage(msgID, message, severity, saveUnsentMsg) {
|
|
81
90
|
if (_isReady) {
|
|
82
91
|
var isSampledIn = _canSampledIn();
|
|
83
92
|
if (!isSampledIn) {
|
|
@@ -112,41 +121,49 @@ var ThrottleMgr = /** @class */ (function () {
|
|
|
112
121
|
};
|
|
113
122
|
}
|
|
114
123
|
else {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
124
|
+
if (!!saveUnsentMsg) {
|
|
125
|
+
_queue.push({
|
|
126
|
+
msgID: msgID,
|
|
127
|
+
message: message,
|
|
128
|
+
severity: severity
|
|
129
|
+
});
|
|
130
|
+
}
|
|
120
131
|
}
|
|
121
132
|
return null;
|
|
122
|
-
}
|
|
133
|
+
}
|
|
123
134
|
function _initConfig() {
|
|
124
|
-
var _a, _b;
|
|
125
|
-
_canUseLocalStorage = utlCanUseLocalStorage();
|
|
126
135
|
_logger = safeGetLogger(core);
|
|
127
136
|
_isTriggered = false;
|
|
128
|
-
_namePrefix = isNotNullOrUndefined(namePrefix) ? namePrefix : "";
|
|
129
137
|
_queue = [];
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
var
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
138
|
+
_namePrefix = isNotNullOrUndefined(namePrefix) ? namePrefix : "";
|
|
139
|
+
unloadHookContainer = unloadHookContainer || createUnloadHookContainer();
|
|
140
|
+
// Make sure the root config is dynamic as it may be the global config
|
|
141
|
+
config = createDynamicConfig(config || {}, null, _logger).cfg;
|
|
142
|
+
var unloadHook = onConfigChange((config), function () {
|
|
143
|
+
var _a, _b;
|
|
144
|
+
_canUseLocalStorage = utlCanUseLocalStorage();
|
|
145
|
+
var configMgr = config || {};
|
|
146
|
+
_config = {};
|
|
147
|
+
_config[_DYN_DISABLED /* @min:%2edisabled */] = !!configMgr[_DYN_DISABLED /* @min:%2edisabled */];
|
|
148
|
+
_config.msgKey = configMgr.msgKey || 0 /* IThrottleMsgKey.default */;
|
|
149
|
+
var configInterval = configMgr[_DYN_INTERVAL /* @min:%2einterval */] || {};
|
|
150
|
+
_isSpecificDaysGiven = (configInterval === null || configInterval === void 0 ? void 0 : configInterval.daysOfMonth) && (configInterval === null || configInterval === void 0 ? void 0 : configInterval.daysOfMonth[_DYN_LENGTH /* @min:%2elength */]) > 0;
|
|
151
|
+
_config[_DYN_INTERVAL /* @min:%2einterval */] = _getIntervalConfig(configInterval);
|
|
152
|
+
var limit = {
|
|
153
|
+
samplingRate: ((_a = configMgr.limit) === null || _a === void 0 ? void 0 : _a.samplingRate) || 100,
|
|
154
|
+
// dafault: every time sent only 1 event
|
|
155
|
+
maxSendNumber: ((_b = configMgr.limit) === null || _b === void 0 ? void 0 : _b.maxSendNumber) || 1
|
|
156
|
+
};
|
|
157
|
+
_config.limit = limit;
|
|
158
|
+
_localStorageName = _getLocalStorageName(_config.msgKey, _namePrefix);
|
|
159
|
+
if (_canUseLocalStorage && _localStorageName) {
|
|
160
|
+
_localStorageObj = _getLocalStorageObj(utlGetLocalStorage(_logger, _localStorageName), _logger, _localStorageName);
|
|
161
|
+
}
|
|
162
|
+
if (_localStorageObj) {
|
|
163
|
+
_isTriggered = _isTriggeredOnCurDate(_localStorageObj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */]);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
unloadHookContainer && unloadHookContainer.add(unloadHook);
|
|
150
167
|
}
|
|
151
168
|
function _getIntervalConfig(interval) {
|
|
152
169
|
interval = interval || {};
|
|
@@ -268,6 +285,9 @@ var ThrottleMgr = /** @class */ (function () {
|
|
|
268
285
|
return false;
|
|
269
286
|
}
|
|
270
287
|
function _checkInterval(interval, start, current) {
|
|
288
|
+
if (interval <= 0) {
|
|
289
|
+
return 1;
|
|
290
|
+
}
|
|
271
291
|
// count from start year
|
|
272
292
|
return (current >= start) && (current - start) % interval == 0 ? Math.floor((current - start) / interval) + 1 : -1;
|
|
273
293
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ThrottleMgr.js.map","sources":["ThrottleMgr.js"],"sourcesContent":["import { _throwInternal, arrForEach, arrIndexOf, isNotNullOrUndefined, isNullOrUndefined, randomValue, safeGetLogger, strTrim } from \"@microsoft/applicationinsights-core-js\";\r\nimport { utlCanUseLocalStorage, utlGetLocalStorage, utlSetLocalStorage } from \"./StorageHelperFuncs\";\r\nimport { _DYN_COUNT, _DYN_DATE, _DYN_DAYS_OF_MONTH, _DYN_DISABLED, _DYN_GET_UTCDATE, _DYN_INTERVAL, _DYN_LENGTH, _DYN_MESSAGE, _DYN_PRE_TRIGGER_DATE, _DYN_STRINGIFY } from \"./__DynamicConstants\";\r\nvar THROTTLE_STORAGE_PREFIX = \"appInsightsThrottle\";\r\nvar ThrottleMgr = /** @class */ (function () {\r\n function ThrottleMgr(throttleMgr, core, namePrefix) {\r\n var _self = this;\r\n var _canUseLocalStorage;\r\n var _logger;\r\n var _config;\r\n var _localStorageName;\r\n var _localStorageObj;\r\n var _isTriggered; //_isTriggered is to make sure that we only trigger throttle once a day\r\n var _namePrefix;\r\n var _queue;\r\n var _isReady = false;\r\n var _isSpecificDaysGiven = false;\r\n _initConfig();\r\n _self.getConfig = function () {\r\n return _config;\r\n };\r\n /**\r\n * Check if it is the correct day to send message.\r\n * If _isTriggered is true, even if canThrottle returns true, message will not be sent,\r\n * because we only allow triggering sendMessage() once a day.\r\n * @returns if the current date is the valid date to send message\r\n */\r\n _self.canThrottle = function () {\r\n return _canThrottle(_config, _canUseLocalStorage, _localStorageObj);\r\n };\r\n /**\r\n * Check if throttle is triggered on current day(UTC)\r\n * if canThrottle returns false, isTriggered will return false\r\n * @returns if throttle is triggered on current day(UTC)\r\n */\r\n _self.isTriggered = function () {\r\n return _isTriggered;\r\n };\r\n /**\r\n * Before isReady set to true, all message will be stored in queue.\r\n * Message will only be sent out after isReady set to true.\r\n * Initial and default value: false\r\n * @returns isReady state\r\n */\r\n _self.isReady = function () {\r\n return _isReady;\r\n };\r\n /**\r\n * Flush all message in queue with isReady state set to true.\r\n * @returns if message queue is flushed\r\n */\r\n _self.flush = function () {\r\n try {\r\n if (_isReady && _queue[_DYN_LENGTH /* @min:%2elength */] > 0) {\r\n arrForEach(_queue, function (item) {\r\n _self.sendMessage(item.msgID, item[_DYN_MESSAGE /* @min:%2emessage */], item.severity);\r\n });\r\n return true;\r\n }\r\n }\r\n catch (err) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return false;\r\n };\r\n /**\r\n * Set isReady State\r\n * if isReady set to true, message queue will be flushed automatically.\r\n * @param isReady isReady State\r\n * @returns if message queue is flushed\r\n */\r\n _self.onReadyState = function (isReady) {\r\n _isReady = isNullOrUndefined(isReady) ? true : isReady;\r\n return _self.flush();\r\n };\r\n _self.sendMessage = function (msgID, message, severity) {\r\n if (_isReady) {\r\n var isSampledIn = _canSampledIn();\r\n if (!isSampledIn) {\r\n return;\r\n }\r\n var canThrottle = _canThrottle(_config, _canUseLocalStorage, _localStorageObj);\r\n var throttled = false;\r\n var number = 0;\r\n try {\r\n if (canThrottle && !_isTriggered) {\r\n number = Math.min(_config.limit.maxSendNumber, _localStorageObj[_DYN_COUNT /* @min:%2ecount */] + 1);\r\n _localStorageObj[_DYN_COUNT /* @min:%2ecount */] = 0;\r\n throttled = true;\r\n _isTriggered = true;\r\n _localStorageObj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */] = new Date();\r\n }\r\n else {\r\n _isTriggered = canThrottle;\r\n _localStorageObj[_DYN_COUNT /* @min:%2ecount */] += 1;\r\n }\r\n _resetLocalStorage(_logger, _localStorageName, _localStorageObj);\r\n for (var i = 0; i < number; i++) {\r\n _sendMessage(msgID, _logger, message, severity);\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return {\r\n isThrottled: throttled,\r\n throttleNum: number\r\n };\r\n }\r\n else {\r\n _queue.push({\r\n msgID: msgID,\r\n message: message,\r\n severity: severity\r\n });\r\n }\r\n return null;\r\n };\r\n function _initConfig() {\r\n var _a, _b;\r\n _canUseLocalStorage = utlCanUseLocalStorage();\r\n _logger = safeGetLogger(core);\r\n _isTriggered = false;\r\n _namePrefix = isNotNullOrUndefined(namePrefix) ? namePrefix : \"\";\r\n _queue = [];\r\n var configMgr = throttleMgr;\r\n _config = {};\r\n _config[_DYN_DISABLED /* @min:%2edisabled */] = !!configMgr[_DYN_DISABLED /* @min:%2edisabled */];\r\n _config.msgKey = configMgr.msgKey;\r\n var configInterval = configMgr[_DYN_INTERVAL /* @min:%2einterval */] || {};\r\n _isSpecificDaysGiven = (configInterval === null || configInterval === void 0 ? void 0 : configInterval.daysOfMonth) && (configInterval === null || configInterval === void 0 ? void 0 : configInterval.daysOfMonth[_DYN_LENGTH /* @min:%2elength */]) > 0;\r\n _config[_DYN_INTERVAL /* @min:%2einterval */] = _getIntervalConfig(configInterval);\r\n var limit = {\r\n samplingRate: ((_a = configMgr.limit) === null || _a === void 0 ? void 0 : _a.samplingRate) || 100,\r\n // dafault: every time sent only 1 event\r\n maxSendNumber: ((_b = configMgr.limit) === null || _b === void 0 ? void 0 : _b.maxSendNumber) || 1\r\n };\r\n _config.limit = limit;\r\n _localStorageName = _getLocalStorageName(_config.msgKey, _namePrefix);\r\n if (_canUseLocalStorage && _localStorageName) {\r\n _localStorageObj = _getLocalStorageObj(utlGetLocalStorage(_logger, _localStorageName), _logger, _localStorageName);\r\n }\r\n if (_localStorageObj) {\r\n _isTriggered = _isTriggeredOnCurDate(_localStorageObj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */]);\r\n }\r\n }\r\n function _getIntervalConfig(interval) {\r\n interval = interval || {};\r\n var monthInterval = interval === null || interval === void 0 ? void 0 : interval.monthInterval;\r\n var dayInterval = interval === null || interval === void 0 ? void 0 : interval.dayInterval;\r\n // default: send data every 3 month each year\r\n if (isNullOrUndefined(monthInterval) && isNullOrUndefined(dayInterval)) {\r\n interval.monthInterval = 3;\r\n if (!_isSpecificDaysGiven) {\r\n // default: send data on 28th\r\n interval[_DYN_DAYS_OF_MONTH /* @min:%2edaysOfMonth */] = [28];\r\n _isSpecificDaysGiven = true;\r\n }\r\n }\r\n interval = {\r\n // dafault: sent every three months\r\n monthInterval: interval === null || interval === void 0 ? void 0 : interval.monthInterval,\r\n dayInterval: interval === null || interval === void 0 ? void 0 : interval.dayInterval,\r\n daysOfMonth: interval === null || interval === void 0 ? void 0 : interval.daysOfMonth\r\n };\r\n return interval;\r\n }\r\n function _canThrottle(config, canUseLocalStorage, localStorageObj) {\r\n if (!config[_DYN_DISABLED /* @min:%2edisabled */] && canUseLocalStorage && isNotNullOrUndefined(localStorageObj)) {\r\n var curDate = _getThrottleDate();\r\n var date = localStorageObj[_DYN_DATE /* @min:%2edate */];\r\n var interval = config[_DYN_INTERVAL /* @min:%2einterval */];\r\n var monthCheck = 1;\r\n if (interval === null || interval === void 0 ? void 0 : interval.monthInterval) {\r\n var monthExpand = (curDate.getUTCFullYear() - date.getUTCFullYear()) * 12 + curDate.getUTCMonth() - date.getUTCMonth();\r\n monthCheck = _checkInterval(interval.monthInterval, 0, monthExpand);\r\n }\r\n var dayCheck = 1;\r\n if (_isSpecificDaysGiven) {\r\n dayCheck = arrIndexOf(interval[_DYN_DAYS_OF_MONTH /* @min:%2edaysOfMonth */], curDate[_DYN_GET_UTCDATE /* @min:%2egetUTCDate */]());\r\n }\r\n else if (interval === null || interval === void 0 ? void 0 : interval.dayInterval) {\r\n var daySpan = Math.floor((curDate.getTime() - date.getTime()) / 86400000);\r\n dayCheck = _checkInterval(interval.dayInterval, 0, daySpan);\r\n }\r\n return monthCheck >= 0 && dayCheck >= 0;\r\n }\r\n return false;\r\n }\r\n function _getLocalStorageName(msgKey, prefix) {\r\n var fix = isNotNullOrUndefined(prefix) ? prefix : \"\";\r\n if (msgKey) {\r\n return THROTTLE_STORAGE_PREFIX + fix + \"-\" + msgKey;\r\n }\r\n return null;\r\n }\r\n // returns if throttle is triggered on current Date\r\n function _isTriggeredOnCurDate(preTriggerDate) {\r\n try {\r\n if (preTriggerDate) {\r\n var curDate = new Date();\r\n return preTriggerDate.getUTCFullYear() === curDate.getUTCFullYear() &&\r\n preTriggerDate.getUTCMonth() === curDate.getUTCMonth() &&\r\n preTriggerDate[_DYN_GET_UTCDATE /* @min:%2egetUTCDate */]() === curDate[_DYN_GET_UTCDATE /* @min:%2egetUTCDate */]();\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return false;\r\n }\r\n // transfer local storage string value to object that identifies start date, current count and preTriggerDate\r\n function _getLocalStorageObj(value, logger, storageName) {\r\n try {\r\n var storageObj = {\r\n date: _getThrottleDate(),\r\n count: 0\r\n };\r\n if (value) {\r\n var obj = JSON.parse(value);\r\n return {\r\n date: _getThrottleDate(obj[_DYN_DATE /* @min:%2edate */]) || storageObj[_DYN_DATE /* @min:%2edate */],\r\n count: obj[_DYN_COUNT /* @min:%2ecount */] || storageObj[_DYN_COUNT /* @min:%2ecount */],\r\n preTriggerDate: obj.preTriggerDate ? _getThrottleDate(obj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */]) : undefined\r\n };\r\n }\r\n else {\r\n _resetLocalStorage(logger, storageName, storageObj);\r\n return storageObj;\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return null;\r\n }\r\n // if datestr is not defined, current date will be returned\r\n function _getThrottleDate(dateStr) {\r\n // if new Date() can't be created through the provided dateStr, null will be returned.\r\n try {\r\n if (dateStr) {\r\n var date = new Date(dateStr);\r\n //make sure it is a valid Date Object\r\n if (!isNaN(date.getDate())) {\r\n return date;\r\n }\r\n }\r\n else {\r\n return new Date();\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return null;\r\n }\r\n function _resetLocalStorage(logger, storageName, obj) {\r\n try {\r\n return utlSetLocalStorage(logger, storageName, strTrim(JSON[_DYN_STRINGIFY /* @min:%2estringify */](obj)));\r\n }\r\n catch (e) {\r\n // // eslint-disable-next-line no-empty\r\n }\r\n return false;\r\n }\r\n function _checkInterval(interval, start, current) {\r\n // count from start year\r\n return (current >= start) && (current - start) % interval == 0 ? Math.floor((current - start) / interval) + 1 : -1;\r\n }\r\n function _sendMessage(msgID, logger, message, severity) {\r\n _throwInternal(logger, severity || 1 /* eLoggingSeverity.CRITICAL */, msgID, message);\r\n }\r\n // NOTE: config.limit.samplingRate is set to 4 decimal places,\r\n // so config.limit.samplingRate = 1 means 0.0001%\r\n function _canSampledIn() {\r\n return randomValue(1000000) <= _config.limit.samplingRate;\r\n }\r\n }\r\n return ThrottleMgr;\r\n}());\r\nexport { ThrottleMgr };\r\n//# sourceMappingURL=ThrottleMgr.js.map"],"names":[],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA"}
|
|
1
|
+
{"version":3,"file":"ThrottleMgr.js.map","sources":["ThrottleMgr.js"],"sourcesContent":["import { _throwInternal, arrForEach, arrIndexOf, createDynamicConfig, createUnloadHookContainer, isNotNullOrUndefined, isNullOrUndefined, onConfigChange, randomValue, safeGetLogger, strTrim } from \"@microsoft/applicationinsights-core-js\";\r\nimport { utlCanUseLocalStorage, utlGetLocalStorage, utlSetLocalStorage } from \"./StorageHelperFuncs\";\r\nimport { _DYN_COUNT, _DYN_DATE, _DYN_DAYS_OF_MONTH, _DYN_DISABLED, _DYN_GET_UTCDATE, _DYN_INTERVAL, _DYN_LENGTH, _DYN_MESSAGE, _DYN_PRE_TRIGGER_DATE, _DYN_STRINGIFY } from \"./__DynamicConstants\";\r\nvar THROTTLE_STORAGE_PREFIX = \"appInsightsThrottle\";\r\nvar ThrottleMgr = /** @class */ (function () {\r\n function ThrottleMgr(config, core, namePrefix, unloadHookContainer) {\r\n var _self = this;\r\n var _canUseLocalStorage;\r\n var _logger;\r\n var _config;\r\n var _localStorageName;\r\n var _localStorageObj;\r\n var _isTriggered; //_isTriggered is to make sure that we only trigger throttle once a day\r\n var _namePrefix;\r\n var _queue;\r\n var _isReady = false;\r\n var _isSpecificDaysGiven = false;\r\n _initConfig();\r\n // Special internal method to allow the unit tests and DebugPlugin to hook embedded objects\r\n _self[\"_getDbgPlgTargets\"] = function () {\r\n return [_queue];\r\n };\r\n _self.getConfig = function () {\r\n return _config;\r\n };\r\n /**\r\n * Check if it is the correct day to send message.\r\n * If _isTriggered is true, even if canThrottle returns true, message will not be sent,\r\n * because we only allow triggering sendMessage() once a day.\r\n * @returns if the current date is the valid date to send message\r\n */\r\n _self.canThrottle = function () {\r\n return _canThrottle(_config, _canUseLocalStorage, _localStorageObj);\r\n };\r\n /**\r\n * Check if throttle is triggered on current day(UTC)\r\n * if canThrottle returns false, isTriggered will return false\r\n * @returns if throttle is triggered on current day(UTC)\r\n */\r\n _self.isTriggered = function () {\r\n return _isTriggered;\r\n };\r\n /**\r\n * Before isReady set to true, all message will be stored in queue.\r\n * Message will only be sent out after isReady set to true.\r\n * Initial and default value: false\r\n * @returns isReady state\r\n */\r\n _self.isReady = function () {\r\n return _isReady;\r\n };\r\n /**\r\n * Flush all message in queue with isReady state set to true.\r\n * @returns if message queue is flushed\r\n */\r\n _self.flush = function () {\r\n try {\r\n if (_isReady && _queue[_DYN_LENGTH /* @min:%2elength */] > 0) {\r\n var items = _queue.slice(0);\r\n _queue = [];\r\n arrForEach(items, function (item) {\r\n _flushMessage(item.msgID, item[_DYN_MESSAGE /* @min:%2emessage */], item.severity, false);\r\n });\r\n return true;\r\n }\r\n }\r\n catch (err) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return false;\r\n };\r\n /**\r\n * Set isReady State\r\n * if isReady set to true, message queue will be flushed automatically.\r\n * @param isReady isReady State\r\n * @returns if message queue is flushed\r\n */\r\n _self.onReadyState = function (isReady) {\r\n _isReady = isNullOrUndefined(isReady) ? true : isReady;\r\n return _self.flush();\r\n };\r\n _self.sendMessage = function (msgID, message, severity) {\r\n return _flushMessage(msgID, message, severity, true);\r\n };\r\n function _flushMessage(msgID, message, severity, saveUnsentMsg) {\r\n if (_isReady) {\r\n var isSampledIn = _canSampledIn();\r\n if (!isSampledIn) {\r\n return;\r\n }\r\n var canThrottle = _canThrottle(_config, _canUseLocalStorage, _localStorageObj);\r\n var throttled = false;\r\n var number = 0;\r\n try {\r\n if (canThrottle && !_isTriggered) {\r\n number = Math.min(_config.limit.maxSendNumber, _localStorageObj[_DYN_COUNT /* @min:%2ecount */] + 1);\r\n _localStorageObj[_DYN_COUNT /* @min:%2ecount */] = 0;\r\n throttled = true;\r\n _isTriggered = true;\r\n _localStorageObj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */] = new Date();\r\n }\r\n else {\r\n _isTriggered = canThrottle;\r\n _localStorageObj[_DYN_COUNT /* @min:%2ecount */] += 1;\r\n }\r\n _resetLocalStorage(_logger, _localStorageName, _localStorageObj);\r\n for (var i = 0; i < number; i++) {\r\n _sendMessage(msgID, _logger, message, severity);\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return {\r\n isThrottled: throttled,\r\n throttleNum: number\r\n };\r\n }\r\n else {\r\n if (!!saveUnsentMsg) {\r\n _queue.push({\r\n msgID: msgID,\r\n message: message,\r\n severity: severity\r\n });\r\n }\r\n }\r\n return null;\r\n }\r\n function _initConfig() {\r\n _logger = safeGetLogger(core);\r\n _isTriggered = false;\r\n _queue = [];\r\n _namePrefix = isNotNullOrUndefined(namePrefix) ? namePrefix : \"\";\r\n unloadHookContainer = unloadHookContainer || createUnloadHookContainer();\r\n // Make sure the root config is dynamic as it may be the global config\r\n config = createDynamicConfig(config || {}, null, _logger).cfg;\r\n var unloadHook = onConfigChange((config), function () {\r\n var _a, _b;\r\n _canUseLocalStorage = utlCanUseLocalStorage();\r\n var configMgr = config || {};\r\n _config = {};\r\n _config[_DYN_DISABLED /* @min:%2edisabled */] = !!configMgr[_DYN_DISABLED /* @min:%2edisabled */];\r\n _config.msgKey = configMgr.msgKey || 0 /* IThrottleMsgKey.default */;\r\n var configInterval = configMgr[_DYN_INTERVAL /* @min:%2einterval */] || {};\r\n _isSpecificDaysGiven = (configInterval === null || configInterval === void 0 ? void 0 : configInterval.daysOfMonth) && (configInterval === null || configInterval === void 0 ? void 0 : configInterval.daysOfMonth[_DYN_LENGTH /* @min:%2elength */]) > 0;\r\n _config[_DYN_INTERVAL /* @min:%2einterval */] = _getIntervalConfig(configInterval);\r\n var limit = {\r\n samplingRate: ((_a = configMgr.limit) === null || _a === void 0 ? void 0 : _a.samplingRate) || 100,\r\n // dafault: every time sent only 1 event\r\n maxSendNumber: ((_b = configMgr.limit) === null || _b === void 0 ? void 0 : _b.maxSendNumber) || 1\r\n };\r\n _config.limit = limit;\r\n _localStorageName = _getLocalStorageName(_config.msgKey, _namePrefix);\r\n if (_canUseLocalStorage && _localStorageName) {\r\n _localStorageObj = _getLocalStorageObj(utlGetLocalStorage(_logger, _localStorageName), _logger, _localStorageName);\r\n }\r\n if (_localStorageObj) {\r\n _isTriggered = _isTriggeredOnCurDate(_localStorageObj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */]);\r\n }\r\n });\r\n unloadHookContainer && unloadHookContainer.add(unloadHook);\r\n }\r\n function _getIntervalConfig(interval) {\r\n interval = interval || {};\r\n var monthInterval = interval === null || interval === void 0 ? void 0 : interval.monthInterval;\r\n var dayInterval = interval === null || interval === void 0 ? void 0 : interval.dayInterval;\r\n // default: send data every 3 month each year\r\n if (isNullOrUndefined(monthInterval) && isNullOrUndefined(dayInterval)) {\r\n interval.monthInterval = 3;\r\n if (!_isSpecificDaysGiven) {\r\n // default: send data on 28th\r\n interval[_DYN_DAYS_OF_MONTH /* @min:%2edaysOfMonth */] = [28];\r\n _isSpecificDaysGiven = true;\r\n }\r\n }\r\n interval = {\r\n // dafault: sent every three months\r\n monthInterval: interval === null || interval === void 0 ? void 0 : interval.monthInterval,\r\n dayInterval: interval === null || interval === void 0 ? void 0 : interval.dayInterval,\r\n daysOfMonth: interval === null || interval === void 0 ? void 0 : interval.daysOfMonth\r\n };\r\n return interval;\r\n }\r\n function _canThrottle(config, canUseLocalStorage, localStorageObj) {\r\n if (!config[_DYN_DISABLED /* @min:%2edisabled */] && canUseLocalStorage && isNotNullOrUndefined(localStorageObj)) {\r\n var curDate = _getThrottleDate();\r\n var date = localStorageObj[_DYN_DATE /* @min:%2edate */];\r\n var interval = config[_DYN_INTERVAL /* @min:%2einterval */];\r\n var monthCheck = 1;\r\n if (interval === null || interval === void 0 ? void 0 : interval.monthInterval) {\r\n var monthExpand = (curDate.getUTCFullYear() - date.getUTCFullYear()) * 12 + curDate.getUTCMonth() - date.getUTCMonth();\r\n monthCheck = _checkInterval(interval.monthInterval, 0, monthExpand);\r\n }\r\n var dayCheck = 1;\r\n if (_isSpecificDaysGiven) {\r\n dayCheck = arrIndexOf(interval[_DYN_DAYS_OF_MONTH /* @min:%2edaysOfMonth */], curDate[_DYN_GET_UTCDATE /* @min:%2egetUTCDate */]());\r\n }\r\n else if (interval === null || interval === void 0 ? void 0 : interval.dayInterval) {\r\n var daySpan = Math.floor((curDate.getTime() - date.getTime()) / 86400000);\r\n dayCheck = _checkInterval(interval.dayInterval, 0, daySpan);\r\n }\r\n return monthCheck >= 0 && dayCheck >= 0;\r\n }\r\n return false;\r\n }\r\n function _getLocalStorageName(msgKey, prefix) {\r\n var fix = isNotNullOrUndefined(prefix) ? prefix : \"\";\r\n if (msgKey) {\r\n return THROTTLE_STORAGE_PREFIX + fix + \"-\" + msgKey;\r\n }\r\n return null;\r\n }\r\n // returns if throttle is triggered on current Date\r\n function _isTriggeredOnCurDate(preTriggerDate) {\r\n try {\r\n if (preTriggerDate) {\r\n var curDate = new Date();\r\n return preTriggerDate.getUTCFullYear() === curDate.getUTCFullYear() &&\r\n preTriggerDate.getUTCMonth() === curDate.getUTCMonth() &&\r\n preTriggerDate[_DYN_GET_UTCDATE /* @min:%2egetUTCDate */]() === curDate[_DYN_GET_UTCDATE /* @min:%2egetUTCDate */]();\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return false;\r\n }\r\n // transfer local storage string value to object that identifies start date, current count and preTriggerDate\r\n function _getLocalStorageObj(value, logger, storageName) {\r\n try {\r\n var storageObj = {\r\n date: _getThrottleDate(),\r\n count: 0\r\n };\r\n if (value) {\r\n var obj = JSON.parse(value);\r\n return {\r\n date: _getThrottleDate(obj[_DYN_DATE /* @min:%2edate */]) || storageObj[_DYN_DATE /* @min:%2edate */],\r\n count: obj[_DYN_COUNT /* @min:%2ecount */] || storageObj[_DYN_COUNT /* @min:%2ecount */],\r\n preTriggerDate: obj.preTriggerDate ? _getThrottleDate(obj[_DYN_PRE_TRIGGER_DATE /* @min:%2epreTriggerDate */]) : undefined\r\n };\r\n }\r\n else {\r\n _resetLocalStorage(logger, storageName, storageObj);\r\n return storageObj;\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return null;\r\n }\r\n // if datestr is not defined, current date will be returned\r\n function _getThrottleDate(dateStr) {\r\n // if new Date() can't be created through the provided dateStr, null will be returned.\r\n try {\r\n if (dateStr) {\r\n var date = new Date(dateStr);\r\n //make sure it is a valid Date Object\r\n if (!isNaN(date.getDate())) {\r\n return date;\r\n }\r\n }\r\n else {\r\n return new Date();\r\n }\r\n }\r\n catch (e) {\r\n // eslint-disable-next-line no-empty\r\n }\r\n return null;\r\n }\r\n function _resetLocalStorage(logger, storageName, obj) {\r\n try {\r\n return utlSetLocalStorage(logger, storageName, strTrim(JSON[_DYN_STRINGIFY /* @min:%2estringify */](obj)));\r\n }\r\n catch (e) {\r\n // // eslint-disable-next-line no-empty\r\n }\r\n return false;\r\n }\r\n function _checkInterval(interval, start, current) {\r\n if (interval <= 0) {\r\n return 1;\r\n }\r\n // count from start year\r\n return (current >= start) && (current - start) % interval == 0 ? Math.floor((current - start) / interval) + 1 : -1;\r\n }\r\n function _sendMessage(msgID, logger, message, severity) {\r\n _throwInternal(logger, severity || 1 /* eLoggingSeverity.CRITICAL */, msgID, message);\r\n }\r\n // NOTE: config.limit.samplingRate is set to 4 decimal places,\r\n // so config.limit.samplingRate = 1 means 0.0001%\r\n function _canSampledIn() {\r\n return randomValue(1000000) <= _config.limit.samplingRate;\r\n }\r\n }\r\n return ThrottleMgr;\r\n}());\r\nexport { ThrottleMgr };\r\n//# sourceMappingURL=ThrottleMgr.js.map"],"names":[],"mappings":";;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA"}
|
package/dist-esm/Util.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microsoft/applicationinsights-common",
|
|
3
|
-
"version": "3.0.0-beta.2303-
|
|
3
|
+
"version": "3.0.0-beta.2303-03",
|
|
4
4
|
"description": "Microsoft Application Insights Common JavaScript Library",
|
|
5
5
|
"homepage": "https://github.com/microsoft/ApplicationInsights-JS#readme",
|
|
6
6
|
"author": "Microsoft Application Insights Team",
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@microsoft/applicationinsights-shims": "2.0.2",
|
|
54
|
-
"@microsoft/applicationinsights-core-js": "3.0.0-beta.2303-
|
|
54
|
+
"@microsoft/applicationinsights-core-js": "3.0.0-beta.2303-03",
|
|
55
55
|
"@microsoft/dynamicproto-js": "^1.1.7",
|
|
56
56
|
"@nevware21/ts-utils": ">= 0.8.1 < 2.x"
|
|
57
57
|
},
|
package/src/Enums.ts
CHANGED
|
@@ -49,7 +49,17 @@ export const DistributedTracingModes = createEnumStyle<typeof eDistributedTracin
|
|
|
49
49
|
export type DistributedTracingModes = number | eDistributedTracingModes;
|
|
50
50
|
|
|
51
51
|
export const enum IThrottleMsgKey {
|
|
52
|
+
/**
|
|
53
|
+
* Default Message key for non pre-defined message
|
|
54
|
+
*/
|
|
52
55
|
default = 0,
|
|
56
|
+
/**
|
|
57
|
+
* Message key for ikey Deprecation
|
|
58
|
+
*/
|
|
53
59
|
ikeyDeprecate = 1,
|
|
60
|
+
/**
|
|
61
|
+
* Message key for cdn Deprecation
|
|
62
|
+
*/
|
|
54
63
|
cdnDeprecate = 2
|
|
55
64
|
}
|
|
65
|
+
|
|
@@ -50,8 +50,9 @@ export interface IThrottleInterval {
|
|
|
50
50
|
export interface IThrottleMgrConfig {
|
|
51
51
|
/**
|
|
52
52
|
* Identifies message key to be used for local storage key
|
|
53
|
+
* Default: IThrottleMsgKey.default
|
|
53
54
|
*/
|
|
54
|
-
msgKey
|
|
55
|
+
msgKey?: IThrottleMsgKey;
|
|
55
56
|
|
|
56
57
|
/**
|
|
57
58
|
* Identifies if throttle is disabled
|
|
@@ -61,6 +62,7 @@ export interface IThrottleMgrConfig {
|
|
|
61
62
|
|
|
62
63
|
/**
|
|
63
64
|
* Identifies limit number/percentage of items sent per time
|
|
65
|
+
* Default: sampling percentage 0.01% with one item sent per time
|
|
64
66
|
*/
|
|
65
67
|
limit?: IThrottleLimit;
|
|
66
68
|
|
|
@@ -106,4 +108,4 @@ export interface IThrottleResult {
|
|
|
106
108
|
* if isThrottled is false, it will be set to 0
|
|
107
109
|
*/
|
|
108
110
|
throttleNum: number;
|
|
109
|
-
}
|
|
111
|
+
}
|
package/src/ThrottleMgr.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
IAppInsightsCore, IDiagnosticLogger, _eInternalMessageId, _throwInternal, arrForEach, arrIndexOf,
|
|
3
|
-
isNullOrUndefined,
|
|
2
|
+
IAppInsightsCore, IDiagnosticLogger, IUnloadHookContainer, _eInternalMessageId, _throwInternal, arrForEach, arrIndexOf,
|
|
3
|
+
createDynamicConfig, createUnloadHookContainer, eLoggingSeverity, isNotNullOrUndefined, isNullOrUndefined, onConfigChange, randomValue,
|
|
4
|
+
safeGetLogger, strTrim
|
|
4
5
|
} from "@microsoft/applicationinsights-core-js";
|
|
5
6
|
import { IThrottleMsgKey } from "./Enums";
|
|
6
7
|
import { IThrottleInterval, IThrottleLocalStorageObj, IThrottleMgrConfig, IThrottleResult } from "./Interfaces/IThrottleMgr";
|
|
@@ -22,8 +23,9 @@ export class ThrottleMgr {
|
|
|
22
23
|
public isReady: () => boolean
|
|
23
24
|
public onReadyState: (isReady?: boolean) => boolean;
|
|
24
25
|
public flush: () => boolean;
|
|
26
|
+
public config: IThrottleMgrConfig;
|
|
25
27
|
|
|
26
|
-
constructor(
|
|
28
|
+
constructor(config?: IThrottleMgrConfig, core?: IAppInsightsCore, namePrefix?: string, unloadHookContainer?: IUnloadHookContainer) {
|
|
27
29
|
let _self = this;
|
|
28
30
|
let _canUseLocalStorage: boolean;
|
|
29
31
|
let _logger: IDiagnosticLogger | null | undefined;
|
|
@@ -38,6 +40,11 @@ export class ThrottleMgr {
|
|
|
38
40
|
|
|
39
41
|
_initConfig();
|
|
40
42
|
|
|
43
|
+
// Special internal method to allow the unit tests and DebugPlugin to hook embedded objects
|
|
44
|
+
_self["_getDbgPlgTargets"] = () => {
|
|
45
|
+
return [_queue];
|
|
46
|
+
};
|
|
47
|
+
|
|
41
48
|
_self.getConfig = (): IThrottleMgrConfig => {
|
|
42
49
|
return _config;
|
|
43
50
|
}
|
|
@@ -78,8 +85,10 @@ export class ThrottleMgr {
|
|
|
78
85
|
_self.flush = (): boolean => {
|
|
79
86
|
try {
|
|
80
87
|
if (_isReady && _queue.length > 0) {
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
let items = _queue.slice(0);
|
|
89
|
+
_queue = [];
|
|
90
|
+
arrForEach(items, (item: SendMsgParameter) => {
|
|
91
|
+
_flushMessage(item.msgID, item.message, item.severity, false);
|
|
83
92
|
});
|
|
84
93
|
return true;
|
|
85
94
|
}
|
|
@@ -101,6 +110,11 @@ export class ThrottleMgr {
|
|
|
101
110
|
}
|
|
102
111
|
|
|
103
112
|
_self.sendMessage = (msgID: _eInternalMessageId, message: string, severity?: eLoggingSeverity): IThrottleResult | null => {
|
|
113
|
+
return _flushMessage(msgID, message, severity, true);
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function _flushMessage(msgID: _eInternalMessageId, message: string, severity?: eLoggingSeverity, saveUnsentMsg?: boolean) {
|
|
104
118
|
if (_isReady) {
|
|
105
119
|
let isSampledIn = _canSampledIn();
|
|
106
120
|
if (!isSampledIn) {
|
|
@@ -132,44 +146,55 @@ export class ThrottleMgr {
|
|
|
132
146
|
throttleNum: number
|
|
133
147
|
} as IThrottleResult;
|
|
134
148
|
} else {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
149
|
+
if (!!saveUnsentMsg) {
|
|
150
|
+
_queue.push({
|
|
151
|
+
msgID: msgID,
|
|
152
|
+
message: message,
|
|
153
|
+
severity: severity
|
|
154
|
+
} as SendMsgParameter);
|
|
155
|
+
}
|
|
140
156
|
}
|
|
141
157
|
return null;
|
|
142
158
|
}
|
|
143
159
|
|
|
144
160
|
function _initConfig() {
|
|
145
|
-
_canUseLocalStorage = utlCanUseLocalStorage();
|
|
146
161
|
_logger = safeGetLogger(core);
|
|
147
162
|
_isTriggered = false;
|
|
148
|
-
_namePrefix = isNotNullOrUndefined(namePrefix)? namePrefix : "";
|
|
149
163
|
_queue = [];
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
164
|
+
_namePrefix = isNotNullOrUndefined(namePrefix)? namePrefix : "";
|
|
165
|
+
unloadHookContainer = unloadHookContainer || createUnloadHookContainer();
|
|
166
|
+
// Make sure the root config is dynamic as it may be the global config
|
|
167
|
+
config = createDynamicConfig(config as any || {}, null, _logger).cfg;
|
|
154
168
|
|
|
155
|
-
let
|
|
156
|
-
|
|
157
|
-
|
|
169
|
+
let unloadHook = onConfigChange((config), () => {
|
|
170
|
+
_canUseLocalStorage = utlCanUseLocalStorage();
|
|
171
|
+
|
|
172
|
+
let configMgr = config || {};
|
|
173
|
+
_config = {} as any;
|
|
174
|
+
_config.disabled = !!configMgr.disabled;
|
|
158
175
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
176
|
+
_config.msgKey = configMgr.msgKey || IThrottleMsgKey.default;
|
|
177
|
+
|
|
178
|
+
let configInterval = configMgr.interval || {};
|
|
179
|
+
_isSpecificDaysGiven = configInterval?.daysOfMonth && configInterval?.daysOfMonth.length > 0;
|
|
180
|
+
_config.interval = _getIntervalConfig(configInterval);
|
|
181
|
+
|
|
182
|
+
let limit = {
|
|
183
|
+
samplingRate: configMgr.limit?.samplingRate || 100,
|
|
184
|
+
// dafault: every time sent only 1 event
|
|
185
|
+
maxSendNumber: configMgr.limit?.maxSendNumber || 1
|
|
186
|
+
};
|
|
187
|
+
_config.limit = limit;
|
|
188
|
+
_localStorageName = _getLocalStorageName(_config.msgKey, _namePrefix);
|
|
189
|
+
|
|
190
|
+
if (_canUseLocalStorage && _localStorageName) {
|
|
191
|
+
_localStorageObj = _getLocalStorageObj(utlGetLocalStorage(_logger, _localStorageName), _logger, _localStorageName);
|
|
192
|
+
}
|
|
193
|
+
if (_localStorageObj) {
|
|
194
|
+
_isTriggered = _isTriggeredOnCurDate(_localStorageObj.preTriggerDate);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
unloadHookContainer && unloadHookContainer.add(unloadHook);
|
|
173
198
|
}
|
|
174
199
|
|
|
175
200
|
function _getIntervalConfig(interval: IThrottleInterval) {
|
|
@@ -297,6 +322,9 @@ export class ThrottleMgr {
|
|
|
297
322
|
}
|
|
298
323
|
|
|
299
324
|
function _checkInterval(interval: number, start: number, current: number) {
|
|
325
|
+
if (interval <= 0) {
|
|
326
|
+
return 1;
|
|
327
|
+
}
|
|
300
328
|
// count from start year
|
|
301
329
|
return (current >= start) && (current - start) % interval == 0 ? Math.floor((current - start) / interval) + 1 : -1;
|
|
302
330
|
}
|
|
@@ -58,7 +58,6 @@ export { IPropertiesPlugin } from "./Interfaces/IPropertiesPlugin";
|
|
|
58
58
|
export { IUser, IUserContext } from "./Interfaces/Context/IUser";
|
|
59
59
|
export { ITelemetryTrace, ITraceState } from "./Interfaces/Context/ITelemetryTrace";
|
|
60
60
|
export { IRequestContext } from "./Interfaces/IRequestContext";
|
|
61
|
-
export { IThrottleLocalStorageObj, IThrottleMgrConfig, IThrottleResult, IThrottleLimit, IThrottleInterval } from "./Interfaces/IThrottleMgr";
|
|
62
61
|
export { eDistributedTracingModes, DistributedTracingModes, IThrottleMsgKey } from "./Enums";
|
|
63
62
|
export { stringToBoolOrDefault, msToTimeSpan, getExtensionByName, isCrossOriginError } from "./HelperFuncs";
|
|
64
63
|
export {
|
|
@@ -71,6 +70,7 @@ export {
|
|
|
71
70
|
utlCanUseSessionStorage, utlGetSessionStorageKeys, utlGetSessionStorage, utlSetSessionStorage, utlRemoveSessionStorage
|
|
72
71
|
} from "./StorageHelperFuncs";
|
|
73
72
|
export { urlParseUrl, urlGetAbsoluteUrl, urlGetPathName, urlGetCompleteUrl, urlParseHost, urlParseFullHost } from "./UrlHelperFuncs";
|
|
73
|
+
export { IThrottleLimit, IThrottleInterval, IThrottleMgrConfig, IThrottleLocalStorageObj, IThrottleResult } from "./Interfaces/IThrottleMgr";
|
|
74
74
|
|
|
75
75
|
export const PropertiesPluginIdentifier = "AppInsightsPropertiesPlugin";
|
|
76
76
|
export const BreezeChannelIdentifier = "AppInsightsChannelPlugin";
|
package/types/Enums.d.ts
CHANGED
|
@@ -34,7 +34,16 @@ export declare const enum eDistributedTracingModes {
|
|
|
34
34
|
export declare const DistributedTracingModes: import("@microsoft/applicationinsights-core-js").EnumValue<typeof eDistributedTracingModes>;
|
|
35
35
|
export type DistributedTracingModes = number | eDistributedTracingModes;
|
|
36
36
|
export declare const enum IThrottleMsgKey {
|
|
37
|
+
/**
|
|
38
|
+
* Default Message key for non pre-defined message
|
|
39
|
+
*/
|
|
37
40
|
default = 0,
|
|
41
|
+
/**
|
|
42
|
+
* Message key for ikey Deprecation
|
|
43
|
+
*/
|
|
38
44
|
ikeyDeprecate = 1,
|
|
45
|
+
/**
|
|
46
|
+
* Message key for cdn Deprecation
|
|
47
|
+
*/
|
|
39
48
|
cdnDeprecate = 2
|
|
40
49
|
}
|
|
@@ -44,8 +44,9 @@ export interface IThrottleInterval {
|
|
|
44
44
|
export interface IThrottleMgrConfig {
|
|
45
45
|
/**
|
|
46
46
|
* Identifies message key to be used for local storage key
|
|
47
|
+
* Default: IThrottleMsgKey.default
|
|
47
48
|
*/
|
|
48
|
-
msgKey
|
|
49
|
+
msgKey?: IThrottleMsgKey;
|
|
49
50
|
/**
|
|
50
51
|
* Identifies if throttle is disabled
|
|
51
52
|
* Default: false
|
|
@@ -53,6 +54,7 @@ export interface IThrottleMgrConfig {
|
|
|
53
54
|
disabled?: boolean;
|
|
54
55
|
/**
|
|
55
56
|
* Identifies limit number/percentage of items sent per time
|
|
57
|
+
* Default: sampling percentage 0.01% with one item sent per time
|
|
56
58
|
*/
|
|
57
59
|
limit?: IThrottleLimit;
|
|
58
60
|
/**
|
package/types/ThrottleMgr.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IAppInsightsCore, _eInternalMessageId, eLoggingSeverity } from "@microsoft/applicationinsights-core-js";
|
|
1
|
+
import { IAppInsightsCore, IUnloadHookContainer, _eInternalMessageId, eLoggingSeverity } from "@microsoft/applicationinsights-core-js";
|
|
2
2
|
import { IThrottleMgrConfig, IThrottleResult } from "./Interfaces/IThrottleMgr";
|
|
3
3
|
export declare class ThrottleMgr {
|
|
4
4
|
canThrottle: () => boolean;
|
|
@@ -8,5 +8,6 @@ export declare class ThrottleMgr {
|
|
|
8
8
|
isReady: () => boolean;
|
|
9
9
|
onReadyState: (isReady?: boolean) => boolean;
|
|
10
10
|
flush: () => boolean;
|
|
11
|
-
|
|
11
|
+
config: IThrottleMgrConfig;
|
|
12
|
+
constructor(config?: IThrottleMgrConfig, core?: IAppInsightsCore, namePrefix?: string, unloadHookContainer?: IUnloadHookContainer);
|
|
12
13
|
}
|
|
@@ -47,13 +47,13 @@ export { IPropertiesPlugin } from "./Interfaces/IPropertiesPlugin";
|
|
|
47
47
|
export { IUser, IUserContext } from "./Interfaces/Context/IUser";
|
|
48
48
|
export { ITelemetryTrace, ITraceState } from "./Interfaces/Context/ITelemetryTrace";
|
|
49
49
|
export { IRequestContext } from "./Interfaces/IRequestContext";
|
|
50
|
-
export { IThrottleLocalStorageObj, IThrottleMgrConfig, IThrottleResult, IThrottleLimit, IThrottleInterval } from "./Interfaces/IThrottleMgr";
|
|
51
50
|
export { eDistributedTracingModes, DistributedTracingModes, IThrottleMsgKey } from "./Enums";
|
|
52
51
|
export { stringToBoolOrDefault, msToTimeSpan, getExtensionByName, isCrossOriginError } from "./HelperFuncs";
|
|
53
52
|
export { isBeaconsSupported as isBeaconApiSupported, ITraceParent, createTraceParent, parseTraceParent, isValidTraceId, isValidSpanId, isValidTraceParent, isSampledFlag, formatTraceParent, findW3cTraceParent } from "@microsoft/applicationinsights-core-js";
|
|
54
53
|
export { createDomEvent } from "./DomHelperFuncs";
|
|
55
54
|
export { utlDisableStorage, utlEnableStorage, utlCanUseLocalStorage, utlGetLocalStorage, utlSetLocalStorage, utlRemoveStorage, utlCanUseSessionStorage, utlGetSessionStorageKeys, utlGetSessionStorage, utlSetSessionStorage, utlRemoveSessionStorage } from "./StorageHelperFuncs";
|
|
56
55
|
export { urlParseUrl, urlGetAbsoluteUrl, urlGetPathName, urlGetCompleteUrl, urlParseHost, urlParseFullHost } from "./UrlHelperFuncs";
|
|
56
|
+
export { IThrottleLimit, IThrottleInterval, IThrottleMgrConfig, IThrottleLocalStorageObj, IThrottleResult } from "./Interfaces/IThrottleMgr";
|
|
57
57
|
export declare const PropertiesPluginIdentifier = "AppInsightsPropertiesPlugin";
|
|
58
58
|
export declare const BreezeChannelIdentifier = "AppInsightsChannelPlugin";
|
|
59
59
|
export declare const AnalyticsPluginIdentifier = "ApplicationInsightsAnalytics";
|