test_organization_events_stats.py 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. import uuid
  2. from datetime import timedelta
  3. from unittest import mock
  4. from uuid import uuid4
  5. import pytest
  6. from dateutil.parser import parse as parse_date
  7. from django.urls import reverse
  8. from pytz import utc
  9. from snuba_sdk.column import Column
  10. from snuba_sdk.conditions import Condition, Op
  11. from snuba_sdk.function import Function
  12. from sentry.constants import MAX_TOP_EVENTS
  13. from sentry.issues.grouptype import ProfileBlockedThreadGroupType
  14. from sentry.models.transaction_threshold import ProjectTransactionThreshold, TransactionMetric
  15. from sentry.snuba.discover import OTHER_KEY
  16. from sentry.testutils import APITestCase, SnubaTestCase
  17. from sentry.testutils.helpers.datetime import before_now, iso_format
  18. from sentry.testutils.silo import region_silo_test
  19. from sentry.utils.samples import load_data
  20. from tests.sentry.issues.test_utils import SearchIssueTestMixin
  21. pytestmark = pytest.mark.sentry_metrics
  22. @region_silo_test
  23. class OrganizationEventsStatsEndpointTest(APITestCase, SnubaTestCase, SearchIssueTestMixin):
  24. endpoint = "sentry-api-0-organization-events-stats"
  25. def setUp(self):
  26. super().setUp()
  27. self.login_as(user=self.user)
  28. self.authed_user = self.user
  29. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  30. self.project = self.create_project()
  31. self.project2 = self.create_project()
  32. self.user = self.create_user()
  33. self.user2 = self.create_user()
  34. self.store_event(
  35. data={
  36. "event_id": "a" * 32,
  37. "message": "very bad",
  38. "timestamp": iso_format(self.day_ago + timedelta(minutes=1)),
  39. "fingerprint": ["group1"],
  40. "tags": {"sentry:user": self.user.email},
  41. },
  42. project_id=self.project.id,
  43. )
  44. self.store_event(
  45. data={
  46. "event_id": "b" * 32,
  47. "message": "oh my",
  48. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=1)),
  49. "fingerprint": ["group2"],
  50. "tags": {"sentry:user": self.user2.email},
  51. },
  52. project_id=self.project2.id,
  53. )
  54. self.store_event(
  55. data={
  56. "event_id": "c" * 32,
  57. "message": "very bad",
  58. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=2)),
  59. "fingerprint": ["group2"],
  60. "tags": {"sentry:user": self.user2.email},
  61. },
  62. project_id=self.project2.id,
  63. )
  64. self.url = reverse(
  65. "sentry-api-0-organization-events-stats",
  66. kwargs={"organization_slug": self.project.organization.slug},
  67. )
  68. self.features = {}
  69. def do_request(self, data, url=None, features=None):
  70. if features is None:
  71. features = {"organizations:discover-basic": True}
  72. features.update(self.features)
  73. with self.feature(features):
  74. return self.client.get(self.url if url is None else url, data=data, format="json")
  75. def test_simple(self):
  76. response = self.do_request(
  77. {
  78. "start": iso_format(self.day_ago),
  79. "end": iso_format(self.day_ago + timedelta(hours=2)),
  80. "interval": "1h",
  81. },
  82. )
  83. assert response.status_code == 200, response.content
  84. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  85. def test_generic_issue(self):
  86. _, _, group_info = self.store_search_issue(
  87. self.project.id,
  88. self.user.id,
  89. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  90. "prod",
  91. self.day_ago.replace(tzinfo=utc),
  92. )
  93. self.store_search_issue(
  94. self.project.id,
  95. self.user.id,
  96. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  97. "prod",
  98. self.day_ago.replace(tzinfo=utc) + timedelta(hours=1, minutes=1),
  99. )
  100. self.store_search_issue(
  101. self.project.id,
  102. self.user.id,
  103. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  104. "prod",
  105. self.day_ago.replace(tzinfo=utc) + timedelta(hours=1, minutes=2),
  106. )
  107. with self.feature(
  108. [
  109. "organizations:profiling",
  110. ]
  111. ):
  112. response = self.do_request(
  113. {
  114. "start": iso_format(self.day_ago),
  115. "end": iso_format(self.day_ago + timedelta(hours=2)),
  116. "interval": "1h",
  117. "query": f"issue:{group_info.group.qualified_short_id}",
  118. "dataset": "issuePlatform",
  119. },
  120. )
  121. assert response.status_code == 200, response.content
  122. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  123. def test_generic_issue_calculated_interval(self):
  124. """Test that a 4h interval returns the correct generic event stats.
  125. This follows a different code path than 1h or 1d as the IssuePlatformTimeSeriesQueryBuilder
  126. does some calculation to create the time column."""
  127. _, _, group_info = self.store_search_issue(
  128. self.project.id,
  129. self.user.id,
  130. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  131. "prod",
  132. self.day_ago.replace(tzinfo=utc) + timedelta(minutes=1),
  133. )
  134. self.store_search_issue(
  135. self.project.id,
  136. self.user.id,
  137. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  138. "prod",
  139. self.day_ago.replace(tzinfo=utc) + timedelta(minutes=1),
  140. )
  141. self.store_search_issue(
  142. self.project.id,
  143. self.user.id,
  144. [f"{ProfileBlockedThreadGroupType.type_id}-group1"],
  145. "prod",
  146. self.day_ago.replace(tzinfo=utc) + timedelta(minutes=2),
  147. )
  148. with self.feature(
  149. [
  150. "organizations:profiling",
  151. ]
  152. ):
  153. response = self.do_request(
  154. {
  155. "start": iso_format(self.day_ago),
  156. "end": iso_format(self.day_ago + timedelta(hours=4)),
  157. "interval": "4h",
  158. "query": f"issue:{group_info.group.qualified_short_id}",
  159. "dataset": "issuePlatform",
  160. },
  161. )
  162. assert response.status_code == 200, response.content
  163. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 3}], [{"count": 0}]]
  164. def test_misaligned_last_bucket(self):
  165. response = self.do_request(
  166. data={
  167. "start": iso_format(self.day_ago - timedelta(minutes=30)),
  168. "end": iso_format(self.day_ago + timedelta(hours=1, minutes=30)),
  169. "interval": "1h",
  170. "partial": "1",
  171. },
  172. )
  173. assert response.status_code == 200, response.content
  174. assert [attrs for time, attrs in response.data["data"]] == [
  175. [{"count": 0}],
  176. [{"count": 1}],
  177. [{"count": 2}],
  178. ]
  179. def test_no_projects(self):
  180. org = self.create_organization(owner=self.user)
  181. self.login_as(user=self.user)
  182. url = reverse(
  183. "sentry-api-0-organization-events-stats", kwargs={"organization_slug": org.slug}
  184. )
  185. response = self.do_request({}, url)
  186. assert response.status_code == 200, response.content
  187. assert len(response.data["data"]) == 0
  188. def test_user_count(self):
  189. self.store_event(
  190. data={
  191. "event_id": "d" * 32,
  192. "message": "something",
  193. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  194. "tags": {"sentry:user": self.user2.email},
  195. "fingerprint": ["group2"],
  196. },
  197. project_id=self.project2.id,
  198. )
  199. response = self.do_request(
  200. data={
  201. "start": iso_format(self.day_ago),
  202. "end": iso_format(self.day_ago + timedelta(hours=2)),
  203. "interval": "1h",
  204. "yAxis": "user_count",
  205. },
  206. )
  207. assert response.status_code == 200, response.content
  208. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 2}], [{"count": 1}]]
  209. def test_discover2_backwards_compatibility(self):
  210. response = self.do_request(
  211. data={
  212. "project": self.project.id,
  213. "start": iso_format(self.day_ago),
  214. "end": iso_format(self.day_ago + timedelta(hours=2)),
  215. "interval": "1h",
  216. "yAxis": "user_count",
  217. },
  218. )
  219. assert response.status_code == 200, response.content
  220. assert len(response.data["data"]) > 0
  221. response = self.do_request(
  222. data={
  223. "project": self.project.id,
  224. "start": iso_format(self.day_ago),
  225. "end": iso_format(self.day_ago + timedelta(hours=2)),
  226. "interval": "1h",
  227. "yAxis": "event_count",
  228. },
  229. )
  230. assert response.status_code == 200, response.content
  231. assert len(response.data["data"]) > 0
  232. def test_with_event_count_flag(self):
  233. response = self.do_request(
  234. data={
  235. "start": iso_format(self.day_ago),
  236. "end": iso_format(self.day_ago + timedelta(hours=2)),
  237. "interval": "1h",
  238. "yAxis": "event_count",
  239. },
  240. )
  241. assert response.status_code == 200, response.content
  242. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  243. def test_performance_view_feature(self):
  244. response = self.do_request(
  245. data={
  246. "end": iso_format(before_now()),
  247. "start": iso_format(before_now(hours=2)),
  248. "query": "project_id:1",
  249. "interval": "30m",
  250. "yAxis": "count()",
  251. },
  252. features={
  253. "organizations:performance-view": True,
  254. "organizations:discover-basic": False,
  255. },
  256. )
  257. assert response.status_code == 200, response.content
  258. def test_apdex_divide_by_zero(self):
  259. ProjectTransactionThreshold.objects.create(
  260. project=self.project,
  261. organization=self.project.organization,
  262. threshold=600,
  263. metric=TransactionMetric.LCP.value,
  264. )
  265. # Shouldn't count towards apdex
  266. data = load_data(
  267. "transaction",
  268. start_timestamp=self.day_ago + timedelta(minutes=(1)),
  269. timestamp=self.day_ago + timedelta(minutes=(3)),
  270. )
  271. data["transaction"] = "/apdex/new/"
  272. data["user"] = {"email": "1@example.com"}
  273. data["measurements"] = {}
  274. self.store_event(data, project_id=self.project.id)
  275. response = self.do_request(
  276. data={
  277. "start": iso_format(self.day_ago),
  278. "end": iso_format(self.day_ago + timedelta(hours=2)),
  279. "interval": "1h",
  280. "yAxis": "apdex()",
  281. "project": [self.project.id],
  282. },
  283. )
  284. assert response.status_code == 200, response.content
  285. assert len(response.data["data"]) == 2
  286. data = response.data["data"]
  287. # 0 transactions with LCP 0/0
  288. assert [attrs for time, attrs in response.data["data"]] == [
  289. [{"count": 0}],
  290. [{"count": 0}],
  291. ]
  292. def test_aggregate_function_apdex(self):
  293. project1 = self.create_project()
  294. project2 = self.create_project()
  295. events = [
  296. ("one", 400, project1.id),
  297. ("one", 400, project1.id),
  298. ("two", 3000, project2.id),
  299. ("two", 1000, project2.id),
  300. ("three", 3000, project2.id),
  301. ]
  302. for idx, event in enumerate(events):
  303. data = load_data(
  304. "transaction",
  305. start_timestamp=self.day_ago + timedelta(minutes=(1 + idx)),
  306. timestamp=self.day_ago + timedelta(minutes=(1 + idx), milliseconds=event[1]),
  307. )
  308. data["event_id"] = f"{idx}" * 32
  309. data["transaction"] = f"/apdex/new/{event[0]}"
  310. data["user"] = {"email": f"{idx}@example.com"}
  311. self.store_event(data, project_id=event[2])
  312. response = self.do_request(
  313. data={
  314. "start": iso_format(self.day_ago),
  315. "end": iso_format(self.day_ago + timedelta(hours=2)),
  316. "interval": "1h",
  317. "yAxis": "apdex()",
  318. },
  319. )
  320. assert response.status_code == 200, response.content
  321. assert [attrs for time, attrs in response.data["data"]] == [
  322. [{"count": 0.3}],
  323. [{"count": 0}],
  324. ]
  325. ProjectTransactionThreshold.objects.create(
  326. project=project1,
  327. organization=project1.organization,
  328. threshold=100,
  329. metric=TransactionMetric.DURATION.value,
  330. )
  331. ProjectTransactionThreshold.objects.create(
  332. project=project2,
  333. organization=project1.organization,
  334. threshold=100,
  335. metric=TransactionMetric.DURATION.value,
  336. )
  337. response = self.do_request(
  338. data={
  339. "start": iso_format(self.day_ago),
  340. "end": iso_format(self.day_ago + timedelta(hours=2)),
  341. "interval": "1h",
  342. "yAxis": "apdex()",
  343. },
  344. )
  345. assert response.status_code == 200, response.content
  346. assert [attrs for time, attrs in response.data["data"]] == [
  347. [{"count": 0.2}],
  348. [{"count": 0}],
  349. ]
  350. response = self.do_request(
  351. data={
  352. "start": iso_format(self.day_ago),
  353. "end": iso_format(self.day_ago + timedelta(hours=2)),
  354. "interval": "1h",
  355. "yAxis": ["user_count", "apdex()"],
  356. },
  357. )
  358. assert response.status_code == 200, response.content
  359. assert response.data["user_count"]["order"] == 0
  360. assert [attrs for time, attrs in response.data["user_count"]["data"]] == [
  361. [{"count": 5}],
  362. [{"count": 0}],
  363. ]
  364. assert response.data["apdex()"]["order"] == 1
  365. assert [attrs for time, attrs in response.data["apdex()"]["data"]] == [
  366. [{"count": 0.2}],
  367. [{"count": 0}],
  368. ]
  369. def test_aggregate_function_count(self):
  370. response = self.do_request(
  371. data={
  372. "start": iso_format(self.day_ago),
  373. "end": iso_format(self.day_ago + timedelta(hours=2)),
  374. "interval": "1h",
  375. "yAxis": "count()",
  376. },
  377. )
  378. assert response.status_code == 200, response.content
  379. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  380. def test_invalid_aggregate(self):
  381. response = self.do_request(
  382. data={
  383. "start": iso_format(self.day_ago),
  384. "end": iso_format(self.day_ago + timedelta(hours=2)),
  385. "interval": "1h",
  386. "yAxis": "rubbish",
  387. },
  388. )
  389. assert response.status_code == 400, response.content
  390. def test_aggregate_function_user_count(self):
  391. response = self.do_request(
  392. data={
  393. "start": iso_format(self.day_ago),
  394. "end": iso_format(self.day_ago + timedelta(hours=2)),
  395. "interval": "1h",
  396. "yAxis": "count_unique(user)",
  397. },
  398. )
  399. assert response.status_code == 200, response.content
  400. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 1}]]
  401. def test_aggregate_invalid(self):
  402. response = self.do_request(
  403. data={
  404. "start": iso_format(self.day_ago),
  405. "end": iso_format(self.day_ago + timedelta(hours=2)),
  406. "interval": "1h",
  407. "yAxis": "nope(lol)",
  408. },
  409. )
  410. assert response.status_code == 400, response.content
  411. def test_throughput_epm_hour_rollup(self):
  412. project = self.create_project()
  413. # Each of these denotes how many events to create in each hour
  414. event_counts = [6, 0, 6, 3, 0, 3]
  415. for hour, count in enumerate(event_counts):
  416. for minute in range(count):
  417. self.store_event(
  418. data={
  419. "event_id": str(uuid.uuid1()),
  420. "message": "very bad",
  421. "timestamp": iso_format(
  422. self.day_ago + timedelta(hours=hour, minutes=minute)
  423. ),
  424. "fingerprint": ["group1"],
  425. "tags": {"sentry:user": self.user.email},
  426. },
  427. project_id=project.id,
  428. )
  429. for axis in ["epm()", "tpm()"]:
  430. response = self.do_request(
  431. data={
  432. "start": iso_format(self.day_ago),
  433. "end": iso_format(self.day_ago + timedelta(hours=6)),
  434. "interval": "1h",
  435. "yAxis": axis,
  436. "project": project.id,
  437. },
  438. )
  439. assert response.status_code == 200, response.content
  440. data = response.data["data"]
  441. assert len(data) == 6
  442. rows = data[0:6]
  443. for test in zip(event_counts, rows):
  444. assert test[1][1][0]["count"] == test[0] / (3600.0 / 60.0)
  445. def test_throughput_epm_day_rollup(self):
  446. project = self.create_project()
  447. # Each of these denotes how many events to create in each minute
  448. event_counts = [6, 0, 6, 3, 0, 3]
  449. for hour, count in enumerate(event_counts):
  450. for minute in range(count):
  451. self.store_event(
  452. data={
  453. "event_id": str(uuid.uuid1()),
  454. "message": "very bad",
  455. "timestamp": iso_format(
  456. self.day_ago + timedelta(hours=hour, minutes=minute)
  457. ),
  458. "fingerprint": ["group1"],
  459. "tags": {"sentry:user": self.user.email},
  460. },
  461. project_id=project.id,
  462. )
  463. for axis in ["epm()", "tpm()"]:
  464. response = self.do_request(
  465. data={
  466. "start": iso_format(self.day_ago),
  467. "end": iso_format(self.day_ago + timedelta(hours=24)),
  468. "interval": "24h",
  469. "yAxis": axis,
  470. "project": project.id,
  471. },
  472. )
  473. assert response.status_code == 200, response.content
  474. data = response.data["data"]
  475. assert len(data) == 2
  476. assert data[0][1][0]["count"] == sum(event_counts) / (86400.0 / 60.0)
  477. def test_throughput_eps_minute_rollup(self):
  478. project = self.create_project()
  479. # Each of these denotes how many events to create in each minute
  480. event_counts = [6, 0, 6, 3, 0, 3]
  481. for minute, count in enumerate(event_counts):
  482. for second in range(count):
  483. self.store_event(
  484. data={
  485. "event_id": str(uuid.uuid1()),
  486. "message": "very bad",
  487. "timestamp": iso_format(
  488. self.day_ago + timedelta(minutes=minute, seconds=second)
  489. ),
  490. "fingerprint": ["group1"],
  491. "tags": {"sentry:user": self.user.email},
  492. },
  493. project_id=project.id,
  494. )
  495. for axis in ["eps()", "tps()"]:
  496. response = self.do_request(
  497. data={
  498. "start": iso_format(self.day_ago),
  499. "end": iso_format(self.day_ago + timedelta(minutes=6)),
  500. "interval": "1m",
  501. "yAxis": axis,
  502. "project": project.id,
  503. },
  504. )
  505. assert response.status_code == 200, response.content
  506. data = response.data["data"]
  507. assert len(data) == 6
  508. rows = data[0:6]
  509. for test in zip(event_counts, rows):
  510. assert test[1][1][0]["count"] == test[0] / 60.0
  511. def test_throughput_eps_no_rollup(self):
  512. project = self.create_project()
  513. # Each of these denotes how many events to create in each minute
  514. event_counts = [6, 0, 6, 3, 0, 3]
  515. for minute, count in enumerate(event_counts):
  516. for second in range(count):
  517. self.store_event(
  518. data={
  519. "event_id": str(uuid.uuid1()),
  520. "message": "very bad",
  521. "timestamp": iso_format(
  522. self.day_ago + timedelta(minutes=minute, seconds=second)
  523. ),
  524. "fingerprint": ["group1"],
  525. "tags": {"sentry:user": self.user.email},
  526. },
  527. project_id=project.id,
  528. )
  529. response = self.do_request(
  530. data={
  531. "start": iso_format(self.day_ago),
  532. "end": iso_format(self.day_ago + timedelta(minutes=1)),
  533. "interval": "1s",
  534. "yAxis": "eps()",
  535. "project": project.id,
  536. },
  537. )
  538. assert response.status_code == 200, response.content
  539. data = response.data["data"]
  540. # expect 60 data points between time span of 0 and 60 seconds
  541. assert len(data) == 60
  542. rows = data[0:6]
  543. for row in rows:
  544. assert row[1][0]["count"] == 1
  545. def test_transaction_events(self):
  546. prototype = {
  547. "type": "transaction",
  548. "transaction": "api.issue.delete",
  549. "spans": [],
  550. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  551. "tags": {"important": "yes"},
  552. }
  553. fixtures = (
  554. ("d" * 32, before_now(minutes=32)),
  555. ("e" * 32, before_now(hours=1, minutes=2)),
  556. ("f" * 32, before_now(hours=1, minutes=35)),
  557. )
  558. for fixture in fixtures:
  559. data = prototype.copy()
  560. data["event_id"] = fixture[0]
  561. data["timestamp"] = iso_format(fixture[1])
  562. data["start_timestamp"] = iso_format(fixture[1] - timedelta(seconds=1))
  563. self.store_event(data=data, project_id=self.project.id)
  564. response = self.do_request(
  565. data={
  566. "project": self.project.id,
  567. "end": iso_format(before_now()),
  568. "start": iso_format(before_now(hours=2)),
  569. "query": "event.type:transaction",
  570. "interval": "30m",
  571. "yAxis": "count()",
  572. },
  573. )
  574. assert response.status_code == 200, response.content
  575. items = [item for time, item in response.data["data"] if item]
  576. # We could get more results depending on where the 30 min
  577. # windows land.
  578. assert len(items) >= 3
  579. def test_project_id_query_filter(self):
  580. response = self.do_request(
  581. data={
  582. "end": iso_format(before_now()),
  583. "start": iso_format(before_now(hours=2)),
  584. "query": "project_id:1",
  585. "interval": "30m",
  586. "yAxis": "count()",
  587. },
  588. )
  589. assert response.status_code == 200
  590. def test_latest_release_query_filter(self):
  591. response = self.do_request(
  592. data={
  593. "project": self.project.id,
  594. "end": iso_format(before_now()),
  595. "start": iso_format(before_now(hours=2)),
  596. "query": "release:latest",
  597. "interval": "30m",
  598. "yAxis": "count()",
  599. },
  600. )
  601. assert response.status_code == 200
  602. def test_conditional_filter(self):
  603. response = self.do_request(
  604. data={
  605. "start": iso_format(self.day_ago),
  606. "end": iso_format(self.day_ago + timedelta(hours=2)),
  607. "query": "id:{} OR id:{}".format("a" * 32, "b" * 32),
  608. "interval": "30m",
  609. "yAxis": "count()",
  610. },
  611. )
  612. assert response.status_code == 200, response.content
  613. data = response.data["data"]
  614. assert len(data) == 4
  615. assert data[0][1][0]["count"] == 1
  616. assert data[2][1][0]["count"] == 1
  617. def test_simple_multiple_yaxis(self):
  618. response = self.do_request(
  619. data={
  620. "start": iso_format(self.day_ago),
  621. "end": iso_format(self.day_ago + timedelta(hours=2)),
  622. "interval": "1h",
  623. "yAxis": ["user_count", "event_count"],
  624. },
  625. )
  626. assert response.status_code == 200, response.content
  627. assert response.data["user_count"]["order"] == 0
  628. assert [attrs for time, attrs in response.data["user_count"]["data"]] == [
  629. [{"count": 1}],
  630. [{"count": 1}],
  631. ]
  632. assert response.data["event_count"]["order"] == 1
  633. assert [attrs for time, attrs in response.data["event_count"]["data"]] == [
  634. [{"count": 1}],
  635. [{"count": 2}],
  636. ]
  637. def test_equation_yaxis(self):
  638. response = self.do_request(
  639. data={
  640. "start": iso_format(self.day_ago),
  641. "end": iso_format(self.day_ago + timedelta(hours=2)),
  642. "interval": "1h",
  643. "yAxis": ["equation|count() / 100"],
  644. },
  645. )
  646. assert response.status_code == 200, response.content
  647. assert len(response.data["data"]) == 2
  648. assert [attrs for time, attrs in response.data["data"]] == [
  649. [{"count": 0.01}],
  650. [{"count": 0.02}],
  651. ]
  652. def test_equation_mixed_multi_yaxis(self):
  653. response = self.do_request(
  654. data={
  655. "start": iso_format(self.day_ago),
  656. "end": iso_format(self.day_ago + timedelta(hours=2)),
  657. "interval": "1h",
  658. "yAxis": ["count()", "equation|count() * 100"],
  659. },
  660. )
  661. assert response.status_code == 200, response.content
  662. assert response.data["count()"]["order"] == 0
  663. assert [attrs for time, attrs in response.data["count()"]["data"]] == [
  664. [{"count": 1}],
  665. [{"count": 2}],
  666. ]
  667. assert response.data["equation|count() * 100"]["order"] == 1
  668. assert [attrs for time, attrs in response.data["equation|count() * 100"]["data"]] == [
  669. [{"count": 100}],
  670. [{"count": 200}],
  671. ]
  672. def test_equation_multi_yaxis(self):
  673. response = self.do_request(
  674. data={
  675. "start": iso_format(self.day_ago),
  676. "end": iso_format(self.day_ago + timedelta(hours=2)),
  677. "interval": "1h",
  678. "yAxis": ["equation|count() / 100", "equation|count() * 100"],
  679. },
  680. )
  681. assert response.status_code == 200, response.content
  682. assert response.data["equation|count() / 100"]["order"] == 0
  683. assert [attrs for time, attrs in response.data["equation|count() / 100"]["data"]] == [
  684. [{"count": 0.01}],
  685. [{"count": 0.02}],
  686. ]
  687. assert response.data["equation|count() * 100"]["order"] == 1
  688. assert [attrs for time, attrs in response.data["equation|count() * 100"]["data"]] == [
  689. [{"count": 100}],
  690. [{"count": 200}],
  691. ]
  692. def test_large_interval_no_drop_values(self):
  693. self.store_event(
  694. data={
  695. "event_id": "d" * 32,
  696. "message": "not good",
  697. "timestamp": iso_format(self.day_ago - timedelta(minutes=10)),
  698. "fingerprint": ["group3"],
  699. },
  700. project_id=self.project.id,
  701. )
  702. response = self.do_request(
  703. data={
  704. "project": self.project.id,
  705. "end": iso_format(self.day_ago),
  706. "start": iso_format(self.day_ago - timedelta(hours=24)),
  707. "query": 'message:"not good"',
  708. "interval": "1d",
  709. "yAxis": "count()",
  710. },
  711. )
  712. assert response.status_code == 200
  713. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 0}], [{"count": 1}]]
  714. @mock.patch("sentry.snuba.discover.timeseries_query", return_value={})
  715. def test_multiple_yaxis_only_one_query(self, mock_query):
  716. self.do_request(
  717. data={
  718. "project": self.project.id,
  719. "start": iso_format(self.day_ago),
  720. "end": iso_format(self.day_ago + timedelta(hours=2)),
  721. "interval": "1h",
  722. "yAxis": ["user_count", "event_count", "epm()", "eps()"],
  723. },
  724. )
  725. assert mock_query.call_count == 1
  726. @mock.patch("sentry.snuba.discover.bulk_snql_query", return_value=[{"data": []}])
  727. def test_invalid_interval(self, mock_query):
  728. self.do_request(
  729. data={
  730. "end": iso_format(before_now()),
  731. "start": iso_format(before_now(hours=24)),
  732. "query": "",
  733. "interval": "1s",
  734. "yAxis": "count()",
  735. },
  736. )
  737. assert mock_query.call_count == 1
  738. # Should've reset to the default for 24h
  739. assert mock_query.mock_calls[0].args[0][0].query.granularity.granularity == 300
  740. self.do_request(
  741. data={
  742. "end": iso_format(before_now()),
  743. "start": iso_format(before_now(hours=24)),
  744. "query": "",
  745. "interval": "0d",
  746. "yAxis": "count()",
  747. },
  748. )
  749. assert mock_query.call_count == 2
  750. # Should've reset to the default for 24h
  751. assert mock_query.mock_calls[1].args[0][0].query.granularity.granularity == 300
  752. def test_out_of_retention(self):
  753. with self.options({"system.event-retention-days": 10}):
  754. response = self.do_request(
  755. data={
  756. "start": iso_format(before_now(days=20)),
  757. "end": iso_format(before_now(days=15)),
  758. "query": "",
  759. "interval": "30m",
  760. "yAxis": "count()",
  761. },
  762. )
  763. assert response.status_code == 400
  764. @mock.patch("sentry.utils.snuba.quantize_time")
  765. def test_quantize_dates(self, mock_quantize):
  766. mock_quantize.return_value = before_now(days=1).replace(tzinfo=utc)
  767. # Don't quantize short time periods
  768. self.do_request(
  769. data={"statsPeriod": "1h", "query": "", "interval": "30m", "yAxis": "count()"},
  770. )
  771. # Don't quantize absolute date periods
  772. self.do_request(
  773. data={
  774. "start": iso_format(before_now(days=20)),
  775. "end": iso_format(before_now(days=15)),
  776. "query": "",
  777. "interval": "30m",
  778. "yAxis": "count()",
  779. },
  780. )
  781. assert len(mock_quantize.mock_calls) == 0
  782. # Quantize long date periods
  783. self.do_request(
  784. data={"statsPeriod": "90d", "query": "", "interval": "30m", "yAxis": "count()"},
  785. )
  786. assert len(mock_quantize.mock_calls) == 2
  787. def test_with_zerofill(self):
  788. response = self.do_request(
  789. data={
  790. "start": iso_format(self.day_ago),
  791. "end": iso_format(self.day_ago + timedelta(hours=2)),
  792. "interval": "30m",
  793. },
  794. )
  795. assert response.status_code == 200, response.content
  796. assert [attrs for time, attrs in response.data["data"]] == [
  797. [{"count": 1}],
  798. [{"count": 0}],
  799. [{"count": 2}],
  800. [{"count": 0}],
  801. ]
  802. def test_without_zerofill(self):
  803. start = iso_format(self.day_ago)
  804. end = iso_format(self.day_ago + timedelta(hours=2))
  805. response = self.do_request(
  806. data={
  807. "start": start,
  808. "end": end,
  809. "interval": "30m",
  810. "withoutZerofill": "1",
  811. },
  812. features={
  813. "organizations:performance-chart-interpolation": True,
  814. "organizations:discover-basic": True,
  815. },
  816. )
  817. assert response.status_code == 200, response.content
  818. assert [attrs for time, attrs in response.data["data"]] == [
  819. [{"count": 1}],
  820. [{"count": 2}],
  821. ]
  822. assert response.data["start"] == parse_date(start).timestamp()
  823. assert response.data["end"] == parse_date(end).timestamp()
  824. def test_comparison(self):
  825. self.store_event(
  826. data={
  827. "timestamp": iso_format(self.day_ago + timedelta(days=-1, minutes=1)),
  828. },
  829. project_id=self.project.id,
  830. )
  831. self.store_event(
  832. data={
  833. "timestamp": iso_format(self.day_ago + timedelta(days=-1, minutes=2)),
  834. },
  835. project_id=self.project.id,
  836. )
  837. self.store_event(
  838. data={
  839. "timestamp": iso_format(self.day_ago + timedelta(days=-1, hours=1, minutes=1)),
  840. },
  841. project_id=self.project2.id,
  842. )
  843. response = self.do_request(
  844. data={
  845. "start": iso_format(self.day_ago),
  846. "end": iso_format(self.day_ago + timedelta(hours=2)),
  847. "interval": "1h",
  848. "comparisonDelta": int(timedelta(days=1).total_seconds()),
  849. }
  850. )
  851. assert response.status_code == 200, response.content
  852. assert [attrs for time, attrs in response.data["data"]] == [
  853. [{"count": 1, "comparisonCount": 2}],
  854. [{"count": 2, "comparisonCount": 1}],
  855. ]
  856. def test_comparison_invalid(self):
  857. response = self.do_request(
  858. data={
  859. "start": iso_format(self.day_ago),
  860. "end": iso_format(self.day_ago + timedelta(hours=2)),
  861. "interval": "1h",
  862. "comparisonDelta": "17h",
  863. },
  864. )
  865. assert response.status_code == 400, response.content
  866. assert response.data["detail"] == "comparisonDelta must be an integer"
  867. start = before_now(days=85)
  868. end = start + timedelta(days=7)
  869. with self.options({"system.event-retention-days": 90}):
  870. response = self.do_request(
  871. data={
  872. "start": iso_format(start),
  873. "end": iso_format(end),
  874. "interval": "1h",
  875. "comparisonDelta": int(timedelta(days=7).total_seconds()),
  876. }
  877. )
  878. assert response.status_code == 400, response.content
  879. assert response.data["detail"] == "Comparison period is outside retention window"
  880. def test_equations_divide_by_zero(self):
  881. response = self.do_request(
  882. data={
  883. "start": iso_format(self.day_ago),
  884. "end": iso_format(self.day_ago + timedelta(hours=2)),
  885. "interval": "1h",
  886. # force a 0 in the denominator by doing 1 - 1
  887. # since a 0 literal is illegal as the denominator
  888. "yAxis": ["equation|count() / (1-1)"],
  889. },
  890. )
  891. assert response.status_code == 200, response.content
  892. assert len(response.data["data"]) == 2
  893. assert [attrs for time, attrs in response.data["data"]] == [
  894. [{"count": None}],
  895. [{"count": None}],
  896. ]
  897. @mock.patch("sentry.search.events.builder.discover.raw_snql_query")
  898. def test_profiles_dataset_simple(self, mock_snql_query):
  899. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  900. query = {
  901. "yAxis": [
  902. "count()",
  903. "p75()",
  904. "p95()",
  905. "p99()",
  906. "p75(profile.duration)",
  907. "p95(profile.duration)",
  908. "p99(profile.duration)",
  909. ],
  910. "project": [self.project.id],
  911. "dataset": "profiles",
  912. }
  913. response = self.do_request(query, features={"organizations:profiling": True})
  914. assert response.status_code == 200, response.content
  915. def test_tag_with_conflicting_function_alias_simple(self):
  916. for _ in range(7):
  917. self.store_event(
  918. data={
  919. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  920. "tags": {"count": "9001"},
  921. },
  922. project_id=self.project2.id,
  923. )
  924. # Query for count and count()
  925. data = {
  926. "start": iso_format(self.day_ago),
  927. "end": iso_format(self.day_ago + timedelta(minutes=3)),
  928. "interval": "1h",
  929. "yAxis": "count()",
  930. "orderby": ["-count()"],
  931. "field": ["count()", "count"],
  932. "partial": 1,
  933. }
  934. response = self.client.get(self.url, data, format="json")
  935. assert response.status_code == 200
  936. # Expect a count of 8 because one event from setUp
  937. assert response.data["data"][0][1] == [{"count": 8}]
  938. data["query"] = "count:9001"
  939. response = self.client.get(self.url, data, format="json")
  940. assert response.status_code == 200
  941. assert response.data["data"][0][1] == [{"count": 7}]
  942. data["query"] = "count:abc"
  943. response = self.client.get(self.url, data, format="json")
  944. assert response.status_code == 200
  945. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  946. @region_silo_test
  947. class OrganizationEventsStatsTopNEvents(APITestCase, SnubaTestCase):
  948. def setUp(self):
  949. super().setUp()
  950. self.login_as(user=self.user)
  951. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  952. self.project = self.create_project()
  953. self.project2 = self.create_project()
  954. self.user2 = self.create_user()
  955. transaction_data = load_data("transaction")
  956. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  957. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=4))
  958. transaction_data["tags"] = {"shared-tag": "yup"}
  959. self.event_data = [
  960. {
  961. "data": {
  962. "message": "poof",
  963. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  964. "user": {"email": self.user.email},
  965. "tags": {"shared-tag": "yup"},
  966. "fingerprint": ["group1"],
  967. },
  968. "project": self.project2,
  969. "count": 7,
  970. },
  971. {
  972. "data": {
  973. "message": "voof",
  974. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=2)),
  975. "fingerprint": ["group2"],
  976. "user": {"email": self.user2.email},
  977. "tags": {"shared-tag": "yup"},
  978. },
  979. "project": self.project2,
  980. "count": 6,
  981. },
  982. {
  983. "data": {
  984. "message": "very bad",
  985. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  986. "fingerprint": ["group3"],
  987. "user": {"email": "foo@example.com"},
  988. "tags": {"shared-tag": "yup"},
  989. },
  990. "project": self.project,
  991. "count": 5,
  992. },
  993. {
  994. "data": {
  995. "message": "oh no",
  996. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  997. "fingerprint": ["group4"],
  998. "user": {"email": "bar@example.com"},
  999. "tags": {"shared-tag": "yup"},
  1000. },
  1001. "project": self.project,
  1002. "count": 4,
  1003. },
  1004. {"data": transaction_data, "project": self.project, "count": 3},
  1005. # Not in the top 5
  1006. {
  1007. "data": {
  1008. "message": "sorta bad",
  1009. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1010. "fingerprint": ["group5"],
  1011. "user": {"email": "bar@example.com"},
  1012. "tags": {"shared-tag": "yup"},
  1013. },
  1014. "project": self.project,
  1015. "count": 2,
  1016. },
  1017. {
  1018. "data": {
  1019. "message": "not so bad",
  1020. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1021. "fingerprint": ["group6"],
  1022. "user": {"email": "bar@example.com"},
  1023. "tags": {"shared-tag": "yup"},
  1024. },
  1025. "project": self.project,
  1026. "count": 1,
  1027. },
  1028. ]
  1029. self.events = []
  1030. for index, event_data in enumerate(self.event_data):
  1031. data = event_data["data"].copy()
  1032. event = {}
  1033. for i in range(event_data["count"]):
  1034. data["event_id"] = f"{index}{i}" * 16
  1035. event = self.store_event(data, project_id=event_data["project"].id)
  1036. self.events.append(event)
  1037. self.transaction = self.events[4]
  1038. self.enabled_features = {
  1039. "organizations:discover-basic": True,
  1040. }
  1041. self.url = reverse(
  1042. "sentry-api-0-organization-events-stats",
  1043. kwargs={"organization_slug": self.project.organization.slug},
  1044. )
  1045. def test_no_top_events_with_project_field(self):
  1046. project = self.create_project()
  1047. with self.feature(self.enabled_features):
  1048. response = self.client.get(
  1049. self.url,
  1050. data={
  1051. # make sure to query the project with 0 events
  1052. "project": project.id,
  1053. "start": iso_format(self.day_ago),
  1054. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1055. "interval": "1h",
  1056. "yAxis": "count()",
  1057. "orderby": ["-count()"],
  1058. "field": ["count()", "project"],
  1059. "topEvents": 5,
  1060. },
  1061. format="json",
  1062. )
  1063. assert response.status_code == 200, response.content
  1064. # When there are no top events, we do not return an empty dict.
  1065. # Instead, we return a single zero-filled series for an empty graph.
  1066. data = response.data["data"]
  1067. assert [attrs for time, attrs in data] == [[{"count": 0}], [{"count": 0}]]
  1068. def test_no_top_events(self):
  1069. project = self.create_project()
  1070. with self.feature(self.enabled_features):
  1071. response = self.client.get(
  1072. self.url,
  1073. data={
  1074. # make sure to query the project with 0 events
  1075. "project": project.id,
  1076. "start": iso_format(self.day_ago),
  1077. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1078. "interval": "1h",
  1079. "yAxis": "count()",
  1080. "orderby": ["-count()"],
  1081. "field": ["count()", "message", "user.email"],
  1082. "topEvents": 5,
  1083. },
  1084. format="json",
  1085. )
  1086. data = response.data["data"]
  1087. assert response.status_code == 200, response.content
  1088. # When there are no top events, we do not return an empty dict.
  1089. # Instead, we return a single zero-filled series for an empty graph.
  1090. assert [attrs for time, attrs in data] == [[{"count": 0}], [{"count": 0}]]
  1091. def test_no_top_events_with_multi_axis(self):
  1092. project = self.create_project()
  1093. with self.feature(self.enabled_features):
  1094. response = self.client.get(
  1095. self.url,
  1096. data={
  1097. # make sure to query the project with 0 events
  1098. "project": project.id,
  1099. "start": iso_format(self.day_ago),
  1100. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1101. "interval": "1h",
  1102. "yAxis": ["count()", "count_unique(user)"],
  1103. "orderby": ["-count()"],
  1104. "field": ["count()", "count_unique(user)", "message", "user.email"],
  1105. "topEvents": 5,
  1106. },
  1107. format="json",
  1108. )
  1109. assert response.status_code == 200
  1110. data = response.data[""]
  1111. assert [attrs for time, attrs in data["count()"]["data"]] == [
  1112. [{"count": 0}],
  1113. [{"count": 0}],
  1114. ]
  1115. assert [attrs for time, attrs in data["count_unique(user)"]["data"]] == [
  1116. [{"count": 0}],
  1117. [{"count": 0}],
  1118. ]
  1119. def test_simple_top_events(self):
  1120. with self.feature(self.enabled_features):
  1121. response = self.client.get(
  1122. self.url,
  1123. data={
  1124. "start": iso_format(self.day_ago),
  1125. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1126. "interval": "1h",
  1127. "yAxis": "count()",
  1128. "orderby": ["-count()"],
  1129. "field": ["count()", "message", "user.email"],
  1130. "topEvents": 5,
  1131. },
  1132. format="json",
  1133. )
  1134. data = response.data
  1135. assert response.status_code == 200, response.content
  1136. assert len(data) == 6
  1137. for index, event in enumerate(self.events[:5]):
  1138. message = event.message or event.transaction
  1139. results = data[
  1140. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  1141. ]
  1142. assert results["order"] == index
  1143. assert [{"count": self.event_data[index]["count"]}] in [
  1144. attrs for _, attrs in results["data"]
  1145. ]
  1146. other = data["Other"]
  1147. assert other["order"] == 5
  1148. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1149. def test_tag_with_conflicting_function_alias_simple(self):
  1150. event_data = {
  1151. "data": {
  1152. "message": "poof",
  1153. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1154. "user": {"email": self.user.email},
  1155. "tags": {"count": "9001"},
  1156. "fingerprint": ["group1"],
  1157. },
  1158. "project": self.project2,
  1159. "count": 7,
  1160. }
  1161. for i in range(event_data["count"]):
  1162. event_data["data"]["event_id"] = f"a{i}" * 16
  1163. self.store_event(event_data["data"], project_id=event_data["project"].id)
  1164. # Query for count and count()
  1165. data = {
  1166. "start": iso_format(self.day_ago),
  1167. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1168. "interval": "1h",
  1169. "yAxis": "count()",
  1170. "orderby": ["-count()"],
  1171. "field": ["count()", "count"],
  1172. "topEvents": 5,
  1173. "partial": 1,
  1174. }
  1175. with self.feature(self.enabled_features):
  1176. response = self.client.get(self.url, data, format="json")
  1177. assert response.status_code == 200
  1178. assert response.data["9001"]["data"][0][1] == [{"count": 7}]
  1179. data["query"] = "count:9001"
  1180. with self.feature(self.enabled_features):
  1181. response = self.client.get(self.url, data, format="json")
  1182. assert response.status_code == 200
  1183. assert response.data["9001"]["data"][0][1] == [{"count": 7}]
  1184. data["query"] = "count:abc"
  1185. with self.feature(self.enabled_features):
  1186. response = self.client.get(self.url, data, format="json")
  1187. assert response.status_code == 200
  1188. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  1189. @pytest.mark.xfail(
  1190. reason="The response.data[Other] returns 15 locally and returns 16 or 15 remotely."
  1191. )
  1192. def test_tag_with_conflicting_function_alias_with_other_single_grouping(self):
  1193. event_data = [
  1194. {
  1195. "data": {
  1196. "message": "poof",
  1197. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1198. "user": {"email": self.user.email},
  1199. "tags": {"count": "9001"},
  1200. "fingerprint": ["group1"],
  1201. },
  1202. "project": self.project2,
  1203. "count": 7,
  1204. },
  1205. {
  1206. "data": {
  1207. "message": "poof2",
  1208. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1209. "user": {"email": self.user.email},
  1210. "tags": {"count": "abc"},
  1211. "fingerprint": ["group1"],
  1212. },
  1213. "project": self.project2,
  1214. "count": 3,
  1215. },
  1216. ]
  1217. for index, event in enumerate(event_data):
  1218. for i in range(event["count"]):
  1219. event["data"]["event_id"] = f"{index}{i}" * 16
  1220. self.store_event(event["data"], project_id=event["project"].id)
  1221. # Query for count and count()
  1222. data = {
  1223. "start": iso_format(self.day_ago),
  1224. "end": iso_format(self.day_ago + timedelta(hours=1)),
  1225. "interval": "1h",
  1226. "yAxis": "count()",
  1227. "orderby": ["-count"],
  1228. "field": ["count()", "count"],
  1229. "topEvents": 2,
  1230. "partial": 1,
  1231. }
  1232. with self.feature(self.enabled_features):
  1233. response = self.client.get(self.url, data, format="json")
  1234. assert response.status_code == 200
  1235. assert response.data["9001"]["data"][0][1] == [{"count": 7}]
  1236. assert response.data["abc"]["data"][0][1] == [{"count": 3}]
  1237. assert response.data["Other"]["data"][0][1] == [{"count": 16}]
  1238. def test_tag_with_conflicting_function_alias_with_other_multiple_groupings(self):
  1239. event_data = [
  1240. {
  1241. "data": {
  1242. "message": "abc",
  1243. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1244. "user": {"email": self.user.email},
  1245. "tags": {"count": "2"},
  1246. "fingerprint": ["group1"],
  1247. },
  1248. "project": self.project2,
  1249. "count": 3,
  1250. },
  1251. {
  1252. "data": {
  1253. "message": "def",
  1254. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1255. "user": {"email": self.user.email},
  1256. "tags": {"count": "9001"},
  1257. "fingerprint": ["group1"],
  1258. },
  1259. "project": self.project2,
  1260. "count": 7,
  1261. },
  1262. ]
  1263. for index, event in enumerate(event_data):
  1264. for i in range(event["count"]):
  1265. event["data"]["event_id"] = f"{index}{i}" * 16
  1266. self.store_event(event["data"], project_id=event["project"].id)
  1267. # Query for count and count()
  1268. data = {
  1269. "start": iso_format(self.day_ago),
  1270. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1271. "interval": "2d",
  1272. "yAxis": "count()",
  1273. "orderby": ["-count"],
  1274. "field": ["count()", "count", "message"],
  1275. "topEvents": 2,
  1276. "partial": 1,
  1277. }
  1278. with self.feature(self.enabled_features):
  1279. response = self.client.get(self.url, data, format="json")
  1280. assert response.status_code == 200
  1281. assert response.data["abc,2"]["data"][0][1] == [{"count": 3}]
  1282. assert response.data["def,9001"]["data"][0][1] == [{"count": 7}]
  1283. assert response.data["Other"]["data"][0][1] == [{"count": 25}]
  1284. def test_top_events_limits(self):
  1285. data = {
  1286. "start": iso_format(self.day_ago),
  1287. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1288. "interval": "1h",
  1289. "yAxis": "count()",
  1290. "orderby": ["-count()"],
  1291. "field": ["count()", "message", "user.email"],
  1292. }
  1293. with self.feature(self.enabled_features):
  1294. data["topEvents"] = MAX_TOP_EVENTS + 1
  1295. response = self.client.get(self.url, data, format="json")
  1296. assert response.status_code == 400
  1297. data["topEvents"] = 0
  1298. response = self.client.get(self.url, data, format="json")
  1299. assert response.status_code == 400
  1300. data["topEvents"] = "a"
  1301. response = self.client.get(self.url, data, format="json")
  1302. assert response.status_code == 400
  1303. def test_top_events_with_projects(self):
  1304. with self.feature(self.enabled_features):
  1305. response = self.client.get(
  1306. self.url,
  1307. data={
  1308. "start": iso_format(self.day_ago),
  1309. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1310. "interval": "1h",
  1311. "yAxis": "count()",
  1312. "orderby": ["-count()"],
  1313. "field": ["count()", "message", "project"],
  1314. "topEvents": 5,
  1315. },
  1316. format="json",
  1317. )
  1318. data = response.data
  1319. assert response.status_code == 200, response.content
  1320. assert len(data) == 6
  1321. for index, event in enumerate(self.events[:5]):
  1322. message = event.message or event.transaction
  1323. results = data[",".join([message, event.project.slug])]
  1324. assert results["order"] == index
  1325. assert [{"count": self.event_data[index]["count"]}] in [
  1326. attrs for time, attrs in results["data"]
  1327. ]
  1328. other = data["Other"]
  1329. assert other["order"] == 5
  1330. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1331. def test_top_events_with_issue(self):
  1332. # delete a group to make sure if this happens the value becomes unknown
  1333. event_group = self.events[0].group
  1334. event_group.delete()
  1335. with self.feature(self.enabled_features):
  1336. response = self.client.get(
  1337. self.url,
  1338. data={
  1339. "start": iso_format(self.day_ago),
  1340. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1341. "interval": "1h",
  1342. "yAxis": "count()",
  1343. "orderby": ["-count()"],
  1344. "field": ["count()", "message", "issue"],
  1345. "topEvents": 5,
  1346. "query": "!event.type:transaction",
  1347. },
  1348. format="json",
  1349. )
  1350. data = response.data
  1351. assert response.status_code == 200, response.content
  1352. assert len(data) == 6
  1353. for index, event in enumerate(self.events[:4]):
  1354. message = event.message
  1355. # Because we deleted the group for event 0
  1356. if index == 0 or event.group is None:
  1357. issue = "unknown"
  1358. else:
  1359. issue = event.group.qualified_short_id
  1360. results = data[",".join([issue, message])]
  1361. assert results["order"] == index
  1362. assert [{"count": self.event_data[index]["count"]}] in [
  1363. attrs for time, attrs in results["data"]
  1364. ]
  1365. other = data["Other"]
  1366. assert other["order"] == 5
  1367. assert [{"count": 1}] in [attrs for _, attrs in other["data"]]
  1368. def test_top_events_with_transaction_status(self):
  1369. with self.feature(self.enabled_features):
  1370. response = self.client.get(
  1371. self.url,
  1372. data={
  1373. "start": iso_format(self.day_ago),
  1374. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1375. "interval": "1h",
  1376. "yAxis": "count()",
  1377. "orderby": ["-count()"],
  1378. "field": ["count()", "transaction.status"],
  1379. "topEvents": 5,
  1380. },
  1381. format="json",
  1382. )
  1383. data = response.data
  1384. assert response.status_code == 200, response.content
  1385. assert len(data) == 1
  1386. assert "ok" in data
  1387. @mock.patch("sentry.models.GroupManager.get_issues_mapping")
  1388. def test_top_events_with_unknown_issue(self, mock_issues_mapping):
  1389. event = self.events[0]
  1390. event_data = self.event_data[0]
  1391. # ensure that the issue mapping returns None for the issue
  1392. mock_issues_mapping.return_value = {event.group.id: None}
  1393. with self.feature(self.enabled_features):
  1394. response = self.client.get(
  1395. self.url,
  1396. data={
  1397. "start": iso_format(self.day_ago),
  1398. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1399. "interval": "1h",
  1400. "yAxis": "count()",
  1401. "orderby": ["-count()"],
  1402. "field": ["count()", "issue"],
  1403. "topEvents": 5,
  1404. # narrow the search to just one issue
  1405. "query": f"issue.id:{event.group.id}",
  1406. },
  1407. format="json",
  1408. )
  1409. assert response.status_code == 200, response.content
  1410. data = response.data
  1411. assert len(data) == 1
  1412. results = data["unknown"]
  1413. assert results["order"] == 0
  1414. assert [{"count": event_data["count"]}] in [attrs for time, attrs in results["data"]]
  1415. @mock.patch(
  1416. "sentry.search.events.builder.discover.raw_snql_query",
  1417. side_effect=[{"data": [{"issue.id": 1}], "meta": []}, {"data": [], "meta": []}],
  1418. )
  1419. def test_top_events_with_issue_check_query_conditions(self, mock_query):
  1420. """ "Intentionally separate from test_top_events_with_issue
  1421. This is to test against a bug where the condition for issues wasn't included and we'd be missing data for
  1422. the interval since we'd cap out the max rows. This was not caught by the previous test since the results
  1423. would still be correct given the smaller interval & lack of data
  1424. """
  1425. with self.feature(self.enabled_features):
  1426. self.client.get(
  1427. self.url,
  1428. data={
  1429. "start": iso_format(self.day_ago),
  1430. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1431. "interval": "1h",
  1432. "yAxis": "count()",
  1433. "orderby": ["-count()"],
  1434. "field": ["count()", "message", "issue"],
  1435. "topEvents": 5,
  1436. "query": "!event.type:transaction",
  1437. },
  1438. format="json",
  1439. )
  1440. assert (
  1441. Condition(Function("coalesce", [Column("group_id"), 0], "issue.id"), Op.IN, [1])
  1442. in mock_query.mock_calls[1].args[0].query.where
  1443. )
  1444. def test_top_events_with_functions(self):
  1445. with self.feature(self.enabled_features):
  1446. response = self.client.get(
  1447. self.url,
  1448. data={
  1449. "start": iso_format(self.day_ago),
  1450. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1451. "interval": "1h",
  1452. "yAxis": "count()",
  1453. "orderby": ["-p99()"],
  1454. "field": ["transaction", "avg(transaction.duration)", "p99()"],
  1455. "topEvents": 5,
  1456. },
  1457. format="json",
  1458. )
  1459. data = response.data
  1460. assert response.status_code == 200, response.content
  1461. assert len(data) == 1
  1462. results = data[self.transaction.transaction]
  1463. assert results["order"] == 0
  1464. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1465. def test_top_events_with_functions_on_different_transactions(self):
  1466. """Transaction2 has less events, but takes longer so order should be self.transaction then transaction2"""
  1467. transaction_data = load_data("transaction")
  1468. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1469. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=6))
  1470. transaction_data["transaction"] = "/foo_bar/"
  1471. transaction2 = self.store_event(transaction_data, project_id=self.project.id)
  1472. with self.feature(self.enabled_features):
  1473. response = self.client.get(
  1474. self.url,
  1475. data={
  1476. "start": iso_format(self.day_ago),
  1477. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1478. "interval": "1h",
  1479. "yAxis": "count()",
  1480. "orderby": ["-p99()"],
  1481. "field": ["transaction", "avg(transaction.duration)", "p99()"],
  1482. "topEvents": 5,
  1483. },
  1484. format="json",
  1485. )
  1486. data = response.data
  1487. assert response.status_code == 200, response.content
  1488. assert len(data) == 2
  1489. results = data[self.transaction.transaction]
  1490. assert results["order"] == 1
  1491. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1492. results = data[transaction2.transaction]
  1493. assert results["order"] == 0
  1494. assert [attrs for time, attrs in results["data"]] == [[{"count": 1}], [{"count": 0}]]
  1495. def test_top_events_with_query(self):
  1496. transaction_data = load_data("transaction")
  1497. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1498. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=6))
  1499. transaction_data["transaction"] = "/foo_bar/"
  1500. self.store_event(transaction_data, project_id=self.project.id)
  1501. with self.feature(self.enabled_features):
  1502. response = self.client.get(
  1503. self.url,
  1504. data={
  1505. "start": iso_format(self.day_ago),
  1506. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1507. "interval": "1h",
  1508. "yAxis": "count()",
  1509. "orderby": ["-p99()"],
  1510. "query": "transaction:/foo_bar/",
  1511. "field": ["transaction", "avg(transaction.duration)", "p99()"],
  1512. "topEvents": 5,
  1513. },
  1514. format="json",
  1515. )
  1516. data = response.data
  1517. assert response.status_code == 200, response.content
  1518. assert len(data) == 1
  1519. transaction2_data = data["/foo_bar/"]
  1520. assert transaction2_data["order"] == 0
  1521. assert [attrs for time, attrs in transaction2_data["data"]] == [
  1522. [{"count": 1}],
  1523. [{"count": 0}],
  1524. ]
  1525. def test_top_events_with_negated_condition(self):
  1526. with self.feature(self.enabled_features):
  1527. response = self.client.get(
  1528. self.url,
  1529. data={
  1530. "start": iso_format(self.day_ago),
  1531. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1532. "interval": "1h",
  1533. "yAxis": "count()",
  1534. "orderby": ["-count()"],
  1535. "query": f"!message:{self.events[0].message}",
  1536. "field": ["message", "count()"],
  1537. "topEvents": 5,
  1538. },
  1539. format="json",
  1540. )
  1541. data = response.data
  1542. assert response.status_code == 200, response.content
  1543. assert len(data) == 6
  1544. for index, event in enumerate(self.events[1:5]):
  1545. message = event.message or event.transaction
  1546. results = data[message]
  1547. assert results["order"] == index
  1548. assert [{"count": self.event_data[index + 1]["count"]}] in [
  1549. attrs for _, attrs in results["data"]
  1550. ]
  1551. other = data["Other"]
  1552. assert other["order"] == 5
  1553. assert [{"count": 1}] in [attrs for _, attrs in other["data"]]
  1554. def test_top_events_with_epm(self):
  1555. with self.feature(self.enabled_features):
  1556. response = self.client.get(
  1557. self.url,
  1558. data={
  1559. "start": iso_format(self.day_ago),
  1560. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1561. "interval": "1h",
  1562. "yAxis": "epm()",
  1563. "orderby": ["-count()"],
  1564. "field": ["message", "user.email", "count()"],
  1565. "topEvents": 5,
  1566. },
  1567. format="json",
  1568. )
  1569. data = response.data
  1570. assert response.status_code == 200, response.content
  1571. assert len(data) == 6
  1572. for index, event in enumerate(self.events[:5]):
  1573. message = event.message or event.transaction
  1574. results = data[
  1575. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  1576. ]
  1577. assert results["order"] == index
  1578. assert [{"count": self.event_data[index]["count"] / (3600.0 / 60.0)}] in [
  1579. attrs for time, attrs in results["data"]
  1580. ]
  1581. other = data["Other"]
  1582. assert other["order"] == 5
  1583. assert [{"count": 0.05}] in [attrs for _, attrs in other["data"]]
  1584. def test_top_events_with_multiple_yaxis(self):
  1585. with self.feature(self.enabled_features):
  1586. response = self.client.get(
  1587. self.url,
  1588. data={
  1589. "start": iso_format(self.day_ago),
  1590. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1591. "interval": "1h",
  1592. "yAxis": ["epm()", "count()"],
  1593. "orderby": ["-count()"],
  1594. "field": ["message", "user.email", "count()"],
  1595. "topEvents": 5,
  1596. },
  1597. format="json",
  1598. )
  1599. data = response.data
  1600. assert response.status_code == 200, response.content
  1601. assert len(data) == 6
  1602. for index, event in enumerate(self.events[:5]):
  1603. message = event.message or event.transaction
  1604. results = data[
  1605. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  1606. ]
  1607. assert results["order"] == index
  1608. assert results["epm()"]["order"] == 0
  1609. assert results["count()"]["order"] == 1
  1610. assert [{"count": self.event_data[index]["count"] / (3600.0 / 60.0)}] in [
  1611. attrs for time, attrs in results["epm()"]["data"]
  1612. ]
  1613. assert [{"count": self.event_data[index]["count"]}] in [
  1614. attrs for time, attrs in results["count()"]["data"]
  1615. ]
  1616. other = data["Other"]
  1617. assert other["order"] == 5
  1618. assert other["epm()"]["order"] == 0
  1619. assert other["count()"]["order"] == 1
  1620. assert [{"count": 0.05}] in [attrs for _, attrs in other["epm()"]["data"]]
  1621. assert [{"count": 3}] in [attrs for _, attrs in other["count()"]["data"]]
  1622. def test_top_events_with_boolean(self):
  1623. with self.feature(self.enabled_features):
  1624. response = self.client.get(
  1625. self.url,
  1626. data={
  1627. "start": iso_format(self.day_ago),
  1628. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1629. "interval": "1h",
  1630. "yAxis": "count()",
  1631. "orderby": ["-count()"],
  1632. "field": ["count()", "message", "device.charging"],
  1633. "topEvents": 5,
  1634. },
  1635. format="json",
  1636. )
  1637. data = response.data
  1638. assert response.status_code == 200, response.content
  1639. assert len(data) == 6
  1640. for index, event in enumerate(self.events[:5]):
  1641. message = event.message or event.transaction
  1642. results = data[",".join(["False", message])]
  1643. assert results["order"] == index
  1644. assert [{"count": self.event_data[index]["count"]}] in [
  1645. attrs for time, attrs in results["data"]
  1646. ]
  1647. other = data["Other"]
  1648. assert other["order"] == 5
  1649. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1650. def test_top_events_with_error_unhandled(self):
  1651. self.login_as(user=self.user)
  1652. project = self.create_project()
  1653. prototype = load_data("android-ndk")
  1654. prototype["event_id"] = "f" * 32
  1655. prototype["message"] = "not handled"
  1656. prototype["exception"]["values"][0]["value"] = "not handled"
  1657. prototype["exception"]["values"][0]["mechanism"]["handled"] = False
  1658. prototype["timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1659. self.store_event(data=prototype, project_id=project.id)
  1660. with self.feature(self.enabled_features):
  1661. response = self.client.get(
  1662. self.url,
  1663. data={
  1664. "start": iso_format(self.day_ago),
  1665. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1666. "interval": "1h",
  1667. "yAxis": "count()",
  1668. "orderby": ["-count()"],
  1669. "field": ["count()", "error.unhandled"],
  1670. "topEvents": 5,
  1671. },
  1672. format="json",
  1673. )
  1674. data = response.data
  1675. assert response.status_code == 200, response.content
  1676. assert len(data) == 2
  1677. def test_top_events_with_timestamp(self):
  1678. with self.feature(self.enabled_features):
  1679. response = self.client.get(
  1680. self.url,
  1681. data={
  1682. "start": iso_format(self.day_ago),
  1683. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1684. "interval": "1h",
  1685. "yAxis": "count()",
  1686. "orderby": ["-count()"],
  1687. "query": "event.type:default",
  1688. "field": ["count()", "message", "timestamp"],
  1689. "topEvents": 5,
  1690. },
  1691. format="json",
  1692. )
  1693. data = response.data
  1694. assert response.status_code == 200, response.content
  1695. assert len(data) == 6
  1696. # Transactions won't be in the results because of the query
  1697. del self.events[4]
  1698. del self.event_data[4]
  1699. for index, event in enumerate(self.events[:5]):
  1700. results = data[",".join([event.message, event.timestamp])]
  1701. assert results["order"] == index
  1702. assert [{"count": self.event_data[index]["count"]}] in [
  1703. attrs for time, attrs in results["data"]
  1704. ]
  1705. other = data["Other"]
  1706. assert other["order"] == 5
  1707. assert [{"count": 1}] in [attrs for _, attrs in other["data"]]
  1708. def test_top_events_with_int(self):
  1709. with self.feature(self.enabled_features):
  1710. response = self.client.get(
  1711. self.url,
  1712. data={
  1713. "start": iso_format(self.day_ago),
  1714. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1715. "interval": "1h",
  1716. "yAxis": "count()",
  1717. "orderby": ["-count()"],
  1718. "field": ["count()", "message", "transaction.duration"],
  1719. "topEvents": 5,
  1720. },
  1721. format="json",
  1722. )
  1723. data = response.data
  1724. assert response.status_code == 200, response.content
  1725. assert len(data) == 1
  1726. results = data[",".join([self.transaction.transaction, "120000"])]
  1727. assert results["order"] == 0
  1728. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1729. def test_top_events_with_user(self):
  1730. with self.feature(self.enabled_features):
  1731. response = self.client.get(
  1732. self.url,
  1733. data={
  1734. "start": iso_format(self.day_ago),
  1735. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1736. "interval": "1h",
  1737. "yAxis": "count()",
  1738. "orderby": ["-count()", "user"],
  1739. "field": ["user", "count()"],
  1740. "topEvents": 5,
  1741. },
  1742. format="json",
  1743. )
  1744. data = response.data
  1745. assert response.status_code == 200, response.content
  1746. assert len(data) == 5
  1747. assert data["email:bar@example.com"]["order"] == 1
  1748. assert [attrs for time, attrs in data["email:bar@example.com"]["data"]] == [
  1749. [{"count": 7}],
  1750. [{"count": 0}],
  1751. ]
  1752. assert [attrs for time, attrs in data["ip:127.0.0.1"]["data"]] == [
  1753. [{"count": 3}],
  1754. [{"count": 0}],
  1755. ]
  1756. def test_top_events_with_user_and_email(self):
  1757. with self.feature(self.enabled_features):
  1758. response = self.client.get(
  1759. self.url,
  1760. data={
  1761. "start": iso_format(self.day_ago),
  1762. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1763. "interval": "1h",
  1764. "yAxis": "count()",
  1765. "orderby": ["-count()", "user"],
  1766. "field": ["user", "user.email", "count()"],
  1767. "topEvents": 5,
  1768. },
  1769. format="json",
  1770. )
  1771. data = response.data
  1772. assert response.status_code == 200, response.content
  1773. assert len(data) == 5
  1774. assert data["email:bar@example.com,bar@example.com"]["order"] == 1
  1775. assert [attrs for time, attrs in data["email:bar@example.com,bar@example.com"]["data"]] == [
  1776. [{"count": 7}],
  1777. [{"count": 0}],
  1778. ]
  1779. assert [attrs for time, attrs in data["ip:127.0.0.1,None"]["data"]] == [
  1780. [{"count": 3}],
  1781. [{"count": 0}],
  1782. ]
  1783. def test_top_events_with_user_display(self):
  1784. with self.feature(self.enabled_features):
  1785. response = self.client.get(
  1786. self.url,
  1787. data={
  1788. "start": iso_format(self.day_ago),
  1789. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1790. "interval": "1h",
  1791. "yAxis": "count()",
  1792. "orderby": ["-count()"],
  1793. "field": ["message", "user.display", "count()"],
  1794. "topEvents": 5,
  1795. },
  1796. format="json",
  1797. )
  1798. data = response.data
  1799. assert response.status_code == 200, response.content
  1800. assert len(data) == 6
  1801. for index, event in enumerate(self.events[:5]):
  1802. message = event.message or event.transaction
  1803. user = self.event_data[index]["data"]["user"]
  1804. results = data[
  1805. ",".join([message, user.get("email", None) or user.get("ip_address", "None")])
  1806. ]
  1807. assert results["order"] == index
  1808. assert [{"count": self.event_data[index]["count"]}] in [
  1809. attrs for _, attrs in results["data"]
  1810. ]
  1811. other = data["Other"]
  1812. assert other["order"] == 5
  1813. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1814. @pytest.mark.skip(reason="A query with group_id will not return transactions")
  1815. def test_top_events_none_filter(self):
  1816. """When a field is None in one of the top events, make sure we filter by it
  1817. In this case event[4] is a transaction and has no issue
  1818. """
  1819. with self.feature(self.enabled_features):
  1820. response = self.client.get(
  1821. self.url,
  1822. data={
  1823. "start": iso_format(self.day_ago),
  1824. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1825. "interval": "1h",
  1826. "yAxis": "count()",
  1827. "orderby": ["-count()"],
  1828. "field": ["count()", "issue"],
  1829. "topEvents": 5,
  1830. },
  1831. format="json",
  1832. )
  1833. data = response.data
  1834. assert response.status_code == 200, response.content
  1835. assert len(data) == 5
  1836. for index, event in enumerate(self.events[:5]):
  1837. if event.group is None:
  1838. issue = "unknown"
  1839. else:
  1840. issue = event.group.qualified_short_id
  1841. results = data[issue]
  1842. assert results["order"] == index
  1843. assert [{"count": self.event_data[index]["count"]}] in [
  1844. attrs for time, attrs in results["data"]
  1845. ]
  1846. @pytest.mark.skip(reason="Invalid query - transaction events don't have group_id field")
  1847. def test_top_events_one_field_with_none(self):
  1848. with self.feature(self.enabled_features):
  1849. response = self.client.get(
  1850. self.url,
  1851. data={
  1852. "start": iso_format(self.day_ago),
  1853. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1854. "interval": "1h",
  1855. "yAxis": "count()",
  1856. "orderby": ["-count()"],
  1857. "query": "event.type:transaction",
  1858. "field": ["count()", "issue"],
  1859. "topEvents": 5,
  1860. },
  1861. format="json",
  1862. )
  1863. data = response.data
  1864. assert response.status_code == 200, response.content
  1865. assert len(data) == 1
  1866. results = data["unknown"]
  1867. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1868. assert results["order"] == 0
  1869. def test_top_events_with_error_handled(self):
  1870. data = self.event_data[0]
  1871. data["data"]["level"] = "error"
  1872. data["data"]["exception"] = {
  1873. "values": [
  1874. {
  1875. "type": "ValidationError",
  1876. "value": "Bad request",
  1877. "mechanism": {"handled": True, "type": "generic"},
  1878. }
  1879. ]
  1880. }
  1881. self.store_event(data["data"], project_id=data["project"].id)
  1882. data["data"]["exception"] = {
  1883. "values": [
  1884. {
  1885. "type": "ValidationError",
  1886. "value": "Bad request",
  1887. "mechanism": {"handled": False, "type": "generic"},
  1888. }
  1889. ]
  1890. }
  1891. self.store_event(data["data"], project_id=data["project"].id)
  1892. with self.feature(self.enabled_features):
  1893. response = self.client.get(
  1894. self.url,
  1895. data={
  1896. "start": iso_format(self.day_ago),
  1897. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1898. "interval": "1h",
  1899. "yAxis": "count()",
  1900. "orderby": ["-count()"],
  1901. "field": ["count()", "error.handled"],
  1902. "topEvents": 5,
  1903. "query": "!event.type:transaction",
  1904. },
  1905. format="json",
  1906. )
  1907. assert response.status_code == 200, response.content
  1908. data = response.data
  1909. assert len(data) == 2
  1910. results = data["1"]
  1911. assert [attrs for time, attrs in results["data"]] == [[{"count": 20}], [{"count": 6}]]
  1912. results = data["0"]
  1913. assert [attrs for time, attrs in results["data"]] == [[{"count": 1}], [{"count": 0}]]
  1914. def test_top_events_with_aggregate_condition(self):
  1915. with self.feature(self.enabled_features):
  1916. response = self.client.get(
  1917. self.url,
  1918. data={
  1919. "start": iso_format(self.day_ago),
  1920. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1921. "interval": "1h",
  1922. "yAxis": "count()",
  1923. "orderby": ["-count()"],
  1924. "field": ["message", "count()"],
  1925. "query": "count():>4",
  1926. "topEvents": 5,
  1927. },
  1928. format="json",
  1929. )
  1930. assert response.status_code == 200, response.content
  1931. data = response.data
  1932. assert len(data) == 3
  1933. for index, event in enumerate(self.events[:3]):
  1934. message = event.message or event.transaction
  1935. results = data[message]
  1936. assert results["order"] == index
  1937. assert [{"count": self.event_data[index]["count"]}] in [
  1938. attrs for time, attrs in results["data"]
  1939. ]
  1940. @pytest.mark.xfail(reason="There's only 2 rows total, which mean there shouldn't be other")
  1941. def test_top_events_with_to_other(self):
  1942. version = "version -@'\" 1.2,3+(4)"
  1943. version_escaped = "version -@'\\\" 1.2,3+(4)"
  1944. # every symbol is replaced with a underscore to make the alias
  1945. version_alias = "version_______1_2_3__4_"
  1946. # add an event in the current release
  1947. event = self.event_data[0]
  1948. event_data = event["data"].copy()
  1949. event_data["event_id"] = uuid4().hex
  1950. event_data["release"] = version
  1951. self.store_event(event_data, project_id=event["project"].id)
  1952. with self.feature(self.enabled_features):
  1953. response = self.client.get(
  1954. self.url,
  1955. data={
  1956. "start": iso_format(self.day_ago),
  1957. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1958. "interval": "1h",
  1959. "yAxis": "count()",
  1960. # the double underscores around the version alias is because of a comma and quote
  1961. "orderby": [f"-to_other_release__{version_alias}__others_current"],
  1962. "field": [
  1963. "count()",
  1964. f'to_other(release,"{version_escaped}",others,current)',
  1965. ],
  1966. "topEvents": 2,
  1967. },
  1968. format="json",
  1969. )
  1970. assert response.status_code == 200, response.content
  1971. data = response.data
  1972. assert len(data) == 2
  1973. current = data["current"]
  1974. assert current["order"] == 1
  1975. assert sum(attrs[0]["count"] for _, attrs in current["data"]) == 1
  1976. others = data["others"]
  1977. assert others["order"] == 0
  1978. assert sum(attrs[0]["count"] for _, attrs in others["data"]) == sum(
  1979. event_data["count"] for event_data in self.event_data
  1980. )
  1981. def test_top_events_with_equations(self):
  1982. with self.feature(self.enabled_features):
  1983. response = self.client.get(
  1984. self.url,
  1985. data={
  1986. "start": iso_format(self.day_ago),
  1987. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1988. "interval": "1h",
  1989. "yAxis": "equation|count() / 100",
  1990. "orderby": ["-count()"],
  1991. "field": ["count()", "message", "user.email", "equation|count() / 100"],
  1992. "topEvents": 5,
  1993. },
  1994. format="json",
  1995. )
  1996. data = response.data
  1997. assert response.status_code == 200, response.content
  1998. assert len(data) == 6
  1999. for index, event in enumerate(self.events[:5]):
  2000. message = event.message or event.transaction
  2001. results = data[
  2002. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  2003. ]
  2004. assert results["order"] == index
  2005. assert [{"count": self.event_data[index]["count"] / 100}] in [
  2006. attrs for time, attrs in results["data"]
  2007. ]
  2008. other = data["Other"]
  2009. assert other["order"] == 5
  2010. assert [{"count": 0.03}] in [attrs for _, attrs in other["data"]]
  2011. @mock.patch("sentry.snuba.discover.bulk_snql_query", return_value=[{"data": [], "meta": []}])
  2012. @mock.patch(
  2013. "sentry.search.events.builder.discover.raw_snql_query",
  2014. return_value={"data": [], "meta": []},
  2015. )
  2016. def test_invalid_interval(self, mock_raw_query, mock_bulk_query):
  2017. with self.feature(self.enabled_features):
  2018. response = self.client.get(
  2019. self.url,
  2020. format="json",
  2021. data={
  2022. "end": iso_format(before_now()),
  2023. # 7,200 points for each event
  2024. "start": iso_format(before_now(seconds=7200)),
  2025. "field": ["count()", "issue"],
  2026. "query": "",
  2027. "interval": "1s",
  2028. "yAxis": "count()",
  2029. },
  2030. )
  2031. assert response.status_code == 200
  2032. assert mock_bulk_query.call_count == 1
  2033. with self.feature(self.enabled_features):
  2034. response = self.client.get(
  2035. self.url,
  2036. format="json",
  2037. data={
  2038. "end": iso_format(before_now()),
  2039. "start": iso_format(before_now(seconds=7200)),
  2040. "field": ["count()", "issue"],
  2041. "query": "",
  2042. "interval": "1s",
  2043. "yAxis": "count()",
  2044. # 7,200 points for each event * 2, should error
  2045. "topEvents": 2,
  2046. },
  2047. )
  2048. assert response.status_code == 200
  2049. assert mock_raw_query.call_count == 2
  2050. # Should've reset to the default for between 1 and 24h
  2051. assert mock_raw_query.mock_calls[1].args[0].query.granularity.granularity == 300
  2052. with self.feature(self.enabled_features):
  2053. response = self.client.get(
  2054. self.url,
  2055. format="json",
  2056. data={
  2057. "end": iso_format(before_now()),
  2058. # 1999 points * 5 events should just be enough to not error
  2059. "start": iso_format(before_now(seconds=1999)),
  2060. "field": ["count()", "issue"],
  2061. "query": "",
  2062. "interval": "1s",
  2063. "yAxis": "count()",
  2064. "topEvents": 5,
  2065. },
  2066. )
  2067. assert response.status_code == 200
  2068. assert mock_raw_query.call_count == 4
  2069. # Should've left the interval alone since we're just below the limit
  2070. assert mock_raw_query.mock_calls[3].args[0].query.granularity.granularity == 1
  2071. with self.feature(self.enabled_features):
  2072. response = self.client.get(
  2073. self.url,
  2074. format="json",
  2075. data={
  2076. "end": iso_format(before_now()),
  2077. "start": iso_format(before_now(hours=24)),
  2078. "field": ["count()", "issue"],
  2079. "query": "",
  2080. "interval": "0d",
  2081. "yAxis": "count()",
  2082. "topEvents": 5,
  2083. },
  2084. )
  2085. assert response.status_code == 200
  2086. assert mock_raw_query.call_count == 6
  2087. # Should've default to 24h's default of 5m
  2088. assert mock_raw_query.mock_calls[5].args[0].query.granularity.granularity == 300
  2089. def test_top_events_timestamp_fields(self):
  2090. with self.feature(self.enabled_features):
  2091. response = self.client.get(
  2092. self.url,
  2093. format="json",
  2094. data={
  2095. "start": iso_format(self.day_ago),
  2096. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2097. "interval": "1h",
  2098. "yAxis": "count()",
  2099. "orderby": ["-count()"],
  2100. "field": ["count()", "timestamp", "timestamp.to_hour", "timestamp.to_day"],
  2101. "topEvents": 5,
  2102. },
  2103. )
  2104. assert response.status_code == 200
  2105. data = response.data
  2106. assert len(data) == 3
  2107. # these are the timestamps corresponding to the events stored
  2108. timestamps = [
  2109. self.day_ago + timedelta(minutes=2),
  2110. self.day_ago + timedelta(hours=1, minutes=2),
  2111. self.day_ago + timedelta(minutes=4),
  2112. ]
  2113. timestamp_hours = [timestamp.replace(minute=0, second=0) for timestamp in timestamps]
  2114. timestamp_days = [timestamp.replace(hour=0, minute=0, second=0) for timestamp in timestamps]
  2115. for ts, ts_hr, ts_day in zip(timestamps, timestamp_hours, timestamp_days):
  2116. key = f"{iso_format(ts)}+00:00,{iso_format(ts_day)}+00:00,{iso_format(ts_hr)}+00:00"
  2117. count = sum(
  2118. e["count"] for e in self.event_data if e["data"]["timestamp"] == iso_format(ts)
  2119. )
  2120. results = data[key]
  2121. assert [{"count": count}] in [attrs for time, attrs in results["data"]]
  2122. def test_top_events_other_with_matching_columns(self):
  2123. with self.feature(self.enabled_features):
  2124. response = self.client.get(
  2125. self.url,
  2126. data={
  2127. "start": iso_format(self.day_ago),
  2128. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2129. "interval": "1h",
  2130. "yAxis": "count()",
  2131. "orderby": ["-count()"],
  2132. "field": ["count()", "tags[shared-tag]", "message"],
  2133. "topEvents": 5,
  2134. },
  2135. format="json",
  2136. )
  2137. data = response.data
  2138. assert response.status_code == 200, response.content
  2139. assert len(data) == 6
  2140. for index, event in enumerate(self.events[:5]):
  2141. message = event.message or event.transaction
  2142. results = data[",".join([message, "yup"])]
  2143. assert results["order"] == index
  2144. assert [{"count": self.event_data[index]["count"]}] in [
  2145. attrs for _, attrs in results["data"]
  2146. ]
  2147. other = data["Other"]
  2148. assert other["order"] == 5
  2149. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  2150. def test_top_events_with_field_overlapping_other_key(self):
  2151. transaction_data = load_data("transaction")
  2152. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  2153. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=6))
  2154. transaction_data["transaction"] = OTHER_KEY
  2155. for i in range(5):
  2156. data = transaction_data.copy()
  2157. data["event_id"] = "ab" + f"{i}" * 30
  2158. data["contexts"]["trace"]["span_id"] = "ab" + f"{i}" * 14
  2159. self.store_event(data, project_id=self.project.id)
  2160. with self.feature(self.enabled_features):
  2161. response = self.client.get(
  2162. self.url,
  2163. data={
  2164. "start": iso_format(self.day_ago),
  2165. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2166. "interval": "1h",
  2167. "yAxis": "count()",
  2168. "orderby": ["-count()"],
  2169. "field": ["count()", "message"],
  2170. "topEvents": 5,
  2171. },
  2172. format="json",
  2173. )
  2174. data = response.data
  2175. assert response.status_code == 200, response.content
  2176. assert len(data) == 6
  2177. assert f"{OTHER_KEY} (message)" in data
  2178. results = data[f"{OTHER_KEY} (message)"]
  2179. assert [{"count": 5}] in [attrs for _, attrs in results["data"]]
  2180. other = data["Other"]
  2181. assert other["order"] == 5
  2182. assert [{"count": 4}] in [attrs for _, attrs in other["data"]]
  2183. def test_top_events_can_exclude_other_series(self):
  2184. with self.feature(self.enabled_features):
  2185. response = self.client.get(
  2186. self.url,
  2187. data={
  2188. "start": iso_format(self.day_ago),
  2189. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2190. "interval": "1h",
  2191. "yAxis": "count()",
  2192. "orderby": ["count()"],
  2193. "field": ["count()", "message"],
  2194. "topEvents": 5,
  2195. "excludeOther": "1",
  2196. },
  2197. format="json",
  2198. )
  2199. data = response.data
  2200. assert response.status_code == 200, response.content
  2201. assert len(data) == 5
  2202. assert "Other" not in response.data
  2203. def test_top_events_with_equation_including_unselected_fields_passes_field_validation(self):
  2204. with self.feature(self.enabled_features):
  2205. response = self.client.get(
  2206. self.url,
  2207. data={
  2208. "start": iso_format(self.day_ago),
  2209. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2210. "interval": "1h",
  2211. "yAxis": "count()",
  2212. "orderby": ["-equation[0]"],
  2213. "field": ["count()", "message", "equation|count_unique(user) * 2"],
  2214. "topEvents": 5,
  2215. },
  2216. format="json",
  2217. )
  2218. data = response.data
  2219. assert response.status_code == 200, response.content
  2220. assert len(data) == 6
  2221. other = data["Other"]
  2222. assert other["order"] == 5
  2223. assert [{"count": 4}] in [attrs for _, attrs in other["data"]]
  2224. def test_top_events_boolean_condition_and_project_field(self):
  2225. with self.feature(self.enabled_features):
  2226. response = self.client.get(
  2227. self.url,
  2228. data={
  2229. "start": iso_format(self.day_ago),
  2230. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2231. "interval": "1h",
  2232. "yAxis": "count()",
  2233. "orderby": ["-count()"],
  2234. "field": ["project", "count()"],
  2235. "topEvents": 5,
  2236. "query": "event.type:transaction (transaction:*a OR transaction:b*)",
  2237. },
  2238. format="json",
  2239. )
  2240. assert response.status_code == 200