forked from iNavFlight/inav-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventFrequencyAnalyzer.js
78 lines (64 loc) · 1.84 KB
/
eventFrequencyAnalyzer.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
'use strict';
/**
* Simple analyzer that returns frequency of events using 5s buffer
* Usage: register periodic events with 'put', then call 'get' to get results
*/
var eventFrequencyAnalyzer = (function () {
var privateScope = {},
publicScope = {},
bufferPeriod = 5000;
privateScope.data = {};
privateScope.output = {};
privateScope.intervalHandler;
/**
* Periodically executed aggregation task
* @returns {{}|*}
*/
publicScope.analyze = function () {
privateScope.output = {};
for (var i in privateScope.data) {
if (privateScope.data.hasOwnProperty(i)) {
privateScope.output[i] = privateScope.data[i] / bufferPeriod * 1000;
}
}
privateScope.data = {};
return privateScope.output;
};
/**
* Return event list with frequencies
* @returns {{}|*}
*/
publicScope.get = function () {
return privateScope.output;
};
/**
* Returns raw data
* @returns {{}|*}
*/
publicScope.getRaw = function () {
return privateScope.data;
};
/**
* Put event into analyzer
* @param {object} event
*/
publicScope.put = function (event) {
if (privateScope.data[event]) {
privateScope.data[event]++;
} else {
privateScope.data[event] = 1;
}
};
/**
*
* @param {number} buffer buffer length in milliseconds
*/
publicScope.setBufferPeriod = function (buffer) {
bufferPeriod = buffer;
clearInterval(privateScope.intervalHandler);
privateScope.intervalHandler = setInterval(publicScope.analyze, bufferPeriod);
};
privateScope.intervalHandler = setInterval(publicScope.analyze, bufferPeriod);
return publicScope;
})();
module.exports = eventFrequencyAnalyzer;