test_group.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. from datetime import timedelta
  2. from unittest.mock import patch
  3. import pytz
  4. from django.utils import timezone
  5. from sentry.api.serializers import serialize
  6. from sentry.api.serializers.models.group import GroupSerializerSnuba
  7. from sentry.models import (
  8. Group,
  9. GroupEnvironment,
  10. GroupLink,
  11. GroupResolution,
  12. GroupSnooze,
  13. GroupStatus,
  14. GroupSubscription,
  15. NotificationSetting,
  16. UserOption,
  17. )
  18. from sentry.notifications.types import NotificationSettingOptionValues, NotificationSettingTypes
  19. from sentry.testutils import APITestCase, SnubaTestCase
  20. from sentry.testutils.helpers.datetime import before_now, iso_format
  21. from sentry.testutils.performance_issues.store_transaction import PerfIssueTransactionTestMixin
  22. from sentry.testutils.silo import region_silo_test
  23. from sentry.types.integrations import ExternalProviders
  24. from sentry.types.issues import GroupType
  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_is_ignored_with_expired_snooze(self):
  44. now = timezone.now()
  45. user = self.create_user()
  46. group = self.create_group(status=GroupStatus.IGNORED)
  47. GroupSnooze.objects.create(group=group, until=now - timedelta(minutes=1))
  48. result = serialize(group, user, serializer=GroupSerializerSnuba())
  49. assert result["status"] == "unresolved"
  50. assert result["statusDetails"] == {}
  51. def test_is_ignored_with_valid_snooze(self):
  52. now = timezone.now()
  53. user = self.create_user()
  54. group = self.create_group(status=GroupStatus.IGNORED)
  55. snooze = GroupSnooze.objects.create(group=group, until=now + timedelta(minutes=1))
  56. result = serialize(group, user, serializer=GroupSerializerSnuba())
  57. assert result["status"] == "ignored"
  58. assert result["statusDetails"]["ignoreCount"] == snooze.count
  59. assert result["statusDetails"]["ignoreWindow"] == snooze.window
  60. assert result["statusDetails"]["ignoreUserCount"] == snooze.user_count
  61. assert result["statusDetails"]["ignoreUserWindow"] == snooze.user_window
  62. assert result["statusDetails"]["ignoreUntil"] == snooze.until
  63. assert result["statusDetails"]["actor"] is None
  64. def test_is_ignored_with_valid_snooze_and_actor(self):
  65. now = timezone.now()
  66. user = self.create_user()
  67. group = self.create_group(status=GroupStatus.IGNORED)
  68. GroupSnooze.objects.create(group=group, until=now + timedelta(minutes=1), actor_id=user.id)
  69. result = serialize(group, user, serializer=GroupSerializerSnuba())
  70. assert result["status"] == "ignored"
  71. assert result["statusDetails"]["actor"]["id"] == str(user.id)
  72. def test_resolved_in_next_release(self):
  73. release = self.create_release(project=self.project, version="a")
  74. user = self.create_user()
  75. group = self.create_group(status=GroupStatus.RESOLVED)
  76. GroupResolution.objects.create(
  77. group=group, release=release, type=GroupResolution.Type.in_next_release
  78. )
  79. result = serialize(group, user, serializer=GroupSerializerSnuba())
  80. assert result["status"] == "resolved"
  81. assert result["statusDetails"] == {"inNextRelease": True, "actor": None}
  82. def test_resolved_in_release(self):
  83. release = self.create_release(project=self.project, version="a")
  84. user = self.create_user()
  85. group = self.create_group(status=GroupStatus.RESOLVED)
  86. GroupResolution.objects.create(
  87. group=group, release=release, type=GroupResolution.Type.in_release
  88. )
  89. result = serialize(group, user, serializer=GroupSerializerSnuba())
  90. assert result["status"] == "resolved"
  91. assert result["statusDetails"] == {"inRelease": "a", "actor": None}
  92. def test_resolved_with_actor(self):
  93. release = self.create_release(project=self.project, version="a")
  94. user = self.create_user()
  95. group = self.create_group(status=GroupStatus.RESOLVED)
  96. GroupResolution.objects.create(
  97. group=group, release=release, type=GroupResolution.Type.in_release, actor_id=user.id
  98. )
  99. result = serialize(group, user, serializer=GroupSerializerSnuba())
  100. assert result["status"] == "resolved"
  101. assert result["statusDetails"]["actor"]["id"] == str(user.id)
  102. def test_resolved_in_commit(self):
  103. repo = self.create_repo(project=self.project)
  104. commit = self.create_commit(repo=repo)
  105. user = self.create_user()
  106. group = self.create_group(status=GroupStatus.RESOLVED)
  107. GroupLink.objects.create(
  108. group_id=group.id,
  109. project_id=group.project_id,
  110. linked_id=commit.id,
  111. linked_type=GroupLink.LinkedType.commit,
  112. relationship=GroupLink.Relationship.resolves,
  113. )
  114. result = serialize(group, user, serializer=GroupSerializerSnuba())
  115. assert result["status"] == "resolved"
  116. assert result["statusDetails"]["inCommit"]["id"] == commit.key
  117. @patch("sentry.models.Group.is_over_resolve_age")
  118. def test_auto_resolved(self, mock_is_over_resolve_age):
  119. mock_is_over_resolve_age.return_value = True
  120. user = self.create_user()
  121. group = self.create_group(status=GroupStatus.UNRESOLVED)
  122. result = serialize(group, user, serializer=GroupSerializerSnuba())
  123. assert result["status"] == "resolved"
  124. assert result["statusDetails"] == {"autoResolved": True}
  125. def test_subscribed(self):
  126. user = self.create_user()
  127. group = self.create_group()
  128. GroupSubscription.objects.create(
  129. user=user, group=group, project=group.project, is_active=True
  130. )
  131. result = serialize(group, user, serializer=GroupSerializerSnuba())
  132. assert result["isSubscribed"]
  133. assert result["subscriptionDetails"] == {"reason": "unknown"}
  134. def test_explicit_unsubscribed(self):
  135. user = self.create_user()
  136. group = self.create_group()
  137. GroupSubscription.objects.create(
  138. user=user, group=group, project=group.project, is_active=False
  139. )
  140. result = serialize(group, user, serializer=GroupSerializerSnuba())
  141. assert not result["isSubscribed"]
  142. assert not result["subscriptionDetails"]
  143. def test_implicit_subscribed(self):
  144. user = self.create_user()
  145. group = self.create_group()
  146. combinations = (
  147. # (default, project, subscribed, has_details)
  148. (
  149. NotificationSettingOptionValues.ALWAYS,
  150. NotificationSettingOptionValues.DEFAULT,
  151. True,
  152. False,
  153. ),
  154. (
  155. NotificationSettingOptionValues.ALWAYS,
  156. NotificationSettingOptionValues.ALWAYS,
  157. True,
  158. False,
  159. ),
  160. (
  161. NotificationSettingOptionValues.ALWAYS,
  162. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  163. False,
  164. False,
  165. ),
  166. (
  167. NotificationSettingOptionValues.ALWAYS,
  168. NotificationSettingOptionValues.NEVER,
  169. False,
  170. True,
  171. ),
  172. (
  173. NotificationSettingOptionValues.DEFAULT,
  174. NotificationSettingOptionValues.DEFAULT,
  175. False,
  176. False,
  177. ),
  178. (
  179. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  180. NotificationSettingOptionValues.DEFAULT,
  181. False,
  182. False,
  183. ),
  184. (
  185. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  186. NotificationSettingOptionValues.ALWAYS,
  187. True,
  188. False,
  189. ),
  190. (
  191. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  192. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  193. False,
  194. False,
  195. ),
  196. (
  197. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  198. NotificationSettingOptionValues.NEVER,
  199. False,
  200. True,
  201. ),
  202. (
  203. NotificationSettingOptionValues.NEVER,
  204. NotificationSettingOptionValues.DEFAULT,
  205. False,
  206. True,
  207. ),
  208. (
  209. NotificationSettingOptionValues.NEVER,
  210. NotificationSettingOptionValues.ALWAYS,
  211. True,
  212. False,
  213. ),
  214. (
  215. NotificationSettingOptionValues.NEVER,
  216. NotificationSettingOptionValues.SUBSCRIBE_ONLY,
  217. False,
  218. False,
  219. ),
  220. (
  221. NotificationSettingOptionValues.NEVER,
  222. NotificationSettingOptionValues.NEVER,
  223. False,
  224. True,
  225. ),
  226. )
  227. for default_value, project_value, is_subscribed, has_details in combinations:
  228. UserOption.objects.clear_local_cache()
  229. NotificationSetting.objects.update_settings(
  230. ExternalProviders.EMAIL,
  231. NotificationSettingTypes.WORKFLOW,
  232. default_value,
  233. user=user,
  234. )
  235. NotificationSetting.objects.update_settings(
  236. ExternalProviders.EMAIL,
  237. NotificationSettingTypes.WORKFLOW,
  238. project_value,
  239. user=user,
  240. project=group.project,
  241. )
  242. NotificationSetting.objects.update_settings(
  243. ExternalProviders.SLACK,
  244. NotificationSettingTypes.WORKFLOW,
  245. default_value,
  246. user=user,
  247. )
  248. NotificationSetting.objects.update_settings(
  249. ExternalProviders.SLACK,
  250. NotificationSettingTypes.WORKFLOW,
  251. project_value,
  252. user=user,
  253. project=group.project,
  254. )
  255. result = serialize(group, user, serializer=GroupSerializerSnuba())
  256. subscription_details = result.get("subscriptionDetails")
  257. assert result["isSubscribed"] is is_subscribed
  258. assert (
  259. subscription_details == {"disabled": True}
  260. if has_details
  261. else subscription_details is None
  262. )
  263. def test_global_no_conversations_overrides_group_subscription(self):
  264. user = self.create_user()
  265. group = self.create_group()
  266. GroupSubscription.objects.create(
  267. user=user, group=group, project=group.project, is_active=True
  268. )
  269. for provider in [ExternalProviders.EMAIL, ExternalProviders.SLACK]:
  270. NotificationSetting.objects.update_settings(
  271. provider,
  272. NotificationSettingTypes.WORKFLOW,
  273. NotificationSettingOptionValues.NEVER,
  274. user=user,
  275. )
  276. result = serialize(group, user, serializer=GroupSerializerSnuba())
  277. assert not result["isSubscribed"]
  278. assert result["subscriptionDetails"] == {"disabled": True}
  279. def test_project_no_conversations_overrides_group_subscription(self):
  280. user = self.create_user()
  281. group = self.create_group()
  282. GroupSubscription.objects.create(
  283. user=user, group=group, project=group.project, is_active=True
  284. )
  285. for provider in [ExternalProviders.EMAIL, ExternalProviders.SLACK]:
  286. NotificationSetting.objects.update_settings(
  287. provider,
  288. NotificationSettingTypes.WORKFLOW,
  289. NotificationSettingOptionValues.NEVER,
  290. user=user,
  291. project=group.project,
  292. )
  293. result = serialize(group, user, serializer=GroupSerializerSnuba())
  294. assert not result["isSubscribed"]
  295. assert result["subscriptionDetails"] == {"disabled": True}
  296. def test_no_user_unsubscribed(self):
  297. group = self.create_group()
  298. result = serialize(group, serializer=GroupSerializerSnuba())
  299. assert not result["isSubscribed"]
  300. def test_seen_stats(self):
  301. environment = self.create_environment(project=self.project)
  302. environment2 = self.create_environment(project=self.project)
  303. events = []
  304. for event_id, env, user_id, timestamp in [
  305. ("a" * 32, environment, 1, iso_format(self.min_ago)),
  306. ("b" * 32, environment, 2, iso_format(self.min_ago)),
  307. ("c" * 32, environment2, 3, iso_format(self.week_ago)),
  308. ]:
  309. events.append(
  310. self.store_event(
  311. data={
  312. "event_id": event_id,
  313. "fingerprint": ["put-me-in-group1"],
  314. "timestamp": timestamp,
  315. "environment": env.name,
  316. "user": {"id": user_id},
  317. },
  318. project_id=self.project.id,
  319. )
  320. )
  321. # Assert all events are in the same group
  322. (group_id,) = {e.group.id for e in events}
  323. group = Group.objects.get(id=group_id)
  324. group.times_seen = 3
  325. group.first_seen = self.week_ago - timedelta(days=5)
  326. group.last_seen = self.week_ago
  327. group.save()
  328. # should use group columns when no environments arg passed
  329. result = serialize(group, serializer=GroupSerializerSnuba(environment_ids=[]))
  330. assert result["count"] == "3"
  331. assert iso_format(result["lastSeen"]) == iso_format(self.min_ago)
  332. assert result["firstSeen"] == group.first_seen
  333. # update this to something different to make sure it's being used
  334. group_env = GroupEnvironment.objects.get(group_id=group_id, environment_id=environment.id)
  335. group_env.first_seen = self.day_ago - timedelta(days=3)
  336. group_env.save()
  337. group_env2 = GroupEnvironment.objects.get(group_id=group_id, environment_id=environment2.id)
  338. result = serialize(
  339. group,
  340. serializer=GroupSerializerSnuba(environment_ids=[environment.id, environment2.id]),
  341. )
  342. assert result["count"] == "3"
  343. # result is rounded down to nearest second
  344. assert iso_format(result["lastSeen"]) == iso_format(self.min_ago)
  345. assert iso_format(result["firstSeen"]) == iso_format(group_env.first_seen)
  346. assert iso_format(group_env2.first_seen) > iso_format(group_env.first_seen)
  347. assert result["userCount"] == 3
  348. result = serialize(
  349. group,
  350. serializer=GroupSerializerSnuba(
  351. environment_ids=[environment.id, environment2.id],
  352. start=self.week_ago - timedelta(hours=1),
  353. end=self.week_ago + timedelta(hours=1),
  354. ),
  355. )
  356. assert result["userCount"] == 1
  357. assert iso_format(result["lastSeen"]) == iso_format(self.week_ago)
  358. assert iso_format(result["firstSeen"]) == iso_format(self.week_ago)
  359. assert result["count"] == "1"
  360. def test_get_start_from_seen_stats(self):
  361. for days, expected in [(None, 30), (0, 14), (1000, 90)]:
  362. last_seen = None if days is None else before_now(days=days).replace(tzinfo=pytz.UTC)
  363. start = GroupSerializerSnuba._get_start_from_seen_stats({"": {"last_seen": last_seen}})
  364. assert iso_format(start) == iso_format(before_now(days=expected))
  365. @region_silo_test
  366. class PerformanceGroupSerializerSnubaTest(
  367. APITestCase,
  368. SnubaTestCase,
  369. PerfIssueTransactionTestMixin,
  370. ):
  371. def test_perf_seen_stats(self):
  372. proj = self.create_project()
  373. environment = self.create_environment(project=proj)
  374. first_group_fingerprint = f"{GroupType.PERFORMANCE_RENDER_BLOCKING_ASSET_SPAN.value}-group1"
  375. timestamp = timezone.now() - timedelta(days=5)
  376. times = 5
  377. for _ in range(0, times):
  378. self.store_transaction(
  379. proj.id,
  380. "user1",
  381. [first_group_fingerprint],
  382. environment.name,
  383. timestamp=timestamp + timedelta(minutes=1),
  384. )
  385. event = self.store_transaction(
  386. proj.id,
  387. "user2",
  388. [first_group_fingerprint],
  389. environment.name,
  390. timestamp=timestamp + timedelta(minutes=2),
  391. )
  392. first_group = event.groups[0]
  393. result = serialize(
  394. first_group,
  395. serializer=GroupSerializerSnuba(
  396. environment_ids=[environment.id],
  397. start=timestamp - timedelta(hours=1),
  398. end=timestamp + timedelta(hours=1),
  399. ),
  400. )
  401. assert result["userCount"] == 2
  402. assert iso_format(result["lastSeen"]) == iso_format(timestamp + timedelta(minutes=2))
  403. assert iso_format(result["firstSeen"]) == iso_format(timestamp + timedelta(minutes=1))
  404. assert result["count"] == str(times + 1)
  405. @region_silo_test
  406. class ProfilingGroupSerializerSnubaTest(
  407. APITestCase,
  408. SnubaTestCase,
  409. SearchIssueTestMixin,
  410. ):
  411. def test_profiling_seen_stats(self):
  412. proj = self.create_project()
  413. environment = self.create_environment(project=proj)
  414. first_group_fingerprint = f"{GroupType.PROFILE_BLOCKED_THREAD.value}-group1"
  415. timestamp = timezone.now().replace(hour=0, minute=0, second=0)
  416. times = 5
  417. for incr in range(0, times):
  418. # for user_0 - user_4, first_group
  419. self.store_search_issue(
  420. proj.id,
  421. incr,
  422. [first_group_fingerprint],
  423. environment.name,
  424. timestamp + timedelta(minutes=incr),
  425. )
  426. # user_5, another_group
  427. event, issue_occurrence, group_info = self.store_search_issue(
  428. proj.id,
  429. 5,
  430. [first_group_fingerprint],
  431. environment.name,
  432. timestamp + timedelta(minutes=5),
  433. )
  434. first_group = group_info.group
  435. result = serialize(
  436. first_group,
  437. serializer=GroupSerializerSnuba(
  438. environment_ids=[environment.id],
  439. start=timestamp - timedelta(days=1),
  440. end=timestamp + timedelta(days=1),
  441. ),
  442. )
  443. assert result["userCount"] == 6
  444. assert iso_format(result["lastSeen"]) == iso_format(timestamp + timedelta(minutes=5))
  445. assert iso_format(result["firstSeen"]) == iso_format(timestamp)
  446. assert result["count"] == str(times + 1)