test_group.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. from datetime import timedelta, timezone
  2. from unittest import mock
  3. from django.utils import timezone as django_timezone
  4. from sentry.api.event_search import SearchFilter, SearchKey, SearchValue
  5. from sentry.api.serializers import serialize
  6. from sentry.api.serializers.models.group import GroupSerializerSnuba
  7. from sentry.issues.grouptype import PerformanceNPlusOneGroupType, ProfileFileIOGroupType
  8. from sentry.models.group import Group, GroupStatus
  9. from sentry.models.groupenvironment import GroupEnvironment
  10. from sentry.models.grouplink import GroupLink
  11. from sentry.models.groupresolution import GroupResolution
  12. from sentry.models.groupsnooze import GroupSnooze
  13. from sentry.models.groupsubscription import GroupSubscription
  14. from sentry.models.notificationsettingoption import NotificationSettingOption
  15. from sentry.models.options.user_option import UserOption
  16. from sentry.notifications.types import NotificationSettingsOptionEnum
  17. from sentry.silo import SiloMode
  18. from sentry.testutils.cases import APITestCase, PerformanceIssueTestCase, SnubaTestCase
  19. from sentry.testutils.helpers.datetime import before_now, iso_format
  20. from sentry.testutils.helpers.features import with_feature
  21. from sentry.testutils.performance_issues.store_transaction import PerfIssueTransactionTestMixin
  22. from sentry.testutils.silo import assume_test_silo_mode, region_silo_test
  23. from sentry.types.group import PriorityLevel
  24. from sentry.utils.samples import load_data
  25. from tests.sentry.issues.test_utils import SearchIssueTestMixin
  26. @region_silo_test
  27. class GroupSerializerSnubaTest(APITestCase, SnubaTestCase):
  28. def setUp(self):
  29. super().setUp()
  30. self.min_ago = before_now(minutes=1)
  31. self.day_ago = before_now(days=1)
  32. self.week_ago = before_now(days=7)
  33. def test_permalink(self):
  34. group = self.create_group()
  35. result = serialize(group, self.user, serializer=GroupSerializerSnuba())
  36. assert "http://" in result["permalink"]
  37. assert f"{group.organization.slug}/issues/{group.id}" in result["permalink"]
  38. def test_permalink_outside_org(self):
  39. outside_user = self.create_user()
  40. group = self.create_group()
  41. result = serialize(group, outside_user, serializer=GroupSerializerSnuba())
  42. assert result["permalink"] is None
  43. def test_priority_no_ff(self):
  44. outside_user = self.create_user()
  45. group = self.create_group()
  46. result = serialize(group, outside_user, serializer=GroupSerializerSnuba())
  47. assert "priority" not in result
  48. assert "priorityLockedAt" not in result
  49. @with_feature("projects:issue-priority")
  50. def test_priority_high(self):
  51. outside_user = self.create_user()
  52. group = self.create_group(priority=PriorityLevel.HIGH)
  53. result = serialize(group, outside_user, serializer=GroupSerializerSnuba())
  54. assert result["priority"] == "high"
  55. @with_feature("projects:issue-priority")
  56. def test_priority_medium(self):
  57. outside_user = self.create_user()
  58. group = self.create_group(priority=PriorityLevel.MEDIUM)
  59. result = serialize(group, outside_user, serializer=GroupSerializerSnuba())
  60. assert result["priority"] == "medium"
  61. @with_feature("projects:issue-priority")
  62. def test_priority_none(self):
  63. outside_user = self.create_user()
  64. group = self.create_group()
  65. result = serialize(group, outside_user, serializer=GroupSerializerSnuba())
  66. assert result["priority"] is None
  67. assert result["priorityLockedAt"] is None
  68. def test_is_ignored_with_expired_snooze(self):
  69. now = django_timezone.now()
  70. user = self.create_user()
  71. group = self.create_group(status=GroupStatus.IGNORED)
  72. GroupSnooze.objects.create(group=group, until=now - timedelta(minutes=1))
  73. result = serialize(group, user, serializer=GroupSerializerSnuba())
  74. assert result["status"] == "unresolved"
  75. assert result["statusDetails"] == {}
  76. def test_is_ignored_with_valid_snooze(self):
  77. now = django_timezone.now()
  78. user = self.create_user()
  79. group = self.create_group(status=GroupStatus.IGNORED)
  80. snooze = GroupSnooze.objects.create(group=group, until=now + timedelta(minutes=1))
  81. result = serialize(group, user, serializer=GroupSerializerSnuba())
  82. assert result["status"] == "ignored"
  83. assert result["statusDetails"]["ignoreCount"] == snooze.count
  84. assert result["statusDetails"]["ignoreWindow"] == snooze.window
  85. assert result["statusDetails"]["ignoreUserCount"] == snooze.user_count
  86. assert result["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  87. assert result["statusDetails"]["ignoreUntil"] == snooze.until
  88. assert result["statusDetails"]["actor"] is None
  89. def test_is_ignored_with_valid_snooze_and_actor(self):
  90. now = django_timezone.now()
  91. user = self.create_user()
  92. group = self.create_group(status=GroupStatus.IGNORED)
  93. GroupSnooze.objects.create(group=group, until=now + timedelta(minutes=1), actor_id=user.id)
  94. result = serialize(group, user, serializer=GroupSerializerSnuba())
  95. assert result["status"] == "ignored"
  96. assert result["statusDetails"]["actor"]["id"] == str(user.id)
  97. def test_resolved_in_next_release(self):
  98. release = self.create_release(project=self.project, version="a")
  99. user = self.create_user()
  100. group = self.create_group(status=GroupStatus.RESOLVED)
  101. GroupResolution.objects.create(
  102. group=group, release=release, type=GroupResolution.Type.in_next_release
  103. )
  104. result = serialize(group, user, serializer=GroupSerializerSnuba())
  105. assert result["status"] == "resolved"
  106. assert result["statusDetails"] == {"inNextRelease": True, "actor": None}
  107. def test_resolved_in_release(self):
  108. release = self.create_release(project=self.project, version="a")
  109. user = self.create_user()
  110. group = self.create_group(status=GroupStatus.RESOLVED)
  111. GroupResolution.objects.create(
  112. group=group, release=release, type=GroupResolution.Type.in_release
  113. )
  114. result = serialize(group, user, serializer=GroupSerializerSnuba())
  115. assert result["status"] == "resolved"
  116. assert result["statusDetails"] == {"inRelease": "a", "actor": None}
  117. def test_resolved_with_actor(self):
  118. release = self.create_release(project=self.project, version="a")
  119. user = self.create_user()
  120. group = self.create_group(status=GroupStatus.RESOLVED)
  121. GroupResolution.objects.create(
  122. group=group, release=release, type=GroupResolution.Type.in_release, actor_id=user.id
  123. )
  124. result = serialize(group, user, serializer=GroupSerializerSnuba())
  125. assert result["status"] == "resolved"
  126. assert result["statusDetails"]["actor"]["id"] == str(user.id)
  127. def test_resolved_in_commit(self):
  128. repo = self.create_repo(project=self.project)
  129. commit = self.create_commit(repo=repo)
  130. user = self.create_user()
  131. group = self.create_group(status=GroupStatus.RESOLVED)
  132. GroupLink.objects.create(
  133. group_id=group.id,
  134. project_id=group.project_id,
  135. linked_id=commit.id,
  136. linked_type=GroupLink.LinkedType.commit,
  137. relationship=GroupLink.Relationship.resolves,
  138. )
  139. result = serialize(group, user, serializer=GroupSerializerSnuba())
  140. assert result["status"] == "resolved"
  141. assert result["statusDetails"]["inCommit"]["id"] == commit.key
  142. @mock.patch("sentry.models.Group.is_over_resolve_age")
  143. def test_auto_resolved(self, mock_is_over_resolve_age):
  144. mock_is_over_resolve_age.return_value = True
  145. user = self.create_user()
  146. group = self.create_group(status=GroupStatus.UNRESOLVED)
  147. result = serialize(group, user, serializer=GroupSerializerSnuba())
  148. assert result["status"] == "resolved"
  149. assert result["statusDetails"] == {"autoResolved": True}
  150. def test_subscribed(self):
  151. user = self.create_user()
  152. group = self.create_group()
  153. GroupSubscription.objects.create(
  154. user_id=user.id, group=group, project=group.project, is_active=True
  155. )
  156. result = serialize(group, user, serializer=GroupSerializerSnuba())
  157. assert result["isSubscribed"]
  158. assert result["subscriptionDetails"] == {"reason": "unknown"}
  159. def test_explicit_unsubscribed(self):
  160. user = self.create_user()
  161. group = self.create_group()
  162. GroupSubscription.objects.create(
  163. user_id=user.id, group=group, project=group.project, is_active=False
  164. )
  165. result = serialize(group, user, serializer=GroupSerializerSnuba())
  166. assert not result["isSubscribed"]
  167. assert not result["subscriptionDetails"]
  168. def test_implicit_subscribed(self):
  169. user = self.create_user()
  170. group = self.create_group()
  171. combinations = (
  172. # (default, project, subscribed, has_details)
  173. (
  174. NotificationSettingsOptionEnum.ALWAYS,
  175. NotificationSettingsOptionEnum.DEFAULT,
  176. True,
  177. False,
  178. ),
  179. (
  180. NotificationSettingsOptionEnum.ALWAYS,
  181. NotificationSettingsOptionEnum.ALWAYS,
  182. True,
  183. False,
  184. ),
  185. (
  186. NotificationSettingsOptionEnum.ALWAYS,
  187. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  188. False,
  189. False,
  190. ),
  191. (
  192. NotificationSettingsOptionEnum.ALWAYS,
  193. NotificationSettingsOptionEnum.NEVER,
  194. False,
  195. True,
  196. ),
  197. (
  198. NotificationSettingsOptionEnum.DEFAULT,
  199. NotificationSettingsOptionEnum.DEFAULT,
  200. False,
  201. False,
  202. ),
  203. (
  204. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  205. NotificationSettingsOptionEnum.DEFAULT,
  206. False,
  207. False,
  208. ),
  209. (
  210. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  211. NotificationSettingsOptionEnum.ALWAYS,
  212. True,
  213. False,
  214. ),
  215. (
  216. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  217. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  218. False,
  219. False,
  220. ),
  221. (
  222. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  223. NotificationSettingsOptionEnum.NEVER,
  224. False,
  225. True,
  226. ),
  227. (
  228. NotificationSettingsOptionEnum.NEVER,
  229. NotificationSettingsOptionEnum.DEFAULT,
  230. False,
  231. True,
  232. ),
  233. (
  234. NotificationSettingsOptionEnum.NEVER,
  235. NotificationSettingsOptionEnum.ALWAYS,
  236. True,
  237. False,
  238. ),
  239. (
  240. NotificationSettingsOptionEnum.NEVER,
  241. NotificationSettingsOptionEnum.SUBSCRIBE_ONLY,
  242. False,
  243. False,
  244. ),
  245. (
  246. NotificationSettingsOptionEnum.NEVER,
  247. NotificationSettingsOptionEnum.NEVER,
  248. False,
  249. True,
  250. ),
  251. )
  252. for default_value, project_value, is_subscribed, has_details in combinations:
  253. with assume_test_silo_mode(SiloMode.CONTROL):
  254. UserOption.objects.clear_local_cache()
  255. NotificationSettingOption.objects.all().delete()
  256. if default_value != NotificationSettingsOptionEnum.DEFAULT:
  257. NotificationSettingOption.objects.create(
  258. user_id=user.id,
  259. scope_type="user",
  260. scope_identifier=user.id,
  261. value=default_value.value,
  262. type="workflow",
  263. )
  264. if project_value != NotificationSettingsOptionEnum.DEFAULT:
  265. NotificationSettingOption.objects.create(
  266. user_id=user.id,
  267. scope_type="project",
  268. scope_identifier=group.project.id,
  269. value=project_value.value,
  270. type="workflow",
  271. )
  272. result = serialize(group, user, serializer=GroupSerializerSnuba())
  273. subscription_details = result.get("subscriptionDetails")
  274. assert result["isSubscribed"] is is_subscribed
  275. assert (
  276. subscription_details == {"disabled": True}
  277. if has_details
  278. else subscription_details is None
  279. )
  280. def test_global_no_conversations_overrides_group_subscription(self):
  281. user = self.create_user()
  282. group = self.create_group()
  283. GroupSubscription.objects.create(
  284. user_id=user.id, group=group, project=group.project, is_active=True
  285. )
  286. with assume_test_silo_mode(SiloMode.CONTROL):
  287. NotificationSettingOption.objects.create(
  288. user_id=user.id,
  289. scope_type="user",
  290. scope_identifier=user.id,
  291. value="never",
  292. type="workflow",
  293. )
  294. result = serialize(group, user, serializer=GroupSerializerSnuba())
  295. assert not result["isSubscribed"]
  296. assert result["subscriptionDetails"] == {"disabled": True}
  297. def test_project_no_conversations_overrides_group_subscription(self):
  298. user = self.create_user()
  299. group = self.create_group()
  300. GroupSubscription.objects.create(
  301. user_id=user.id, group=group, project=group.project, is_active=True
  302. )
  303. with assume_test_silo_mode(SiloMode.CONTROL):
  304. NotificationSettingOption.objects.create(
  305. user_id=user.id,
  306. scope_type="project",
  307. scope_identifier=group.project.id,
  308. value="never",
  309. type="workflow",
  310. )
  311. result = serialize(group, user, serializer=GroupSerializerSnuba())
  312. assert not result["isSubscribed"]
  313. assert result["subscriptionDetails"] == {"disabled": True}
  314. def test_no_user_unsubscribed(self):
  315. group = self.create_group()
  316. result = serialize(group, serializer=GroupSerializerSnuba())
  317. assert not result["isSubscribed"]
  318. def test_seen_stats(self):
  319. environment = self.create_environment(project=self.project)
  320. environment2 = self.create_environment(project=self.project)
  321. events = []
  322. for event_id, env, user_id, timestamp in [
  323. ("a" * 32, environment, 1, iso_format(self.min_ago)),
  324. ("b" * 32, environment, 2, iso_format(self.min_ago)),
  325. ("c" * 32, environment2, 3, iso_format(self.week_ago)),
  326. ]:
  327. events.append(
  328. self.store_event(
  329. data={
  330. "event_id": event_id,
  331. "fingerprint": ["put-me-in-group1"],
  332. "timestamp": timestamp,
  333. "environment": env.name,
  334. "user": {"id": user_id},
  335. },
  336. project_id=self.project.id,
  337. )
  338. )
  339. # Assert all events are in the same group
  340. (group_id,) = {e.group.id for e in events}
  341. group = Group.objects.get(id=group_id)
  342. group.times_seen = 3
  343. group.first_seen = self.week_ago - timedelta(days=5)
  344. group.last_seen = self.week_ago
  345. group.save()
  346. # should use group columns when no environments arg passed
  347. result = serialize(group, serializer=GroupSerializerSnuba(environment_ids=[]))
  348. assert result["count"] == "3"
  349. assert iso_format(result["lastSeen"]) == iso_format(self.min_ago)
  350. assert result["firstSeen"] == group.first_seen
  351. # update this to something different to make sure it's being used
  352. group_env = GroupEnvironment.objects.get(group_id=group_id, environment_id=environment.id)
  353. group_env.first_seen = self.day_ago - timedelta(days=3)
  354. group_env.save()
  355. group_env2 = GroupEnvironment.objects.get(group_id=group_id, environment_id=environment2.id)
  356. result = serialize(
  357. group,
  358. serializer=GroupSerializerSnuba(environment_ids=[environment.id, environment2.id]),
  359. )
  360. assert result["count"] == "3"
  361. # result is rounded down to nearest second
  362. assert iso_format(result["lastSeen"]) == iso_format(self.min_ago)
  363. assert iso_format(result["firstSeen"]) == iso_format(group_env.first_seen)
  364. assert iso_format(group_env2.first_seen) > iso_format(group_env.first_seen)
  365. assert result["userCount"] == 3
  366. result = serialize(
  367. group,
  368. serializer=GroupSerializerSnuba(
  369. environment_ids=[environment.id, environment2.id],
  370. start=self.week_ago - timedelta(hours=1),
  371. end=self.week_ago + timedelta(hours=1),
  372. ),
  373. )
  374. assert result["userCount"] == 1
  375. assert iso_format(result["lastSeen"]) == iso_format(self.week_ago)
  376. assert iso_format(result["firstSeen"]) == iso_format(self.week_ago)
  377. assert result["count"] == "1"
  378. def test_get_start_from_seen_stats(self):
  379. for days, expected in [(None, 30), (0, 14), (1000, 90)]:
  380. last_seen = None if days is None else before_now(days=days).replace(tzinfo=timezone.utc)
  381. start = GroupSerializerSnuba._get_start_from_seen_stats(
  382. {
  383. mock.sentinel.group: {
  384. "last_seen": last_seen,
  385. "first_seen": None,
  386. "times_seen": 0,
  387. "user_count": 0,
  388. }
  389. }
  390. )
  391. assert iso_format(start) == iso_format(before_now(days=expected))
  392. def test_skipped_date_timestamp_filters(self):
  393. group = self.create_group()
  394. serializer = GroupSerializerSnuba(
  395. search_filters=[
  396. SearchFilter(
  397. SearchKey("timestamp"),
  398. ">",
  399. SearchValue(before_now(hours=1).replace(tzinfo=timezone.utc)),
  400. ),
  401. SearchFilter(
  402. SearchKey("timestamp"),
  403. "<",
  404. SearchValue(before_now(seconds=1).replace(tzinfo=timezone.utc)),
  405. ),
  406. SearchFilter(
  407. SearchKey("date"),
  408. ">",
  409. SearchValue(before_now(hours=1).replace(tzinfo=timezone.utc)),
  410. ),
  411. SearchFilter(
  412. SearchKey("date"),
  413. "<",
  414. SearchValue(before_now(seconds=1).replace(tzinfo=timezone.utc)),
  415. ),
  416. ]
  417. )
  418. assert not serializer.conditions
  419. result = serialize(group, self.user, serializer=serializer)
  420. assert result["id"] == str(group.id)
  421. @region_silo_test
  422. class PerformanceGroupSerializerSnubaTest(
  423. APITestCase,
  424. SnubaTestCase,
  425. PerfIssueTransactionTestMixin,
  426. PerformanceIssueTestCase,
  427. ):
  428. def test_perf_seen_stats(self):
  429. proj = self.create_project()
  430. first_group_fingerprint = f"{PerformanceNPlusOneGroupType.type_id}-group1"
  431. timestamp = django_timezone.now() - timedelta(days=5)
  432. times = 5
  433. for _ in range(0, times):
  434. event_data = load_data(
  435. "transaction-n-plus-one",
  436. timestamp=timestamp + timedelta(minutes=1),
  437. start_timestamp=timestamp + timedelta(minutes=1),
  438. )
  439. event_data["user"] = {"email": "test1@example.com"}
  440. self.create_performance_issue(
  441. event_data=event_data, fingerprint=first_group_fingerprint, project_id=proj.id
  442. )
  443. event_data = load_data(
  444. "transaction-n-plus-one",
  445. timestamp=timestamp + timedelta(minutes=2),
  446. start_timestamp=timestamp + timedelta(minutes=2),
  447. )
  448. event_data["user"] = {"email": "test2@example.com"}
  449. event = self.create_performance_issue(
  450. event_data=event_data, fingerprint=first_group_fingerprint, project_id=proj.id
  451. )
  452. first_group = event.group
  453. result = serialize(
  454. first_group,
  455. serializer=GroupSerializerSnuba(
  456. start=django_timezone.now() - timedelta(days=60),
  457. end=django_timezone.now() + timedelta(days=10),
  458. ),
  459. )
  460. assert result["userCount"] == 2
  461. assert iso_format(result["lastSeen"]) == iso_format(timestamp + timedelta(minutes=2))
  462. assert iso_format(result["firstSeen"]) == iso_format(timestamp + timedelta(minutes=1))
  463. assert result["count"] == str(times + 1)
  464. @region_silo_test
  465. class ProfilingGroupSerializerSnubaTest(
  466. APITestCase,
  467. SnubaTestCase,
  468. SearchIssueTestMixin,
  469. ):
  470. def test_profiling_seen_stats(self):
  471. proj = self.create_project()
  472. environment = self.create_environment(project=proj)
  473. first_group_fingerprint = f"{ProfileFileIOGroupType.type_id}-group1"
  474. timestamp = (django_timezone.now() - timedelta(days=5)).replace(hour=0, minute=0, second=0)
  475. times = 5
  476. for incr in range(0, times):
  477. # for user_0 - user_4, first_group
  478. self.store_search_issue(
  479. proj.id,
  480. incr,
  481. [first_group_fingerprint],
  482. environment.name,
  483. timestamp + timedelta(minutes=incr),
  484. )
  485. # user_5, another_group
  486. event, issue_occurrence, group_info = self.store_search_issue(
  487. proj.id,
  488. 5,
  489. [first_group_fingerprint],
  490. environment.name,
  491. timestamp + timedelta(minutes=5),
  492. )
  493. assert group_info is not None
  494. first_group = group_info.group
  495. result = serialize(
  496. first_group,
  497. serializer=GroupSerializerSnuba(
  498. environment_ids=[environment.id],
  499. start=timestamp - timedelta(days=1),
  500. end=timestamp + timedelta(days=1),
  501. ),
  502. )
  503. assert result["userCount"] == 6
  504. assert iso_format(result["lastSeen"]) == iso_format(timestamp + timedelta(minutes=5))
  505. assert iso_format(result["firstSeen"]) == iso_format(timestamp)
  506. assert result["count"] == str(times + 1)