forked from ambroseus/sfcc-logs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.js
89 lines (77 loc) · 1.96 KB
/
analyzer.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
'use strict';
module.exports = { analyzeLogs }
let _strace;
let _errors;
function analyzeLogs(logs) {
_strace = {};
_errors = {};
logs.map(trace).map(analyze);
return _errors;
}
const RE = {
STAMP : /^\[.+GMT\]\s(.+)/,
SITES : /Sites-(.*?)-?Site/,
SESSION : /^(.+?)\s+\d{17,20}\s*\-?\s*/,
STRACE : /^Stack trace <(ref:)?(\w+)>/,
ERROR : /ERROR\sPipelineCallServlet/
}
const Liner = require('./liner.js');
function trace(log) {
console.log(`trace: ${log}`);
let liner = new Liner(log);
let line = liner && liner.next() || null;
while (line !== null) {
const found = line.match(RE.STRACE);
if ( found && !found[1] ) {
const key = found[2];
_strace[key] = liner.next();
line = liner.next();
if ( !RE.STAMP.test(line) ) _strace[key] += line;
}
line = liner.next();
}
return log;
}
const hash = require('crypto').createHash;
function analyze(log) {
console.log(`analyze: ${log}`);
let liner = new Liner(log);
let line = liner && liner.next() || null;
let site, pipe, msg;
while (line !== null) {
const found = line.match(RE.STAMP);
if (found) {
const parts = found[1].split('|');
site = parts && RE.ERROR.test(parts[0]) && parts[2];
if (site) {
pipe = parts[3];
site = site.replace(RE.SITES, '$1');
msg = parts[5] && parts[5].replace(RE.SESSION, '') || '';
}
}
else {
const found = line.match(RE.STRACE);
if ( found && site ) {
const desc = _strace[found[2]] || "no-trace-available";
const key = hash('md5').update(desc).digest('hex');;
let count;
if ( !(key in _errors) ) {
_errors[key] = {
'total': 0,
'sites': {},
'pipes': {},
'desc': desc,
'msg': msg
};
}
_errors[key].total++;
count = _errors[key].sites[site];
_errors[key].sites[site] = count ? count + 1 : 1;
count = _errors[key].pipes[pipe];
_errors[key].pipes[pipe] = count ? count + 1 : 1;
}
}
line = liner.next();
}
return log;
}