Skip to content

[fix] Don't evaluate threshold for historical data #666 #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions openwisp_monitoring/monitoring/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ def check_threshold(self, value, time=None, retention_policy=None, send_alert=Tr
alert_settings = self.alertsettings
except ObjectDoesNotExist:
return
if time and alert_settings._is_historical_data(time):
# Device is uploading historical data (could be due to a network outage).
# We don't want to send alerts in this scenario.
return
is_healthy_changed = self._set_is_healthy(alert_settings, value)
tolerance_healthy_changed_first_time = self._set_is_healthy_tolerant(
alert_settings, value, time, retention_policy, send_alert
Expand Down Expand Up @@ -942,6 +946,13 @@ def operator(self):
return self.config_dict["operator"]
return self.custom_operator

def _is_historical_data(self, time):
"""
Data older than 5 minutes is considered historical data.
"""
recent_time = timezone.now() - timedelta(minutes=5)
return time < recent_time

def _value_crossed(self, current_value):
threshold_value = self.threshold
method = "__gt__" if self.operator == ">" else "__lt__"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,40 @@ def test_object_check_threshold_crossed_for_long_time(self):
self.assertEqual(om.is_healthy_tolerant, True)
self.assertEqual(Notification.objects.count(), 2)

def test_object_check_threshold_crossed_historical_data(self):
"""
Do not evaluate threshold crossed for historical data
"""
self._create_admin()
om = self._create_object_metric(name="load")
self._create_alert_settings(
metric=om, custom_operator=">", custom_threshold=90, custom_tolerance=10
)

self._write_metric(om, 99, time=start_time - timedelta(minutes=120))
om.refresh_from_db()
self.assertEqual(om.is_healthy, True)
self.assertEqual(om.is_healthy_tolerant, True)
self.assertEqual(Notification.objects.count(), 0)

self._write_metric(om, 99, time=start_time - timedelta(minutes=61))
om.refresh_from_db()
self.assertEqual(om.is_healthy, True)
self.assertEqual(om.is_healthy_tolerant, True)
self.assertEqual(Notification.objects.count(), 0)

self._write_metric(om, 99, time=start_time - timedelta(minutes=60))
om.refresh_from_db()
self.assertEqual(om.is_healthy, True)
self.assertEqual(om.is_healthy_tolerant, True)
self.assertEqual(Notification.objects.count(), 0)

self._write_metric(om, 99, time=start_time - timedelta(minutes=10))
om.refresh_from_db()
self.assertEqual(om.is_healthy, False)
self.assertEqual(om.is_healthy_tolerant, False)
self.assertEqual(Notification.objects.count(), 1)

def test_flapping_metric_with_tolerance(self):
self._create_admin()
om = self._create_object_metric(name="ping")
Expand Down
Loading