-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (106 loc) · 2.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const EventEmitter = require('events');
class Rule extends EventEmitter {
constructor(options) {
super();
//this.triggerFunction = cb;
this.name = options.name;
this.maxEvents = options.maxEvents;
this.timeframe = options.timeframe;
this.autoreset = options.autoreset;
this.once = options.once;
this.triggered = false;
this.eventsCount = 0;
this.lastOccurence = null;
this.eventBuffer = [];
}
count() {
if (this.once && this.triggered) { return; }
this.eventsCount++;
const now = Date.now();
this.triggerEvent(now, this.eventsCount);
this.checkCondition(now);
}
checkCondition(now) {
// Calculate ms since lastOccurence
const diff = this.eventBuffer.length == 0 ? 0 : now - this.lastOccurence;
this.lastOccurence = now;
this.eventBuffer.push( diff );
// Check Condition for "maxEvents"
if (this.eventBuffer.length > this.maxEvents) {
if (this.timeframe <= 0) {
this.eventBuffer.shift();
return this.triggerAlarm();
}
this.eventBuffer[0] = 0;
// FROM ALL EVENTS WE OBSERVE, CALCULATE IN WHICH TIMEFRAME THEY OCCURED
const sumTime = this.eventBuffer.reduce((a, b) => a + b, 0);
this.eventBuffer.shift();
if (sumTime <= this.timeframe) {
this.triggerAlarm();
}
}
}
triggerEvent(timestamp, totalEvents) {
this.emit("event", timestamp, totalEvents);
}
triggerAlarm() {
this.triggered = true;
this.eventBuffer = this.autoreset ? [] : this.eventBuffer;
this.emit("alarm");
}
reset() {
this.triggered = false;
this.eventBuffer = [];
}
}
class Alarm extends EventEmitter {
constructor() {
super();
this.alarmStack = {};
}
add (options) {
const rule = new Rule(options);
rule.on('event', (timestamp, totalEvents) => {
this.emit('event', {
name : rule.name,
timestamp : timestamp,
totalEvents : totalEvents
});
});
rule.on('alarm', () => {
this.emit('alarm', {
name : rule.name
});
});
this.alarmStack[rule.name] = rule;
return rule;
}
remove (name) {
if (!!this.alarmStack[name]) {
this.alarmStack[name] = null;
delete this.alarmStack[name];
}
}
count (name) {
if (!!this.alarmStack[name]) {
this.alarmStack[name].count();
}
}
reset (name) {
if (!!this.alarmStack[name]) {
this.alarmStack[name].reset();
}
}
getLastOccurence (name, humanReadable) {
if (!!this.alarmStack[name]) {
let lastOccurence = this.alarmStack[name].lastOccurence;
if (!humanReadable) { return new Date(lastOccurence); } else {
let now = Date.now();
let diff = now - lastOccurence;
return `${diff} ms ago`
}
}
}
}
/////////////////////////////
module.exports = Alarm