test_group_stream.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import time
  2. from datetime import timedelta, timezone
  3. from unittest import mock
  4. from django.utils import timezone as django_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 snuba_tsdb
  8. from sentry.api.serializers.models.group_stream import StreamGroupSerializerSnuba
  9. from sentry.models.environment import Environment
  10. from sentry.testutils.cases import APITestCase, SnubaTestCase
  11. from sentry.testutils.helpers.datetime import before_now, iso_format
  12. from sentry.testutils.silo import region_silo_test
  13. from sentry.utils.cache import cache
  14. from sentry.utils.hashlib import hash_values
  15. @region_silo_test
  16. class StreamGroupSerializerTestCase(APITestCase, SnubaTestCase):
  17. def test_environment(self):
  18. group = self.group
  19. organization_id = group.project.organization_id
  20. environment = Environment.get_or_create(group.project, "production")
  21. with mock.patch(
  22. "sentry.api.serializers.models.group_stream.snuba_tsdb.get_range",
  23. side_effect=snuba_tsdb.get_range,
  24. ) as get_range:
  25. serialize(
  26. [group],
  27. serializer=StreamGroupSerializerSnuba(
  28. environment_ids=[environment.id],
  29. stats_period="14d",
  30. organization_id=organization_id,
  31. ),
  32. )
  33. assert get_range.call_count == 1
  34. for args, kwargs in get_range.call_args_list:
  35. assert kwargs["environment_ids"] == [environment.id]
  36. with mock.patch(
  37. "sentry.api.serializers.models.group.snuba_tsdb.get_range",
  38. side_effect=snuba_tsdb.get_range,
  39. ) as get_range:
  40. serialize(
  41. [group],
  42. serializer=StreamGroupSerializerSnuba(
  43. environment_ids=None, stats_period="14d", organization_id=organization_id
  44. ),
  45. )
  46. assert get_range.call_count == 1
  47. for args, kwargs in get_range.call_args_list:
  48. assert kwargs["environment_ids"] is None
  49. def test_session_count(self):
  50. group = self.group
  51. organization_id = group.project.organization_id
  52. environment = Environment.get_or_create(group.project, "prod")
  53. dev_environment = Environment.get_or_create(group.project, "dev")
  54. no_sessions_environment = Environment.get_or_create(group.project, "no_sessions")
  55. self.received = time.time()
  56. self.session_started = time.time() // 60 * 60
  57. self.session_release = "foo@1.0.0"
  58. self.session_crashed_release = "foo@2.0.0"
  59. self.store_session(
  60. {
  61. "session_id": "5d52fd05-fcc9-4bf3-9dc9-267783670341",
  62. "distinct_id": "39887d89-13b2-4c84-8c23-5d13d2102667",
  63. "status": "ok",
  64. "seq": 0,
  65. "release": self.session_release,
  66. "environment": "dev",
  67. "retention_days": 90,
  68. "org_id": self.project.organization_id,
  69. "project_id": self.project.id,
  70. "duration": 1,
  71. "errors": 0,
  72. "started": self.session_started - 120,
  73. "received": self.received - 120,
  74. }
  75. )
  76. self.store_session(
  77. {
  78. "session_id": "5e910c1a-6941-460e-9843-24103fb6a63c",
  79. "distinct_id": "39887d89-13b2-4c84-8c23-5d13d2102668",
  80. "status": "ok",
  81. "seq": 0,
  82. "release": self.session_release,
  83. "environment": "prod",
  84. "retention_days": 90,
  85. "org_id": self.project.organization_id,
  86. "project_id": self.project.id,
  87. "duration": 60.0,
  88. "errors": 0,
  89. "started": self.session_started - 240,
  90. "received": self.received - 240,
  91. }
  92. )
  93. self.store_session(
  94. {
  95. "session_id": "5e910c1a-6941-460e-9843-24103fb6a63c",
  96. "distinct_id": "39887d89-13b2-4c84-8c23-5d13d2102669",
  97. "status": "exited",
  98. "seq": 1,
  99. "release": self.session_release,
  100. "environment": "prod",
  101. "retention_days": 90,
  102. "org_id": self.project.organization_id,
  103. "project_id": self.project.id,
  104. "duration": 30.0,
  105. "errors": 0,
  106. "started": self.session_started,
  107. "received": self.received,
  108. }
  109. )
  110. self.store_session(
  111. {
  112. "session_id": "a148c0c5-06a2-423b-8901-6b43b812cf82",
  113. "distinct_id": "39887d89-13b2-4c84-8c23-5d13d2102660",
  114. "status": "crashed",
  115. "seq": 0,
  116. "release": self.session_crashed_release,
  117. "environment": "prod",
  118. "retention_days": 90,
  119. "org_id": self.project.organization_id,
  120. "project_id": self.project.id,
  121. "duration": 60.0,
  122. "errors": 0,
  123. "started": self.session_started,
  124. "received": self.received,
  125. }
  126. )
  127. result = serialize(
  128. [group],
  129. serializer=StreamGroupSerializerSnuba(
  130. stats_period="14d", organization_id=organization_id
  131. ),
  132. )
  133. assert "sessionCount" not in result[0]
  134. result = serialize(
  135. [group],
  136. serializer=StreamGroupSerializerSnuba(
  137. stats_period="14d", expand=["sessions"], organization_id=organization_id
  138. ),
  139. )
  140. assert result[0]["sessionCount"] == 3
  141. result = serialize(
  142. [group],
  143. serializer=StreamGroupSerializerSnuba(
  144. environment_ids=[environment.id],
  145. stats_period="14d",
  146. expand=["sessions"],
  147. organization_id=organization_id,
  148. ),
  149. )
  150. assert result[0]["sessionCount"] == 2
  151. result = serialize(
  152. [group],
  153. serializer=StreamGroupSerializerSnuba(
  154. environment_ids=[no_sessions_environment.id],
  155. stats_period="14d",
  156. expand=["sessions"],
  157. organization_id=organization_id,
  158. ),
  159. )
  160. assert result[0]["sessionCount"] is None
  161. result = serialize(
  162. [group],
  163. serializer=StreamGroupSerializerSnuba(
  164. environment_ids=[dev_environment.id],
  165. stats_period="14d",
  166. expand=["sessions"],
  167. organization_id=organization_id,
  168. ),
  169. )
  170. assert result[0]["sessionCount"] == 1
  171. self.store_session(
  172. {
  173. "session_id": "a148c0c5-06a2-423b-8901-6b43b812cf83",
  174. "distinct_id": "39887d89-13b2-4c84-8c23-5d13d2102627",
  175. "status": "ok",
  176. "seq": 0,
  177. "release": self.session_release,
  178. "environment": "dev",
  179. "retention_days": 90,
  180. "org_id": self.project.organization_id,
  181. "project_id": self.project.id,
  182. "duration": 60.0,
  183. "errors": 0,
  184. "started": self.session_started - 1590061, # approximately 18 days
  185. "received": self.received - 1590061, # approximately 18 days
  186. }
  187. )
  188. result = serialize(
  189. [group],
  190. serializer=StreamGroupSerializerSnuba(
  191. environment_ids=[dev_environment.id],
  192. stats_period="14d",
  193. expand=["sessions"],
  194. start=django_timezone.now() - timedelta(days=30),
  195. end=django_timezone.now() - timedelta(days=15),
  196. organization_id=organization_id,
  197. ),
  198. )
  199. assert result[0]["sessionCount"] == 1
  200. # Delete the cache from the query we did above, else this result comes back as 1 instead of 0.5
  201. key_hash = hash_values([group.project.id, "", "", f"{dev_environment.id}"])
  202. cache.delete(f"w-s:{key_hash}")
  203. project2 = self.create_project(
  204. organization=self.organization, teams=[self.team], name="Another project"
  205. )
  206. data = {
  207. "fingerprint": ["meow"],
  208. "timestamp": iso_format(django_timezone.now()),
  209. "type": "error",
  210. "exception": [{"type": "Foo"}],
  211. }
  212. event = self.store_event(data=data, project_id=project2.id)
  213. self.store_event(data=data, project_id=project2.id)
  214. self.store_event(data=data, project_id=project2.id)
  215. result = serialize(
  216. [group, event.group],
  217. serializer=StreamGroupSerializerSnuba(
  218. environment_ids=[dev_environment.id],
  219. stats_period="14d",
  220. expand=["sessions"],
  221. organization_id=organization_id,
  222. ),
  223. )
  224. assert result[0]["sessionCount"] == 2
  225. # No sessions in project2
  226. assert result[1]["sessionCount"] is None
  227. def test_skipped_date_timestamp_filters(self):
  228. group = self.create_group()
  229. serializer = StreamGroupSerializerSnuba(
  230. search_filters=[
  231. SearchFilter(
  232. SearchKey("timestamp"),
  233. ">",
  234. SearchValue(before_now(hours=1).replace(tzinfo=timezone.utc)),
  235. ),
  236. SearchFilter(
  237. SearchKey("timestamp"),
  238. "<",
  239. SearchValue(before_now(seconds=1).replace(tzinfo=timezone.utc)),
  240. ),
  241. SearchFilter(
  242. SearchKey("date"),
  243. ">",
  244. SearchValue(before_now(hours=1).replace(tzinfo=timezone.utc)),
  245. ),
  246. SearchFilter(
  247. SearchKey("date"),
  248. "<",
  249. SearchValue(before_now(seconds=1).replace(tzinfo=timezone.utc)),
  250. ),
  251. ]
  252. )
  253. assert not serializer.conditions
  254. result = serialize([group], self.user, serializer=serializer)
  255. assert result[0]["id"] == str(group.id)