forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_summarizer.js
101 lines (92 loc) · 2.39 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const { getContextKinds } = require('./context');
function getKinds(event) {
if (event.context) {
return getContextKinds(event.context);
}
if (event.contextKeys) {
return Object.keys(event.contextKeys);
}
return [];
}
function EventSummarizer() {
const es = {};
let startDate = 0,
endDate = 0,
counters = {},
contextKinds = {};
es.summarizeEvent = event => {
if (event.kind === 'feature') {
const counterKey =
event.key +
':' +
(event.variation !== null && event.variation !== undefined ? event.variation : '') +
':' +
(event.version !== null && event.version !== undefined ? event.version : '');
const counterVal = counters[counterKey];
let kinds = contextKinds[event.key];
if (!kinds) {
kinds = new Set();
contextKinds[event.key] = kinds;
}
getKinds(event).forEach(kind => kinds.add(kind));
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 = () => {
const flagsOut = {};
for (const c of Object.values(counters)) {
let flag = flagsOut[c.key];
if (!flag) {
flag = {
default: c.default,
counters: [],
contextKinds: [...contextKinds[c.key]],
};
flagsOut[c.key] = flag;
}
const counterOut = {
value: c.value,
count: c.count,
};
if (c.variation !== undefined && c.variation !== null) {
counterOut.variation = c.variation;
}
if (c.version !== undefined && c.version !== null) {
counterOut.version = c.version;
} else {
counterOut.unknown = true;
}
flag.counters.push(counterOut);
}
return {
startDate: startDate,
endDate: endDate,
features: flagsOut,
};
};
es.clearSummary = () => {
startDate = 0;
endDate = 0;
counters = {};
contextKinds = {};
};
return es;
}
module.exports = EventSummarizer;