-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
594 lines (533 loc) · 19.1 KB
/
app.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// #region ==================== ELEMENTS
let accessLogTextInputBox = qs("#access-log-text-input");
let pathGroupsInputBox = qs("#path-groups-input");
let logsBody = qs("#logs-table tbody");
let statEls = {
daysCount : qs("#days-count"),
requestsCount : qs("#requests-count"),
ipsCount : qs("#ips-count"),
bytesSum : qs("#bytes-sum"),
referrersCount : qs("#referrers-count"),
uaCount : qs("#ua-count"),
};
let ipCountBody = qs("#ip-count-table tbody");
let dateCountBody = qs("#date-count-table tbody");
let methodCountBody = qs("#method-count-table tbody");
let pathGroupsCountBody = qs("#path-groups-count-table tbody");
let protocolCountBody = qs("#protocol-count-table tbody");
let statusCountBody = qs("#status-count-table tbody");
let referrerCountBody = qs("#referrer-count-table tbody");
let uaCountBody = qs("#ua-count-table tbody");
let ipBytesBody = qs("#ip-bytes-table tbody");
let dateBytesBody = qs("#date-bytes-table tbody");
let methodBytesBody = qs("#method-bytes-table tbody");
let pathGroupsBytesBody = qs("#path-groups-bytes-table tbody");
let protocolBytesBody = qs("#protocol-bytes-table tbody");
let statusBytesBody = qs("#status-bytes-table tbody");
let referrerBytesBody = qs("#referrer-bytes-table tbody");
let uaBytesBody = qs("#ua-bytes-table tbody");
// #endregion
// #region ==================== EVENTS
on(pathGroupsInputBox, "input", debounce(() => {
pathGroupsInputBox.value = trimTextWhitespace(pathGroupsInputBox.value);
}));
on(accessLogTextInputBox, "paste", function logPasteDuration() {
const startTime = performance.now();
setTimeout(function() {
const endTime = performance.now();
const pasteTime = endTime - startTime;
let charCount = accessLogTextInputBox.value.length;
let size = (charCount / 1024 / 1024).toFixed(2);
console.log(`Pasted ${charCount} chars (${size} MB) in ${Math.round(pasteTime)} ms`);
});
});
trigger(pathGroupsInputBox, "input");
on("#parse-btn", "click", () => {
let sw = new Stopwatch();
sw.time("all");
sw.time("parse");
let logs = parseAccessLogs(accessLogTextInputBox.value);
sw.timeEnd("parse");
sw.time("print lines");
printLogsLines(logs);
sw.timeEnd("print lines");
sw.time("stats");
statEls.daysCount.textContent = new Set(
getColumn(
logs,
log => isoDateFromParsedDate(parseISODateTime(log.datetime))
)
).size;
statEls.requestsCount.textContent = logs.length;
statEls.ipsCount.textContent = new Set(getColumn(logs, "ip")).size;
statEls.bytesSum.textContent = (
sum(
getColumn(logs, "length")
.filter(value => value != "-")
.map(value => parseInt(value))
)
/ 1024 / 1024
).toFixed(2);
statEls.referrersCount.textContent = new Set(
getColumn(logs, "referrer").map(ref => getLogReferrerHost(ref))
).size;
statEls.uaCount.textContent = new Set(getColumn(logs, "ua")).size;
sw.timeEnd("stats");
sw.time("counts");
printIpRequestCounts(logs);
printDateRequestCounts(logs);
printMethodRequestCounts(logs);
printPathGroupsRequestCounts(logs);
printProtocolRequestCounts(logs);
printStatusRequestCounts(logs);
printReferrerRequestCounts(logs);
printUARequestCounts(logs);
sw.timeEnd("counts");
sw.time("bytes");
sw.time("bytes - ip");
printIpRequestBytes(logs);
sw.timeEnd("bytes - ip");
sw.time("bytes - date");
printDateRequestBytes(logs);
sw.timeEnd("bytes - date");
sw.time("bytes - method");
printMethodRequestBytes(logs);
sw.timeEnd("bytes - method");
sw.time("bytes - path");
printPathGroupsRequestBytes(logs);
sw.timeEnd("bytes - path");
sw.time("bytes - protocol");
printProtocolRequestBytes(logs);
sw.timeEnd("bytes - protocol");
sw.time("bytes - status");
printStatusRequestBytes(logs);
sw.timeEnd("bytes - status");
sw.time("bytes - referrer");
printReferrerRequestBytes(logs);
sw.timeEnd("bytes - referrer");
sw.time("bytes - ua");
printUARequestBytes(logs);
sw.timeEnd("bytes - ua");
sw.timeEnd("bytes");
sw.timeEnd("all");
console.table(sw.getDurations());
});
// #endregion
// #region ==================== LOGS
function printLogsLines(logs) {
let maxLogRowCount = 1000;
let logIndex = 1;
logsBody.innerHTML = "";
for (const log of logs) {
logsBody.append( buildLogLine(log, logIndex++) );
if (logIndex > maxLogRowCount) {
break;
}
}
}
function buildLogLine(log, index) {
let row = elem(
"tr",
{
"class" : "*:px-2 *:py-[2px] *:border hover:bg-slate-50",
},
[
elem("td", index),
elem("td", log.ip),
elem("td", log.identity),
elem("td", log.user),
elem("td", {"class" : "whitespace-nowrap"}, log.datetime),
],
);
if (log.request.method) {
row.append(
elem("td", log.request.method),
elem("td", log.request.path),
elem("td", log.request.protocol),
);
} else {
row.append(
elem("td", {"colspan" : 3, "class" : "bg-red-50"}, log.request.raw.slice(1, -1)),
);
}
row.append(
elem("td", log.status),
elem("td", log.length),
elem("td", log.referrer?.slice(1, -1)),
elem("td", log.ua?.slice(1, -1)),
);
return row;
}
// #endregion
// #region ==================== COUNTS
function printIpRequestCounts(logs) {
let ipFrequency = calcFrequency(getColumn(logs, "ip"));
ipFrequency = Object.entries(ipFrequency);
ipFrequency = sortBy(ipFrequency, [1, -1], 0);
ipFrequency = ipFrequency.slice(0, 10);
let ipIndex = 1;
ipCountBody.innerHTML = "";
for (const entry of ipFrequency) {
ipCountBody.append( buildCountLine(entry, ipIndex++) );
}
}
function printDateRequestCounts(logs) {
let dateFrequency = calcFrequency(
getColumn(logs, log => {
return isoDateFromParsedDate(parseISODateTime(log.datetime));
})
);
dateFrequency = Object.entries(dateFrequency);
// dateFrequency = sortBy(dateFrequency, [1, -1], 0);
let dateIndex = 1;
dateCountBody.innerHTML = "";
for (const entry of dateFrequency) {
dateCountBody.append( buildCountLine(entry, dateIndex++) );
}
}
function printMethodRequestCounts(logs) {
let methodFrequency = calcFrequency(getColumn(logs, "request.method"));
methodFrequency = Object.entries(methodFrequency);
methodFrequency = sortBy(methodFrequency, [1, -1], 0);
let methodIndex = 1;
methodCountBody.innerHTML = "";
for (const entry of methodFrequency) {
methodCountBody.append( buildCountLine(entry, methodIndex++) );
}
}
function printPathGroupsRequestCounts(logs) {
let pathGroups = parsePathGroups(pathGroupsInputBox.value);
let pathGroupsFrequency = {
"[OTHER]" : 0,
"[INVALID]" : 0,
};
pathGroupsCountBody.innerHTML = "";
if (Object.keys(pathGroups).length) {
for (const log of logs) {
if (!log.request.path) {
pathGroupsFrequency["[INVALID]"]++;
continue;
}
let groupMatch = false;
for (const pathGroupName in pathGroups) {
const pathGroupRegex = pathGroups[pathGroupName];
if (pathGroupRegex.exec(log.request.path)) {
groupMatch = true;
if (pathGroupsFrequency[pathGroupName]) {
pathGroupsFrequency[pathGroupName]++;
} else {
pathGroupsFrequency[pathGroupName] = 1;
}
}
}
if (!groupMatch) {
pathGroupsFrequency["[OTHER]"]++;
}
}
pathGroupsFrequency = Object.entries(pathGroupsFrequency);
pathGroupsFrequency = sortBy(pathGroupsFrequency, [1, -1], 0);
let pathGroupIndex = 1;
for (const entry of pathGroupsFrequency) {
pathGroupsCountBody.append( buildCountLine(entry, pathGroupIndex++) );
}
}
}
function printProtocolRequestCounts(logs) {
let protocolFrequency = calcFrequency(getColumn(logs, "request.protocol"));
protocolFrequency = Object.entries(protocolFrequency);
protocolFrequency = sortBy(protocolFrequency, [1, -1], 0);
let protocolIndex = 1;
protocolCountBody.innerHTML = "";
for (const entry of protocolFrequency) {
protocolCountBody.append( buildCountLine(entry, protocolIndex++) );
}
}
function printStatusRequestCounts(logs) {
let statusFrequency = calcFrequency(getColumn(logs, "status"));
statusFrequency = Object.entries(statusFrequency);
statusFrequency = sortBy(statusFrequency, [1, -1], 0);
let statusIndex = 1;
statusCountBody.innerHTML = "";
for (const entry of statusFrequency) {
statusCountBody.append( buildCountLine(entry, statusIndex++) );
}
}
function printReferrerRequestCounts(logs) {
let referrers = getColumn(logs, "referrer");
referrers = referrers.map(ref => getLogReferrerHost(ref));
let referrerFrequency = calcFrequency(referrers);
referrerFrequency = Object.entries(referrerFrequency);
referrerFrequency = sortBy(referrerFrequency, [1, -1], 0);
let referrerIndex = 1;
referrerCountBody.innerHTML = "";
for (const entry of referrerFrequency) {
referrerCountBody.append( buildCountLine(entry, referrerIndex++) );
}
}
function printUARequestCounts(logs) {
let uaFrequency = calcFrequency(getColumn(logs, "ua"));
uaFrequency = Object.entries(uaFrequency);
uaFrequency = sortBy(uaFrequency, [1, -1], 0);
uaFrequency = uaFrequency.slice(0, 10);
uaFrequency = uaFrequency.map(entry => {
entry[0] = unquote(entry[0]);
if (entry[0].startsWith("Mozilla/5.0")) {
entry[0] = entry[0].replace("Mozilla/5.0 ", "");
}
entry[0] = ellipsis(entry[0], 64);
return entry;
});
let uaIndex = 1;
uaCountBody.innerHTML = "";
for (const entry of uaFrequency) {
uaCountBody.append( buildCountLine(entry, uaIndex++) );
}
}
function buildCountLine(entry, index) {
return elem(
"tr",
{
"class" : "*:px-2 *:py-[2px] *:border hover:bg-slate-50",
},
[
elem("td", index),
elem("td", entry[0] == "null" ? "" : entry[0]),
elem("td", entry[1]),
],
);
}
// #endregion
// #region ==================== BYTES
function printIpRequestBytes(logs) {
let bytes = {};
let sw = new Stopwatch();
sw.time("bytes - ip - get");
let ips = Array.from(new Set(getColumn(logs, "ip")));
sw.timeEnd("bytes - ip - get");
sw.time("bytes - ip - calc");
for (const ip of ips) {
sw.time("bytes - ip - calc - requests");
/**
* Woah.
* 550m loops for 7500 ips and 74000 requests.
* This takes the most time.
* // TODO: group the requests by ips?
*/
let ipRequests = logs.filter(log => log.ip == ip);
sw.timeAggregate("bytes - ip - calc - requests");
sw.time("bytes - ip - calc - bytes");
let ipBytes = getColumn(ipRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
sw.timeAggregate("bytes - ip - calc - bytes");
sw.time("bytes - ip - calc - sum");
ipBytes = sum(ipBytes);
ipBytes = parseFloat((ipBytes / 1024 / 1024).toFixed(2));
bytes[ip] = ipBytes;
sw.timeAggregate("bytes - ip - calc - sum");
}
sw.timeEnd("bytes - ip - calc");
sw.time("bytes - ip - sort");
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
sw.timeEnd("bytes - ip - sort");
sw.time("bytes - ip - print");
let byteIndex = 1;
ipBytesBody.innerHTML = "";
for (const entry of bytes) {
ipBytesBody.append( buildCountLine(entry, byteIndex++) );
}
sw.timeEnd("bytes - ip - print");
console.table(sw.getDurations());
console.table(sw.getAggregateDurations());
}
function printDateRequestBytes(logs) {
let bytes = {};
let dates = Array.from(
new Set(
getColumn(
logs,
log => isoDateFromParsedDate(parseISODateTime(log.datetime))
)
)
);
for (const date of dates) {
let dateRequests = logs.filter(
log => isoDateFromParsedDate(parseISODateTime(log.datetime)) == date
);
let dateBytes = getColumn(dateRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
dateBytes = sum(dateBytes);
dateBytes = parseFloat((dateBytes / 1024 / 1024).toFixed(2));
bytes[date] = dateBytes;
}
bytes = Object.entries(bytes);
// bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
let byteIndex = 1;
dateBytesBody.innerHTML = "";
for (const entry of bytes) {
dateBytesBody.append( buildCountLine(entry, byteIndex++) );
}
}
function printMethodRequestBytes(logs) {
let bytes = {};
let methods = Array.from(new Set(getColumn(logs, "request.method")));
for (const method of methods) {
let methodRequests = logs.filter(log => log.request.method == method);
let methodBytes = getColumn(methodRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
methodBytes = sum(methodBytes);
methodBytes = parseFloat((methodBytes / 1024 / 1024).toFixed(2));
bytes[method] = methodBytes;
}
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
let byteIndex = 1;
methodBytesBody.innerHTML = "";
for (const entry of bytes) {
methodBytesBody.append( buildCountLine(entry, byteIndex++) );
}
}
function printPathGroupsRequestBytes(logs) {
let pathGroups = parsePathGroups(pathGroupsInputBox.value);
let bytes = {
"[OTHER]" : [],
"[INVALID]" : [],
};
pathGroupsBytesBody.innerHTML = "";
if (Object.keys(pathGroups).length) {
for (const log of logs) {
let contentLength = log.length != "-" ? log.length : 0;
if (!log.request.path) {
bytes["[INVALID]"].push(contentLength);
continue;
}
let groupMatch = false;
for (const pathGroupName in pathGroups) {
const pathGroupRegex = pathGroups[pathGroupName];
if (pathGroupRegex.exec(log.request.path)) {
groupMatch = true;
if (bytes[pathGroupName]) {
bytes[pathGroupName].push(contentLength);
} else {
bytes[pathGroupName] = [];
}
}
}
if (!groupMatch) {
bytes["[OTHER]"].push(contentLength);
}
}
for (const pathGroupName in bytes) {
bytes[pathGroupName] = sum(bytes[pathGroupName]);
bytes[pathGroupName] = parseFloat((bytes[pathGroupName] / 1024 / 1024).toFixed(2));
}
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
let byteIndex = 1;
pathGroupsBytesBody.innerHTML = "";
for (const entry of bytes) {
pathGroupsBytesBody.append( buildCountLine(entry, byteIndex++) );
}
}
}
function printProtocolRequestBytes(logs) {
let bytes = {};
let protocols = Array.from(new Set(getColumn(logs, "request.protocol")));
for (const protocol of protocols) {
let protocolRequests = logs.filter(log => log.request.protocol == protocol);
let protocolBytes = getColumn(protocolRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
protocolBytes = sum(protocolBytes);
protocolBytes = parseFloat((protocolBytes / 1024 / 1024).toFixed(2));
bytes[protocol] = protocolBytes;
}
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
let byteIndex = 1;
protocolBytesBody.innerHTML = "";
for (const entry of bytes) {
protocolBytesBody.append( buildCountLine(entry, byteIndex++) );
}
}
function printStatusRequestBytes(logs) {
let bytes = {};
let statuses = Array.from(new Set(getColumn(logs, "status")));
for (const status of statuses) {
let statusRequests = logs.filter(log => log.status == status);
let statusBytes = getColumn(statusRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
statusBytes = sum(statusBytes);
statusBytes = parseFloat((statusBytes / 1024 / 1024).toFixed(2));
bytes[status] = statusBytes;
}
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
let byteIndex = 1;
statusBytesBody.innerHTML = "";
for (const entry of bytes) {
statusBytesBody.append( buildCountLine(entry, byteIndex++) );
}
}
function printReferrerRequestBytes(logs) {
let bytes = {};
let referrers = getColumn(logs, "referrer");
referrers = referrers.map(ref => getLogReferrerHost(ref));
referrers = Array.from(new Set(referrers));
for (const referrer of referrers) {
let referrerRequests = logs.filter(log => getLogReferrerHost(log.referrer) == referrer);
let referrerBytes = getColumn(referrerRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
referrerBytes = sum(referrerBytes);
referrerBytes = parseFloat((referrerBytes / 1024 / 1024).toFixed(2));
bytes[referrer] = referrerBytes;
}
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
let byteIndex = 1;
referrerBytesBody.innerHTML = "";
for (const entry of bytes) {
referrerBytesBody.append( buildCountLine(entry, byteIndex++) );
}
}
function printUARequestBytes(logs) {
let bytes = {};
let uas = Array.from(new Set(getColumn(logs, "ua")));
for (const ua of uas) {
let uaRequests = logs.filter(log => log.ua == ua);
let uaBytes = getColumn(uaRequests, "length")
.filter(value => value != "-")
.map(value => parseInt(value));
uaBytes = sum(uaBytes);
uaBytes = parseFloat((uaBytes / 1024 / 1024).toFixed(2));
bytes[ua] = uaBytes;
}
bytes = Object.entries(bytes);
bytes = sortBy(bytes, [1, -1], 0);
bytes = bytes.slice(0, 10);
bytes = bytes.map(entry => {
entry[0] = unquote(entry[0]);
if (entry[0].startsWith("Mozilla/5.0")) {
entry[0] = entry[0].replace("Mozilla/5.0 ", "");
}
entry[0] = ellipsis(entry[0], 64);
return entry;
});
let uaIndex = 1;
uaBytesBody.innerHTML = "";
for (const entry of bytes) {
uaBytesBody.append( buildCountLine(entry, uaIndex++) );
}
}
// #endregion