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",
"version": "1.0.5",
"version": "1.0.6",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -46,12 +46,11 @@ class Monitor extends BeanModel {
start(io) {
let previousBeat = null;
const beat = async () => {
if (! previousBeat) {
previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
this.id
])
]);
}
let bean = R.dispense("heartbeat")
@ -111,9 +110,12 @@ class Monitor extends BeanModel {
bean.msg = error.message;
}
// Mark as important if status changed
if (! previousBeat || previousBeat.status !== bean.status) {
bean.important = true;
// Do the nottifications if status changed or if two subsequent beats are faulty
if (! previousBeat || previousBeat.status !== bean.status || (previousBeat && previousBeat.status !== 1 && bean.status != 1)) {
// 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
if (previousBeat || bean.status !== 1) {
@ -133,7 +135,23 @@ class Monitor extends BeanModel {
let msg = `[${this.name}] [${text}] ${bean.msg}`;
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);

View File

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

View File

@ -30,6 +30,14 @@
<input type="text" class="form-control" id="name" required v-model="notification.name">
</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'">
<div class="mb-3">
<label for="telegram-bot-token" class="form-label">Bot Token</label>
@ -368,6 +376,7 @@ export default {
// Default set to Telegram
this.notification.type = "telegram"
this.notification.gotifyPriority = 8
this.notification.gotifyPriority = 1
}
this.modal.show()