test_event_frequency.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. import time
  2. from copy import deepcopy
  3. from datetime import timedelta
  4. from unittest.mock import patch
  5. from uuid import uuid4
  6. import pytest
  7. from django.utils import timezone
  8. from sentry.issues.grouptype import PerformanceNPlusOneGroupType
  9. from sentry.models.project import Project
  10. from sentry.models.rule import Rule
  11. from sentry.rules.conditions.event_frequency import (
  12. EventFrequencyCondition,
  13. EventFrequencyPercentCondition,
  14. EventUniqueUserFrequencyCondition,
  15. )
  16. from sentry.testutils.abstract import Abstract
  17. from sentry.testutils.cases import (
  18. BaseMetricsTestCase,
  19. PerformanceIssueTestCase,
  20. RuleTestCase,
  21. SnubaTestCase,
  22. )
  23. from sentry.testutils.helpers.datetime import before_now, freeze_time, iso_format
  24. from sentry.testutils.skips import requires_snuba
  25. from sentry.utils.samples import load_data
  26. pytestmark = [pytest.mark.sentry_metrics, requires_snuba]
  27. class BaseEventFrequencyPercentTest(BaseMetricsTestCase):
  28. def _make_sessions(
  29. self, num: int, environment_name: str | None = None, project: Project | None = None
  30. ):
  31. received = time.time()
  32. def make_session(i):
  33. return dict(
  34. distinct_id=uuid4().hex,
  35. session_id=uuid4().hex,
  36. org_id=project.organization_id if project else self.project.organization_id,
  37. project_id=project.id if project else self.project.id,
  38. status="ok",
  39. seq=0,
  40. release="foo@1.0.0",
  41. environment=environment_name if environment_name else "prod",
  42. retention_days=90,
  43. duration=None,
  44. errors=0,
  45. # The line below is crucial to spread sessions throughout the time period.
  46. started=received - i - 1,
  47. received=received,
  48. )
  49. self.bulk_store_sessions([make_session(i) for i in range(num)])
  50. class EventFrequencyQueryTestBase(SnubaTestCase, RuleTestCase, PerformanceIssueTestCase):
  51. def setUp(self):
  52. super().setUp()
  53. self.start = before_now(minutes=1)
  54. self.end = timezone.now()
  55. self.event = self.store_event(
  56. data={
  57. "event_id": "a" * 32,
  58. "environment": self.environment.name,
  59. "timestamp": iso_format(before_now(seconds=30)),
  60. "fingerprint": ["group-1"],
  61. "user": {"id": uuid4().hex},
  62. },
  63. project_id=self.project.id,
  64. )
  65. self.event2 = self.store_event(
  66. data={
  67. "event_id": "b" * 32,
  68. "environment": self.environment.name,
  69. "timestamp": iso_format(before_now(seconds=12)),
  70. "fingerprint": ["group-2"],
  71. "user": {"id": uuid4().hex},
  72. },
  73. project_id=self.project.id,
  74. )
  75. self.environment2 = self.create_environment(name="staging")
  76. self.event3 = self.store_event(
  77. data={
  78. "event_id": "c" * 32,
  79. "environment": self.environment2.name,
  80. "timestamp": iso_format(before_now(seconds=12)),
  81. "fingerprint": ["group-3"],
  82. "user": {"id": uuid4().hex},
  83. },
  84. project_id=self.project.id,
  85. )
  86. fingerprint = f"{PerformanceNPlusOneGroupType.type_id}-something_random"
  87. perf_event_data = load_data(
  88. "transaction-n-plus-one",
  89. timestamp=before_now(seconds=12),
  90. start_timestamp=before_now(seconds=13),
  91. fingerprint=[fingerprint],
  92. )
  93. perf_event_data["user"] = {"id": uuid4().hex}
  94. perf_event_data["environment"] = self.environment.name
  95. # Store a performance event
  96. self.perf_event = self.create_performance_issue(
  97. event_data=perf_event_data,
  98. project_id=self.project.id,
  99. fingerprint=fingerprint,
  100. )
  101. self.data = {"interval": "5m", "value": 30}
  102. self.condition_inst = self.get_rule(
  103. data=self.data,
  104. project=self.event.group.project,
  105. rule=Rule(environment_id=self.environment.id),
  106. )
  107. self.condition_inst2 = self.get_rule(
  108. data=self.data,
  109. project=self.event.group.project,
  110. rule=Rule(environment_id=self.environment2.id),
  111. )
  112. class EventFrequencyQueryTest(EventFrequencyQueryTestBase):
  113. rule_cls = EventFrequencyCondition
  114. def test_batch_query(self):
  115. batch_query = self.condition_inst.batch_query_hook(
  116. group_ids=[self.event.group_id, self.event2.group_id, self.perf_event.group_id],
  117. start=self.start,
  118. end=self.end,
  119. environment_id=self.environment.id,
  120. )
  121. assert batch_query == {
  122. self.event.group_id: 1,
  123. self.event2.group_id: 1,
  124. self.perf_event.group_id: 1,
  125. }
  126. batch_query = self.condition_inst2.batch_query_hook(
  127. group_ids=[self.event3.group_id],
  128. start=self.start,
  129. end=self.end,
  130. environment_id=self.environment2.id,
  131. )
  132. assert batch_query == {self.event3.group_id: 1}
  133. class EventUniqueUserFrequencyQueryTest(EventFrequencyQueryTestBase):
  134. rule_cls = EventUniqueUserFrequencyCondition
  135. def test_batch_query_user(self):
  136. batch_query = self.condition_inst.batch_query_hook(
  137. group_ids=[self.event.group_id, self.event2.group_id, self.perf_event.group_id],
  138. start=self.start,
  139. end=self.end,
  140. environment_id=self.environment.id,
  141. )
  142. assert batch_query == {
  143. self.event.group_id: 1,
  144. self.event2.group_id: 1,
  145. self.perf_event.group_id: 1,
  146. }
  147. batch_query = self.condition_inst2.batch_query_hook(
  148. group_ids=[self.event3.group_id],
  149. start=self.start,
  150. end=self.end,
  151. environment_id=self.environment2.id,
  152. )
  153. assert batch_query == {self.event3.group_id: 1}
  154. class EventFrequencyPercentConditionQueryTest(
  155. EventFrequencyQueryTestBase, BaseEventFrequencyPercentTest
  156. ):
  157. rule_cls = EventFrequencyPercentCondition
  158. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  159. def test_batch_query_percent(self):
  160. self._make_sessions(60, self.environment2.name)
  161. self._make_sessions(60, self.environment.name)
  162. batch_query = self.condition_inst.batch_query_hook(
  163. group_ids=[self.event.group_id, self.event2.group_id, self.perf_event.group_id],
  164. start=self.start,
  165. end=self.end,
  166. environment_id=self.environment.id,
  167. )
  168. percent_of_sessions = 20
  169. assert batch_query == {
  170. self.event.group_id: percent_of_sessions,
  171. self.event2.group_id: percent_of_sessions,
  172. }
  173. batch_query = self.condition_inst2.batch_query_hook(
  174. group_ids=[self.event3.group_id],
  175. start=self.start,
  176. end=self.end,
  177. environment_id=self.environment2.id,
  178. )
  179. assert batch_query == {self.event3.group_id: percent_of_sessions}
  180. class ErrorEventMixin(SnubaTestCase):
  181. def add_event(self, data, project_id, timestamp):
  182. data["timestamp"] = iso_format(timestamp)
  183. # Store an error event
  184. event = self.store_event(
  185. data=data,
  186. project_id=project_id,
  187. )
  188. return event.for_group(event.group)
  189. class PerfIssuePlatformEventMixin(PerformanceIssueTestCase):
  190. def add_event(self, data, project_id, timestamp):
  191. fingerprint = data["fingerprint"][0]
  192. fingerprint = (
  193. fingerprint
  194. if "-" in fingerprint
  195. else f"{PerformanceNPlusOneGroupType.type_id}-{data['fingerprint'][0]}"
  196. )
  197. event_data = load_data(
  198. "transaction-n-plus-one",
  199. timestamp=timestamp,
  200. start_timestamp=timestamp,
  201. fingerprint=[fingerprint],
  202. )
  203. event_data["user"] = {"id": uuid4().hex}
  204. event_data["environment"] = data.get("environment")
  205. for tag in event_data["tags"]:
  206. if tag[0] == "environment":
  207. tag[1] = data.get("environment")
  208. break
  209. else:
  210. event_data["tags"].append(data.get("environment"))
  211. # Store a performance event
  212. event = self.create_performance_issue(
  213. event_data=event_data,
  214. project_id=project_id,
  215. fingerprint=fingerprint,
  216. )
  217. return event
  218. @pytest.mark.snuba_ci
  219. class StandardIntervalTestBase(SnubaTestCase, RuleTestCase, PerformanceIssueTestCase):
  220. __test__ = Abstract(__module__, __qualname__)
  221. def add_event(self, data, project_id, timestamp):
  222. raise NotImplementedError
  223. def increment(self, event, count, environment=None, timestamp=None):
  224. raise NotImplementedError
  225. def _run_test(self, minutes, data, passes, add_events=False):
  226. if not self.environment:
  227. self.environment = self.create_environment(name="prod")
  228. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  229. environment_rule = self.get_rule(data=data, rule=Rule(environment_id=self.environment.id))
  230. event = self.add_event(
  231. data={
  232. "fingerprint": ["something_random"],
  233. "user": {"id": uuid4().hex},
  234. },
  235. project_id=self.project.id,
  236. timestamp=before_now(minutes=minutes),
  237. )
  238. if add_events:
  239. self.increment(
  240. event,
  241. data["value"] + 1,
  242. environment=self.environment.name,
  243. timestamp=timezone.now() - timedelta(minutes=minutes),
  244. )
  245. self.increment(
  246. event,
  247. data["value"] + 1,
  248. timestamp=timezone.now() - timedelta(minutes=minutes),
  249. )
  250. if passes:
  251. self.assertPasses(rule, event, is_new=False)
  252. self.assertPasses(environment_rule, event, is_new=False)
  253. else:
  254. self.assertDoesNotPass(rule, event, is_new=False)
  255. self.assertDoesNotPass(environment_rule, event, is_new=False)
  256. def test_comparison_interval_empty_string(self):
  257. data = {
  258. "interval": "1m",
  259. "value": 16,
  260. "comparisonType": "count",
  261. "comparisonInterval": "",
  262. }
  263. self._run_test(data=data, minutes=1, passes=False)
  264. def test_one_minute_with_events(self):
  265. data = {"interval": "1m", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  266. self._run_test(data=data, minutes=1, passes=True, add_events=True)
  267. data = {
  268. "interval": "1m",
  269. "value": 16,
  270. "comparisonType": "count",
  271. "comparisonInterval": "5m",
  272. }
  273. self._run_test(data=data, minutes=1, passes=False)
  274. def test_one_hour_with_events(self):
  275. data = {"interval": "1h", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  276. self._run_test(data=data, minutes=60, passes=True, add_events=True)
  277. data = {
  278. "interval": "1h",
  279. "value": 16,
  280. "comparisonType": "count",
  281. "comparisonInterval": "5m",
  282. }
  283. self._run_test(data=data, minutes=60, passes=False)
  284. def test_one_day_with_events(self):
  285. data = {"interval": "1d", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  286. self._run_test(data=data, minutes=1440, passes=True, add_events=True)
  287. data = {
  288. "interval": "1d",
  289. "value": 16,
  290. "comparisonType": "count",
  291. "comparisonInterval": "5m",
  292. }
  293. self._run_test(data=data, minutes=1440, passes=False)
  294. def test_one_week_with_events(self):
  295. data = {"interval": "1w", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  296. self._run_test(data=data, minutes=10080, passes=True, add_events=True)
  297. data = {
  298. "interval": "1w",
  299. "value": 16,
  300. "comparisonType": "count",
  301. "comparisonInterval": "5m",
  302. }
  303. self._run_test(data=data, minutes=10080, passes=False)
  304. def test_one_minute_no_events(self):
  305. data = {"interval": "1m", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  306. self._run_test(data=data, minutes=1, passes=False)
  307. def test_one_hour_no_events(self):
  308. data = {"interval": "1h", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  309. self._run_test(data=data, minutes=60, passes=False)
  310. def test_one_day_no_events(self):
  311. data = {"interval": "1d", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  312. self._run_test(data=data, minutes=1440, passes=False)
  313. def test_one_week_no_events(self):
  314. data = {"interval": "1w", "value": 6, "comparisonType": "count", "comparisonInterval": "5m"}
  315. self._run_test(data=data, minutes=10080, passes=False)
  316. def test_comparison(self):
  317. # Test data is 4 events in the current period and 2 events in the comparison period, so
  318. # a 100% increase.
  319. event = self.add_event(
  320. data={
  321. "fingerprint": ["something_random"],
  322. "user": {"id": uuid4().hex},
  323. },
  324. project_id=self.project.id,
  325. timestamp=before_now(minutes=1),
  326. )
  327. self.increment(
  328. event,
  329. 3,
  330. timestamp=timezone.now() - timedelta(minutes=1),
  331. )
  332. self.increment(
  333. event,
  334. 2,
  335. timestamp=timezone.now() - timedelta(days=1, minutes=20),
  336. )
  337. data = {
  338. "interval": "1h",
  339. "value": 99,
  340. "comparisonType": "percent",
  341. "comparisonInterval": "1d",
  342. }
  343. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  344. self.assertPasses(rule, event, is_new=False)
  345. data = {
  346. "interval": "1h",
  347. "value": 101,
  348. "comparisonType": "percent",
  349. "comparisonInterval": "1d",
  350. }
  351. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  352. self.assertDoesNotPass(rule, event, is_new=False)
  353. def test_comparison_empty_comparison_period(self):
  354. # Test data is 1 event in the current period and 0 events in the comparison period. This
  355. # should always result in 0 and never fire.
  356. event = self.add_event(
  357. data={
  358. "fingerprint": ["something_random"],
  359. "user": {"id": uuid4().hex},
  360. },
  361. project_id=self.project.id,
  362. timestamp=before_now(minutes=1),
  363. )
  364. data = {
  365. "interval": "1h",
  366. "value": 0,
  367. "comparisonType": "percent",
  368. "comparisonInterval": "1d",
  369. }
  370. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  371. self.assertDoesNotPass(rule, event, is_new=False)
  372. data = {
  373. "interval": "1h",
  374. "value": 100,
  375. "comparisonType": "percent",
  376. "comparisonInterval": "1d",
  377. }
  378. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  379. self.assertDoesNotPass(rule, event, is_new=False)
  380. @patch("sentry.rules.conditions.event_frequency.BaseEventFrequencyCondition.get_rate")
  381. def test_is_new_issue_skips_snuba(self, mock_get_rate):
  382. # Looking for more than 1 event
  383. data = {"interval": "1m", "value": 6}
  384. minutes = 1
  385. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  386. environment_rule = self.get_rule(data=data, rule=Rule(environment_id=self.environment.id))
  387. event = self.add_event(
  388. data={
  389. "fingerprint": ["something_random"],
  390. "user": {"id": uuid4().hex},
  391. },
  392. project_id=self.project.id,
  393. timestamp=before_now(minutes=minutes),
  394. )
  395. # Issue is new and is the first event
  396. self.assertDoesNotPass(rule, event, is_new=True)
  397. self.assertDoesNotPass(environment_rule, event, is_new=True)
  398. assert mock_get_rate.call_count == 0
  399. class EventFrequencyConditionTestCase(StandardIntervalTestBase):
  400. __test__ = Abstract(__module__, __qualname__)
  401. rule_cls = EventFrequencyCondition
  402. def increment(self, event, count, environment=None, timestamp=None):
  403. timestamp = timestamp if timestamp else before_now(minutes=1)
  404. data = {"fingerprint": event.data["fingerprint"]}
  405. if environment:
  406. data["environment"] = environment
  407. for _ in range(count):
  408. self.add_event(
  409. data=data,
  410. project_id=self.project.id,
  411. timestamp=timestamp,
  412. )
  413. class EventUniqueUserFrequencyConditionTestCase(StandardIntervalTestBase):
  414. __test__ = Abstract(__module__, __qualname__)
  415. rule_cls = EventUniqueUserFrequencyCondition
  416. def increment(self, event, count, environment=None, timestamp=None):
  417. timestamp = timestamp if timestamp else before_now(minutes=1)
  418. data = {"fingerprint": event.data["fingerprint"]}
  419. if environment:
  420. data["environment"] = environment
  421. for _ in range(count):
  422. event_data = deepcopy(data)
  423. event_data["user"] = {"id": uuid4().hex}
  424. self.add_event(
  425. data=event_data,
  426. project_id=self.project.id,
  427. timestamp=timestamp,
  428. )
  429. class EventFrequencyPercentConditionTestCase(BaseEventFrequencyPercentTest, RuleTestCase):
  430. __test__ = Abstract(__module__, __qualname__)
  431. rule_cls = EventFrequencyPercentCondition
  432. def add_event(self, data, project_id, timestamp):
  433. raise NotImplementedError
  434. def _run_test(self, minutes, data, passes, add_events=False):
  435. if not self.environment or self.environment.name != "prod":
  436. self.environment = self.create_environment(name="prod")
  437. if not hasattr(self, "test_event"):
  438. self.test_event = self.add_event(
  439. data={
  440. "fingerprint": ["something_random"],
  441. "user": {"id": uuid4().hex},
  442. "environment": self.environment.name,
  443. },
  444. project_id=self.project.id,
  445. timestamp=before_now(minutes=minutes),
  446. )
  447. if add_events:
  448. self.increment(
  449. self.test_event,
  450. max(1, int(minutes / 2)) - 1,
  451. environment=self.environment.name,
  452. timestamp=timezone.now() - timedelta(minutes=minutes),
  453. )
  454. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  455. environment_rule = self.get_rule(data=data, rule=Rule(environment_id=self.environment.id))
  456. if passes:
  457. self.assertPasses(rule, self.test_event, is_new=False)
  458. self.assertPasses(environment_rule, self.test_event, is_new=False)
  459. else:
  460. self.assertDoesNotPass(rule, self.test_event)
  461. self.assertDoesNotPass(environment_rule, self.test_event)
  462. def increment(self, event, count, environment=None, timestamp=None):
  463. data = {
  464. "fingerprint": event.data["fingerprint"],
  465. }
  466. timestamp = timestamp if timestamp else before_now(minutes=1)
  467. if environment:
  468. data["environment"] = environment
  469. for _ in range(count):
  470. event_data = deepcopy(data)
  471. event_data["user"] = {"id": uuid4().hex}
  472. self.add_event(
  473. data=event_data,
  474. project_id=self.project.id,
  475. timestamp=timestamp,
  476. )
  477. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  478. def test_five_minutes_with_events(self):
  479. self._make_sessions(60)
  480. data = {
  481. "interval": "5m",
  482. "value": 39,
  483. "comparisonType": "count",
  484. "comparisonInterval": "5m",
  485. }
  486. self._run_test(data=data, minutes=5, passes=True, add_events=True)
  487. data = {
  488. "interval": "5m",
  489. "value": 41,
  490. "comparisonType": "count",
  491. "comparisonInterval": "5m",
  492. }
  493. self._run_test(data=data, minutes=5, passes=False)
  494. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  495. def test_ten_minutes_with_events(self):
  496. self._make_sessions(60)
  497. data = {
  498. "interval": "10m",
  499. "value": 49,
  500. "comparisonType": "count",
  501. "comparisonInterval": "5m",
  502. }
  503. self._run_test(data=data, minutes=10, passes=True, add_events=True)
  504. data = {
  505. "interval": "10m",
  506. "value": 51,
  507. "comparisonType": "count",
  508. "comparisonInterval": "5m",
  509. }
  510. self._run_test(data=data, minutes=10, passes=False)
  511. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  512. def test_thirty_minutes_with_events(self):
  513. self._make_sessions(60)
  514. data = {
  515. "interval": "30m",
  516. "value": 49,
  517. "comparisonType": "count",
  518. "comparisonInterval": "5m",
  519. }
  520. self._run_test(data=data, minutes=30, passes=True, add_events=True)
  521. data = {
  522. "interval": "30m",
  523. "value": 51,
  524. "comparisonType": "count",
  525. "comparisonInterval": "5m",
  526. }
  527. self._run_test(data=data, minutes=30, passes=False)
  528. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  529. def test_one_hour_with_events(self):
  530. self._make_sessions(60)
  531. data = {
  532. "interval": "1h",
  533. "value": 49,
  534. "comparisonType": "count",
  535. "comparisonInterval": "5m",
  536. }
  537. self._run_test(data=data, minutes=60, add_events=True, passes=True)
  538. data = {
  539. "interval": "1h",
  540. "value": 51,
  541. "comparisonType": "count",
  542. "comparisonInterval": "5m",
  543. }
  544. self._run_test(data=data, minutes=60, passes=False)
  545. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  546. def test_five_minutes_no_events(self):
  547. self._make_sessions(60)
  548. data = {
  549. "interval": "5m",
  550. "value": 39,
  551. "comparisonType": "count",
  552. "comparisonInterval": "5m",
  553. }
  554. self._run_test(data=data, minutes=5, passes=True, add_events=True)
  555. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  556. def test_ten_minutes_no_events(self):
  557. self._make_sessions(60)
  558. data = {
  559. "interval": "10m",
  560. "value": 49,
  561. "comparisonType": "count",
  562. "comparisonInterval": "5m",
  563. }
  564. self._run_test(data=data, minutes=10, passes=True, add_events=True)
  565. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  566. def test_thirty_minutes_no_events(self):
  567. self._make_sessions(60)
  568. data = {
  569. "interval": "30m",
  570. "value": 49,
  571. "comparisonType": "count",
  572. "comparisonInterval": "5m",
  573. }
  574. self._run_test(data=data, minutes=30, passes=True, add_events=True)
  575. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  576. def test_one_hour_no_events(self):
  577. self._make_sessions(60)
  578. data = {
  579. "interval": "1h",
  580. "value": 49,
  581. "comparisonType": "count",
  582. "comparisonInterval": "5m",
  583. }
  584. self._run_test(data=data, minutes=60, passes=False)
  585. @patch("sentry.rules.conditions.event_frequency.MIN_SESSIONS_TO_FIRE", 1)
  586. def test_comparison(self):
  587. self._make_sessions(10)
  588. # Create sessions for previous period
  589. self._make_sessions(10)
  590. # Test data is 2 events in the current period and 1 events in the comparison period.
  591. # Number of sessions is 20 in each period, so current period is 20% of sessions, prev
  592. # is 10%. Overall a 100% increase comparatively.
  593. event = self.add_event(
  594. data={"fingerprint": ["something_random"]},
  595. project_id=self.project.id,
  596. timestamp=before_now(minutes=1),
  597. )
  598. self.increment(
  599. event,
  600. 1,
  601. timestamp=timezone.now() - timedelta(minutes=1),
  602. )
  603. self.increment(
  604. event,
  605. 1,
  606. timestamp=timezone.now() - timedelta(days=1, minutes=20),
  607. )
  608. data = {
  609. "interval": "1h",
  610. "value": 99,
  611. "comparisonType": "percent",
  612. "comparisonInterval": "1d",
  613. }
  614. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  615. self.assertPasses(rule, event, is_new=False)
  616. data = {
  617. "interval": "1h",
  618. "value": 101,
  619. "comparisonType": "percent",
  620. "comparisonInterval": "1d",
  621. }
  622. rule = self.get_rule(data=data, rule=Rule(environment_id=None))
  623. self.assertDoesNotPass(rule, event, is_new=False)
  624. @freeze_time(
  625. (timezone.now() - timedelta(days=2)).replace(hour=12, minute=40, second=0, microsecond=0)
  626. )
  627. class ErrorIssueFrequencyConditionTestCase(ErrorEventMixin, EventFrequencyConditionTestCase):
  628. pass
  629. @freeze_time(
  630. (timezone.now() - timedelta(days=2)).replace(hour=12, minute=40, second=0, microsecond=0)
  631. )
  632. class PerfIssuePlatformIssueFrequencyConditionTestCase(
  633. PerfIssuePlatformEventMixin,
  634. EventFrequencyConditionTestCase,
  635. ):
  636. pass
  637. @freeze_time(
  638. (timezone.now() - timedelta(days=2)).replace(hour=12, minute=40, second=0, microsecond=0)
  639. )
  640. class ErrorIssueUniqueUserFrequencyConditionTestCase(
  641. ErrorEventMixin,
  642. EventUniqueUserFrequencyConditionTestCase,
  643. ):
  644. pass
  645. @freeze_time(
  646. (timezone.now() - timedelta(days=2)).replace(hour=12, minute=40, second=0, microsecond=0)
  647. )
  648. class PerfIssuePlatformIssueUniqueUserFrequencyConditionTestCase(
  649. PerfIssuePlatformEventMixin,
  650. EventUniqueUserFrequencyConditionTestCase,
  651. ):
  652. pass
  653. @freeze_time(
  654. (timezone.now() - timedelta(days=2)).replace(hour=12, minute=40, second=0, microsecond=0)
  655. )
  656. class ErrorIssueEventFrequencyPercentConditionTestCase(
  657. ErrorEventMixin, EventFrequencyPercentConditionTestCase
  658. ):
  659. pass
  660. @freeze_time(
  661. (timezone.now() - timedelta(days=2)).replace(hour=12, minute=40, second=0, microsecond=0)
  662. )
  663. class PerfIssuePlatformIssueEventFrequencyPercentConditionTestCase(
  664. PerfIssuePlatformEventMixin,
  665. EventFrequencyPercentConditionTestCase,
  666. ):
  667. pass