Completed
Push — develop ( 998e35...3d15b0 )
by William
28s
created

log-process.js ➔ ... ➔ Raven.context   C

Complexity

Conditions 8
Paths 27

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
c 0
b 0
f 0
nc 27
nop 0
dl 0
loc 38
rs 5.3846
1
/**
2
 * @file The file what opens logs.
3
 * @author willyb321
4
 * @copyright MIT
5
 */
6
/**
7
 * @module Reader
8
 */
9
import LineByLineReader from 'line-by-line';
10
import {dialog} from 'electron';
11
import _ from 'lodash';
12
import moment from 'moment';
13
import pug from 'pug'
14
import tableify from 'tableify';
15
import {logPath, currentData, win} from '../main/index';
16
import path from 'path';
17
import Raven from 'raven';
18
Raven.config('https://8f7736c757ed4d2882fc24a2846d1ce8:[email protected]/226655', {
19
	release: require('electron').app.getVersion(),
20
	autoBreadcrumbs: true
21
}).install();
22
/**
23
 * Opens dialog that returns the path of a log file.
24
 * @returns {Promise} Array with path.
25
 */
26
export function getLogPath() {
27
	return new Promise((resolve, reject) => {
28
		const files = dialog.showOpenDialog({
29
			defaultPath: logPath,
30
			buttonLabel: 'Load File',
31
			filters: [{
32
				name: 'Logs and saved HTML/JSON',
33
				extensions: ['log', 'html', 'json']
34
			}, {
35
				name: 'All files',
36
				extensions: ['*']
37
			}]
38
		}, {
39
			properties: ['openFile']
40
		});
41
		if (files) {
42
			process.loadfile = files;
43
			resolve(files)
44
		} else {
45
			reject();
46
		}
47
	})
48
}
49
50
/**
51
 * Reads log files line by line, then compiles with Pug and loads it.
52
 */
53
export function readLog(log, filter) {
54
	let toPug = [];
55
	let tablified = [];
56
	let checkEvents = [];
57
	if (!filter) {
58
		filter = 'All Events';
59
	}
60
	currentData.currentPath = log;
61
	const lr = new LineByLineReader(log);
62
	lr.on('error', err => {
63
		Raven.captureException(err);
64
	});
65
	lr.on('line', line => {
66
		Raven.context(function() {
67
			Raven.captureBreadcrumb({
68
				data: {
69
					line: line,
70
					filename: log
71
				}
72
			});
73
			let parsed;
74
			if (!parsed) {
0 ignored issues
show
Bug introduced by
The variable parsed seems to be never initialized.
Loading history...
75
				try {
76
					parsed = JSON.parse(line);
77
				} catch (e) {
78
					Raven.captureException(e);
79
					line = line
80
						.replace(/\u000e/igm, '')
81
						.replace(/\u000f/igm, '');
82
					parsed = JSON.parse(line);
83
				}
84
			}
85
			if (parsed) {
86
				checkEvents.push(parsed.event);
87
				if (filter && parsed.event !== filter && filter !== 'All Events') {
88
					parsed = null;
89
				}
90
				parsed.timestamp = moment(parsed.timestamp).format('h:mm a - D/M ');
91
				toPug.push(parsed);
92
				_.each(Object.keys(parsed), elem => {
93
					if (!(elem.endsWith('_Localised') || !parsed[elem].toString().startsWith('$'))) {
94
						delete parsed[elem];
95
					}
96
				});
97
				if (currentData.events.indexOf(parsed.event)) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
98
99
				}
100
				currentData.events.push(parsed.event);
101
				tablified.push(tableify(parsed, undefined, undefined, true));
102
			}
103
		})
104
	});
105
	lr.on('end', err => {
106
		if (err) {
107
			Raven.captureException(err);
108
		} else {
109
			if (currentData.events !== checkEvents) {
110
				currentData.events = checkEvents;
111
			}
112
			currentData.events = _.uniq(currentData.events);
113
			const filterLog = pug.renderFile(path.join(__dirname, '..', 'filter.pug'), {
114
				basedir: path.join(__dirname, '..'),
115
				events: currentData.events,
116
				currentEvent: filter || 'All Events'
117
			});
118
			const compiledLog = pug.renderFile(path.join(__dirname, '..', 'logload.pug'), {
119
				basedir: path.join(__dirname, '..'),
120
				data: toPug,
121
				tabled: tablified,
122
				filename: log,
123
				events: currentData.events,
124
				filterLog
125
			});
126
			currentData.log = compiledLog;
127
			win.loadURL('data:text/html,' + compiledLog, {baseURLForDataURL: `file://${path.join(__dirname, '..')}`});
128
		}
129
	})
130
}
131