@@ -992,39 +992,38 @@ def _is_crossed_by(self, current_value, time=None, retention_policy=None):
992992 extra_fields = []
993993 if self .metric .alert_on_related_field :
994994 extra_fields = [self .metric .alert_field ]
995- if time is None :
996- # retrieves latest measurements, ordered by most recent first
997- points = self .metric .read (
998- since = timezone .now () - timedelta (minutes = self ._tolerance_search_range ),
999- limit = None ,
1000- order = "-time" ,
1001- retention_policy = retention_policy ,
1002- extra_fields = extra_fields ,
1003- )
1004- # store a list with the results
1005- results = [value_crossed ]
1006- # loop on each measurement starting from the most recent
1007- for i , point in enumerate (points , 1 ):
1008- # skip the first point because it was just added before this
1009- # check started and its value coincides with ``current_value``
1010- if i <= 1 :
1011- continue
1012- utc_time = utc .localize (datetime .utcfromtimestamp (point ["time" ]))
1013- # did this point cross the threshold? Append to result list
1014- results .append (self ._value_crossed (point [self .metric .alert_field ]))
1015- # tolerance is trepassed
1016- if self ._time_crossed (utc_time ):
1017- # if the latest results are consistent, the metric being
1018- # monitored is not flapping and we can confidently return
1019- # wheter the value crosses the threshold or not
1020- if len (set (results )) == 1 :
1021- return value_crossed
1022- # otherwise, the results are flapping, the situation has not changed
1023- # we will return a value that will not trigger changes
1024- return not self .metric .is_healthy_tolerant
1025- # otherwise keep looking back
995+ time = time or timezone .now ()
996+ # retrieves latest measurements, ordered by most recent first
997+ points = self .metric .read (
998+ since = timezone .now () - timedelta (minutes = self ._tolerance_search_range ),
999+ limit = None ,
1000+ order = "-time" ,
1001+ retention_policy = retention_policy ,
1002+ extra_fields = extra_fields ,
1003+ )
1004+ # store a list with the results
1005+ results = [value_crossed ]
1006+ # loop on each measurement starting from the most recent
1007+ for i , point in enumerate (points , 1 ):
1008+ # skip the first point because it was just added before this
1009+ # check started and its value coincides with ``current_value``
1010+ if i <= 1 :
10261011 continue
1027- # the search has not yielded any conclusion
1028- # return result based on the current value and time
1029- time = timezone .now ()
1012+ utc_time = utc .localize (datetime .utcfromtimestamp (point ["time" ]))
1013+ # did this point cross the threshold? Append to result list
1014+ results .append (self ._value_crossed (point [self .metric .alert_field ]))
1015+ # tolerance is trepassed
1016+ if self ._time_crossed (utc_time ):
1017+ # if the latest results are consistent, the metric being
1018+ # monitored is not flapping and we can confidently return
1019+ # wheter the value crosses the threshold or not
1020+ if len (set (results )) == 1 :
1021+ return value_crossed
1022+ # otherwise, the results are flapping, the situation has not changed
1023+ # we will return a value that will not trigger changes
1024+ return not self .metric .is_healthy_tolerant
1025+ # otherwise keep looking back
1026+ continue
1027+ # the search has not yielded any conclusion
1028+ # return result based on the current value and time
10301029 return self ._time_crossed (time ) and value_crossed
0 commit comments