mirror of
https://github.com/eduardogsilva/routerfleet.git
synced 2025-08-30 14:50:16 +02:00
Improved status change notifications to prevent messages for hosts that go offline and then come back online a few seconds later.
This commit is contained in:
parent
36ee868b30
commit
ca5d2d7c9a
1 changed files with 21 additions and 2 deletions
|
@ -14,6 +14,7 @@ from .functions import notify_router_status_update, notify_backup_fail, send_not
|
||||||
from backup_data.models import RouterBackup
|
from backup_data.models import RouterBackup
|
||||||
import datetime
|
import datetime
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.db.models import Q
|
||||||
|
|
||||||
|
|
||||||
@login_required()
|
@login_required()
|
||||||
|
@ -132,6 +133,21 @@ def view_cron_send_messages(request):
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
|
||||||
|
def remove_offline_notifications_for_online_routers():
|
||||||
|
# This function is used to remove change status notifications for routers that are currently online but have
|
||||||
|
# pending offline notifications.
|
||||||
|
# This prevents the system from sending a double status change notification when the router goes offline and then
|
||||||
|
# online again.
|
||||||
|
offline_list = Notification.objects.filter(
|
||||||
|
notification_type='status_offline', router__routerstatus__status_online=True
|
||||||
|
)
|
||||||
|
for notification in offline_list:
|
||||||
|
Notification.objects.filter(router=notification.router).filter(
|
||||||
|
Q(notification_type='status_offline') | Q(notification_type='status_online')
|
||||||
|
).delete()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def view_cron_concatenate_notifications(request):
|
def view_cron_concatenate_notifications(request):
|
||||||
data = {
|
data = {
|
||||||
'status': 'success',
|
'status': 'success',
|
||||||
|
@ -145,10 +161,13 @@ def view_cron_concatenate_notifications(request):
|
||||||
status_change_limit = timezone.now() - datetime.timedelta(seconds=message_settings.status_change_delay)
|
status_change_limit = timezone.now() - datetime.timedelta(seconds=message_settings.status_change_delay)
|
||||||
backup_fail_limit = timezone.now() - datetime.timedelta(seconds=message_settings.backup_fails_delay)
|
backup_fail_limit = timezone.now() - datetime.timedelta(seconds=message_settings.backup_fails_delay)
|
||||||
|
|
||||||
if Notification.objects.filter(created__lte=status_change_limit, notification_type='status_online').exists():
|
if message_settings.concatenate_status_change:
|
||||||
data['notification_type']['status_online'] = concatenate_notifications('status_online')
|
remove_offline_notifications_for_online_routers()
|
||||||
|
|
||||||
if Notification.objects.filter(created__lte=status_change_limit, notification_type='status_offline').exists():
|
if Notification.objects.filter(created__lte=status_change_limit, notification_type='status_offline').exists():
|
||||||
data['notification_type']['status_offline'] = concatenate_notifications('status_offline')
|
data['notification_type']['status_offline'] = concatenate_notifications('status_offline')
|
||||||
|
if Notification.objects.filter(created__lte=status_change_limit, notification_type='status_online').exists():
|
||||||
|
data['notification_type']['status_online'] = concatenate_notifications('status_online')
|
||||||
if Notification.objects.filter(created__lte=backup_fail_limit, notification_type='backup_fail').exists():
|
if Notification.objects.filter(created__lte=backup_fail_limit, notification_type='backup_fail').exists():
|
||||||
data['notification_type']['backup_fail'] = concatenate_notifications('backup_fail')
|
data['notification_type']['backup_fail'] = concatenate_notifications('backup_fail')
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue