mirror of
https://github.com/KevinMidboe/ISPDowntimeMonitor.git
synced 2025-10-29 17:50:12 +00:00
Any send email if first incident of month
New db function for getting all events for this month that also have isOk as false. We use this to resolve into notifyIfDown function if this is first incident this period.
This commit is contained in:
17
src/db.js
17
src/db.js
@@ -42,10 +42,25 @@ const getAlternatingEventStatuses = () => Event.find().exec()
|
||||
const getEventStatus = () => Event.find().select('date isOk').exec()
|
||||
.then(events => events.reverse())
|
||||
|
||||
const anyDowntimeThisCalendarMonth = async () => {
|
||||
const today = new Date();
|
||||
const year = today.getFullYear()
|
||||
const month = today.getMonth();
|
||||
|
||||
const query = {
|
||||
"date": { "$gte": new Date(year, month, 1) },
|
||||
"isOk": false
|
||||
}
|
||||
|
||||
return Event.find(query)
|
||||
.then(errorsThisMonth => errorsThisMonth.length > 0)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
commitServiceEventToDatabase,
|
||||
getAllEvents,
|
||||
getEventById,
|
||||
getAlternatingEventStatuses,
|
||||
getEventStatus
|
||||
getEventStatus,
|
||||
anyDowntimeThisCalendarMonth
|
||||
}
|
||||
11
src/index.js
11
src/index.js
@@ -6,7 +6,11 @@ const config = require('../config')
|
||||
const Mail = require( './mail.js');
|
||||
const mail = new Mail();
|
||||
|
||||
const { commitServiceEventToDatabase, getAllEvents } = require('./db.js');
|
||||
const {
|
||||
commitServiceEventToDatabase,
|
||||
getAllEvents,
|
||||
anyDowntimeThisCalendarMonth
|
||||
} = require('./db.js');
|
||||
|
||||
let browser = undefined;
|
||||
if (config.debug == false)
|
||||
@@ -118,6 +122,8 @@ function closeBrowserAndExit(status=0) {
|
||||
process.exit(status);
|
||||
}
|
||||
|
||||
const resolveIfNoDowntimeYetThisMonth = msg => anyDowntimeThisCalendarMonth()
|
||||
.then(anyDowntime => anyDowntime == false ? Promise.resolve(msg) : Promise.reject())
|
||||
|
||||
function run() {
|
||||
webscraper(config.url)
|
||||
@@ -125,8 +131,9 @@ function run() {
|
||||
.then(page => savePageToPDF(page))
|
||||
.then(page => getServiceMessages(page))
|
||||
.then(serviceMessages => commitServiceEventToDatabase(serviceMessages, pdfFilename))
|
||||
.then(serviceMessages => resolveIfNoDowntimeYetThisMonth(serviceMessages))
|
||||
.then(serviceMessages => notifyIfDown(serviceMessages))
|
||||
.then(closeBrowserAndExit)
|
||||
.finally(closeBrowserAndExit)
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
Reference in New Issue
Block a user