-
Notifications
You must be signed in to change notification settings - Fork 0
/
alertMiddleware.js
67 lines (63 loc) · 2.48 KB
/
alertMiddleware.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
import { engine, thresholdRule } from './ruleEngine.js';
import 'dotenv/config'
import { MongoClient } from 'mongodb'
import { sendEmailAlert } from './sendEmail.js'
const mongodbDatabase = process.env.MONGO_DB_DATABASE
const mongodbCollection = process.env.MONGO_DB_COLLECTION
const uri = process.env.MONGO_DB_URI
const mongoClient = new MongoClient(uri)
await mongoClient.connect();
const db = mongoClient.db(mongodbDatabase);
const collection = db.collection(mongodbCollection);
const alertMiddleware = async (req, res, next) => {
const mainJSON = req.body;
mainJSON.Payload = JSON.parse(req.body.Payload);
await validateRules(mainJSON);
next();
};
async function validateRules(data) {
const docArray = await collection.find({}).toArray();
engine.addFact('webhookData', data);
const { events } = await engine.run();
for (const event of events) {
const message = event.params.data;
const eventType = event.type;
var item = docArray.find(item => item[eventType]);
var diff = item ? new Date(message.Timestamp).getTime() - item[eventType].lastUpdatedTime : 0;
if (!item) {
item = {
[eventType]: {
count: 1,
lastUpdatedTime: new Date(message.Timestamp).getTime()
}
};
docArray.push(item);
await collection.insertOne(item);
} else if (diff > thresholdRule[eventType].threshold) {
item[eventType].count = 1;
diff = thresholdRule[eventType].threshold;
item[eventType].lastUpdatedTime = new Date(message.Timestamp).getTime();
await updateItem(item, eventType);
} else {
item[eventType].count++;
item[eventType].lastUpdatedTime = new Date(message.Timestamp).getTime();
await updateItem(item, eventType);
}
console.log(item[eventType])
if (item[eventType].count === thresholdRule[eventType].count && diff <= thresholdRule[eventType].threshold) {
console.log("Threshold Reached!");
sendEmailAlert(event.params.message, message);
}
}
}
async function updateItem(item, eventType) {
const filter = { _id: item._id };
const updateDoc = {
$set: {
[`${eventType}.count`]: item[eventType].count,
[`${eventType}.lastUpdatedTime`]: item[eventType].lastUpdatedTime
}
};
await collection.updateOne(filter, updateDoc);
}
export { alertMiddleware };