-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogSummarizer.js
38 lines (33 loc) · 1.11 KB
/
logSummarizer.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
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
const MailService = require("./mailService");
const bodyParser = require("body-parser");
const mailService = new MailService();
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post("/logs", (req, res) => {
if (req && req.body && req.body.payload && req.body.payload.events) {
mailService.sendEmail(
jsonToEmailFormat(groupAndCount(req.body.payload.events))
);
res.status(200).send();
} else {
res.status(400).send();
}
});
const groupAndCount = events => {
return events.reduce((total, event) => {
total[event.message] = (total[event.message] || 0) + 1;
return total;
}, {});
};
const jsonToEmailFormat = summarizedEvents => {
let mailContent =
"Following is the count of each message from the registred logs in Papertrail.\n\n";
for (var key of Object.keys(summarizedEvents)) {
mailContent += '"' + key + '" Rows: ' + summarizedEvents[key] + "\n";
}
return mailContent;
};
app.listen(port, () => console.log(`Listening on port ${port}`));