test_organization_events_stats.py 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207
  1. from __future__ import annotations
  2. import uuid
  3. from datetime import timedelta
  4. from typing import Any, TypedDict
  5. from unittest import mock
  6. from uuid import uuid4
  7. import pytest
  8. from dateutil.parser import parse as parse_date
  9. from django.urls import reverse
  10. from snuba_sdk import Entity
  11. from snuba_sdk.column import Column
  12. from snuba_sdk.conditions import Condition, Op
  13. from snuba_sdk.function import Function
  14. from sentry.constants import MAX_TOP_EVENTS
  15. from sentry.issues.grouptype import ProfileFileIOGroupType
  16. from sentry.models.project import Project
  17. from sentry.models.transaction_threshold import ProjectTransactionThreshold, TransactionMetric
  18. from sentry.snuba.discover import OTHER_KEY
  19. from sentry.testutils.cases import APITestCase, ProfilesSnubaTestCase, SnubaTestCase
  20. from sentry.testutils.helpers.datetime import before_now, iso_format
  21. from sentry.utils.samples import load_data
  22. from tests.sentry.issues.test_utils import SearchIssueTestMixin
  23. pytestmark = pytest.mark.sentry_metrics
  24. class _EventDataDict(TypedDict):
  25. data: dict[str, Any]
  26. project: Project
  27. count: int
  28. class OrganizationEventsStatsEndpointTest(APITestCase, SnubaTestCase, SearchIssueTestMixin):
  29. endpoint = "sentry-api-0-organization-events-stats"
  30. def setUp(self):
  31. super().setUp()
  32. self.login_as(user=self.user)
  33. self.authed_user = self.user
  34. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  35. self.project = self.create_project()
  36. self.project2 = self.create_project()
  37. self.user = self.create_user()
  38. self.user2 = self.create_user()
  39. with self.options({"issues.group_attributes.send_kafka": True}):
  40. self.store_event(
  41. data={
  42. "event_id": "a" * 32,
  43. "message": "very bad",
  44. "timestamp": iso_format(self.day_ago + timedelta(minutes=1)),
  45. "fingerprint": ["group1"],
  46. "tags": {"sentry:user": self.user.email},
  47. },
  48. project_id=self.project.id,
  49. )
  50. self.store_event(
  51. data={
  52. "event_id": "b" * 32,
  53. "message": "oh my",
  54. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=1)),
  55. "fingerprint": ["group2"],
  56. "tags": {"sentry:user": self.user2.email},
  57. },
  58. project_id=self.project2.id,
  59. )
  60. self.store_event(
  61. data={
  62. "event_id": "c" * 32,
  63. "message": "very bad",
  64. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=2)),
  65. "fingerprint": ["group2"],
  66. "tags": {"sentry:user": self.user2.email},
  67. },
  68. project_id=self.project2.id,
  69. )
  70. self.url = reverse(
  71. "sentry-api-0-organization-events-stats",
  72. kwargs={"organization_id_or_slug": self.project.organization.slug},
  73. )
  74. self.features = {}
  75. def do_request(self, data, url=None, features=None):
  76. if features is None:
  77. features = {"organizations:discover-basic": True}
  78. features.update(self.features)
  79. with self.feature(features):
  80. return self.client.get(self.url if url is None else url, data=data, format="json")
  81. @pytest.mark.querybuilder
  82. def test_simple(self):
  83. response = self.do_request(
  84. {
  85. "start": iso_format(self.day_ago),
  86. "end": iso_format(self.day_ago + timedelta(hours=2)),
  87. "interval": "1h",
  88. },
  89. )
  90. assert response.status_code == 200, response.content
  91. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  92. def test_generic_issue(self):
  93. _, _, group_info = self.store_search_issue(
  94. self.project.id,
  95. self.user.id,
  96. [f"{ProfileFileIOGroupType.type_id}-group1"],
  97. "prod",
  98. self.day_ago,
  99. )
  100. assert group_info is not None
  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 + timedelta(hours=1, minutes=1),
  107. )
  108. self.store_search_issue(
  109. self.project.id,
  110. self.user.id,
  111. [f"{ProfileFileIOGroupType.type_id}-group1"],
  112. "prod",
  113. self.day_ago + timedelta(hours=1, minutes=2),
  114. )
  115. with self.feature(
  116. [
  117. "organizations:profiling",
  118. ]
  119. ):
  120. response = self.do_request(
  121. {
  122. "start": iso_format(self.day_ago),
  123. "end": iso_format(self.day_ago + timedelta(hours=2)),
  124. "interval": "1h",
  125. "query": f"issue:{group_info.group.qualified_short_id}",
  126. "dataset": "issuePlatform",
  127. },
  128. )
  129. assert response.status_code == 200, response.content
  130. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  131. def test_generic_issue_calculated_interval(self):
  132. """Test that a 4h interval returns the correct generic event stats.
  133. This follows a different code path than 1h or 1d as the IssuePlatformTimeSeriesQueryBuilder
  134. does some calculation to create the time column."""
  135. _, _, group_info = self.store_search_issue(
  136. self.project.id,
  137. self.user.id,
  138. [f"{ProfileFileIOGroupType.type_id}-group1"],
  139. "prod",
  140. self.day_ago + timedelta(minutes=1),
  141. )
  142. assert group_info is not None
  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 + timedelta(minutes=1),
  149. )
  150. self.store_search_issue(
  151. self.project.id,
  152. self.user.id,
  153. [f"{ProfileFileIOGroupType.type_id}-group1"],
  154. "prod",
  155. self.day_ago + timedelta(minutes=2),
  156. )
  157. with self.feature(
  158. [
  159. "organizations:profiling",
  160. ]
  161. ):
  162. response = self.do_request(
  163. {
  164. "start": iso_format(self.day_ago),
  165. "end": iso_format(self.day_ago + timedelta(hours=4)),
  166. "interval": "4h",
  167. "query": f"issue:{group_info.group.qualified_short_id}",
  168. "dataset": "issuePlatform",
  169. },
  170. )
  171. assert response.status_code == 200, response.content
  172. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 3}], [{"count": 0}]]
  173. def test_errors_dataset(self):
  174. response = self.do_request(
  175. {
  176. "start": iso_format(self.day_ago),
  177. "end": iso_format(self.day_ago + timedelta(hours=2)),
  178. "interval": "1h",
  179. "dataset": "errors",
  180. "query": "is:unresolved",
  181. },
  182. )
  183. assert response.status_code == 200, response.content
  184. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  185. def test_errors_dataset_no_query(self):
  186. response = self.do_request(
  187. {
  188. "start": iso_format(self.day_ago),
  189. "end": iso_format(self.day_ago + timedelta(hours=2)),
  190. "interval": "1h",
  191. "dataset": "errors",
  192. },
  193. )
  194. assert response.status_code == 200, response.content
  195. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  196. def test_misaligned_last_bucket(self):
  197. response = self.do_request(
  198. data={
  199. "start": iso_format(self.day_ago - timedelta(minutes=30)),
  200. "end": iso_format(self.day_ago + timedelta(hours=1, minutes=30)),
  201. "interval": "1h",
  202. "partial": "1",
  203. },
  204. )
  205. assert response.status_code == 200, response.content
  206. assert [attrs for time, attrs in response.data["data"]] == [
  207. [{"count": 0}],
  208. [{"count": 1}],
  209. [{"count": 2}],
  210. ]
  211. def test_no_projects(self):
  212. org = self.create_organization(owner=self.user)
  213. self.login_as(user=self.user)
  214. url = reverse(
  215. "sentry-api-0-organization-events-stats", kwargs={"organization_id_or_slug": org.slug}
  216. )
  217. response = self.do_request({}, url)
  218. assert response.status_code == 200, response.content
  219. assert len(response.data["data"]) == 0
  220. def test_user_count(self):
  221. self.store_event(
  222. data={
  223. "event_id": "d" * 32,
  224. "message": "something",
  225. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  226. "tags": {"sentry:user": self.user2.email},
  227. "fingerprint": ["group2"],
  228. },
  229. project_id=self.project2.id,
  230. )
  231. response = self.do_request(
  232. data={
  233. "start": iso_format(self.day_ago),
  234. "end": iso_format(self.day_ago + timedelta(hours=2)),
  235. "interval": "1h",
  236. "yAxis": "user_count",
  237. },
  238. )
  239. assert response.status_code == 200, response.content
  240. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 2}], [{"count": 1}]]
  241. def test_discover2_backwards_compatibility(self):
  242. response = self.do_request(
  243. data={
  244. "project": self.project.id,
  245. "start": iso_format(self.day_ago),
  246. "end": iso_format(self.day_ago + timedelta(hours=2)),
  247. "interval": "1h",
  248. "yAxis": "user_count",
  249. },
  250. )
  251. assert response.status_code == 200, response.content
  252. assert len(response.data["data"]) > 0
  253. response = self.do_request(
  254. data={
  255. "project": self.project.id,
  256. "start": iso_format(self.day_ago),
  257. "end": iso_format(self.day_ago + timedelta(hours=2)),
  258. "interval": "1h",
  259. "yAxis": "event_count",
  260. },
  261. )
  262. assert response.status_code == 200, response.content
  263. assert len(response.data["data"]) > 0
  264. def test_with_event_count_flag(self):
  265. response = self.do_request(
  266. data={
  267. "start": iso_format(self.day_ago),
  268. "end": iso_format(self.day_ago + timedelta(hours=2)),
  269. "interval": "1h",
  270. "yAxis": "event_count",
  271. },
  272. )
  273. assert response.status_code == 200, response.content
  274. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  275. def test_performance_view_feature(self):
  276. response = self.do_request(
  277. data={
  278. "end": iso_format(before_now()),
  279. "start": iso_format(before_now(hours=2)),
  280. "query": "project_id:1",
  281. "interval": "30m",
  282. "yAxis": "count()",
  283. },
  284. features={
  285. "organizations:performance-view": True,
  286. "organizations:discover-basic": False,
  287. },
  288. )
  289. assert response.status_code == 200, response.content
  290. def test_apdex_divide_by_zero(self):
  291. ProjectTransactionThreshold.objects.create(
  292. project=self.project,
  293. organization=self.project.organization,
  294. threshold=600,
  295. metric=TransactionMetric.LCP.value,
  296. )
  297. # Shouldn't count towards apdex
  298. data = load_data(
  299. "transaction",
  300. start_timestamp=self.day_ago + timedelta(minutes=(1)),
  301. timestamp=self.day_ago + timedelta(minutes=(3)),
  302. )
  303. data["transaction"] = "/apdex/new/"
  304. data["user"] = {"email": "1@example.com"}
  305. data["measurements"] = {}
  306. self.store_event(data, project_id=self.project.id)
  307. response = self.do_request(
  308. data={
  309. "start": iso_format(self.day_ago),
  310. "end": iso_format(self.day_ago + timedelta(hours=2)),
  311. "interval": "1h",
  312. "yAxis": "apdex()",
  313. "project": [self.project.id],
  314. },
  315. )
  316. assert response.status_code == 200, response.content
  317. assert len(response.data["data"]) == 2
  318. data = response.data["data"]
  319. # 0 transactions with LCP 0/0
  320. assert [attrs for time, attrs in response.data["data"]] == [
  321. [{"count": 0}],
  322. [{"count": 0}],
  323. ]
  324. def test_aggregate_function_apdex(self):
  325. project1 = self.create_project()
  326. project2 = self.create_project()
  327. events = [
  328. ("one", 400, project1.id),
  329. ("one", 400, project1.id),
  330. ("two", 3000, project2.id),
  331. ("two", 1000, project2.id),
  332. ("three", 3000, project2.id),
  333. ]
  334. for idx, event in enumerate(events):
  335. data = load_data(
  336. "transaction",
  337. start_timestamp=self.day_ago + timedelta(minutes=(1 + idx)),
  338. timestamp=self.day_ago + timedelta(minutes=(1 + idx), milliseconds=event[1]),
  339. )
  340. data["event_id"] = f"{idx}" * 32
  341. data["transaction"] = f"/apdex/new/{event[0]}"
  342. data["user"] = {"email": f"{idx}@example.com"}
  343. self.store_event(data, project_id=event[2])
  344. response = self.do_request(
  345. data={
  346. "start": iso_format(self.day_ago),
  347. "end": iso_format(self.day_ago + timedelta(hours=2)),
  348. "interval": "1h",
  349. "yAxis": "apdex()",
  350. },
  351. )
  352. assert response.status_code == 200, response.content
  353. assert [attrs for time, attrs in response.data["data"]] == [
  354. [{"count": 0.3}],
  355. [{"count": 0}],
  356. ]
  357. ProjectTransactionThreshold.objects.create(
  358. project=project1,
  359. organization=project1.organization,
  360. threshold=100,
  361. metric=TransactionMetric.DURATION.value,
  362. )
  363. ProjectTransactionThreshold.objects.create(
  364. project=project2,
  365. organization=project1.organization,
  366. threshold=100,
  367. metric=TransactionMetric.DURATION.value,
  368. )
  369. response = self.do_request(
  370. data={
  371. "start": iso_format(self.day_ago),
  372. "end": iso_format(self.day_ago + timedelta(hours=2)),
  373. "interval": "1h",
  374. "yAxis": "apdex()",
  375. },
  376. )
  377. assert response.status_code == 200, response.content
  378. assert [attrs for time, attrs in response.data["data"]] == [
  379. [{"count": 0.2}],
  380. [{"count": 0}],
  381. ]
  382. response = self.do_request(
  383. data={
  384. "start": iso_format(self.day_ago),
  385. "end": iso_format(self.day_ago + timedelta(hours=2)),
  386. "interval": "1h",
  387. "yAxis": ["user_count", "apdex()"],
  388. },
  389. )
  390. assert response.status_code == 200, response.content
  391. assert response.data["user_count"]["order"] == 0
  392. assert [attrs for time, attrs in response.data["user_count"]["data"]] == [
  393. [{"count": 5}],
  394. [{"count": 0}],
  395. ]
  396. assert response.data["apdex()"]["order"] == 1
  397. assert [attrs for time, attrs in response.data["apdex()"]["data"]] == [
  398. [{"count": 0.2}],
  399. [{"count": 0}],
  400. ]
  401. def test_aggregate_function_count(self):
  402. response = self.do_request(
  403. data={
  404. "start": iso_format(self.day_ago),
  405. "end": iso_format(self.day_ago + timedelta(hours=2)),
  406. "interval": "1h",
  407. "yAxis": "count()",
  408. },
  409. )
  410. assert response.status_code == 200, response.content
  411. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 2}]]
  412. def test_invalid_aggregate(self):
  413. response = self.do_request(
  414. data={
  415. "start": iso_format(self.day_ago),
  416. "end": iso_format(self.day_ago + timedelta(hours=2)),
  417. "interval": "1h",
  418. "yAxis": "rubbish",
  419. },
  420. )
  421. assert response.status_code == 400, response.content
  422. def test_aggregate_function_user_count(self):
  423. response = self.do_request(
  424. data={
  425. "start": iso_format(self.day_ago),
  426. "end": iso_format(self.day_ago + timedelta(hours=2)),
  427. "interval": "1h",
  428. "yAxis": "count_unique(user)",
  429. },
  430. )
  431. assert response.status_code == 200, response.content
  432. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 1}], [{"count": 1}]]
  433. def test_aggregate_invalid(self):
  434. response = self.do_request(
  435. data={
  436. "start": iso_format(self.day_ago),
  437. "end": iso_format(self.day_ago + timedelta(hours=2)),
  438. "interval": "1h",
  439. "yAxis": "nope(lol)",
  440. },
  441. )
  442. assert response.status_code == 400, response.content
  443. def test_throughput_epm_hour_rollup(self):
  444. project = self.create_project()
  445. # Each of these denotes how many events to create in each hour
  446. event_counts = [6, 0, 6, 3, 0, 3]
  447. for hour, count in enumerate(event_counts):
  448. for minute in range(count):
  449. self.store_event(
  450. data={
  451. "event_id": str(uuid.uuid1()),
  452. "message": "very bad",
  453. "timestamp": iso_format(
  454. self.day_ago + timedelta(hours=hour, minutes=minute)
  455. ),
  456. "fingerprint": ["group1"],
  457. "tags": {"sentry:user": self.user.email},
  458. },
  459. project_id=project.id,
  460. )
  461. for axis in ["epm()", "tpm()"]:
  462. response = self.do_request(
  463. data={
  464. "start": iso_format(self.day_ago),
  465. "end": iso_format(self.day_ago + timedelta(hours=6)),
  466. "interval": "1h",
  467. "yAxis": axis,
  468. "project": project.id,
  469. },
  470. )
  471. assert response.status_code == 200, response.content
  472. data = response.data["data"]
  473. assert len(data) == 6
  474. rows = data[0:6]
  475. for test in zip(event_counts, rows):
  476. assert test[1][1][0]["count"] == test[0] / (3600.0 / 60.0)
  477. def test_throughput_epm_day_rollup(self):
  478. project = self.create_project()
  479. # Each of these denotes how many events to create in each minute
  480. event_counts = [6, 0, 6, 3, 0, 3]
  481. for hour, count in enumerate(event_counts):
  482. for minute in range(count):
  483. self.store_event(
  484. data={
  485. "event_id": str(uuid.uuid1()),
  486. "message": "very bad",
  487. "timestamp": iso_format(
  488. self.day_ago + timedelta(hours=hour, minutes=minute)
  489. ),
  490. "fingerprint": ["group1"],
  491. "tags": {"sentry:user": self.user.email},
  492. },
  493. project_id=project.id,
  494. )
  495. for axis in ["epm()", "tpm()"]:
  496. response = self.do_request(
  497. data={
  498. "start": iso_format(self.day_ago),
  499. "end": iso_format(self.day_ago + timedelta(hours=24)),
  500. "interval": "24h",
  501. "yAxis": axis,
  502. "project": project.id,
  503. },
  504. )
  505. assert response.status_code == 200, response.content
  506. data = response.data["data"]
  507. assert len(data) == 2
  508. assert data[0][1][0]["count"] == sum(event_counts) / (86400.0 / 60.0)
  509. def test_throughput_eps_minute_rollup(self):
  510. project = self.create_project()
  511. # Each of these denotes how many events to create in each minute
  512. event_counts = [6, 0, 6, 3, 0, 3]
  513. for minute, count in enumerate(event_counts):
  514. for second in range(count):
  515. self.store_event(
  516. data={
  517. "event_id": str(uuid.uuid1()),
  518. "message": "very bad",
  519. "timestamp": iso_format(
  520. self.day_ago + timedelta(minutes=minute, seconds=second)
  521. ),
  522. "fingerprint": ["group1"],
  523. "tags": {"sentry:user": self.user.email},
  524. },
  525. project_id=project.id,
  526. )
  527. for axis in ["eps()", "tps()"]:
  528. response = self.do_request(
  529. data={
  530. "start": iso_format(self.day_ago),
  531. "end": iso_format(self.day_ago + timedelta(minutes=6)),
  532. "interval": "1m",
  533. "yAxis": axis,
  534. "project": project.id,
  535. },
  536. )
  537. assert response.status_code == 200, response.content
  538. data = response.data["data"]
  539. assert len(data) == 6
  540. rows = data[0:6]
  541. for test in zip(event_counts, rows):
  542. assert test[1][1][0]["count"] == test[0] / 60.0
  543. def test_throughput_eps_no_rollup(self):
  544. project = self.create_project()
  545. # Each of these denotes how many events to create in each minute
  546. event_counts = [6, 0, 6, 3, 0, 3]
  547. for minute, count in enumerate(event_counts):
  548. for second in range(count):
  549. self.store_event(
  550. data={
  551. "event_id": str(uuid.uuid1()),
  552. "message": "very bad",
  553. "timestamp": iso_format(
  554. self.day_ago + timedelta(minutes=minute, seconds=second)
  555. ),
  556. "fingerprint": ["group1"],
  557. "tags": {"sentry:user": self.user.email},
  558. },
  559. project_id=project.id,
  560. )
  561. response = self.do_request(
  562. data={
  563. "start": iso_format(self.day_ago),
  564. "end": iso_format(self.day_ago + timedelta(minutes=1)),
  565. "interval": "1s",
  566. "yAxis": "eps()",
  567. "project": project.id,
  568. },
  569. )
  570. assert response.status_code == 200, response.content
  571. data = response.data["data"]
  572. # expect 60 data points between time span of 0 and 60 seconds
  573. assert len(data) == 60
  574. rows = data[0:6]
  575. for row in rows:
  576. assert row[1][0]["count"] == 1
  577. def test_transaction_events(self):
  578. prototype = {
  579. "type": "transaction",
  580. "transaction": "api.issue.delete",
  581. "spans": [],
  582. "contexts": {"trace": {"op": "foobar", "trace_id": "a" * 32, "span_id": "a" * 16}},
  583. "tags": {"important": "yes"},
  584. }
  585. fixtures = (
  586. ("d" * 32, before_now(minutes=32)),
  587. ("e" * 32, before_now(hours=1, minutes=2)),
  588. ("f" * 32, before_now(hours=1, minutes=35)),
  589. )
  590. for fixture in fixtures:
  591. data = prototype.copy()
  592. data["event_id"] = fixture[0]
  593. data["timestamp"] = iso_format(fixture[1])
  594. data["start_timestamp"] = iso_format(fixture[1] - timedelta(seconds=1))
  595. self.store_event(data=data, project_id=self.project.id)
  596. for dataset in ["discover", "transactions"]:
  597. response = self.do_request(
  598. data={
  599. "project": self.project.id,
  600. "end": iso_format(before_now()),
  601. "start": iso_format(before_now(hours=2)),
  602. "query": "event.type:transaction",
  603. "interval": "30m",
  604. "yAxis": "count()",
  605. "dataset": dataset,
  606. },
  607. )
  608. assert response.status_code == 200, response.content
  609. items = [item for time, item in response.data["data"] if item]
  610. # We could get more results depending on where the 30 min
  611. # windows land.
  612. assert len(items) >= 3
  613. def test_project_id_query_filter(self):
  614. response = self.do_request(
  615. data={
  616. "end": iso_format(before_now()),
  617. "start": iso_format(before_now(hours=2)),
  618. "query": "project_id:1",
  619. "interval": "30m",
  620. "yAxis": "count()",
  621. },
  622. )
  623. assert response.status_code == 200
  624. def test_latest_release_query_filter(self):
  625. response = self.do_request(
  626. data={
  627. "project": self.project.id,
  628. "end": iso_format(before_now()),
  629. "start": iso_format(before_now(hours=2)),
  630. "query": "release:latest",
  631. "interval": "30m",
  632. "yAxis": "count()",
  633. },
  634. )
  635. assert response.status_code == 200
  636. def test_conditional_filter(self):
  637. response = self.do_request(
  638. data={
  639. "start": iso_format(self.day_ago),
  640. "end": iso_format(self.day_ago + timedelta(hours=2)),
  641. "query": "id:{} OR id:{}".format("a" * 32, "b" * 32),
  642. "interval": "30m",
  643. "yAxis": "count()",
  644. },
  645. )
  646. assert response.status_code == 200, response.content
  647. data = response.data["data"]
  648. assert len(data) == 4
  649. assert data[0][1][0]["count"] == 1
  650. assert data[2][1][0]["count"] == 1
  651. def test_simple_multiple_yaxis(self):
  652. response = self.do_request(
  653. data={
  654. "start": iso_format(self.day_ago),
  655. "end": iso_format(self.day_ago + timedelta(hours=2)),
  656. "interval": "1h",
  657. "yAxis": ["user_count", "event_count"],
  658. },
  659. )
  660. assert response.status_code == 200, response.content
  661. assert response.data["user_count"]["order"] == 0
  662. assert [attrs for time, attrs in response.data["user_count"]["data"]] == [
  663. [{"count": 1}],
  664. [{"count": 1}],
  665. ]
  666. assert response.data["event_count"]["order"] == 1
  667. assert [attrs for time, attrs in response.data["event_count"]["data"]] == [
  668. [{"count": 1}],
  669. [{"count": 2}],
  670. ]
  671. def test_equation_yaxis(self):
  672. response = self.do_request(
  673. data={
  674. "start": iso_format(self.day_ago),
  675. "end": iso_format(self.day_ago + timedelta(hours=2)),
  676. "interval": "1h",
  677. "yAxis": ["equation|count() / 100"],
  678. },
  679. )
  680. assert response.status_code == 200, response.content
  681. assert len(response.data["data"]) == 2
  682. assert [attrs for time, attrs in response.data["data"]] == [
  683. [{"count": 0.01}],
  684. [{"count": 0.02}],
  685. ]
  686. def test_equation_mixed_multi_yaxis(self):
  687. response = self.do_request(
  688. data={
  689. "start": iso_format(self.day_ago),
  690. "end": iso_format(self.day_ago + timedelta(hours=2)),
  691. "interval": "1h",
  692. "yAxis": ["count()", "equation|count() * 100"],
  693. },
  694. )
  695. assert response.status_code == 200, response.content
  696. assert response.data["count()"]["order"] == 0
  697. assert [attrs for time, attrs in response.data["count()"]["data"]] == [
  698. [{"count": 1}],
  699. [{"count": 2}],
  700. ]
  701. assert response.data["equation|count() * 100"]["order"] == 1
  702. assert [attrs for time, attrs in response.data["equation|count() * 100"]["data"]] == [
  703. [{"count": 100}],
  704. [{"count": 200}],
  705. ]
  706. def test_equation_multi_yaxis(self):
  707. response = self.do_request(
  708. data={
  709. "start": iso_format(self.day_ago),
  710. "end": iso_format(self.day_ago + timedelta(hours=2)),
  711. "interval": "1h",
  712. "yAxis": ["equation|count() / 100", "equation|count() * 100"],
  713. },
  714. )
  715. assert response.status_code == 200, response.content
  716. assert response.data["equation|count() / 100"]["order"] == 0
  717. assert [attrs for time, attrs in response.data["equation|count() / 100"]["data"]] == [
  718. [{"count": 0.01}],
  719. [{"count": 0.02}],
  720. ]
  721. assert response.data["equation|count() * 100"]["order"] == 1
  722. assert [attrs for time, attrs in response.data["equation|count() * 100"]["data"]] == [
  723. [{"count": 100}],
  724. [{"count": 200}],
  725. ]
  726. def test_large_interval_no_drop_values(self):
  727. self.store_event(
  728. data={
  729. "event_id": "d" * 32,
  730. "message": "not good",
  731. "timestamp": iso_format(self.day_ago - timedelta(minutes=10)),
  732. "fingerprint": ["group3"],
  733. },
  734. project_id=self.project.id,
  735. )
  736. response = self.do_request(
  737. data={
  738. "project": self.project.id,
  739. "end": iso_format(self.day_ago),
  740. "start": iso_format(self.day_ago - timedelta(hours=24)),
  741. "query": 'message:"not good"',
  742. "interval": "1d",
  743. "yAxis": "count()",
  744. },
  745. )
  746. assert response.status_code == 200
  747. assert [attrs for time, attrs in response.data["data"]] == [[{"count": 0}], [{"count": 1}]]
  748. @mock.patch("sentry.snuba.discover.timeseries_query", return_value={})
  749. def test_multiple_yaxis_only_one_query(self, mock_query):
  750. self.do_request(
  751. data={
  752. "project": self.project.id,
  753. "start": iso_format(self.day_ago),
  754. "end": iso_format(self.day_ago + timedelta(hours=2)),
  755. "interval": "1h",
  756. "yAxis": ["user_count", "event_count", "epm()", "eps()"],
  757. },
  758. )
  759. assert mock_query.call_count == 1
  760. @mock.patch("sentry.snuba.discover.bulk_snuba_queries", return_value=[{"data": []}])
  761. def test_invalid_interval(self, mock_query):
  762. self.do_request(
  763. data={
  764. "end": iso_format(before_now()),
  765. "start": iso_format(before_now(hours=24)),
  766. "query": "",
  767. "interval": "1s",
  768. "yAxis": "count()",
  769. },
  770. )
  771. assert mock_query.call_count == 1
  772. # Should've reset to the default for 24h
  773. assert mock_query.mock_calls[0].args[0][0].query.granularity.granularity == 300
  774. self.do_request(
  775. data={
  776. "end": iso_format(before_now()),
  777. "start": iso_format(before_now(hours=24)),
  778. "query": "",
  779. "interval": "0d",
  780. "yAxis": "count()",
  781. },
  782. )
  783. assert mock_query.call_count == 2
  784. # Should've reset to the default for 24h
  785. assert mock_query.mock_calls[1].args[0][0].query.granularity.granularity == 300
  786. def test_out_of_retention(self):
  787. with self.options({"system.event-retention-days": 10}):
  788. response = self.do_request(
  789. data={
  790. "start": iso_format(before_now(days=20)),
  791. "end": iso_format(before_now(days=15)),
  792. "query": "",
  793. "interval": "30m",
  794. "yAxis": "count()",
  795. },
  796. )
  797. assert response.status_code == 400
  798. @mock.patch("sentry.utils.snuba.quantize_time")
  799. def test_quantize_dates(self, mock_quantize):
  800. mock_quantize.return_value = before_now(days=1)
  801. # Don't quantize short time periods
  802. self.do_request(
  803. data={"statsPeriod": "1h", "query": "", "interval": "30m", "yAxis": "count()"},
  804. )
  805. # Don't quantize absolute date periods
  806. self.do_request(
  807. data={
  808. "start": iso_format(before_now(days=20)),
  809. "end": iso_format(before_now(days=15)),
  810. "query": "",
  811. "interval": "30m",
  812. "yAxis": "count()",
  813. },
  814. )
  815. assert len(mock_quantize.mock_calls) == 0
  816. # Quantize long date periods
  817. self.do_request(
  818. data={"statsPeriod": "90d", "query": "", "interval": "30m", "yAxis": "count()"},
  819. )
  820. assert len(mock_quantize.mock_calls) == 2
  821. def test_with_zerofill(self):
  822. response = self.do_request(
  823. data={
  824. "start": iso_format(self.day_ago),
  825. "end": iso_format(self.day_ago + timedelta(hours=2)),
  826. "interval": "30m",
  827. },
  828. )
  829. assert response.status_code == 200, response.content
  830. assert [attrs for time, attrs in response.data["data"]] == [
  831. [{"count": 1}],
  832. [{"count": 0}],
  833. [{"count": 2}],
  834. [{"count": 0}],
  835. ]
  836. def test_without_zerofill(self):
  837. start = iso_format(self.day_ago)
  838. end = iso_format(self.day_ago + timedelta(hours=2))
  839. response = self.do_request(
  840. data={
  841. "start": start,
  842. "end": end,
  843. "interval": "30m",
  844. "withoutZerofill": "1",
  845. },
  846. features={
  847. "organizations:performance-chart-interpolation": True,
  848. "organizations:discover-basic": True,
  849. },
  850. )
  851. assert response.status_code == 200, response.content
  852. assert [attrs for time, attrs in response.data["data"]] == [
  853. [{"count": 1}],
  854. [{"count": 2}],
  855. ]
  856. assert response.data["start"] == parse_date(start).timestamp()
  857. assert response.data["end"] == parse_date(end).timestamp()
  858. def test_comparison_error_dataset(self):
  859. self.store_event(
  860. data={
  861. "timestamp": iso_format(self.day_ago + timedelta(days=-1, minutes=1)),
  862. },
  863. project_id=self.project.id,
  864. )
  865. self.store_event(
  866. data={
  867. "timestamp": iso_format(self.day_ago + timedelta(days=-1, minutes=2)),
  868. },
  869. project_id=self.project.id,
  870. )
  871. self.store_event(
  872. data={
  873. "timestamp": iso_format(self.day_ago + timedelta(days=-1, hours=1, minutes=1)),
  874. },
  875. project_id=self.project2.id,
  876. )
  877. response = self.do_request(
  878. data={
  879. "start": iso_format(self.day_ago),
  880. "end": iso_format(self.day_ago + timedelta(hours=2)),
  881. "interval": "1h",
  882. "comparisonDelta": int(timedelta(days=1).total_seconds()),
  883. "dataset": "errors",
  884. }
  885. )
  886. assert response.status_code == 200, response.content
  887. assert [attrs for time, attrs in response.data["data"]] == [
  888. [{"count": 1, "comparisonCount": 2}],
  889. [{"count": 2, "comparisonCount": 1}],
  890. ]
  891. def test_comparison(self):
  892. self.store_event(
  893. data={
  894. "timestamp": iso_format(self.day_ago + timedelta(days=-1, minutes=1)),
  895. },
  896. project_id=self.project.id,
  897. )
  898. self.store_event(
  899. data={
  900. "timestamp": iso_format(self.day_ago + timedelta(days=-1, minutes=2)),
  901. },
  902. project_id=self.project.id,
  903. )
  904. self.store_event(
  905. data={
  906. "timestamp": iso_format(self.day_ago + timedelta(days=-1, hours=1, minutes=1)),
  907. },
  908. project_id=self.project2.id,
  909. )
  910. response = self.do_request(
  911. data={
  912. "start": iso_format(self.day_ago),
  913. "end": iso_format(self.day_ago + timedelta(hours=2)),
  914. "interval": "1h",
  915. "comparisonDelta": int(timedelta(days=1).total_seconds()),
  916. }
  917. )
  918. assert response.status_code == 200, response.content
  919. assert [attrs for time, attrs in response.data["data"]] == [
  920. [{"count": 1, "comparisonCount": 2}],
  921. [{"count": 2, "comparisonCount": 1}],
  922. ]
  923. def test_comparison_invalid(self):
  924. response = self.do_request(
  925. data={
  926. "start": iso_format(self.day_ago),
  927. "end": iso_format(self.day_ago + timedelta(hours=2)),
  928. "interval": "1h",
  929. "comparisonDelta": "17h",
  930. },
  931. )
  932. assert response.status_code == 400, response.content
  933. assert response.data["detail"] == "comparisonDelta must be an integer"
  934. start = before_now(days=85)
  935. end = start + timedelta(days=7)
  936. with self.options({"system.event-retention-days": 90}):
  937. response = self.do_request(
  938. data={
  939. "start": iso_format(start),
  940. "end": iso_format(end),
  941. "interval": "1h",
  942. "comparisonDelta": int(timedelta(days=7).total_seconds()),
  943. }
  944. )
  945. assert response.status_code == 400, response.content
  946. assert response.data["detail"] == "Comparison period is outside retention window"
  947. def test_equations_divide_by_zero(self):
  948. response = self.do_request(
  949. data={
  950. "start": iso_format(self.day_ago),
  951. "end": iso_format(self.day_ago + timedelta(hours=2)),
  952. "interval": "1h",
  953. # force a 0 in the denominator by doing 1 - 1
  954. # since a 0 literal is illegal as the denominator
  955. "yAxis": ["equation|count() / (1-1)"],
  956. },
  957. )
  958. assert response.status_code == 200, response.content
  959. assert len(response.data["data"]) == 2
  960. assert [attrs for time, attrs in response.data["data"]] == [
  961. [{"count": None}],
  962. [{"count": None}],
  963. ]
  964. @mock.patch("sentry.search.events.builder.base.raw_snql_query")
  965. def test_profiles_dataset_simple(self, mock_snql_query):
  966. mock_snql_query.side_effect = [{"meta": {}, "data": []}]
  967. query = {
  968. "yAxis": [
  969. "count()",
  970. "p75()",
  971. "p95()",
  972. "p99()",
  973. "p75(profile.duration)",
  974. "p95(profile.duration)",
  975. "p99(profile.duration)",
  976. ],
  977. "project": [self.project.id],
  978. "dataset": "profiles",
  979. }
  980. response = self.do_request(query, features={"organizations:profiling": True})
  981. assert response.status_code == 200, response.content
  982. def test_tag_with_conflicting_function_alias_simple(self):
  983. for _ in range(7):
  984. self.store_event(
  985. data={
  986. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  987. "tags": {"count": "9001"},
  988. },
  989. project_id=self.project2.id,
  990. )
  991. # Query for count and count()
  992. data = {
  993. "start": iso_format(self.day_ago),
  994. "end": iso_format(self.day_ago + timedelta(minutes=3)),
  995. "interval": "1h",
  996. "yAxis": "count()",
  997. "orderby": ["-count()"],
  998. "field": ["count()", "count"],
  999. "partial": 1,
  1000. }
  1001. response = self.client.get(self.url, data, format="json")
  1002. assert response.status_code == 200
  1003. # Expect a count of 8 because one event from setUp
  1004. assert response.data["data"][0][1] == [{"count": 8}]
  1005. data["query"] = "count:9001"
  1006. response = self.client.get(self.url, data, format="json")
  1007. assert response.status_code == 200
  1008. assert response.data["data"][0][1] == [{"count": 7}]
  1009. data["query"] = "count:abc"
  1010. response = self.client.get(self.url, data, format="json")
  1011. assert response.status_code == 200
  1012. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  1013. def test_group_id_tag_simple(self):
  1014. event_data: _EventDataDict = {
  1015. "data": {
  1016. "message": "poof",
  1017. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1018. "user": {"email": self.user.email},
  1019. "tags": {"group_id": "testing"},
  1020. "fingerprint": ["group1"],
  1021. },
  1022. "project": self.project2,
  1023. "count": 7,
  1024. }
  1025. for i in range(event_data["count"]):
  1026. event_data["data"]["event_id"] = f"a{i}" * 16
  1027. self.store_event(event_data["data"], project_id=event_data["project"].id)
  1028. data = {
  1029. "start": iso_format(self.day_ago),
  1030. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1031. "interval": "1h",
  1032. "yAxis": "count()",
  1033. "orderby": ["-count()"],
  1034. "field": ["count()", "group_id"],
  1035. "partial": 1,
  1036. }
  1037. response = self.client.get(self.url, data, format="json")
  1038. assert response.status_code == 200
  1039. assert response.data["data"][0][1] == [{"count": 8}]
  1040. data["query"] = "group_id:testing"
  1041. response = self.client.get(self.url, data, format="json")
  1042. assert response.status_code == 200
  1043. assert response.data["data"][0][1] == [{"count": 7}]
  1044. data["query"] = "group_id:abc"
  1045. response = self.client.get(self.url, data, format="json")
  1046. assert response.status_code == 200
  1047. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  1048. class OrganizationEventsStatsTopNEvents(APITestCase, SnubaTestCase):
  1049. def setUp(self):
  1050. super().setUp()
  1051. self.login_as(user=self.user)
  1052. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  1053. self.project = self.create_project()
  1054. self.project2 = self.create_project()
  1055. self.user2 = self.create_user()
  1056. transaction_data = load_data("transaction")
  1057. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1058. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=4))
  1059. transaction_data["tags"] = {"shared-tag": "yup"}
  1060. self.event_data: list[_EventDataDict] = [
  1061. {
  1062. "data": {
  1063. "message": "poof",
  1064. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1065. "user": {"email": self.user.email},
  1066. "tags": {"shared-tag": "yup"},
  1067. "fingerprint": ["group1"],
  1068. },
  1069. "project": self.project2,
  1070. "count": 7,
  1071. },
  1072. {
  1073. "data": {
  1074. "message": "voof",
  1075. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=2)),
  1076. "fingerprint": ["group2"],
  1077. "user": {"email": self.user2.email},
  1078. "tags": {"shared-tag": "yup"},
  1079. },
  1080. "project": self.project2,
  1081. "count": 6,
  1082. },
  1083. {
  1084. "data": {
  1085. "message": "very bad",
  1086. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1087. "fingerprint": ["group3"],
  1088. "user": {"email": "foo@example.com"},
  1089. "tags": {"shared-tag": "yup"},
  1090. },
  1091. "project": self.project,
  1092. "count": 5,
  1093. },
  1094. {
  1095. "data": {
  1096. "message": "oh no",
  1097. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1098. "fingerprint": ["group4"],
  1099. "user": {"email": "bar@example.com"},
  1100. "tags": {"shared-tag": "yup"},
  1101. },
  1102. "project": self.project,
  1103. "count": 4,
  1104. },
  1105. {"data": transaction_data, "project": self.project, "count": 3},
  1106. # Not in the top 5
  1107. {
  1108. "data": {
  1109. "message": "sorta bad",
  1110. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1111. "fingerprint": ["group5"],
  1112. "user": {"email": "bar@example.com"},
  1113. "tags": {"shared-tag": "yup"},
  1114. },
  1115. "project": self.project,
  1116. "count": 2,
  1117. },
  1118. {
  1119. "data": {
  1120. "message": "not so bad",
  1121. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1122. "fingerprint": ["group6"],
  1123. "user": {"email": "bar@example.com"},
  1124. "tags": {"shared-tag": "yup"},
  1125. },
  1126. "project": self.project,
  1127. "count": 1,
  1128. },
  1129. ]
  1130. self.events = []
  1131. for index, event_data in enumerate(self.event_data):
  1132. data = event_data["data"].copy()
  1133. for i in range(event_data["count"]):
  1134. data["event_id"] = f"{index}{i}" * 16
  1135. event = self.store_event(data, project_id=event_data["project"].id)
  1136. self.events.append(event)
  1137. self.transaction = self.events[4]
  1138. self.enabled_features = {
  1139. "organizations:discover-basic": True,
  1140. }
  1141. self.url = reverse(
  1142. "sentry-api-0-organization-events-stats",
  1143. kwargs={"organization_id_or_slug": self.project.organization.slug},
  1144. )
  1145. def test_no_top_events_with_project_field(self):
  1146. project = self.create_project()
  1147. with self.feature(self.enabled_features):
  1148. response = self.client.get(
  1149. self.url,
  1150. data={
  1151. # make sure to query the project with 0 events
  1152. "project": project.id,
  1153. "start": iso_format(self.day_ago),
  1154. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1155. "interval": "1h",
  1156. "yAxis": "count()",
  1157. "orderby": ["-count()"],
  1158. "field": ["count()", "project"],
  1159. "topEvents": 5,
  1160. },
  1161. format="json",
  1162. )
  1163. assert response.status_code == 200, response.content
  1164. # When there are no top events, we do not return an empty dict.
  1165. # Instead, we return a single zero-filled series for an empty graph.
  1166. data = response.data["data"]
  1167. assert [attrs for time, attrs in data] == [[{"count": 0}], [{"count": 0}]]
  1168. def test_no_top_events(self):
  1169. project = self.create_project()
  1170. with self.feature(self.enabled_features):
  1171. response = self.client.get(
  1172. self.url,
  1173. data={
  1174. # make sure to query the project with 0 events
  1175. "project": project.id,
  1176. "start": iso_format(self.day_ago),
  1177. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1178. "interval": "1h",
  1179. "yAxis": "count()",
  1180. "orderby": ["-count()"],
  1181. "field": ["count()", "message", "user.email"],
  1182. "topEvents": 5,
  1183. },
  1184. format="json",
  1185. )
  1186. data = response.data["data"]
  1187. assert response.status_code == 200, response.content
  1188. # When there are no top events, we do not return an empty dict.
  1189. # Instead, we return a single zero-filled series for an empty graph.
  1190. assert [attrs for time, attrs in data] == [[{"count": 0}], [{"count": 0}]]
  1191. def test_no_top_events_with_multi_axis(self):
  1192. project = self.create_project()
  1193. with self.feature(self.enabled_features):
  1194. response = self.client.get(
  1195. self.url,
  1196. data={
  1197. # make sure to query the project with 0 events
  1198. "project": project.id,
  1199. "start": iso_format(self.day_ago),
  1200. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1201. "interval": "1h",
  1202. "yAxis": ["count()", "count_unique(user)"],
  1203. "orderby": ["-count()"],
  1204. "field": ["count()", "count_unique(user)", "message", "user.email"],
  1205. "topEvents": 5,
  1206. },
  1207. format="json",
  1208. )
  1209. assert response.status_code == 200
  1210. data = response.data[""]
  1211. assert [attrs for time, attrs in data["count()"]["data"]] == [
  1212. [{"count": 0}],
  1213. [{"count": 0}],
  1214. ]
  1215. assert [attrs for time, attrs in data["count_unique(user)"]["data"]] == [
  1216. [{"count": 0}],
  1217. [{"count": 0}],
  1218. ]
  1219. def test_simple_top_events(self):
  1220. with self.feature(self.enabled_features):
  1221. response = self.client.get(
  1222. self.url,
  1223. data={
  1224. "start": iso_format(self.day_ago),
  1225. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1226. "interval": "1h",
  1227. "yAxis": "count()",
  1228. "orderby": ["-count()"],
  1229. "field": ["count()", "message", "user.email"],
  1230. "topEvents": 5,
  1231. },
  1232. format="json",
  1233. )
  1234. data = response.data
  1235. assert response.status_code == 200, response.content
  1236. assert len(data) == 6
  1237. for index, event in enumerate(self.events[:5]):
  1238. message = event.message or event.transaction
  1239. results = data[
  1240. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  1241. ]
  1242. assert results["order"] == index
  1243. assert [{"count": self.event_data[index]["count"]}] in [
  1244. attrs for _, attrs in results["data"]
  1245. ]
  1246. other = data["Other"]
  1247. assert other["order"] == 5
  1248. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1249. def test_top_events_with_projects_other(self):
  1250. with self.feature(self.enabled_features):
  1251. response = self.client.get(
  1252. self.url,
  1253. data={
  1254. "start": iso_format(self.day_ago),
  1255. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1256. "interval": "1h",
  1257. "yAxis": "count()",
  1258. "orderby": ["-count()"],
  1259. "field": ["count()", "project"],
  1260. "topEvents": 1,
  1261. },
  1262. format="json",
  1263. )
  1264. data = response.data
  1265. assert response.status_code == 200, response.content
  1266. assert set(data.keys()) == {"Other", self.project.slug}
  1267. assert data[self.project.slug]["order"] == 0
  1268. assert [attrs[0]["count"] for _, attrs in data[self.project.slug]["data"]] == [15, 0]
  1269. assert data["Other"]["order"] == 1
  1270. assert [attrs[0]["count"] for _, attrs in data["Other"]["data"]] == [7, 6]
  1271. def test_top_events_with_projects_fields(self):
  1272. # We need to handle the project name fields differently
  1273. for project_field in ["project", "project.name"]:
  1274. with self.feature(self.enabled_features):
  1275. response = self.client.get(
  1276. self.url,
  1277. data={
  1278. "start": iso_format(self.day_ago),
  1279. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1280. "interval": "1h",
  1281. "yAxis": "count()",
  1282. "orderby": ["-count()"],
  1283. "field": ["count()", project_field],
  1284. "topEvents": 5,
  1285. },
  1286. format="json",
  1287. )
  1288. data = response.data
  1289. assert response.status_code == 200, response.content
  1290. assert data[self.project.slug]["order"] == 0, project_field
  1291. assert [attrs[0]["count"] for _, attrs in data[self.project.slug]["data"]] == [
  1292. 15,
  1293. 0,
  1294. ], project_field
  1295. assert data[self.project2.slug]["order"] == 1, project_field
  1296. assert [attrs[0]["count"] for _, attrs in data[self.project2.slug]["data"]] == [
  1297. 7,
  1298. 6,
  1299. ], project_field
  1300. def test_tag_with_conflicting_function_alias_simple(self):
  1301. event_data: _EventDataDict = {
  1302. "data": {
  1303. "message": "poof",
  1304. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1305. "user": {"email": self.user.email},
  1306. "tags": {"count": "9001"},
  1307. "fingerprint": ["group1"],
  1308. },
  1309. "project": self.project2,
  1310. "count": 7,
  1311. }
  1312. for i in range(event_data["count"]):
  1313. event_data["data"]["event_id"] = f"a{i}" * 16
  1314. self.store_event(event_data["data"], project_id=event_data["project"].id)
  1315. # Query for count and count()
  1316. data = {
  1317. "start": iso_format(self.day_ago),
  1318. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1319. "interval": "1h",
  1320. "yAxis": "count()",
  1321. "orderby": ["-count()"],
  1322. "field": ["count()", "count"],
  1323. "topEvents": 5,
  1324. "partial": 1,
  1325. }
  1326. with self.feature(self.enabled_features):
  1327. response = self.client.get(self.url, data, format="json")
  1328. assert response.status_code == 200
  1329. assert response.data["9001"]["data"][0][1] == [{"count": 7}]
  1330. data["query"] = "count:9001"
  1331. with self.feature(self.enabled_features):
  1332. response = self.client.get(self.url, data, format="json")
  1333. assert response.status_code == 200
  1334. assert response.data["9001"]["data"][0][1] == [{"count": 7}]
  1335. data["query"] = "count:abc"
  1336. with self.feature(self.enabled_features):
  1337. response = self.client.get(self.url, data, format="json")
  1338. assert response.status_code == 200
  1339. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  1340. @pytest.mark.xfail(
  1341. reason="The response.data[Other] returns 15 locally and returns 16 or 15 remotely."
  1342. )
  1343. def test_tag_with_conflicting_function_alias_with_other_single_grouping(self):
  1344. event_data: list[_EventDataDict] = [
  1345. {
  1346. "data": {
  1347. "message": "poof",
  1348. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1349. "user": {"email": self.user.email},
  1350. "tags": {"count": "9001"},
  1351. "fingerprint": ["group1"],
  1352. },
  1353. "project": self.project2,
  1354. "count": 7,
  1355. },
  1356. {
  1357. "data": {
  1358. "message": "poof2",
  1359. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1360. "user": {"email": self.user.email},
  1361. "tags": {"count": "abc"},
  1362. "fingerprint": ["group1"],
  1363. },
  1364. "project": self.project2,
  1365. "count": 3,
  1366. },
  1367. ]
  1368. for index, event in enumerate(event_data):
  1369. for i in range(event["count"]):
  1370. event["data"]["event_id"] = f"{index}{i}" * 16
  1371. self.store_event(event["data"], project_id=event["project"].id)
  1372. # Query for count and count()
  1373. data = {
  1374. "start": iso_format(self.day_ago),
  1375. "end": iso_format(self.day_ago + timedelta(hours=1)),
  1376. "interval": "1h",
  1377. "yAxis": "count()",
  1378. "orderby": ["-count"],
  1379. "field": ["count()", "count"],
  1380. "topEvents": 2,
  1381. "partial": 1,
  1382. }
  1383. with self.feature(self.enabled_features):
  1384. response = self.client.get(self.url, data, format="json")
  1385. assert response.status_code == 200
  1386. assert response.data["9001"]["data"][0][1] == [{"count": 7}]
  1387. assert response.data["abc"]["data"][0][1] == [{"count": 3}]
  1388. assert response.data["Other"]["data"][0][1] == [{"count": 16}]
  1389. def test_tag_with_conflicting_function_alias_with_other_multiple_groupings(self):
  1390. event_data: list[_EventDataDict] = [
  1391. {
  1392. "data": {
  1393. "message": "abc",
  1394. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1395. "user": {"email": self.user.email},
  1396. "tags": {"count": "2"},
  1397. "fingerprint": ["group1"],
  1398. },
  1399. "project": self.project2,
  1400. "count": 3,
  1401. },
  1402. {
  1403. "data": {
  1404. "message": "def",
  1405. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1406. "user": {"email": self.user.email},
  1407. "tags": {"count": "9001"},
  1408. "fingerprint": ["group1"],
  1409. },
  1410. "project": self.project2,
  1411. "count": 7,
  1412. },
  1413. ]
  1414. for index, event in enumerate(event_data):
  1415. for i in range(event["count"]):
  1416. event["data"]["event_id"] = f"{index}{i}" * 16
  1417. self.store_event(event["data"], project_id=event["project"].id)
  1418. # Query for count and count()
  1419. data = {
  1420. "start": iso_format(self.day_ago),
  1421. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1422. "interval": "2d",
  1423. "yAxis": "count()",
  1424. "orderby": ["-count"],
  1425. "field": ["count()", "count", "message"],
  1426. "topEvents": 2,
  1427. "partial": 1,
  1428. }
  1429. with self.feature(self.enabled_features):
  1430. response = self.client.get(self.url, data, format="json")
  1431. assert response.status_code == 200
  1432. assert response.data["abc,2"]["data"][0][1] == [{"count": 3}]
  1433. assert response.data["def,9001"]["data"][0][1] == [{"count": 7}]
  1434. assert response.data["Other"]["data"][0][1] == [{"count": 25}]
  1435. def test_group_id_tag_simple(self):
  1436. event_data: _EventDataDict = {
  1437. "data": {
  1438. "message": "poof",
  1439. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  1440. "user": {"email": self.user.email},
  1441. "tags": {"group_id": "the tag"},
  1442. "fingerprint": ["group1"],
  1443. },
  1444. "project": self.project2,
  1445. "count": 7,
  1446. }
  1447. for i in range(event_data["count"]):
  1448. event_data["data"]["event_id"] = f"a{i}" * 16
  1449. self.store_event(event_data["data"], project_id=event_data["project"].id)
  1450. data = {
  1451. "start": iso_format(self.day_ago),
  1452. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1453. "interval": "1h",
  1454. "yAxis": "count()",
  1455. "orderby": ["-count()"],
  1456. "field": ["count()", "group_id"],
  1457. "topEvents": 5,
  1458. "partial": 1,
  1459. }
  1460. with self.feature(self.enabled_features):
  1461. response = self.client.get(self.url, data, format="json")
  1462. assert response.status_code == 200, response.content
  1463. assert response.data["the tag"]["data"][0][1] == [{"count": 7}]
  1464. data["query"] = 'group_id:"the tag"'
  1465. with self.feature(self.enabled_features):
  1466. response = self.client.get(self.url, data, format="json")
  1467. assert response.status_code == 200
  1468. assert response.data["the tag"]["data"][0][1] == [{"count": 7}]
  1469. data["query"] = "group_id:abc"
  1470. with self.feature(self.enabled_features):
  1471. response = self.client.get(self.url, data, format="json")
  1472. assert response.status_code == 200
  1473. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  1474. def test_top_events_limits(self):
  1475. data = {
  1476. "start": iso_format(self.day_ago),
  1477. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1478. "interval": "1h",
  1479. "yAxis": "count()",
  1480. "orderby": ["-count()"],
  1481. "field": ["count()", "message", "user.email"],
  1482. }
  1483. with self.feature(self.enabled_features):
  1484. data["topEvents"] = MAX_TOP_EVENTS + 1
  1485. response = self.client.get(self.url, data, format="json")
  1486. assert response.status_code == 400
  1487. data["topEvents"] = 0
  1488. response = self.client.get(self.url, data, format="json")
  1489. assert response.status_code == 400
  1490. data["topEvents"] = "a"
  1491. response = self.client.get(self.url, data, format="json")
  1492. assert response.status_code == 400
  1493. @pytest.mark.xfail(
  1494. reason="The response is wrong whenever we have a top events timeseries on project + any other field + aggregation"
  1495. )
  1496. def test_top_events_with_projects(self):
  1497. with self.feature(self.enabled_features):
  1498. response = self.client.get(
  1499. self.url,
  1500. data={
  1501. "start": iso_format(self.day_ago),
  1502. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1503. "interval": "1h",
  1504. "yAxis": "count()",
  1505. "orderby": ["-count()"],
  1506. "field": ["count()", "message", "project"],
  1507. "topEvents": 5,
  1508. },
  1509. format="json",
  1510. )
  1511. data = response.data
  1512. assert response.status_code == 200, response.content
  1513. assert len(data) == 6
  1514. for index, event in enumerate(self.events[:5]):
  1515. message = event.message or event.transaction
  1516. results = data[",".join([message, event.project.slug])]
  1517. assert results["order"] == index
  1518. assert [{"count": self.event_data[index]["count"]}] in [
  1519. attrs for time, attrs in results["data"]
  1520. ]
  1521. other = data["Other"]
  1522. assert other["order"] == 5
  1523. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1524. def test_top_events_with_issue(self):
  1525. # delete a group to make sure if this happens the value becomes unknown
  1526. event_group = self.events[0].group
  1527. event_group.delete()
  1528. with self.feature(self.enabled_features):
  1529. response = self.client.get(
  1530. self.url,
  1531. data={
  1532. "start": iso_format(self.day_ago),
  1533. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1534. "interval": "1h",
  1535. "yAxis": "count()",
  1536. "orderby": ["-count()"],
  1537. "field": ["count()", "message", "issue"],
  1538. "topEvents": 5,
  1539. "query": "!event.type:transaction",
  1540. },
  1541. format="json",
  1542. )
  1543. data = response.data
  1544. assert response.status_code == 200, response.content
  1545. assert len(data) == 6
  1546. for index, event in enumerate(self.events[:4]):
  1547. message = event.message
  1548. # Because we deleted the group for event 0
  1549. if index == 0 or event.group is None:
  1550. issue = "unknown"
  1551. else:
  1552. issue = event.group.qualified_short_id
  1553. results = data[",".join([issue, message])]
  1554. assert results["order"] == index
  1555. assert [{"count": self.event_data[index]["count"]}] in [
  1556. attrs for time, attrs in results["data"]
  1557. ]
  1558. other = data["Other"]
  1559. assert other["order"] == 5
  1560. assert [{"count": 1}] in [attrs for _, attrs in other["data"]]
  1561. def test_top_events_with_transaction_status(self):
  1562. with self.feature(self.enabled_features):
  1563. response = self.client.get(
  1564. self.url,
  1565. data={
  1566. "start": iso_format(self.day_ago),
  1567. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1568. "interval": "1h",
  1569. "yAxis": "count()",
  1570. "orderby": ["-count()"],
  1571. "field": ["count()", "transaction.status"],
  1572. "topEvents": 5,
  1573. },
  1574. format="json",
  1575. )
  1576. data = response.data
  1577. assert response.status_code == 200, response.content
  1578. assert len(data) == 1
  1579. assert "ok" in data
  1580. @mock.patch("sentry.models.GroupManager.get_issues_mapping")
  1581. def test_top_events_with_unknown_issue(self, mock_issues_mapping):
  1582. event = self.events[0]
  1583. event_data = self.event_data[0]
  1584. # ensure that the issue mapping returns None for the issue
  1585. mock_issues_mapping.return_value = {event.group.id: None}
  1586. with self.feature(self.enabled_features):
  1587. response = self.client.get(
  1588. self.url,
  1589. data={
  1590. "start": iso_format(self.day_ago),
  1591. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1592. "interval": "1h",
  1593. "yAxis": "count()",
  1594. "orderby": ["-count()"],
  1595. "field": ["count()", "issue"],
  1596. "topEvents": 5,
  1597. # narrow the search to just one issue
  1598. "query": f"issue.id:{event.group.id}",
  1599. },
  1600. format="json",
  1601. )
  1602. assert response.status_code == 200, response.content
  1603. data = response.data
  1604. assert len(data) == 1
  1605. results = data["unknown"]
  1606. assert results["order"] == 0
  1607. assert [{"count": event_data["count"]}] in [attrs for time, attrs in results["data"]]
  1608. @mock.patch(
  1609. "sentry.search.events.builder.base.raw_snql_query",
  1610. side_effect=[{"data": [{"issue.id": 1}], "meta": []}, {"data": [], "meta": []}],
  1611. )
  1612. def test_top_events_with_issue_check_query_conditions(self, mock_query):
  1613. """ "Intentionally separate from test_top_events_with_issue
  1614. This is to test against a bug where the condition for issues wasn't included and we'd be missing data for
  1615. the interval since we'd cap out the max rows. This was not caught by the previous test since the results
  1616. would still be correct given the smaller interval & lack of data
  1617. """
  1618. with self.feature(self.enabled_features):
  1619. self.client.get(
  1620. self.url,
  1621. data={
  1622. "start": iso_format(self.day_ago),
  1623. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1624. "interval": "1h",
  1625. "yAxis": "count()",
  1626. "orderby": ["-count()"],
  1627. "field": ["count()", "message", "issue"],
  1628. "topEvents": 5,
  1629. "query": "!event.type:transaction",
  1630. },
  1631. format="json",
  1632. )
  1633. assert (
  1634. Condition(Function("coalesce", [Column("group_id"), 0], "issue.id"), Op.IN, [1])
  1635. in mock_query.mock_calls[1].args[0].query.where
  1636. )
  1637. def test_top_events_with_functions(self):
  1638. for dataset in ["transactions", "discover"]:
  1639. with self.feature(self.enabled_features):
  1640. response = self.client.get(
  1641. self.url,
  1642. data={
  1643. "start": iso_format(self.day_ago),
  1644. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1645. "interval": "1h",
  1646. "yAxis": "count()",
  1647. "orderby": ["-p99()"],
  1648. "field": ["transaction", "avg(transaction.duration)", "p99()"],
  1649. "topEvents": 5,
  1650. "dataset": dataset,
  1651. },
  1652. format="json",
  1653. )
  1654. data = response.data
  1655. assert response.status_code == 200, response.content
  1656. assert len(data) == 1
  1657. results = data[self.transaction.transaction]
  1658. assert results["order"] == 0
  1659. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1660. def test_top_events_with_functions_on_different_transactions(self):
  1661. """Transaction2 has less events, but takes longer so order should be self.transaction then transaction2"""
  1662. transaction_data = load_data("transaction")
  1663. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1664. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=6))
  1665. transaction_data["transaction"] = "/foo_bar/"
  1666. transaction2 = self.store_event(transaction_data, project_id=self.project.id)
  1667. with self.feature(self.enabled_features):
  1668. response = self.client.get(
  1669. self.url,
  1670. data={
  1671. "start": iso_format(self.day_ago),
  1672. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1673. "interval": "1h",
  1674. "yAxis": "count()",
  1675. "orderby": ["-p90()"],
  1676. "field": ["transaction", "avg(transaction.duration)", "p90()"],
  1677. "topEvents": 5,
  1678. },
  1679. format="json",
  1680. )
  1681. data = response.data
  1682. assert response.status_code == 200, response.content
  1683. assert len(data) == 2
  1684. results = data[self.transaction.transaction]
  1685. assert results["order"] == 1
  1686. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1687. results = data[transaction2.transaction]
  1688. assert results["order"] == 0
  1689. assert [attrs for time, attrs in results["data"]] == [[{"count": 1}], [{"count": 0}]]
  1690. def test_top_events_with_query(self):
  1691. transaction_data = load_data("transaction")
  1692. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1693. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=6))
  1694. transaction_data["transaction"] = "/foo_bar/"
  1695. self.store_event(transaction_data, project_id=self.project.id)
  1696. with self.feature(self.enabled_features):
  1697. response = self.client.get(
  1698. self.url,
  1699. data={
  1700. "start": iso_format(self.day_ago),
  1701. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1702. "interval": "1h",
  1703. "yAxis": "count()",
  1704. "orderby": ["-p99()"],
  1705. "query": "transaction:/foo_bar/",
  1706. "field": ["transaction", "avg(transaction.duration)", "p99()"],
  1707. "topEvents": 5,
  1708. },
  1709. format="json",
  1710. )
  1711. data = response.data
  1712. assert response.status_code == 200, response.content
  1713. assert len(data) == 1
  1714. transaction2_data = data["/foo_bar/"]
  1715. assert transaction2_data["order"] == 0
  1716. assert [attrs for time, attrs in transaction2_data["data"]] == [
  1717. [{"count": 1}],
  1718. [{"count": 0}],
  1719. ]
  1720. def test_top_events_with_negated_condition(self):
  1721. with self.feature(self.enabled_features):
  1722. response = self.client.get(
  1723. self.url,
  1724. data={
  1725. "start": iso_format(self.day_ago),
  1726. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1727. "interval": "1h",
  1728. "yAxis": "count()",
  1729. "orderby": ["-count()"],
  1730. "query": f"!message:{self.events[0].message}",
  1731. "field": ["message", "count()"],
  1732. "topEvents": 5,
  1733. },
  1734. format="json",
  1735. )
  1736. data = response.data
  1737. assert response.status_code == 200, response.content
  1738. assert len(data) == 6
  1739. for index, event in enumerate(self.events[1:5]):
  1740. message = event.message or event.transaction
  1741. results = data[message]
  1742. assert results["order"] == index
  1743. assert [{"count": self.event_data[index + 1]["count"]}] in [
  1744. attrs for _, attrs in results["data"]
  1745. ]
  1746. other = data["Other"]
  1747. assert other["order"] == 5
  1748. assert [{"count": 1}] in [attrs for _, attrs in other["data"]]
  1749. def test_top_events_with_epm(self):
  1750. with self.feature(self.enabled_features):
  1751. response = self.client.get(
  1752. self.url,
  1753. data={
  1754. "start": iso_format(self.day_ago),
  1755. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1756. "interval": "1h",
  1757. "yAxis": "epm()",
  1758. "orderby": ["-count()"],
  1759. "field": ["message", "user.email", "count()"],
  1760. "topEvents": 5,
  1761. },
  1762. format="json",
  1763. )
  1764. data = response.data
  1765. assert response.status_code == 200, response.content
  1766. assert len(data) == 6
  1767. for index, event in enumerate(self.events[:5]):
  1768. message = event.message or event.transaction
  1769. results = data[
  1770. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  1771. ]
  1772. assert results["order"] == index
  1773. assert [{"count": self.event_data[index]["count"] / (3600.0 / 60.0)}] in [
  1774. attrs for time, attrs in results["data"]
  1775. ]
  1776. other = data["Other"]
  1777. assert other["order"] == 5
  1778. assert [{"count": 0.05}] in [attrs for _, attrs in other["data"]]
  1779. def test_top_events_with_multiple_yaxis(self):
  1780. with self.feature(self.enabled_features):
  1781. response = self.client.get(
  1782. self.url,
  1783. data={
  1784. "start": iso_format(self.day_ago),
  1785. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1786. "interval": "1h",
  1787. "yAxis": ["epm()", "count()"],
  1788. "orderby": ["-count()"],
  1789. "field": ["message", "user.email", "count()"],
  1790. "topEvents": 5,
  1791. },
  1792. format="json",
  1793. )
  1794. data = response.data
  1795. assert response.status_code == 200, response.content
  1796. assert len(data) == 6
  1797. for index, event in enumerate(self.events[:5]):
  1798. message = event.message or event.transaction
  1799. results = data[
  1800. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  1801. ]
  1802. assert results["order"] == index
  1803. assert results["epm()"]["order"] == 0
  1804. assert results["count()"]["order"] == 1
  1805. assert [{"count": self.event_data[index]["count"] / (3600.0 / 60.0)}] in [
  1806. attrs for time, attrs in results["epm()"]["data"]
  1807. ]
  1808. assert [{"count": self.event_data[index]["count"]}] in [
  1809. attrs for time, attrs in results["count()"]["data"]
  1810. ]
  1811. other = data["Other"]
  1812. assert other["order"] == 5
  1813. assert other["epm()"]["order"] == 0
  1814. assert other["count()"]["order"] == 1
  1815. assert [{"count": 0.05}] in [attrs for _, attrs in other["epm()"]["data"]]
  1816. assert [{"count": 3}] in [attrs for _, attrs in other["count()"]["data"]]
  1817. def test_top_events_with_boolean(self):
  1818. with self.feature(self.enabled_features):
  1819. response = self.client.get(
  1820. self.url,
  1821. data={
  1822. "start": iso_format(self.day_ago),
  1823. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1824. "interval": "1h",
  1825. "yAxis": "count()",
  1826. "orderby": ["-count()"],
  1827. "field": ["count()", "message", "device.charging"],
  1828. "topEvents": 5,
  1829. },
  1830. format="json",
  1831. )
  1832. data = response.data
  1833. assert response.status_code == 200, response.content
  1834. assert len(data) == 6
  1835. for index, event in enumerate(self.events[:5]):
  1836. message = event.message or event.transaction
  1837. results = data[",".join(["False", message])]
  1838. assert results["order"] == index
  1839. assert [{"count": self.event_data[index]["count"]}] in [
  1840. attrs for time, attrs in results["data"]
  1841. ]
  1842. other = data["Other"]
  1843. assert other["order"] == 5
  1844. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  1845. def test_top_events_with_error_unhandled(self):
  1846. self.login_as(user=self.user)
  1847. project = self.create_project()
  1848. prototype = load_data("android-ndk")
  1849. prototype["event_id"] = "f" * 32
  1850. prototype["logentry"] = {"formatted": "not handled"}
  1851. prototype["exception"]["values"][0]["value"] = "not handled"
  1852. prototype["exception"]["values"][0]["mechanism"]["handled"] = False
  1853. prototype["timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  1854. self.store_event(data=prototype, project_id=project.id)
  1855. with self.feature(self.enabled_features):
  1856. response = self.client.get(
  1857. self.url,
  1858. data={
  1859. "start": iso_format(self.day_ago),
  1860. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1861. "interval": "1h",
  1862. "yAxis": "count()",
  1863. "orderby": ["-count()"],
  1864. "field": ["count()", "error.unhandled"],
  1865. "topEvents": 5,
  1866. },
  1867. format="json",
  1868. )
  1869. data = response.data
  1870. assert response.status_code == 200, response.content
  1871. assert len(data) == 2
  1872. def test_top_events_with_timestamp(self):
  1873. with self.feature(self.enabled_features):
  1874. response = self.client.get(
  1875. self.url,
  1876. data={
  1877. "start": iso_format(self.day_ago),
  1878. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1879. "interval": "1h",
  1880. "yAxis": "count()",
  1881. "orderby": ["-count()"],
  1882. "query": "event.type:default",
  1883. "field": ["count()", "message", "timestamp"],
  1884. "topEvents": 5,
  1885. },
  1886. format="json",
  1887. )
  1888. data = response.data
  1889. assert response.status_code == 200, response.content
  1890. assert len(data) == 6
  1891. # Transactions won't be in the results because of the query
  1892. del self.events[4]
  1893. del self.event_data[4]
  1894. for index, event in enumerate(self.events[:5]):
  1895. results = data[",".join([event.message, event.timestamp])]
  1896. assert results["order"] == index
  1897. assert [{"count": self.event_data[index]["count"]}] in [
  1898. attrs for time, attrs in results["data"]
  1899. ]
  1900. other = data["Other"]
  1901. assert other["order"] == 5
  1902. assert [{"count": 1}] in [attrs for _, attrs in other["data"]]
  1903. def test_top_events_with_int(self):
  1904. with self.feature(self.enabled_features):
  1905. response = self.client.get(
  1906. self.url,
  1907. data={
  1908. "start": iso_format(self.day_ago),
  1909. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1910. "interval": "1h",
  1911. "yAxis": "count()",
  1912. "orderby": ["-count()"],
  1913. "field": ["count()", "message", "transaction.duration"],
  1914. "topEvents": 5,
  1915. },
  1916. format="json",
  1917. )
  1918. data = response.data
  1919. assert response.status_code == 200, response.content
  1920. assert len(data) == 1
  1921. results = data[",".join([self.transaction.transaction, "120000"])]
  1922. assert results["order"] == 0
  1923. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  1924. def test_top_events_with_user(self):
  1925. with self.feature(self.enabled_features):
  1926. response = self.client.get(
  1927. self.url,
  1928. data={
  1929. "start": iso_format(self.day_ago),
  1930. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1931. "interval": "1h",
  1932. "yAxis": "count()",
  1933. "orderby": ["-count()", "user"],
  1934. "field": ["user", "count()"],
  1935. "topEvents": 5,
  1936. },
  1937. format="json",
  1938. )
  1939. data = response.data
  1940. assert response.status_code == 200, response.content
  1941. assert len(data) == 5
  1942. assert data["email:bar@example.com"]["order"] == 1
  1943. assert [attrs for time, attrs in data["email:bar@example.com"]["data"]] == [
  1944. [{"count": 7}],
  1945. [{"count": 0}],
  1946. ]
  1947. assert [attrs for time, attrs in data["ip:127.0.0.1"]["data"]] == [
  1948. [{"count": 3}],
  1949. [{"count": 0}],
  1950. ]
  1951. def test_top_events_with_user_and_email(self):
  1952. with self.feature(self.enabled_features):
  1953. response = self.client.get(
  1954. self.url,
  1955. data={
  1956. "start": iso_format(self.day_ago),
  1957. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1958. "interval": "1h",
  1959. "yAxis": "count()",
  1960. "orderby": ["-count()", "user"],
  1961. "field": ["user", "user.email", "count()"],
  1962. "topEvents": 5,
  1963. },
  1964. format="json",
  1965. )
  1966. data = response.data
  1967. assert response.status_code == 200, response.content
  1968. assert len(data) == 5
  1969. assert data["email:bar@example.com,bar@example.com"]["order"] == 1
  1970. assert [attrs for time, attrs in data["email:bar@example.com,bar@example.com"]["data"]] == [
  1971. [{"count": 7}],
  1972. [{"count": 0}],
  1973. ]
  1974. assert [attrs for time, attrs in data["ip:127.0.0.1,None"]["data"]] == [
  1975. [{"count": 3}],
  1976. [{"count": 0}],
  1977. ]
  1978. def test_top_events_with_user_display(self):
  1979. with self.feature(self.enabled_features):
  1980. response = self.client.get(
  1981. self.url,
  1982. data={
  1983. "start": iso_format(self.day_ago),
  1984. "end": iso_format(self.day_ago + timedelta(hours=2)),
  1985. "interval": "1h",
  1986. "yAxis": "count()",
  1987. "orderby": ["-count()"],
  1988. "field": ["message", "user.display", "count()"],
  1989. "topEvents": 5,
  1990. },
  1991. format="json",
  1992. )
  1993. data = response.data
  1994. assert response.status_code == 200, response.content
  1995. assert len(data) == 6
  1996. for index, event in enumerate(self.events[:5]):
  1997. message = event.message or event.transaction
  1998. user = self.event_data[index]["data"]["user"]
  1999. results = data[
  2000. ",".join([message, user.get("email", None) or user.get("ip_address", "None")])
  2001. ]
  2002. assert results["order"] == index
  2003. assert [{"count": self.event_data[index]["count"]}] in [
  2004. attrs for _, attrs in results["data"]
  2005. ]
  2006. other = data["Other"]
  2007. assert other["order"] == 5
  2008. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  2009. @pytest.mark.skip(reason="A query with group_id will not return transactions")
  2010. def test_top_events_none_filter(self):
  2011. """When a field is None in one of the top events, make sure we filter by it
  2012. In this case event[4] is a transaction and has no issue
  2013. """
  2014. with self.feature(self.enabled_features):
  2015. response = self.client.get(
  2016. self.url,
  2017. data={
  2018. "start": iso_format(self.day_ago),
  2019. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2020. "interval": "1h",
  2021. "yAxis": "count()",
  2022. "orderby": ["-count()"],
  2023. "field": ["count()", "issue"],
  2024. "topEvents": 5,
  2025. },
  2026. format="json",
  2027. )
  2028. data = response.data
  2029. assert response.status_code == 200, response.content
  2030. assert len(data) == 5
  2031. for index, event in enumerate(self.events[:5]):
  2032. if event.group is None:
  2033. issue = "unknown"
  2034. else:
  2035. issue = event.group.qualified_short_id
  2036. results = data[issue]
  2037. assert results["order"] == index
  2038. assert [{"count": self.event_data[index]["count"]}] in [
  2039. attrs for time, attrs in results["data"]
  2040. ]
  2041. @pytest.mark.skip(reason="Invalid query - transaction events don't have group_id field")
  2042. def test_top_events_one_field_with_none(self):
  2043. with self.feature(self.enabled_features):
  2044. response = self.client.get(
  2045. self.url,
  2046. data={
  2047. "start": iso_format(self.day_ago),
  2048. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2049. "interval": "1h",
  2050. "yAxis": "count()",
  2051. "orderby": ["-count()"],
  2052. "query": "event.type:transaction",
  2053. "field": ["count()", "issue"],
  2054. "topEvents": 5,
  2055. },
  2056. format="json",
  2057. )
  2058. data = response.data
  2059. assert response.status_code == 200, response.content
  2060. assert len(data) == 1
  2061. results = data["unknown"]
  2062. assert [attrs for time, attrs in results["data"]] == [[{"count": 3}], [{"count": 0}]]
  2063. assert results["order"] == 0
  2064. def test_top_events_with_error_handled(self):
  2065. data = self.event_data[0]
  2066. data["data"]["level"] = "error"
  2067. data["data"]["exception"] = {
  2068. "values": [
  2069. {
  2070. "type": "ValidationError",
  2071. "value": "Bad request",
  2072. "mechanism": {"handled": True, "type": "generic"},
  2073. }
  2074. ]
  2075. }
  2076. self.store_event(data["data"], project_id=data["project"].id)
  2077. data["data"]["exception"] = {
  2078. "values": [
  2079. {
  2080. "type": "ValidationError",
  2081. "value": "Bad request",
  2082. "mechanism": {"handled": False, "type": "generic"},
  2083. }
  2084. ]
  2085. }
  2086. self.store_event(data["data"], project_id=data["project"].id)
  2087. with self.feature(self.enabled_features):
  2088. response = self.client.get(
  2089. self.url,
  2090. data={
  2091. "start": iso_format(self.day_ago),
  2092. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2093. "interval": "1h",
  2094. "yAxis": "count()",
  2095. "orderby": ["-count()"],
  2096. "field": ["count()", "error.handled"],
  2097. "topEvents": 5,
  2098. "query": "!event.type:transaction",
  2099. },
  2100. format="json",
  2101. )
  2102. assert response.status_code == 200, response.content
  2103. res_data = response.data
  2104. assert len(res_data) == 2
  2105. results = res_data["1"]
  2106. assert [attrs for time, attrs in results["data"]] == [[{"count": 20}], [{"count": 6}]]
  2107. results = res_data["0"]
  2108. assert [attrs for time, attrs in results["data"]] == [[{"count": 1}], [{"count": 0}]]
  2109. def test_top_events_with_aggregate_condition(self):
  2110. with self.feature(self.enabled_features):
  2111. response = self.client.get(
  2112. self.url,
  2113. data={
  2114. "start": iso_format(self.day_ago),
  2115. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2116. "interval": "1h",
  2117. "yAxis": "count()",
  2118. "orderby": ["-count()"],
  2119. "field": ["message", "count()"],
  2120. "query": "count():>4",
  2121. "topEvents": 5,
  2122. },
  2123. format="json",
  2124. )
  2125. assert response.status_code == 200, response.content
  2126. data = response.data
  2127. assert len(data) == 3
  2128. for index, event in enumerate(self.events[:3]):
  2129. message = event.message or event.transaction
  2130. results = data[message]
  2131. assert results["order"] == index
  2132. assert [{"count": self.event_data[index]["count"]}] in [
  2133. attrs for time, attrs in results["data"]
  2134. ]
  2135. @pytest.mark.xfail(reason="There's only 2 rows total, which mean there shouldn't be other")
  2136. def test_top_events_with_to_other(self):
  2137. version = "version -@'\" 1.2,3+(4)"
  2138. version_escaped = "version -@'\\\" 1.2,3+(4)"
  2139. # every symbol is replaced with a underscore to make the alias
  2140. version_alias = "version_______1_2_3__4_"
  2141. # add an event in the current release
  2142. event = self.event_data[0]
  2143. event_data = event["data"].copy()
  2144. event_data["event_id"] = uuid4().hex
  2145. event_data["release"] = version
  2146. self.store_event(event_data, project_id=event["project"].id)
  2147. with self.feature(self.enabled_features):
  2148. response = self.client.get(
  2149. self.url,
  2150. data={
  2151. "start": iso_format(self.day_ago),
  2152. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2153. "interval": "1h",
  2154. "yAxis": "count()",
  2155. # the double underscores around the version alias is because of a comma and quote
  2156. "orderby": [f"-to_other_release__{version_alias}__others_current"],
  2157. "field": [
  2158. "count()",
  2159. f'to_other(release,"{version_escaped}",others,current)',
  2160. ],
  2161. "topEvents": 2,
  2162. },
  2163. format="json",
  2164. )
  2165. assert response.status_code == 200, response.content
  2166. data = response.data
  2167. assert len(data) == 2
  2168. current = data["current"]
  2169. assert current["order"] == 1
  2170. assert sum(attrs[0]["count"] for _, attrs in current["data"]) == 1
  2171. others = data["others"]
  2172. assert others["order"] == 0
  2173. assert sum(attrs[0]["count"] for _, attrs in others["data"]) == sum(
  2174. event_data["count"] for event_data in self.event_data
  2175. )
  2176. def test_top_events_with_equations(self):
  2177. with self.feature(self.enabled_features):
  2178. response = self.client.get(
  2179. self.url,
  2180. data={
  2181. "start": iso_format(self.day_ago),
  2182. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2183. "interval": "1h",
  2184. "yAxis": "equation|count() / 100",
  2185. "orderby": ["-count()"],
  2186. "field": ["count()", "message", "user.email", "equation|count() / 100"],
  2187. "topEvents": 5,
  2188. },
  2189. format="json",
  2190. )
  2191. data = response.data
  2192. assert response.status_code == 200, response.content
  2193. assert len(data) == 6
  2194. for index, event in enumerate(self.events[:5]):
  2195. message = event.message or event.transaction
  2196. results = data[
  2197. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  2198. ]
  2199. assert results["order"] == index
  2200. assert [{"count": self.event_data[index]["count"] / 100}] in [
  2201. attrs for time, attrs in results["data"]
  2202. ]
  2203. other = data["Other"]
  2204. assert other["order"] == 5
  2205. assert [{"count": 0.03}] in [attrs for _, attrs in other["data"]]
  2206. @mock.patch("sentry.snuba.discover.bulk_snuba_queries", return_value=[{"data": [], "meta": []}])
  2207. @mock.patch(
  2208. "sentry.search.events.builder.base.raw_snql_query",
  2209. return_value={"data": [], "meta": []},
  2210. )
  2211. def test_invalid_interval(self, mock_raw_query, mock_bulk_query):
  2212. with self.feature(self.enabled_features):
  2213. response = self.client.get(
  2214. self.url,
  2215. format="json",
  2216. data={
  2217. "end": iso_format(before_now()),
  2218. # 7,200 points for each event
  2219. "start": iso_format(before_now(seconds=7200)),
  2220. "field": ["count()", "issue"],
  2221. "query": "",
  2222. "interval": "1s",
  2223. "yAxis": "count()",
  2224. },
  2225. )
  2226. assert response.status_code == 200
  2227. assert mock_bulk_query.call_count == 1
  2228. with self.feature(self.enabled_features):
  2229. response = self.client.get(
  2230. self.url,
  2231. format="json",
  2232. data={
  2233. "end": iso_format(before_now()),
  2234. "start": iso_format(before_now(seconds=7200)),
  2235. "field": ["count()", "issue"],
  2236. "query": "",
  2237. "interval": "1s",
  2238. "yAxis": "count()",
  2239. # 7,200 points for each event * 2, should error
  2240. "topEvents": 2,
  2241. },
  2242. )
  2243. assert response.status_code == 200
  2244. assert mock_raw_query.call_count == 2
  2245. # Should've reset to the default for between 1 and 24h
  2246. assert mock_raw_query.mock_calls[1].args[0].query.granularity.granularity == 300
  2247. with self.feature(self.enabled_features):
  2248. response = self.client.get(
  2249. self.url,
  2250. format="json",
  2251. data={
  2252. "end": iso_format(before_now()),
  2253. # 1999 points * 5 events should just be enough to not error
  2254. "start": iso_format(before_now(seconds=1999)),
  2255. "field": ["count()", "issue"],
  2256. "query": "",
  2257. "interval": "1s",
  2258. "yAxis": "count()",
  2259. "topEvents": 5,
  2260. },
  2261. )
  2262. assert response.status_code == 200
  2263. assert mock_raw_query.call_count == 4
  2264. # Should've left the interval alone since we're just below the limit
  2265. assert mock_raw_query.mock_calls[3].args[0].query.granularity.granularity == 1
  2266. with self.feature(self.enabled_features):
  2267. response = self.client.get(
  2268. self.url,
  2269. format="json",
  2270. data={
  2271. "end": iso_format(before_now()),
  2272. "start": iso_format(before_now(hours=24)),
  2273. "field": ["count()", "issue"],
  2274. "query": "",
  2275. "interval": "0d",
  2276. "yAxis": "count()",
  2277. "topEvents": 5,
  2278. },
  2279. )
  2280. assert response.status_code == 200
  2281. assert mock_raw_query.call_count == 6
  2282. # Should've default to 24h's default of 5m
  2283. assert mock_raw_query.mock_calls[5].args[0].query.granularity.granularity == 300
  2284. def test_top_events_timestamp_fields(self):
  2285. with self.feature(self.enabled_features):
  2286. response = self.client.get(
  2287. self.url,
  2288. format="json",
  2289. data={
  2290. "start": iso_format(self.day_ago),
  2291. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2292. "interval": "1h",
  2293. "yAxis": "count()",
  2294. "orderby": ["-count()"],
  2295. "field": ["count()", "timestamp", "timestamp.to_hour", "timestamp.to_day"],
  2296. "topEvents": 5,
  2297. },
  2298. )
  2299. assert response.status_code == 200
  2300. data = response.data
  2301. assert len(data) == 3
  2302. # these are the timestamps corresponding to the events stored
  2303. timestamps = [
  2304. self.day_ago + timedelta(minutes=2),
  2305. self.day_ago + timedelta(hours=1, minutes=2),
  2306. self.day_ago + timedelta(minutes=4),
  2307. ]
  2308. timestamp_hours = [timestamp.replace(minute=0, second=0) for timestamp in timestamps]
  2309. timestamp_days = [timestamp.replace(hour=0, minute=0, second=0) for timestamp in timestamps]
  2310. for ts, ts_hr, ts_day in zip(timestamps, timestamp_hours, timestamp_days):
  2311. key = f"{iso_format(ts)}+00:00,{iso_format(ts_day)}+00:00,{iso_format(ts_hr)}+00:00"
  2312. count = sum(
  2313. e["count"] for e in self.event_data if e["data"]["timestamp"] == iso_format(ts)
  2314. )
  2315. results = data[key]
  2316. assert [{"count": count}] in [attrs for time, attrs in results["data"]]
  2317. def test_top_events_other_with_matching_columns(self):
  2318. with self.feature(self.enabled_features):
  2319. response = self.client.get(
  2320. self.url,
  2321. data={
  2322. "start": iso_format(self.day_ago),
  2323. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2324. "interval": "1h",
  2325. "yAxis": "count()",
  2326. "orderby": ["-count()"],
  2327. "field": ["count()", "tags[shared-tag]", "message"],
  2328. "topEvents": 5,
  2329. },
  2330. format="json",
  2331. )
  2332. data = response.data
  2333. assert response.status_code == 200, response.content
  2334. assert len(data) == 6
  2335. for index, event in enumerate(self.events[:5]):
  2336. message = event.message or event.transaction
  2337. results = data[",".join([message, "yup"])]
  2338. assert results["order"] == index
  2339. assert [{"count": self.event_data[index]["count"]}] in [
  2340. attrs for _, attrs in results["data"]
  2341. ]
  2342. other = data["Other"]
  2343. assert other["order"] == 5
  2344. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  2345. def test_top_events_with_field_overlapping_other_key(self):
  2346. transaction_data = load_data("transaction")
  2347. transaction_data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  2348. transaction_data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=6))
  2349. transaction_data["transaction"] = OTHER_KEY
  2350. for i in range(5):
  2351. data = transaction_data.copy()
  2352. data["event_id"] = "ab" + f"{i}" * 30
  2353. data["contexts"]["trace"]["span_id"] = "ab" + f"{i}" * 14
  2354. self.store_event(data, project_id=self.project.id)
  2355. with self.feature(self.enabled_features):
  2356. response = self.client.get(
  2357. self.url,
  2358. data={
  2359. "start": iso_format(self.day_ago),
  2360. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2361. "interval": "1h",
  2362. "yAxis": "count()",
  2363. "orderby": ["-count()"],
  2364. "field": ["count()", "message"],
  2365. "topEvents": 5,
  2366. },
  2367. format="json",
  2368. )
  2369. data = response.data
  2370. assert response.status_code == 200, response.content
  2371. assert len(data) == 6
  2372. assert f"{OTHER_KEY} (message)" in data
  2373. results = data[f"{OTHER_KEY} (message)"]
  2374. assert [{"count": 5}] in [attrs for _, attrs in results["data"]]
  2375. other = data["Other"]
  2376. assert other["order"] == 5
  2377. assert [{"count": 4}] in [attrs for _, attrs in other["data"]]
  2378. def test_top_events_can_exclude_other_series(self):
  2379. with self.feature(self.enabled_features):
  2380. response = self.client.get(
  2381. self.url,
  2382. data={
  2383. "start": iso_format(self.day_ago),
  2384. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2385. "interval": "1h",
  2386. "yAxis": "count()",
  2387. "orderby": ["count()"],
  2388. "field": ["count()", "message"],
  2389. "topEvents": 5,
  2390. "excludeOther": "1",
  2391. },
  2392. format="json",
  2393. )
  2394. data = response.data
  2395. assert response.status_code == 200, response.content
  2396. assert len(data) == 5
  2397. assert "Other" not in response.data
  2398. @pytest.mark.xfail(reason="Started failing on ClickHouse 21.8")
  2399. def test_top_events_with_equation_including_unselected_fields_passes_field_validation(self):
  2400. with self.feature(self.enabled_features):
  2401. response = self.client.get(
  2402. self.url,
  2403. data={
  2404. "start": iso_format(self.day_ago),
  2405. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2406. "interval": "1h",
  2407. "yAxis": "count()",
  2408. "orderby": ["-equation[0]"],
  2409. "field": ["count()", "message", "equation|count_unique(user) * 2"],
  2410. "topEvents": 5,
  2411. },
  2412. format="json",
  2413. )
  2414. data = response.data
  2415. assert response.status_code == 200, response.content
  2416. assert len(data) == 6
  2417. other = data["Other"]
  2418. assert other["order"] == 5
  2419. assert [{"count": 4}] in [attrs for _, attrs in other["data"]]
  2420. def test_top_events_boolean_condition_and_project_field(self):
  2421. with self.feature(self.enabled_features):
  2422. response = self.client.get(
  2423. self.url,
  2424. data={
  2425. "start": iso_format(self.day_ago),
  2426. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2427. "interval": "1h",
  2428. "yAxis": "count()",
  2429. "orderby": ["-count()"],
  2430. "field": ["project", "count()"],
  2431. "topEvents": 5,
  2432. "query": "event.type:transaction (transaction:*a OR transaction:b*)",
  2433. },
  2434. format="json",
  2435. )
  2436. assert response.status_code == 200
  2437. class OrganizationEventsStatsProfileFunctionDatasetEndpointTest(
  2438. APITestCase, ProfilesSnubaTestCase, SearchIssueTestMixin
  2439. ):
  2440. endpoint = "sentry-api-0-organization-events-stats"
  2441. def setUp(self):
  2442. super().setUp()
  2443. self.login_as(user=self.user)
  2444. self.one_day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  2445. self.two_days_ago = before_now(days=2).replace(hour=10, minute=0, second=0, microsecond=0)
  2446. self.three_days_ago = before_now(days=3).replace(hour=10, minute=0, second=0, microsecond=0)
  2447. self.project = self.create_project()
  2448. self.url = reverse(
  2449. "sentry-api-0-organization-events-stats",
  2450. kwargs={"organization_id_or_slug": self.project.organization.slug},
  2451. )
  2452. def test_functions_dataset_simple(self):
  2453. self.store_functions(
  2454. [
  2455. {
  2456. "self_times_ns": [100 for _ in range(100)],
  2457. "package": "foo",
  2458. "function": "bar",
  2459. "in_app": True,
  2460. },
  2461. ],
  2462. project=self.project,
  2463. timestamp=self.two_days_ago,
  2464. )
  2465. data = {
  2466. "dataset": "profileFunctions",
  2467. "start": iso_format(self.three_days_ago),
  2468. "end": iso_format(self.one_day_ago),
  2469. "interval": "1d",
  2470. "yAxis": ["cpm()", "p95(function.duration)"],
  2471. }
  2472. response = self.client.get(self.url, data=data, format="json")
  2473. assert response.status_code == 200, response.content
  2474. assert sum(row[1][0]["count"] for row in response.data["cpm()"]["data"]) == pytest.approx(
  2475. 100 / ((self.one_day_ago - self.three_days_ago).total_seconds() / 60), rel=1e-3
  2476. )
  2477. assert any(
  2478. row[1][0]["count"] > 0 for row in response.data["p95(function.duration)"]["data"]
  2479. )
  2480. for y_axis in ["cpm()", "p95(function.duration)"]:
  2481. assert response.data[y_axis]["meta"]["fields"] == {
  2482. "time": "date",
  2483. "cpm": "number",
  2484. "p95_function_duration": "duration",
  2485. }
  2486. assert response.data[y_axis]["meta"]["units"] == {
  2487. "time": None,
  2488. "cpm": None,
  2489. "p95_function_duration": "nanosecond",
  2490. }
  2491. class OrganizationEventsStatsTopNEventsProfileFunctionDatasetEndpointTest(
  2492. APITestCase, ProfilesSnubaTestCase, SearchIssueTestMixin
  2493. ):
  2494. endpoint = "sentry-api-0-organization-events-stats"
  2495. def setUp(self):
  2496. super().setUp()
  2497. self.login_as(user=self.user)
  2498. self.one_day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  2499. self.two_days_ago = before_now(days=2).replace(hour=10, minute=0, second=0, microsecond=0)
  2500. self.three_days_ago = before_now(days=3).replace(hour=10, minute=0, second=0, microsecond=0)
  2501. self.project = self.create_project()
  2502. self.url = reverse(
  2503. "sentry-api-0-organization-events-stats",
  2504. kwargs={"organization_id_or_slug": self.project.organization.slug},
  2505. )
  2506. def test_functions_dataset_simple(self):
  2507. self.store_functions(
  2508. [
  2509. {
  2510. "self_times_ns": [100 for _ in range(100)],
  2511. "package": "pkg",
  2512. "function": "foo",
  2513. "in_app": True,
  2514. },
  2515. {
  2516. "self_times_ns": [100 for _ in range(10)],
  2517. "package": "pkg",
  2518. "function": "bar",
  2519. "in_app": True,
  2520. },
  2521. ],
  2522. project=self.project,
  2523. timestamp=self.two_days_ago,
  2524. )
  2525. data = {
  2526. "dataset": "profileFunctions",
  2527. "field": ["function", "count()"],
  2528. "start": iso_format(self.three_days_ago),
  2529. "end": iso_format(self.one_day_ago),
  2530. "yAxis": ["cpm()", "p95(function.duration)"],
  2531. "interval": "1d",
  2532. "topEvents": 2,
  2533. "excludeOther": 1,
  2534. }
  2535. response = self.client.get(self.url, data=data, format="json")
  2536. assert response.status_code == 200, response.content
  2537. assert sum(
  2538. row[1][0]["count"] for row in response.data["foo"]["cpm()"]["data"]
  2539. ) == pytest.approx(
  2540. 100 / ((self.one_day_ago - self.three_days_ago).total_seconds() / 60), rel=1e-3
  2541. )
  2542. assert sum(
  2543. row[1][0]["count"] for row in response.data["bar"]["cpm()"]["data"]
  2544. ) == pytest.approx(
  2545. 10 / ((self.one_day_ago - self.three_days_ago).total_seconds() / 60), rel=1e-3
  2546. )
  2547. assert any(
  2548. row[1][0]["count"] > 0 for row in response.data["foo"]["p95(function.duration)"]["data"]
  2549. )
  2550. assert any(
  2551. row[1][0]["count"] > 0 for row in response.data["bar"]["p95(function.duration)"]["data"]
  2552. )
  2553. class OrganizationEventsStatsTopNEventsErrors(APITestCase, SnubaTestCase):
  2554. def setUp(self):
  2555. super().setUp()
  2556. self.login_as(user=self.user)
  2557. self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
  2558. self.project = self.create_project()
  2559. self.project2 = self.create_project()
  2560. self.user2 = self.create_user()
  2561. self.event_data: list[_EventDataDict] = [
  2562. {
  2563. "data": {
  2564. "message": "poof",
  2565. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2566. "user": {"email": self.user.email},
  2567. "tags": {"shared-tag": "yup"},
  2568. "fingerprint": ["group1"],
  2569. },
  2570. "project": self.project2,
  2571. "count": 7,
  2572. },
  2573. {
  2574. "data": {
  2575. "message": "voof",
  2576. "timestamp": iso_format(self.day_ago + timedelta(hours=1, minutes=2)),
  2577. "fingerprint": ["group2"],
  2578. "user": {"email": self.user2.email},
  2579. "tags": {"shared-tag": "yup"},
  2580. },
  2581. "project": self.project2,
  2582. "count": 6,
  2583. },
  2584. {
  2585. "data": {
  2586. "message": "very bad",
  2587. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2588. "fingerprint": ["group3"],
  2589. "user": {"email": "foo@example.com"},
  2590. "tags": {"shared-tag": "yup"},
  2591. },
  2592. "project": self.project,
  2593. "count": 5,
  2594. },
  2595. {
  2596. "data": {
  2597. "message": "oh no",
  2598. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2599. "fingerprint": ["group4"],
  2600. "user": {"email": "bar@example.com"},
  2601. "tags": {"shared-tag": "yup"},
  2602. },
  2603. "project": self.project,
  2604. "count": 4,
  2605. },
  2606. {
  2607. "data": {
  2608. "message": "kinda bad",
  2609. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2610. "user": {"email": self.user.email},
  2611. "tags": {"shared-tag": "yup"},
  2612. "fingerprint": ["group7"],
  2613. },
  2614. "project": self.project,
  2615. "count": 3,
  2616. },
  2617. # Not in the top 5
  2618. {
  2619. "data": {
  2620. "message": "sorta bad",
  2621. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2622. "fingerprint": ["group5"],
  2623. "user": {"email": "bar@example.com"},
  2624. "tags": {"shared-tag": "yup"},
  2625. },
  2626. "project": self.project,
  2627. "count": 2,
  2628. },
  2629. {
  2630. "data": {
  2631. "message": "not so bad",
  2632. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2633. "fingerprint": ["group6"],
  2634. "user": {"email": "bar@example.com"},
  2635. "tags": {"shared-tag": "yup"},
  2636. },
  2637. "project": self.project,
  2638. "count": 1,
  2639. },
  2640. ]
  2641. self.events = []
  2642. for index, event_data in enumerate(self.event_data):
  2643. data = event_data["data"].copy()
  2644. for i in range(event_data["count"]):
  2645. data["event_id"] = f"{index}{i}" * 16
  2646. event = self.store_event(data, project_id=event_data["project"].id)
  2647. self.events.append(event)
  2648. self.enabled_features = {
  2649. "organizations:discover-basic": True,
  2650. }
  2651. self.url = reverse(
  2652. "sentry-api-0-organization-events-stats",
  2653. kwargs={"organization_id_or_slug": self.project.organization.slug},
  2654. )
  2655. def test_simple_top_events(self):
  2656. with self.feature(self.enabled_features):
  2657. response = self.client.get(
  2658. self.url,
  2659. data={
  2660. "start": iso_format(self.day_ago),
  2661. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2662. "interval": "1h",
  2663. "yAxis": "count()",
  2664. "orderby": ["-count()"],
  2665. "field": ["count()", "message", "user.email"],
  2666. "dataset": "errors",
  2667. "topEvents": 5,
  2668. },
  2669. format="json",
  2670. )
  2671. data = response.data
  2672. assert response.status_code == 200, response.content
  2673. assert len(data) == 6
  2674. for index, event in enumerate(self.events[:5]):
  2675. message = event.message or event.transaction
  2676. results = data[
  2677. ",".join([message, self.event_data[index]["data"]["user"].get("email", "None")])
  2678. ]
  2679. assert results["order"] == index
  2680. assert [{"count": self.event_data[index]["count"]}] in [
  2681. attrs for _, attrs in results["data"]
  2682. ]
  2683. other = data["Other"]
  2684. assert other["order"] == 5
  2685. assert [{"count": 3}] in [attrs for _, attrs in other["data"]]
  2686. def test_top_events_with_projects_other(self):
  2687. with self.feature(self.enabled_features):
  2688. response = self.client.get(
  2689. self.url,
  2690. data={
  2691. "start": iso_format(self.day_ago),
  2692. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2693. "interval": "1h",
  2694. "yAxis": "count()",
  2695. "orderby": ["-count()"],
  2696. "field": ["count()", "project"],
  2697. "dataset": "errors",
  2698. "topEvents": 1,
  2699. },
  2700. format="json",
  2701. )
  2702. data = response.data
  2703. assert response.status_code == 200, response.content
  2704. assert set(data.keys()) == {"Other", self.project.slug}
  2705. assert data[self.project.slug]["order"] == 0
  2706. assert [attrs[0]["count"] for _, attrs in data[self.project.slug]["data"]] == [15, 0]
  2707. assert data["Other"]["order"] == 1
  2708. assert [attrs[0]["count"] for _, attrs in data["Other"]["data"]] == [7, 6]
  2709. def test_top_events_with_issue(self):
  2710. # delete a group to make sure if this happens the value becomes unknown
  2711. event_group = self.events[0].group
  2712. event_group.delete()
  2713. with self.feature(self.enabled_features):
  2714. response = self.client.get(
  2715. self.url,
  2716. data={
  2717. "start": iso_format(self.day_ago),
  2718. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2719. "interval": "1h",
  2720. "yAxis": "count()",
  2721. "orderby": ["-count()"],
  2722. "field": ["count()", "message", "issue"],
  2723. "topEvents": 5,
  2724. "query": "",
  2725. "dataset": "errors",
  2726. },
  2727. format="json",
  2728. )
  2729. data = response.data
  2730. assert response.status_code == 200, response.content
  2731. assert len(data) == 6
  2732. for index, event in enumerate(self.events[:4]):
  2733. message = event.message
  2734. # Because we deleted the group for event 0
  2735. if index == 0 or event.group is None:
  2736. issue = "unknown"
  2737. else:
  2738. issue = event.group.qualified_short_id
  2739. results = data[",".join([issue, message])]
  2740. assert results["order"] == index
  2741. assert [{"count": self.event_data[index]["count"]}] in [
  2742. attrs for time, attrs in results["data"]
  2743. ]
  2744. other = data["Other"]
  2745. assert other["order"] == 5
  2746. assert [attrs[0]["count"] for _, attrs in data["Other"]["data"]] == [3, 0]
  2747. @mock.patch("sentry.models.GroupManager.get_issues_mapping")
  2748. def test_top_events_with_unknown_issue(self, mock_issues_mapping):
  2749. event = self.events[0]
  2750. event_data = self.event_data[0]
  2751. # ensure that the issue mapping returns None for the issue
  2752. mock_issues_mapping.return_value = {event.group.id: None}
  2753. with self.feature(self.enabled_features):
  2754. response = self.client.get(
  2755. self.url,
  2756. data={
  2757. "start": iso_format(self.day_ago),
  2758. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2759. "interval": "1h",
  2760. "yAxis": "count()",
  2761. "orderby": ["-count()"],
  2762. "field": ["count()", "issue"],
  2763. "topEvents": 5,
  2764. # narrow the search to just one issue
  2765. "query": f"issue.id:{event.group.id}",
  2766. "dataset": "errors",
  2767. },
  2768. format="json",
  2769. )
  2770. assert response.status_code == 200, response.content
  2771. data = response.data
  2772. assert len(data) == 1
  2773. results = data["unknown"]
  2774. assert results["order"] == 0
  2775. assert [{"count": event_data["count"]}] in [attrs for time, attrs in results["data"]]
  2776. @mock.patch(
  2777. "sentry.search.events.builder.base.raw_snql_query",
  2778. side_effect=[{"data": [{"issue.id": 1}], "meta": []}, {"data": [], "meta": []}],
  2779. )
  2780. def test_top_events_with_issue_check_query_conditions(self, mock_query):
  2781. """ "Intentionally separate from test_top_events_with_issue
  2782. This is to test against a bug where the condition for issues wasn't included and we'd be missing data for
  2783. the interval since we'd cap out the max rows. This was not caught by the previous test since the results
  2784. would still be correct given the smaller interval & lack of data
  2785. """
  2786. with self.feature(self.enabled_features):
  2787. self.client.get(
  2788. self.url,
  2789. data={
  2790. "start": iso_format(self.day_ago),
  2791. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2792. "interval": "1h",
  2793. "yAxis": "count()",
  2794. "orderby": ["-count()"],
  2795. "field": ["count()", "message", "issue"],
  2796. "topEvents": 5,
  2797. "query": "!event.type:transaction",
  2798. "dataset": "errors",
  2799. },
  2800. format="json",
  2801. )
  2802. assert (
  2803. Condition(
  2804. Function(
  2805. "coalesce",
  2806. [Column("group_id", entity=Entity("events", alias="events")), 0],
  2807. "issue.id",
  2808. ),
  2809. Op.IN,
  2810. [1],
  2811. )
  2812. in mock_query.mock_calls[1].args[0].query.where
  2813. )
  2814. def test_group_id_tag_simple(self):
  2815. event_data: _EventDataDict = {
  2816. "data": {
  2817. "message": "poof",
  2818. "timestamp": iso_format(self.day_ago + timedelta(minutes=2)),
  2819. "user": {"email": self.user.email},
  2820. "tags": {"group_id": "the tag"},
  2821. "fingerprint": ["group1"],
  2822. },
  2823. "project": self.project2,
  2824. "count": 7,
  2825. }
  2826. for i in range(event_data["count"]):
  2827. event_data["data"]["event_id"] = f"a{i}" * 16
  2828. self.store_event(event_data["data"], project_id=event_data["project"].id)
  2829. data = {
  2830. "start": iso_format(self.day_ago),
  2831. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2832. "interval": "1h",
  2833. "yAxis": "count()",
  2834. "orderby": ["-count()"],
  2835. "field": ["count()", "group_id"],
  2836. "topEvents": 5,
  2837. "partial": 1,
  2838. }
  2839. with self.feature(self.enabled_features):
  2840. response = self.client.get(self.url, data, format="json")
  2841. assert response.status_code == 200, response.content
  2842. assert response.data["the tag"]["data"][0][1] == [{"count": 7}]
  2843. data["query"] = 'group_id:"the tag"'
  2844. with self.feature(self.enabled_features):
  2845. response = self.client.get(self.url, data, format="json")
  2846. assert response.status_code == 200
  2847. assert response.data["the tag"]["data"][0][1] == [{"count": 7}]
  2848. data["query"] = "group_id:abc"
  2849. with self.feature(self.enabled_features):
  2850. response = self.client.get(self.url, data, format="json")
  2851. assert response.status_code == 200
  2852. assert all([interval[1][0]["count"] == 0 for interval in response.data["data"]])
  2853. def test_top_events_with_error_unhandled(self):
  2854. self.login_as(user=self.user)
  2855. project = self.create_project()
  2856. prototype = load_data("android-ndk")
  2857. prototype["event_id"] = "f" * 32
  2858. prototype["logentry"] = {"formatted": "not handled"}
  2859. prototype["exception"]["values"][0]["value"] = "not handled"
  2860. prototype["exception"]["values"][0]["mechanism"]["handled"] = False
  2861. prototype["timestamp"] = iso_format(self.day_ago + timedelta(minutes=2))
  2862. self.store_event(data=prototype, project_id=project.id)
  2863. with self.feature(self.enabled_features):
  2864. response = self.client.get(
  2865. self.url,
  2866. data={
  2867. "start": iso_format(self.day_ago),
  2868. "end": iso_format(self.day_ago + timedelta(hours=2)),
  2869. "interval": "1h",
  2870. "yAxis": "count()",
  2871. "orderby": ["-count()"],
  2872. "field": ["count()", "error.unhandled"],
  2873. "topEvents": 5,
  2874. },
  2875. format="json",
  2876. )
  2877. data = response.data
  2878. assert response.status_code == 200, response.content
  2879. assert len(data) == 2