-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
295 lines (253 loc) · 8.61 KB
/
renderer.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
var fs = require('fs'),
readline = require('readline'),
stream = require('stream');
function parseFile(file)
{
if(tail!==undefined)
tail.unwatch();
console.log("parsing file "+file);
$('a[href="#graphs"]').tab('show');
$("#charts").html(getSpinner(true));
$("#errors").html("");
var instream = fs.createReadStream(file);
var outstream = new stream;
outstream.readable = true;
outstream.writable = true;
var labels = [];
var internaldata = [];
var data = [];
var datacount = 0;
var linecount = 0;
var user_labels = [];
var user_data = [];
var ou_labels = [];
var ou_data = [];
var success_data = [0,0];
var errors = [];
var hourly_labels = [];
var hourly_data = [];
var daily_labels = [];
var daily_data = [];
var highestday = 0;
var packages = 0;
var users = [];
var users_timestampdata = [];
var failed_users = [];
var users_by_day = [];
tail = new Tail(file);
tail.on("line", function(data) {
console.log("new line: "+data);
actOnLine(data);
//update byAP graph
byAP.data.datasets[0].data = data;
byAP.data.datasets[0].labels = labels;
byAP.update();
//update byDay graph
byDay.data.datasets[0].data = daily_data;
byDay.data.labels = daily_labels;
byDay.update();
//update byHour graph
byHour.data.datasets[0].data = hourly_data;
byHour.data.labels = hourly_labels;
byHour.update();
//update bySuccess graph
bySuccess.data.datasets[0].data = success_data;
bySuccess.update();
});
var rl = readline.createInterface({
input: instream,
output: outstream,
terminal: false
});
rl.on('close', function() {
tail.unwatch();
console.log('finished. Soo many packages: '+packages);
console.log('highest day: '+highestday)
for(var i=1;i<=(highestday+1);i++)
{
daily_labels[daily_labels.length] = i;
}
for(var i=0;i<=23;i++)
{
hourly_labels[hourly_labels.length] = i;
}
prepareCharts();
renderByAP(labels,data);
renderSuccess(success_data);
renderErrorTable(errors);
renderByDay(daily_labels, daily_data)
renderTimeline(hourly_labels,hourly_data);
//console.log(users_timestampdata);
renderUsersTable(users,users_timestampdata);
$( ".userinfo" ).click(function() {
var user = $(this).text();
var data = users_by_day[user];
console.log(data);
var content = "<h2>Daily info about: <strong>"+user+"</strong></h2><div class=\"table-responsive table-striped\"><table class=\"table\">"
var day = 1;
for(i=0; i<5; i++){
content += '<tr>';
for(j=0; j<7; j++){
if(day>31) continue;
if(data[day]!==undefined)
content+= '<td style="height:50px;background-color:#0099e6;">' + 'day ' + day + '</td>';
else
content+= '<td style="height:50px;">' + 'day ' + day + '</td>';
day++;
}
content += '</tr>';
}
content += "</table></div>"
$("#userview").html(content);
});
});
function actOnLine(line)
{
var parsed = parseNPSLine(line);
if(!parsed) return;
packages++;
var day = parsed.timestamp.getUTCDate();
if(parsed.user!==false)
{
if(users_by_day[parsed.user]===undefined)
users_by_day[parsed.user] = [];
users_by_day[parsed.user][day] = 1;
}
if(parsed.ou!==undefined)
{
var labelindex = ou_labels.indexOf(parsed.ou)
if(labelindex==-1)
{
labelindex = ou_labels.length;
ou_labels[labelindex] = parsed.ou;
}
if(ou_data[labelindex]==NaN || ou_data[labelindex] == undefined)
ou_data[labelindex] = 0;
ou_data[labelindex]++;
}
//saving ap data
var labelindex = labels.indexOf(parsed.ap_radname)
if(labelindex==-1)
{
labelindex = labels.length;
labels[labelindex] = parsed.ap_radname;
}
if(data[labelindex]==NaN || data[labelindex] == undefined)
data[labelindex] = 0;
data[labelindex]++;
//saving hourly data
var index = parsed.timestamp.getHours();//parseInt(parsed.timestamp.getUTCDate()+1);
if(hourly_data[index]==NaN || hourly_data[index]==undefined)
hourly_data[index] = 0;
hourly_data[index]++;
//saving daily data
var index = parseInt(parsed.timestamp.getUTCDate())-1//parseInt(parsed.timestamp.getUTCDate()+1);
if(daily_data[index]==NaN || daily_data[index]==undefined)
daily_data[index] = 0;
daily_data[index]++;
if(highestday==0)
{
//console.log("timestamp",parsed.timestamp);
var month = parseInt(parsed.date.split('/')[0])-1;
var d = new Date(parsed.timestamp.getFullYear(), month + 1, 0);
console.log(d)
console.log(d.getUTCDate())
highestday = d.getUTCDate();
}
//console.log(date+" "+time);
switch(parseInt(parsed.type))
{
case 1: //Requesting access
break;
case 2: //Accepted
//console.log(origin+" is accepted on "+ ap_radname);
//echo "$origin is accepted on $ap_radname_full\n\n";
success_data[1]++;
//saving connected user data
var index = parsed.user
var labelindex = users.indexOf(index)
if(labelindex==-1)
{
labelindex = users.length;
users[labelindex] = index;
}
users_timestampdata[labelindex] = parsed.timestamp;
break;
case 3: //Rejected
//console.log(origin+" is rejected because "+ rs);
//echo "$origin is rejected because: $rs\n\n";
success_data[0]++;
errors[errors.length] = {time:parsed.timestamp,user:parsed.origin,reason:parsed.rs,reasoncode:parsed.reason,mac:parsed.client_mac};
//saving connected user data
var index = parsed.user
var labelindex = failed_users.indexOf(index)
if(labelindex==-1)
{
labelindex = failed_users.length;
failed_users[labelindex] = index;
}
break;
case 4: //Accounting-Request
break;
case 5: //Accounting-Response
break;
case 11: //Access-Challenge
break;
default:
//echo "$reason\t$origin\t$timestamp\t$client\t$tt\t$ap_radname_full\n";
}
}
rl.on('line', function(line) {
actOnLine(line);
});
}
function renderErrorTable(data)
{
var data = getLastNItems(data,20);
var table = '<table class="table table-hover">';
table+='<tr>\
<th>Time</th>\
<th>User</th>\
<th>Error</th>\
</tr>';
for(var i=0;i<data.length;i++)
{
var o = data[i];
//console.log(o);
table+='<tr>\
<td>'+ o.time +'</td>\
<td>'+ o.user +'</td>\
<td>'+ o.reason +'</td>\
</tr>\
';
//mac2vendor(o.mac);
}
table+='</table>';
$("#errors").html(table)
}
function renderUsersTable(data,timestampdata)
{
var table = '<table class="table table-hover">';
table+='<tr>\
<th>User</th>\
<th>Last seen</th>\
<th></th>\
</tr>';
for(var i=0;i<data.length;i++)
{
var o = data[i];
//console.log(o);
var moment = require('moment');
//console.log(moment(timestampdata[i]).fromNow());
var relative = moment(timestampdata[i]).fromNow()
table+='<tr>\
<td><a href="#" class="userinfo">'+ o +'</a></td>\
<td>'+ relative +'</td>\
<td>'+ timestampdata[i] +'</td>\
</tr>\
';
//mac2vendor(o.mac);
}
table+='</table>';
$("#users").html('<div id="userview"></div>'+table)
}