-
Notifications
You must be signed in to change notification settings - Fork 2
/
daily-report.js
102 lines (90 loc) · 3.46 KB
/
daily-report.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
100
101
102
'use strict';
var E = require('linq');
var assert = require('chai').assert;
var moment = require('moment');
var mailer = require('./mailer');
var dataForge = require('data-forge');
var DailyReport = function (logStoragePlugin, config) {
assert.isObject(logStoragePlugin);
assert.isObject(config);
var self = this;
//
// Find all logs between the specified dates that contain
// the requested text.
//
self.findLogs = function (startDate, endDate, spec) {
assert.instanceOf(startDate, Date);
assert.instanceOf(endDate, Date);
assert.isArray(spec);
spec.forEach(s => assert.isString(s));
var self = this;
var lwrSpec = spec.map(s => s.toLowerCase());
return logStoragePlugin.retrieveLogs(startDate, endDate)
.then(logs => E.from(logs)
.where(log =>
E.from(lwrSpec)
.any(s =>
log.Level.toLowerCase().indexOf(s) !== -1 ||
log.RenderedMessage.toLowerCase().indexOf(s) !== -1
)
)
.toArray()
)
;
};
//
// Generate and email the daily report for the past 24 hours.
//
self.emailDailyReport = function (spec) {
assert.isArray(spec);
spec.forEach(s => assert.isString(s));
var self = this;
var startTime = moment().subtract(24, 'hours').toDate();
var endTime = moment().toDate();
return self.findLogs(startTime, endTime, spec)
.then(logs => {
if (logs.length > 0) {
return mailer.send({
text: "Logs ahoy!",
attachments: [
{
filename: 'Logs.csv',
content: new dataForge.DataFrame({ values: logs })
.generateSeries({
Date: row => moment(row.Timestamp).format('DD-MM-YYYY'),
Time: row => moment(row.Timestamp).format('HH:mm:ss'),
})
.dropSeries([
"Timestamp",
"_id",
"MessageTemplate",
"Properties",
])
.toCSV(),
},
],
});
}
else {
return mailer.send({
text: "No errors or warnings worth mentioning.",
});
}
})
;
};
};
if (require.main === module) {
console.log('Starting from command line.');
var config = require('confucious');
config.pushJsonFile('config.json');
var logStoragePlugin = require('./mongodb-output')(config);
var dailyReport = new DailyReport(logStoragePlugin, config);
dailyReport.emailDailyReport(config.get('mail:dailyReportSpec'))
.then(() => console.log("Done"))
.catch(err => console.error(err.stack))
;
}
else {
module.exports = DailyReport;
}