test_organization_events_stats.py 97 KB

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