-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
log-timings.js
66 lines (56 loc) · 2.37 KB
/
log-timings.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
const Rx = require('rxjs');
const _ = require('lodash');
const formatDate = require('date-fns/format');
const { bufferCount, take } = require('rxjs/operators');
const BaseHandler = require('./base-handler');
module.exports = class LogTimings extends BaseHandler {
constructor({ logger, timestampFormat }) {
super({ logger });
this.timestampFormat = timestampFormat;
}
printExitInfoTimingTable(exitInfos) {
const exitInfoTable = _(exitInfos)
.sortBy(({timings}) => timings.durationSeconds)
.reverse()
.map(({ command, timings, killed, exitCode }) => {
const readableDurationMs = (timings.endDate - timings.startDate).toLocaleString();
return {
name: command.name,
duration: `${readableDurationMs}ms`,
'exit code': exitCode,
killed,
command: command.command,
};
})
.value();
console.log('\nTimings:');
console.table(exitInfoTable);
return exitInfos;
};
handle(commands) {
if (!this.logger) { return {commands}; }
const controllerInstance = this;
// individual process timings
commands.forEach(command => {
command.timer.subscribe( {
next: ({startDate, endDate}) => {
if (!endDate) {
controllerInstance.logger.logCommandEvent( `${ command.command } started at ${ formatDate(startDate, controllerInstance.timestampFormat) }`, command );
} else {
const durationMs = (endDate.getTime() - startDate.getTime());
controllerInstance.logger.logCommandEvent( `${ command.command } stopped at ${ formatDate(endDate, controllerInstance.timestampFormat) } after ${durationMs.toLocaleString()}ms`, command );
}
},
} );
});
// overall summary timings
const closeStreams = commands.map(command => command.close);
this.allProcessesClosed = Rx.merge(...closeStreams)
.pipe(
bufferCount(closeStreams.length),
take(1),
);
this.allProcessesClosed.subscribe((exitInfos) => this.printExitInfoTimingTable(exitInfos));
return {commands};
}
};