-
Notifications
You must be signed in to change notification settings - Fork 116
/
log.js
70 lines (68 loc) · 1.5 KB
/
log.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
class Count {
constructor() {
this.map = {};
}
add(key) {
if( typeof this.map[key] == 'undefined' ) {
this.map[key] = 1;
} else {
this.map[key]++;
}
}
display() {
let r = [];
for( let key in this.map ) {
r.push({name: key, count: this.map[key]});
}
r.sort((a, b) => {
return b.count - a.count;
});
return r.slice(0, 10).map((obj) => {
return `${obj.name} : ${obj.count}`;
}).join('\r\n');
}
}
function main() {
let argv = require('process').argv.slice(2);
let date = argv[0];
var fileReader = require('readline').createInterface({
input: require('fs').createReadStream('./access.log')
});
let log = [];
let userReg = /^([\d\.]+)/, user = new Count();
let versionReg = /bilimini\/([\d\.])+/, version = new Count();
let osReg = /Mozilla\/5\.0 \((.+?)\)/, os = new Count();
fileReader.on('line', (line) => {
new Promise((resolve) => {
if( date ) {
if( line.indexOf(date) > -1 ) {
log.push(line);
resolve();
}
} else {
log.push(line);
resolve();
}
}).then(() => {
let u = userReg.exec(line);
if( u ) {
user.add(u[0]);
}
let v = versionReg.exec(line);
if( v ) {
version.add(v[0]);
}
let o = osReg.exec(line);
if( o ) {
os.add(o[1]);
}
}).catch(() => {});
});
fileReader.on('close', () => {
console.log(`Total: ${log.length}\n`);
console.log(`Version: \n${version.display()}\n`);
console.log(`OS: \n${os.display()}\n`);
console.log(`User List: \n${user.display()}\n`);
});
}
main();