-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestReadNginxLog.js
51 lines (42 loc) · 1.06 KB
/
testReadNginxLog.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
/**
* Created by chaclus on 2017/2/6.
*/
var fs = require('fs');
var readline = require('readline');
var rl = readline.createInterface({
input: fs.createReadStream('./log/apiserver.log'),
// output: process.stdout
});
var urlMap = {}, sum = 0;
rl.on('line', function(line) {
var arr = line.split('|');
var a = arr[10].split(' ');
var url = a[2];
if(url.lastIndexOf('?') > 0){
url = url.substring(0, url.lastIndexOf('?'));
}
if(url.split('/').pop().length == 24) {
url = url.substring(0, url.length - 24 -1);
}
if(urlMap[url]){
urlMap[url] = urlMap[url] + 1;
}else{
urlMap[url] = 1;
}
}).on('close', function() {
console.log('finish!');
var urlsSortArray = [];
for(var key in urlMap) {
sum = sum + urlMap[key];
urlsSortArray.push({url: key, val: urlMap[key]});
}
urlsSortArray.sort(function (a, b) {
if (a.val < b.val) {
return 1;
} else {
return -1;
}
});
console.log(urlsSortArray);
process.exit(0);
});