test_group.py 21 KB

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