Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
192 lines (167 loc) · 7.02 KB
/
Copy pathmain.js
File metadata and controls
192 lines (167 loc) · 7.02 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const debug = require('debug')('logSorter');
const LineByLineReader = require('line-by-line');
const fs = require('fs');
const moment = require('moment');
const EventEmitter = require('events');
const INPUT_FILES_COUNT = 10;
/**
* Class to create one output file (output/merged.log) containing all the entries of log files (logs/*.logs) sorted by date and time.
* @assumption Input log files are named as 1.log, 2.log...10.log
* @returns {Promise}
*/
const logSorter = function() {
let lineReaders = []; //cache of line readers for each file
let currentlyProcessedLogLines = []; //aux storage for in memory logs being processed
//custom events emitter
class LogSorterInternalEvents extends EventEmitter {}
const logSorterInternalEvents = new LogSorterInternalEvents();
//save reference to itself
const self = this;
//write stream for output log file
//and in addition with streams we ensure the order
const writeStream = fs.createWriteStream("./output/merged.log");
return new Promise( (resolve, reject) => {
//iterate on each log and setup line readers
for (let i = 0; i < INPUT_FILES_COUNT; i++) {
lineReaders.push({
lr: new LineByLineReader('./logs/' + (i + 1) + '.log'), //save line reader object
exhausted: false //save whether this file has been exhausted
});
//add handler for the `line read` event
lineReaders[i].lr.on('line', function (line) {
//push a line to currentlyProcessedLogLines cache
currentlyProcessedLogLines.push({
index: i, //this helps to keep track which file this line came from
line: line, //actual log line
datetime: moment(line.slice(0, 32)) //separate date - Assumption it's initial 32 chars
});
debug('Paused file ' + i);
//pause after pushing each line because we want to control the flow
lineReaders[i].lr.pause();
//emit line pushed event
logSorterInternalEvents.emit('linePushed');
});
//add handler for the `lines exhausted in file` event
lineReaders[i].lr.on('end', function () {
debug("Exhausted file " + i);
//set exhausted flag
lineReaders[i].exhausted = true;
//emit a file exhausted event
logSorterInternalEvents.emit('fileExhausted')
});
//add handler for the `error in line reader` event
lineReaders[i].lr.on('error', function (err) {
reject(err);
return;
});
}
/**
* Method to find out when one line from each file is pushed in to currentlyProcessedLogLines cache
* @returns {Promise}
*/
const initialLogLinesReady = function () {
return new Promise((resolve, reject) => {
logSorterInternalEvents.on('linePushed', () => {
if (currentlyProcessedLogLines.length === INPUT_FILES_COUNT) {
resolve();
}
});
})
};
/**
* Method to find out when all lines from all the files have been read
* @returns {Promise}
*/
const allFilesExhausted = function () {
return new Promise((resolve, reject) => {
//whenever a file is exhausted we check if all files are exhausted
logSorterInternalEvents.on('fileExhausted', () => {
const allExhausted = lineReaders.reduce(function (accumulator, currentValue) {
return accumulator && currentValue.exhausted;
}, true);
if (allExhausted) {
resolve();
}
})
})
};
//Handle error in output write stream
writeStream.on('error', (err) => {
reject(err);
});
/**
* Helper method to process currentlyProcessedLogLines cache once
*/
const triggerNextProcessingCycle = function () {
//find the earliest date in currentlyProcessedLogLines cache
let earliest = self.smallestDate(currentlyProcessedLogLines);
//write earliest dated log line to output stream
//delete this item from currentlyProcessedLogLines cache
//pull a new element for currentlyProcessedLogLines cache from the file whose item we just deleted
if (earliest) {
writeStream.write(earliest.line + '\r\n');
currentlyProcessedLogLines = self.findAndDelete(earliest, currentlyProcessedLogLines);
debug('resuming ' + earliest.index);
lineReaders[earliest.index].lr.resume();
}
};
//as soon initial lines (first line from each file) are pushed in currentlyProcessedLogLines cache
//we wait for other events that can trigger a processing cycle
initialLogLinesReady().then(() => {
//new line pushed
logSorterInternalEvents.on('linePushed', () => {
triggerNextProcessingCycle();
});
//a file exhausted (this would indirectly suppress new line pushed event)
logSorterInternalEvents.on('fileExhausted', () => {
triggerNextProcessingCycle();
});
//manually trigger once to cause the chain of events
triggerNextProcessingCycle();
});
//when all files are written we formally exit from here
allFilesExhausted().then(() => {
debug("all files exhausted");
writeStream.end();
resolve();
})
})
};
logSorter.prototype = {
/**
* Helper to find the smallest date in Array of log lines
* @param logLines - Array of log lines
* @returns {*}
*/
smallestDate: function (logLines) {
if (logLines.length === 0) return null;
return logLines.reduce(function (a, b) {
//return smallest date
let smallest;
if(a.datetime.isAfter(b.datetime)){
smallest = b;
}else if(a.datetime.isBefore(b.datetime)){
smallest = a;
}else if(a.index < b.index){
//if two dates are same return the one with smallest index/filename to make logs predictable
smallest = a;
}else{
smallest = b;
}
return smallest;
});
},
/**
* Helper to remove an element from logLines array (shallow lookup)
* @param element - element to be removed
* @param logLines - Array of log lines
* @returns {Array.<T>|*}
*/
findAndDelete: function (element, logLines) {
let newLogLinesArray = logLines.filter(function (a) {
return a !== element
});
return newLogLinesArray;
}
};
module.exports = logSorter;