-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (87 loc) · 2.58 KB
/
index.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
const fs = require('fs');
const byline = require('byline');
const program = require('commander');
program
.version('0.1.0')
.option('-f, --file [log-path]', 'Log path')
.parse(process.argv);
let archivoLog = program.file || './partial-kong.log';
const GROUPS_BY_CONSUMER_FILENAME = './visualize/js/groups-by-CONSUMER.js';
const GROUPS_BY_HOUR_FILENAME = './visualize/js/groups-by-hour.js';
try {
fs.unlinkSync(GROUPS_BY_CONSUMER_FILENAME);
} catch(err){}
try {
fs.unlinkSync(GROUPS_BY_HOUR_FILENAME);
} catch(err){}
const stream = byline(fs.createReadStream(archivoLog, { encoding: 'utf8' }));
const groups = {};
const groupsByHour = {};
function group({ year, month , consumer, service }) {
if (!groups.hasOwnProperty(year)) {
groups[year] = {};
}
if (!groups[year].hasOwnProperty(month)) {
groups[year][month] = {};
}
if (groups[year][month].hasOwnProperty(consumer)) {
groups[year][month][consumer]++;
} else {
groups[year][month][consumer] = 1;
}
}
function groupByHour({ hour, month, year, service, consumer }) {
if (!groupsByHour.hasOwnProperty(year)) {
groupsByHour[year] = {};
}
if (!groupsByHour[year].hasOwnProperty(month)) {
groupsByHour[year][month] = {};
}
if (!groupsByHour[year][month].hasOwnProperty(hour)) {
groupsByHour[year][month][hour] = {};
}
if (groupsByHour[year][month][hour].hasOwnProperty(consumer)) {
groupsByHour[year][month][hour][consumer]++;
} else {
groupsByHour[year][month][hour][consumer] = 1;
}
}
stream.on('data', function(line) {
let logEnJson;
try {
logEnJson = JSON.parse(line);
const logDate = new Date(logEnJson.started_at);
group({
year: logDate.getFullYear(),
month: logDate.getMonth(),
consumer: logEnJson.consumer.username,
service: logEnJson.api.name,
});
groupByHour({
year: logDate.getFullYear(),
hour: logDate.getHours(),
month: logDate.getMonth(),
consumer: logEnJson.consumer.username,
service: logEnJson.api.name,
});
} catch(err) {
console.error(err);
console.log('Unable to parse line', line);
}
});
stream.on('error', function(error) {
console.log('+++++++++++++++++++++++++++++++++++++++++++');
console.error(error);
});
stream.on('finish', function(error) {
fs.writeFile(GROUPS_BY_CONSUMER_FILENAME, `const data = ${JSON.stringify(groups, null, 2)}`, 'utf8', (err) => {
if (err) {
console.error(err);
}
});
fs.writeFile(GROUPS_BY_HOUR_FILENAME, `const data = ${JSON.stringify(groupsByHour, null, 2)}`, 'utf8', (err) => {
if (err) {
console.error(err);
}
});
});