test_organization_events_stats.py 90 KB

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