test_group_stream.py 9.4 KB

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