2
2
from functools import cached_property
3
3
from unittest .mock import call , patch
4
4
5
+ from django .utils import timezone
6
+
5
7
from sentry .workflow_engine .models .data_condition import Condition , DataCondition
6
8
from sentry .workflow_engine .types import DetectorPriorityLevel
7
9
from tests .sentry .incidents .subscription_processor .test_subscription_processor_base import (
@@ -98,7 +100,9 @@ class ProcessUpdateComparisonAlertTest(ProcessUpdateBaseClass):
98
100
def comparison_detector_above (self ):
99
101
detector = self .metric_detector
100
102
detector .config .update ({"comparison_delta" : 60 * 60 })
103
+ detector .save ()
101
104
self .update_threshold (detector , DetectorPriorityLevel .HIGH , 150 )
105
+ self .update_threshold (detector , DetectorPriorityLevel .OK , 150 )
102
106
snuba_query = self .get_snuba_query (detector )
103
107
snuba_query .update (time_window = 60 * 60 )
104
108
return detector
@@ -107,6 +111,7 @@ def comparison_detector_above(self):
107
111
def comparison_detector_below (self ):
108
112
detector = self .metric_detector
109
113
detector .config .update ({"comparison_delta" : 60 * 60 })
114
+ detector .save ()
110
115
DataCondition .objects .filter (condition_group = detector .workflow_condition_group ).delete ()
111
116
self .set_up_data_conditions (detector , Condition .LESS , 50 , None , 50 )
112
117
snuba_query = self .get_snuba_query (detector )
@@ -116,8 +121,8 @@ def comparison_detector_below(self):
116
121
@patch ("sentry.incidents.utils.process_update_helpers.metrics" )
117
122
def test_comparison_alert_above (self , helper_metrics ):
118
123
detector = self .comparison_detector_above
119
- # comparison_delta = timedelta(seconds=detector.config["comparison_delta"])
120
- self .send_update (self .critical_threshold + 1 , timedelta (minutes = - 10 ), subscription = self . sub )
124
+ comparison_delta = timedelta (seconds = detector .config ["comparison_delta" ])
125
+ self .send_update (self .critical_threshold + 1 , timedelta (minutes = - 10 ))
121
126
122
127
# Shouldn't trigger, since there should be no data in the comparison period
123
128
assert self .get_detector_state (detector ) == DetectorPriorityLevel .OK
@@ -131,3 +136,31 @@ def test_comparison_alert_above(self, helper_metrics):
131
136
call ("incidents.alert_rules.skipping_update_invalid_aggregation_value" ),
132
137
]
133
138
)
139
+ comparison_date = timezone .now () - comparison_delta
140
+
141
+ for i in range (4 ):
142
+ self .store_event (
143
+ data = {"timestamp" : (comparison_date - timedelta (minutes = 30 + i )).isoformat ()},
144
+ project_id = self .project .id ,
145
+ )
146
+
147
+ self .metrics .incr .reset_mock ()
148
+ self .send_update (2 , timedelta (minutes = - 9 ))
149
+ # Shouldn't trigger, since there are 4 events in the comparison period, and 2/4 == 50%
150
+ assert self .get_detector_state (detector ) == DetectorPriorityLevel .OK
151
+
152
+ self .send_update (4 , timedelta (minutes = - 8 ))
153
+ # Shouldn't trigger, since there are 4 events in the comparison period, and 4/4 == 100%
154
+ assert self .get_detector_state (detector ) == DetectorPriorityLevel .OK
155
+
156
+ self .send_update (6 , timedelta (minutes = - 7 ))
157
+ # Shouldn't trigger: 6/4 == 150%, but we want > 150%
158
+ assert self .get_detector_state (detector ) == DetectorPriorityLevel .OK
159
+
160
+ self .send_update (7 , timedelta (minutes = - 6 ))
161
+ # Should trigger: 7/4 == 175% > 150%
162
+ assert self .get_detector_state (detector ) == DetectorPriorityLevel .HIGH
163
+
164
+ # Check that we successfully resolve
165
+ self .send_update (6 , timedelta (minutes = - 5 ))
166
+ assert self .get_detector_state (detector ) == DetectorPriorityLevel .OK
0 commit comments