@icgio/icg-utils 1.9.1 → 1.9.2
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/index.js +0 -1
- package/lib/rate-limit-logger.js +14 -7
- package/package.json +1 -1
- package/lib/circular-buffer.js +0 -42
package/index.js
CHANGED
package/lib/rate-limit-logger.js
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
class RateLimitedLogger {
|
|
2
2
|
constructor(defaultLimit = 1000) {
|
|
3
|
-
this.defaultLimit = defaultLimit
|
|
4
|
-
this.lastLogTime = 0
|
|
3
|
+
this.defaultLimit = defaultLimit
|
|
4
|
+
this.lastLogTime = 0
|
|
5
|
+
this.lastErrorTime = 0
|
|
5
6
|
}
|
|
6
7
|
|
|
7
8
|
log(message, limit = this.defaultLimit) {
|
|
8
|
-
this._rateLimitedOutput(console.log, message, limit)
|
|
9
|
+
this._rateLimitedOutput(console.log, message, limit, 'log')
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
error(message, limit = this.defaultLimit) {
|
|
12
|
-
this._rateLimitedOutput(console.error, message, limit)
|
|
13
|
+
this._rateLimitedOutput(console.error, message, limit, 'error')
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
_rateLimitedOutput(outputFunction, message, limit) {
|
|
16
|
+
_rateLimitedOutput(outputFunction, message, limit, type) {
|
|
16
17
|
const now = Date.now()
|
|
17
|
-
|
|
18
|
+
const lastTime = type === 'log' ? this.lastLogTime : this.lastErrorTime
|
|
19
|
+
|
|
20
|
+
if (now - lastTime >= limit) {
|
|
18
21
|
outputFunction(message)
|
|
19
|
-
|
|
22
|
+
if (type === 'log') {
|
|
23
|
+
this.lastLogTime = now
|
|
24
|
+
} else {
|
|
25
|
+
this.lastErrorTime = now
|
|
26
|
+
}
|
|
20
27
|
}
|
|
21
28
|
}
|
|
22
29
|
}
|
package/package.json
CHANGED
package/lib/circular-buffer.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
class CircularBuffer {
|
|
2
|
-
constructor(size) {
|
|
3
|
-
this.size = size
|
|
4
|
-
this.buffer = new Array(size)
|
|
5
|
-
this.index = 0
|
|
6
|
-
this.count = 0
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
push(value) {
|
|
10
|
-
this.buffer[this.index] = value
|
|
11
|
-
this.index = (this.index + 1) % this.size
|
|
12
|
-
if (this.count < this.size) {
|
|
13
|
-
this.count++
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get_values() {
|
|
18
|
-
return this.buffer.slice(0, this.count)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
min() {
|
|
22
|
-
return Math.min(...this.getValues())
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
max() {
|
|
26
|
-
return Math.max(...this.getValues())
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
mean() {
|
|
30
|
-
const values = this.getValues()
|
|
31
|
-
return values.reduce((sum, val) => sum + val, 0) / values.length
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
std() {
|
|
35
|
-
const values = this.getValues()
|
|
36
|
-
const mean = this.mean()
|
|
37
|
-
const variance = values.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / values.length
|
|
38
|
-
return Math.sqrt(variance)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
module.exports = CircularBuffer
|