-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
175 lines (147 loc) · 4.58 KB
/
main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var maxElements = 100;
var dynamicScaleMinCount = 100; // Min count for dynamic scale
var maxFontSizePercent = 2000; // percent
var minOpacity = 0.1; // [0, 1]
var msTilFadeAway = 1000 * 60 * 60 * 3; // 3 hours in ms
var startTime = Date.now() / 1000;
var elements = {};
var data = {};
var timeScalar = (1 - minOpacity) / msTilFadeAway; // % opacity per ms
var sizeScalar = (maxFontSizePercent - 100) / dynamicScaleMinCount;
function processRequests(requests) {
var body = $('body');
var template = $('#template').html();
var totalRequests = Object.keys(requests).length;
console.log('processing', totalRequests, 'ssids');
var maxCount = 0;
var maxLastSeen = 0;
var created = false;
$.each(requests, render);
startTime = maxLastSeen;
var allNames = Object.keys(data);
var recent = allNames.sort(function(a, b) {
return data[a].lastSeen - data[b].lastSeen;
}).slice(-1 * maxElements);
$.each(elements, function(name, element) {
if (recent.indexOf(name) < 0) {
if (element.parent().length > 0) {
element.remove();
}
} else {
if (elements[name].parent().length === 0) {
body.append(elements[name]);
}
}
});
// dynamically compute a new sizeScalar such that the most seen entry is
// assigned the maxFontSizePercent.
// sizeScalar*maxCount + 100 = maxFontSizePercent
//sizeScalar = Math.min(1, (maxFontSizePercent - 100) / maxCount);
function render(name, request) {
if (request.lastSeen > maxLastSeen) {
maxLastSeen = request.lastSeen;
}
if (request.count > maxCount) {
maxCount = request.count;
}
var $div = elements[request.name];
if ($div) {
update($div, request);
} else {
if (!created) {
created = true;
$div = create(request);
elements[request.name] = $div;
}
}
}
function create(request) {
var $div = $(template).appendTo(body);
update($div, request);
return $div;
}
function update($div, request) {
var macList = Object.keys(request.macs).sort();
var seconds = startTime - request.lastSeen;
var maxTop = body.outerHeight() - $div.outerHeight();
var decayTop = maxTop*Math.log2(seconds*0.05)/10;
var top = Math.max(0, Math.min(maxTop, decayTop));
var opacity = Math.max(minOpacity, 1 - ((startTime - request.lastSeen) * timeScalar));
var fontSize = Math.min(maxFontSizePercent, 100 + sizeScalar*macList.length);
var current = {
fontSize: fontSize,
opacity: opacity,
top: top,
count: request.count,
users: macList.length,
lastSeen: request.lastSeen,
};
var changed = false;
var prev = data[request.name] || {};
for (prop in current) {
if (prev[prop] !== current[prop]) {
changed = true;
}
}
if (changed) {
data[request.name] = current;
$div.css({
top: top + 'px',
opacity: opacity
});
$div.find('.name').text(request.name).css('font-size', fontSize + '%');
$div.find('.stats').text(
request.count + '/' + macList.length
);
$div.find('.macs').html('');
var $users = $div.find('.macs');
macList.forEach(function(mac) {
if ($users.find('.' + mac).length === 0) {
$users.append(macToColor(mac));
}
});
}
}
function macToColor(mac) {
var $user = $('<div>').addClass('user').addClass(mac);
var octets = mac.split(':');
var color = '#' + octets.splice(0,3).join('');
$user.append(createColorSpan(color));
var color = '#' + octets.splice(0,3).join('');
$user.append(createColorSpan(color));
return $user;
}
function createColorSpan(color) {
var $span = $('<span class="color">').css('background-color', color);
return $span;
}
}
$(function() {
//poll('probereq.json');
test('probereq.json');
function poll(url) {
$.getJSON(url, function(data) {
processRequests(data);
setTimeout(poll.bind(null, url), 1000);
});
}
function test(url) {
$.getJSON(url, function(testRequests) {
var names = Object.keys(testRequests);
run();
function run() {
if (Math.random() < 0.1) {
var randomName = names[Math.floor(Math.random() * names.length/2)];
increment(testRequests[randomName]);
}
processRequests(testRequests);
setTimeout(run, 1000);
};
function increment(request) {
request.lastSeen = Date.now() / 1000;
request.count += 1;
console.log('simulating probe request', request.name);
}
});
}
});