@@ -78,7 +78,10 @@ def test_general_check_threshold_crossed_deferred(self):
7878 self ._create_alert_settings (
7979 metric = m , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
8080 )
81- m .write (99 , time = ten_minutes_ago )
81+ with freeze_time (start_time ):
82+ m .write (99 )
83+ with freeze_time (ten_minutes_after ):
84+ m .write (99 )
8285 m .refresh_from_db ()
8386 self .assertEqual (m .is_healthy , False )
8487 self .assertEqual (m .is_healthy_tolerant , False )
@@ -122,7 +125,8 @@ def test_general_check_threshold_crossed_for_long_time(self):
122125 )
123126
124127 with self .subTest ("Test no notification is generated for healthy status" ):
125- m .write (89 , time = ten_minutes_ago )
128+ with freeze_time (ten_minutes_ago ):
129+ m .write (89 )
126130 m .refresh_from_db ()
127131 self .assertEqual (m .is_healthy , True )
128132 self .assertEqual (m .is_healthy_tolerant , True )
@@ -133,7 +137,8 @@ def test_general_check_threshold_crossed_for_long_time(self):
133137 # timeseries database to process the transaction.
134138 self ._read_metric (m )
135139 with self .subTest ("Test no notification is generated when check=False" ):
136- m .write (91 , time = ten_minutes_ago , check = False )
140+ with freeze_time (ten_minutes_ago ):
141+ m .write (91 , check = False )
137142 self .assertEqual (Notification .objects .count (), 0 )
138143
139144 self ._read_metric (m )
@@ -241,7 +246,10 @@ def test_object_check_threshold_crossed_deferred(self):
241246 alert_s = self ._create_alert_settings (
242247 metric = om , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
243248 )
244- om .write (99 , time = ten_minutes_ago )
249+ with freeze_time (start_time ):
250+ om .write (99 )
251+ with freeze_time (ten_minutes_after ):
252+ om .write (99 )
245253 om .refresh_from_db ()
246254 self .assertEqual (om .is_healthy , False )
247255 self .assertEqual (om .is_healthy_tolerant , False )
@@ -271,9 +279,11 @@ def test_object_check_threshold_crossed_for_long_time(self):
271279 alert_s = self ._create_alert_settings (
272280 metric = om , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
273281 )
274- self ._write_metric (om , 89 , time = ten_minutes_ago )
282+ with freeze_time (ten_minutes_ago ):
283+ self ._write_metric (om , 89 )
275284 self .assertEqual (Notification .objects .count (), 0 )
276- self ._write_metric (om , 91 , time = ten_minutes_ago , check = False )
285+ with freeze_time (ten_minutes_ago ):
286+ self ._write_metric (om , 91 , check = False )
277287 self .assertEqual (Notification .objects .count (), 0 )
278288 self ._write_metric (om , 92 )
279289 om .refresh_from_db ()
@@ -319,6 +329,38 @@ def test_object_check_threshold_crossed_for_long_time(self):
319329 self .assertEqual (om .is_healthy_tolerant , True )
320330 self .assertEqual (Notification .objects .count (), 2 )
321331
332+ @freeze_time (start_time )
333+ def test_object_check_threshold_crossed_historical_data (self ):
334+ """
335+ Do not evaluate threshold crossed for historical data
336+ """
337+ self ._create_admin ()
338+ om = self ._create_object_metric (name = "load" )
339+ self ._create_alert_settings (
340+ metric = om , custom_operator = ">" , custom_threshold = 90 , custom_tolerance = 1
341+ )
342+
343+ # We need to write with "time" argument instead of freeze_time to
344+ # simulate historical data
345+ self ._write_metric (om , 99 , time = start_time - timedelta (minutes = 60 ))
346+ om .refresh_from_db ()
347+ self .assertEqual (om .is_healthy , True )
348+ self .assertEqual (om .is_healthy_tolerant , True )
349+ self .assertEqual (Notification .objects .count (), 0 )
350+
351+ self ._write_metric (om , 99 , time = start_time - timedelta (minutes = 10 ))
352+ om .refresh_from_db ()
353+ self .assertEqual (om .is_healthy , True )
354+ self .assertEqual (om .is_healthy_tolerant , True )
355+ self .assertEqual (Notification .objects .count (), 0 )
356+
357+ # Writing real-time data should enforce the threshold check
358+ self ._write_metric (om , 99 , time = start_time )
359+ om .refresh_from_db ()
360+ self .assertEqual (om .is_healthy , False )
361+ self .assertEqual (om .is_healthy_tolerant , True )
362+ self .assertEqual (Notification .objects .count (), 0 )
363+
322364 def test_flapping_metric_with_tolerance (self ):
323365 self ._create_admin ()
324366 om = self ._create_object_metric (name = "ping" )
@@ -403,7 +445,10 @@ def test_alerts_disabled(self):
403445 custom_tolerance = 1 ,
404446 is_active = False ,
405447 )
406- m .write (99 , time = ten_minutes_ago )
448+ with freeze_time (start_time ):
449+ m .write (99 )
450+ with freeze_time (ten_minutes_after ):
451+ m .write (99 )
407452 m .refresh_from_db ()
408453 self .assertEqual (m .is_healthy , False )
409454 self .assertEqual (m .is_healthy_tolerant , False )
@@ -511,7 +556,10 @@ def test_general_check_threshold_with_alert_field_crossed_deferred(self):
511556 self ._create_alert_settings (
512557 metric = m , custom_operator = ">" , custom_threshold = 30 , custom_tolerance = 1
513558 )
514- m .write (10 , time = ten_minutes_ago , extra_values = {"test_related_2" : 35 })
559+ with freeze_time (start_time ):
560+ m .write (10 , extra_values = {"test_related_2" : 35 })
561+ with freeze_time (ten_minutes_after ):
562+ m .write (10 , extra_values = {"test_related_2" : 35 })
515563 m .refresh_from_db ()
516564 self .assertEqual (m .is_healthy , False )
517565 self .assertEqual (m .is_healthy_tolerant , False )
0 commit comments