@ -292,54 +292,13 @@ class Monitor extends BeanModel {
let beatInterval = this . interval ;
let beatInterval = this . interval ;
// * ? -> ANY STATUS = important [isFirstBeat]
let isImportant = Monitor . isImportantBeat ( isFirstBeat , previousBeat . status , bean . status ) ;
// UP -> PENDING = not important
// * UP -> DOWN = important
// UP -> UP = not important
// PENDING -> PENDING = not important
// * PENDING -> DOWN = important
// PENDING -> UP = not important
// DOWN -> PENDING = this case not exists
// DOWN -> DOWN = not important
// * DOWN -> UP = important
let isImportant = isFirstBeat ||
( previousBeat . status === UP && bean . status === DOWN ) ||
( previousBeat . status === DOWN && bean . status === UP ) ||
( previousBeat . status === PENDING && bean . status === DOWN ) ;
// Mark as important if status changed, ignore pending pings,
// Mark as important if status changed, ignore pending pings,
// Don't notify if disrupted changes to up
// Don't notify if disrupted changes to up
if ( isImportant ) {
if ( isImportant ) {
bean . important = true ;
bean . important = true ;
await Monitor . sendNotification ( isFirstBeat , this , bean ) ;
// Send only if the first beat is DOWN
if ( ! isFirstBeat || bean . status === DOWN ) {
let notificationList = await R . getAll ( "SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id " , [
this . id ,
] ) ;
let text ;
if ( bean . status === UP ) {
text = "✅ Up" ;
} else {
text = "🔴 Down" ;
}
let msg = ` [ ${ this . name } ] [ ${ text } ] ${ bean . msg } ` ;
for ( let notification of notificationList ) {
try {
await Notification . send ( JSON . parse ( notification . config ) , msg , await this . toJSON ( ) , bean . toJSON ( ) ) ;
} catch ( e ) {
console . error ( "Cannot send notification to " + notification . name ) ;
console . log ( e ) ;
}
}
// Clear Status Page Cache
apicache . clear ( ) ;
}
} else {
} else {
bean . important = false ;
bean . important = false ;
}
}
@ -546,6 +505,53 @@ class Monitor extends BeanModel {
io . to ( userID ) . emit ( "uptime" , monitorID , duration , uptime ) ;
io . to ( userID ) . emit ( "uptime" , monitorID , duration , uptime ) ;
}
}
static isImportantBeat ( isFirstBeat , previousBeatStatus , currentBeatStatus ) {
// * ? -> ANY STATUS = important [isFirstBeat]
// UP -> PENDING = not important
// * UP -> DOWN = important
// UP -> UP = not important
// PENDING -> PENDING = not important
// * PENDING -> DOWN = important
// PENDING -> UP = not important
// DOWN -> PENDING = this case not exists
// DOWN -> DOWN = not important
// * DOWN -> UP = important
let isImportant = isFirstBeat ||
( previousBeatStatus === UP && currentBeatStatus === DOWN ) ||
( previousBeatStatus === DOWN && currentBeatStatus === UP ) ||
( previousBeatStatus === PENDING && currentBeatStatus === DOWN ) ;
return isImportant ;
}
static async sendNotification ( isFirstBeat , monitor , bean ) {
if ( ! isFirstBeat || bean . status === DOWN ) {
let notificationList = await R . getAll ( "SELECT notification.* FROM notification, monitor_notification WHERE monitor_id = ? AND monitor_notification.notification_id = notification.id " , [
monitor . id ,
] ) ;
let text ;
if ( bean . status === UP ) {
text = "✅ Up" ;
} else {
text = "🔴 Down" ;
}
let msg = ` [ ${ monitor . name } ] [ ${ text } ] ${ bean . msg } ` ;
for ( let notification of notificationList ) {
try {
await Notification . send ( JSON . parse ( notification . config ) , msg , await monitor . toJSON ( ) , bean . toJSON ( ) ) ;
} catch ( e ) {
console . error ( "Cannot send notification to " + notification . name ) ;
console . log ( e ) ;
}
}
// Clear Status Page Cache
apicache . clear ( ) ;
}
}
}
}
module . exports = Monitor ;
module . exports = Monitor ;