This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
event_summarizer.js
80 lines (73 loc) · 1.86 KB
/
event_summarizer.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
function EventSummarizer(config) {
var es = {};
var startDate = 0,
endDate = 0,
counters = {};
es.summarizeEvent = function(event) {
if (event.kind === 'feature') {
var counterKey = event.key +
':' +
((event.variation !== null && event.variation !== undefined) ? event.variation : '') +
':' +
((event.version !== null && event.version !== undefined) ? event.version : '');
var counterVal = counters[counterKey];
if (counterVal) {
counterVal.count = counterVal.count + 1;
} else {
counters[counterKey] = {
count: 1,
key: event.key,
version: event.version,
variation: event.variation,
value: event.value,
default: event.default
};
}
if (startDate === 0 || event.creationDate < startDate) {
startDate = event.creationDate;
}
if (event.creationDate > endDate) {
endDate = event.creationDate;
}
}
}
es.getSummary = function() {
var flagsOut = {};
for (var i in counters) {
var c = counters[i];
var flag = flagsOut[c.key];
if (!flag) {
flag = {
default: c.default,
counters: []
};
flagsOut[c.key] = flag;
}
counterOut = {
value: c.value,
count: c.count
};
if (c.variation !== undefined && c.variation !== null) {
counterOut.variation = c.variation;
}
if (c.version) {
counterOut.version = c.version;
} else {
counterOut.unknown = true;
}
flag.counters.push(counterOut);
}
return {
startDate: startDate,
endDate: endDate,
features: flagsOut
};
}
es.clearSummary = function() {
startDate = 0;
endDate = 0;
counters = {};
}
return es;
}
module.exports = EventSummarizer;