-
Notifications
You must be signed in to change notification settings - Fork 6
/
queries.js
88 lines (62 loc) · 1.83 KB
/
queries.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
function(doc) {
var timeRound = 10 * 60 * 1000;
var tp = doc._id.match(/(\d+)-(\d+)-(\d+)\.(\d\d)(\d\d)(\d\d)/);
var date = new Date(tp[1], parseInt(tp[2])-1, tp[3], tp[4], tp[5], tp[6]);
var time = date.getTime();
time = time - time % timeRound;
for(var i = 0, len = doc.tuples.length; i < len; i++) {
var tuple = doc.tuples[i];
if (!tuple.srcIP || !tuple.dstIP) continue;
if(tp) {
var val = parseInt(tuple.octets, 10);
var key;
if (tuple.srcIP.indexOf('.') == -1) { // No IP :)
key = 'out-' + tuple.srcIP;
}
else if (tuple.dstIP.indexOf('.') == -1) { // No IP :)
key = 'in-' + tuple.dstIP;
}
if (key) {
emit(['total' + key], val);
emit([key, time], val);
emit([key.replace(/-.*$/, '-N'), time], val);
}
}
}
}
function(doc) {
var timeRound = 10 * 60 * 1000;
var tp = doc._id.match(/(\d+)-(\d+)-(\d+)\.(\d\d)(\d\d)(\d\d)/);
if(!tp) {
return;
}
var date = new Date(tp[1], parseInt(tp[2])-1, tp[3], tp[4], tp[5], tp[6]);
var time = date.getTime();
time = time - time % timeRound;
var group = {};
for(var i = 0, len = doc.tuples.length; i < len; i++) {
var tuple = doc.tuples[i];
if (!tuple.srcIP || !tuple.dstIP) continue;
var val = parseInt(tuple.octets, 10);
var key;
if (tuple.srcIP.indexOf('.') == -1) { // No IP :)
key = 'out-' + tuple.srcIP;
}
else if (tuple.dstIP.indexOf('.') == -1) { // No IP :)
key = 'in-' + tuple.dstIP;
}
if(key) {
if(!group[key]) {
group[key] = val;
} else {
group[key] += val;
}
}
}
for(var key in group) {
var val = group[key];
emit(['total' + key], val);
emit([key, time], val);
emit([key.replace(/-.*$/, '-N'), time], val);
}
}