Threshold of the number of repeated failures to happen before sending nottification has been added to the individual Nottification settings

This commit is contained in:
Codeartist 2021-07-22 12:05:11 +12:00
parent d89e6f4649
commit 01f530b733
4 changed files with 37 additions and 7 deletions

2
package-lock.json generated
View file

@ -1,6 +1,6 @@
{ {
"name": "uptime-kuma", "name": "uptime-kuma",
"version": "1.0.5", "version": "1.0.6",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View file

@ -46,12 +46,11 @@ class Monitor extends BeanModel {
start(io) { start(io) {
let previousBeat = null; let previousBeat = null;
const beat = async () => { const beat = async () => {
if (! previousBeat) { if (! previousBeat) {
previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [ previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
this.id this.id
]) ]);
} }
let bean = R.dispense("heartbeat") let bean = R.dispense("heartbeat")
@ -111,9 +110,12 @@ class Monitor extends BeanModel {
bean.msg = error.message; bean.msg = error.message;
} }
// Mark as important if status changed // Do the nottifications if status changed or if two subsequent beats are faulty
if (! previousBeat || previousBeat.status !== bean.status) { if (! previousBeat || previousBeat.status !== bean.status || (previousBeat && previousBeat.status !== 1 && bean.status != 1)) {
bean.important = true; // Don't mark as important two subsequent faulty beats
let repeatedFailures = (previousBeat && previousBeat.status !== 1 && bean.status != 1);
if(repeatedFailures) bean.important = false;
else bean.important = true;
// Do not send if first beat is UP // Do not send if first beat is UP
if (previousBeat || bean.status !== 1) { if (previousBeat || bean.status !== 1) {
@ -133,7 +135,23 @@ class Monitor extends BeanModel {
let msg = `[${this.name}] [${text}] ${bean.msg}`; let msg = `[${this.name}] [${text}] ${bean.msg}`;
for(let notification of notificationList) { for(let notification of notificationList) {
promiseList.push(Notification.send(JSON.parse(notification.config), msg, await this.toJSON(), bean.toJSON())); let notificationConfig = JSON.parse(notification.config);
//Check for how many subsequent failures should happen before sending the notification
notificationConfig.failThreshold = notificationConfig.failThreshold || 1;
if(repeatedFailures && notificationConfig.failThreshold > 1) {
let limit = notificationConfig.failThreshold;
let query = await R.find("heartbeat", " monitor_id = ? ORDER BY time DESC LIMIT " + limit, [
this.id
]);
query = query.map(val => val.status);
repeatedFailures = bean.status !== 1 && bean.status === query[0] && query.slice(0,-1).every( val => val === query[0] ) && [...query].reverse()[0] !== [...query].reverse()[1];
console.log(query);
}
//0 t 2
console.log(bean.status, repeatedFailures, notificationConfig.failThreshold)
if((bean.status === 1 && !repeatedFailures) ||
(notificationConfig.failThreshold <= 1 && !repeatedFailures) ||
(notificationConfig.failThreshold > 1 && repeatedFailures)) promiseList.push(Notification.send(notificationConfig, msg, await this.toJSON(), bean.toJSON()));
} }
await Promise.all(promiseList); await Promise.all(promiseList);

View file

@ -398,6 +398,9 @@ let needSetup = false;
try { try {
checkLogin(socket) checkLogin(socket)
//Sanitize the threshold
notification.failThreshold = Math.abs(parseInt(notification.failThreshold) || 1);
await Notification.save(notification, notificationID, socket.userID) await Notification.save(notification, notificationID, socket.userID)
await sendNotificationList(socket) await sendNotificationList(socket)

View file

@ -30,6 +30,14 @@
<input type="text" class="form-control" id="name" required v-model="notification.name"> <input type="text" class="form-control" id="name" required v-model="notification.name">
</div> </div>
<div class="mb-3">
<label for="type" class="form-label">Number of failures in a row before sending the notification</label>
<input type="text" class="form-control" id="fail-threshold" required v-model="notification.failThreshold">
<div class="form-text">
<p>Only positive integer numbers allowed. Numbers higher than 1 may cause more heavy queries & more load for the server!</p>
</div>
</div>
<template v-if="notification.type === 'telegram'"> <template v-if="notification.type === 'telegram'">
<div class="mb-3"> <div class="mb-3">
<label for="telegram-bot-token" class="form-label">Bot Token</label> <label for="telegram-bot-token" class="form-label">Bot Token</label>
@ -368,6 +376,7 @@ export default {
// Default set to Telegram // Default set to Telegram
this.notification.type = "telegram" this.notification.type = "telegram"
this.notification.gotifyPriority = 8 this.notification.gotifyPriority = 8
this.notification.gotifyPriority = 1
} }
this.modal.show() this.modal.show()