test_backend.py 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937
  1. import uuid
  2. from datetime import datetime, timedelta
  3. from unittest import mock
  4. import pytest
  5. import pytz
  6. from django.utils import timezone
  7. from sentry import options
  8. from sentry.api.issue_search import convert_query_values, issue_search_config, parse_search_query
  9. from sentry.exceptions import InvalidSearchQuery
  10. from sentry.issues.grouptype import (
  11. ErrorGroupType,
  12. NoiseConfig,
  13. PerformanceNPlusOneGroupType,
  14. PerformanceRenderBlockingAssetSpanGroupType,
  15. ProfileFileIOGroupType,
  16. )
  17. from sentry.issues.occurrence_consumer import process_event_and_issue_occurrence
  18. from sentry.models import (
  19. Environment,
  20. Group,
  21. GroupAssignee,
  22. GroupBookmark,
  23. GroupEnvironment,
  24. GroupHistoryStatus,
  25. GroupStatus,
  26. GroupSubscription,
  27. Integration,
  28. record_group_history,
  29. )
  30. from sentry.models.groupowner import GroupOwner
  31. from sentry.search.snuba.backend import (
  32. CdcEventsDatasetSnubaSearchBackend,
  33. EventsDatasetSnubaSearchBackend,
  34. )
  35. from sentry.search.snuba.executors import InvalidQueryForExecutor
  36. from sentry.testutils import SnubaTestCase, TestCase, xfail_if_not_postgres
  37. from sentry.testutils.helpers import Feature
  38. from sentry.testutils.helpers.datetime import before_now, iso_format
  39. from sentry.types.group import GroupSubStatus
  40. from sentry.utils.snuba import SENTRY_SNUBA_MAP, SnubaError
  41. from tests.sentry.issues.test_utils import OccurrenceTestMixin
  42. def date_to_query_format(date):
  43. return date.strftime("%Y-%m-%dT%H:%M:%S")
  44. class SharedSnubaTest(TestCase, SnubaTestCase):
  45. def build_search_filter(self, query, projects=None, user=None, environments=None):
  46. user = user if user is not None else self.user
  47. projects = projects if projects is not None else [self.project]
  48. return convert_query_values(parse_search_query(query), projects, user, environments)
  49. def make_query(
  50. self,
  51. projects=None,
  52. search_filter_query=None,
  53. environments=None,
  54. sort_by="date",
  55. limit=None,
  56. count_hits=False,
  57. date_from=None,
  58. date_to=None,
  59. cursor=None,
  60. ):
  61. search_filters = []
  62. projects = projects if projects is not None else [self.project]
  63. if search_filter_query is not None:
  64. search_filters = self.build_search_filter(
  65. search_filter_query, projects, environments=environments
  66. )
  67. kwargs = {}
  68. if limit is not None:
  69. kwargs["limit"] = limit
  70. return self.backend.query(
  71. projects,
  72. search_filters=search_filters,
  73. environments=environments,
  74. count_hits=count_hits,
  75. sort_by=sort_by,
  76. date_from=date_from,
  77. date_to=date_to,
  78. cursor=cursor,
  79. **kwargs,
  80. )
  81. def store_event(self, data, *args, **kwargs):
  82. event = super().store_event(data, *args, **kwargs)
  83. environment_name = data.get("environment")
  84. if environment_name:
  85. GroupEnvironment.objects.filter(
  86. group_id=event.group_id,
  87. environment__name=environment_name,
  88. first_seen__gt=event.datetime,
  89. ).update(first_seen=event.datetime)
  90. return event
  91. class EventsSnubaSearchTest(SharedSnubaTest):
  92. @property
  93. def backend(self):
  94. return EventsDatasetSnubaSearchBackend()
  95. def setUp(self):
  96. super().setUp()
  97. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  98. event1_timestamp = iso_format(self.base_datetime - timedelta(days=21))
  99. self.event1 = self.store_event(
  100. data={
  101. "fingerprint": ["put-me-in-group1"],
  102. "event_id": "a" * 32,
  103. "message": "foo. Also, this message is intended to be greater than 256 characters so that we can put some unique string identifier after that point in the string. The purpose of this is in order to verify we are using snuba to search messages instead of Postgres (postgres truncates at 256 characters and clickhouse does not). santryrox.",
  104. "environment": "production",
  105. "tags": {"server": "example.com", "sentry:user": "event1@example.com"},
  106. "timestamp": event1_timestamp,
  107. "stacktrace": {"frames": [{"module": "group1"}]},
  108. },
  109. project_id=self.project.id,
  110. )
  111. self.event3 = self.store_event(
  112. data={
  113. "fingerprint": ["put-me-in-group1"],
  114. "event_id": "c" * 32,
  115. "message": "group1",
  116. "environment": "production",
  117. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  118. "timestamp": iso_format(self.base_datetime),
  119. "stacktrace": {"frames": [{"module": "group1"}]},
  120. },
  121. project_id=self.project.id,
  122. )
  123. self.group1 = Group.objects.get(id=self.event1.group.id)
  124. assert self.group1.id == self.event1.group.id
  125. assert self.group1.id == self.event3.group.id
  126. assert self.group1.first_seen == self.event1.datetime
  127. assert self.group1.last_seen == self.event3.datetime
  128. self.group1.times_seen = 5
  129. self.group1.status = GroupStatus.UNRESOLVED
  130. self.group1.substatus = GroupSubStatus.ONGOING
  131. self.group1.update(type=ErrorGroupType.type_id)
  132. self.group1.save()
  133. self.store_group(self.group1)
  134. self.event2 = self.store_event(
  135. data={
  136. "fingerprint": ["put-me-in-group2"],
  137. "event_id": "b" * 32,
  138. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  139. "message": "bar",
  140. "stacktrace": {"frames": [{"module": "group2"}]},
  141. "environment": "staging",
  142. "tags": {
  143. "server": "example.com",
  144. "url": "http://example.com",
  145. "sentry:user": "event2@example.com",
  146. },
  147. },
  148. project_id=self.project.id,
  149. )
  150. self.group2 = Group.objects.get(id=self.event2.group.id)
  151. assert self.group2.id == self.event2.group.id
  152. assert self.group2.first_seen == self.group2.last_seen == self.event2.datetime
  153. self.group2.status = GroupStatus.RESOLVED
  154. self.group2.substatus = None
  155. self.group2.times_seen = 10
  156. self.group2.update(type=ErrorGroupType.type_id)
  157. self.group2.save()
  158. self.store_group(self.group2)
  159. GroupBookmark.objects.create(
  160. user_id=self.user.id, group=self.group2, project=self.group2.project
  161. )
  162. GroupAssignee.objects.create(
  163. user_id=self.user.id, group=self.group2, project=self.group2.project
  164. )
  165. GroupSubscription.objects.create(
  166. user_id=self.user.id, group=self.group1, project=self.group1.project, is_active=True
  167. )
  168. GroupSubscription.objects.create(
  169. user_id=self.user.id, group=self.group2, project=self.group2.project, is_active=False
  170. )
  171. self.environments = {
  172. "production": self.event1.get_environment(),
  173. "staging": self.event2.get_environment(),
  174. }
  175. def set_up_multi_project(self):
  176. self.project2 = self.create_project(organization=self.project.organization)
  177. self.event_p2 = self.store_event(
  178. data={
  179. "event_id": "a" * 32,
  180. "fingerprint": ["put-me-in-groupP2"],
  181. "timestamp": iso_format(self.base_datetime - timedelta(days=21)),
  182. "message": "foo",
  183. "stacktrace": {"frames": [{"module": "group_p2"}]},
  184. "tags": {"server": "example.com"},
  185. "environment": "production",
  186. },
  187. project_id=self.project2.id,
  188. )
  189. self.group_p2 = Group.objects.get(id=self.event_p2.group.id)
  190. self.group_p2.times_seen = 6
  191. self.group_p2.last_seen = self.base_datetime - timedelta(days=1)
  192. self.group_p2.save()
  193. self.store_group(self.group_p2)
  194. def create_group_with_integration_external_issue(self, environment="production"):
  195. event = self.store_event(
  196. data={
  197. "fingerprint": ["linked_group1"],
  198. "event_id": uuid.uuid4().hex,
  199. "timestamp": iso_format(self.base_datetime),
  200. "environment": environment,
  201. },
  202. project_id=self.project.id,
  203. )
  204. integration = Integration.objects.create(provider="example", name="Example")
  205. integration.add_organization(event.group.organization, self.user)
  206. self.create_integration_external_issue(
  207. group=event.group,
  208. integration=integration,
  209. key="APP-123",
  210. )
  211. return event.group
  212. def create_group_with_platform_external_issue(self, environment="production"):
  213. event = self.store_event(
  214. data={
  215. "fingerprint": ["linked_group2"],
  216. "event_id": uuid.uuid4().hex,
  217. "timestamp": iso_format(self.base_datetime),
  218. "environment": environment,
  219. },
  220. project_id=self.project.id,
  221. )
  222. self.create_platform_external_issue(
  223. group=event.group,
  224. service_type="sentry-app",
  225. display_name="App#issue-1",
  226. web_url="https://example.com/app/issues/1",
  227. )
  228. return event.group
  229. def run_test_query_in_syntax(
  230. self, query, expected_groups, expected_negative_groups=None, environments=None
  231. ):
  232. results = self.make_query(search_filter_query=query, environments=environments)
  233. sort_key = lambda result: result.id
  234. assert sorted(results, key=sort_key) == sorted(expected_groups, key=sort_key)
  235. if expected_negative_groups is not None:
  236. results = self.make_query(search_filter_query=f"!{query}")
  237. assert sorted(results, key=sort_key) == sorted(expected_negative_groups, key=sort_key)
  238. def test_query(self):
  239. results = self.make_query(search_filter_query="foo")
  240. assert set(results) == {self.group1}
  241. results = self.make_query(search_filter_query="bar")
  242. assert set(results) == {self.group2}
  243. def test_query_multi_project(self):
  244. self.set_up_multi_project()
  245. results = self.make_query([self.project, self.project2], search_filter_query="foo")
  246. assert set(results) == {self.group1, self.group_p2}
  247. def test_query_with_environment(self):
  248. results = self.make_query(
  249. environments=[self.environments["production"]], search_filter_query="foo"
  250. )
  251. assert set(results) == {self.group1}
  252. results = self.make_query(
  253. environments=[self.environments["production"]], search_filter_query="bar"
  254. )
  255. assert set(results) == set()
  256. results = self.make_query(
  257. environments=[self.environments["staging"]], search_filter_query="bar"
  258. )
  259. assert set(results) == {self.group2}
  260. def test_query_for_text_in_long_message(self):
  261. results = self.make_query(
  262. [self.project],
  263. environments=[self.environments["production"]],
  264. search_filter_query="santryrox",
  265. )
  266. assert set(results) == {self.group1}
  267. def test_multi_environments(self):
  268. self.set_up_multi_project()
  269. results = self.make_query(
  270. [self.project, self.project2],
  271. environments=[self.environments["production"], self.environments["staging"]],
  272. )
  273. assert set(results) == {self.group1, self.group2, self.group_p2}
  274. def test_query_with_environment_multi_project(self):
  275. self.set_up_multi_project()
  276. results = self.make_query(
  277. [self.project, self.project2],
  278. environments=[self.environments["production"]],
  279. search_filter_query="foo",
  280. )
  281. assert set(results) == {self.group1, self.group_p2}
  282. results = self.make_query(
  283. [self.project, self.project2],
  284. environments=[self.environments["production"]],
  285. search_filter_query="bar",
  286. )
  287. assert set(results) == set()
  288. def test_sort(self):
  289. results = self.make_query(sort_by="date")
  290. assert list(results) == [self.group1, self.group2]
  291. results = self.make_query(sort_by="new")
  292. assert list(results) == [self.group2, self.group1]
  293. results = self.make_query(sort_by="freq")
  294. assert list(results) == [self.group1, self.group2]
  295. results = self.make_query(sort_by="priority")
  296. assert list(results) == [self.group1, self.group2]
  297. results = self.make_query(sort_by="user")
  298. assert list(results) == [self.group1, self.group2]
  299. def test_sort_with_environment(self):
  300. for dt in [
  301. self.group1.first_seen + timedelta(days=1),
  302. self.group1.first_seen + timedelta(days=2),
  303. self.group1.last_seen + timedelta(days=1),
  304. ]:
  305. self.store_event(
  306. data={
  307. "fingerprint": ["put-me-in-group2"],
  308. "timestamp": iso_format(dt),
  309. "stacktrace": {"frames": [{"module": "group2"}]},
  310. "environment": "production",
  311. "message": "group2",
  312. },
  313. project_id=self.project.id,
  314. )
  315. results = self.make_query(environments=[self.environments["production"]], sort_by="date")
  316. assert list(results) == [self.group2, self.group1]
  317. results = self.make_query(environments=[self.environments["production"]], sort_by="new")
  318. assert list(results) == [self.group2, self.group1]
  319. results = self.make_query(environments=[self.environments["production"]], sort_by="freq")
  320. assert list(results) == [self.group2, self.group1]
  321. results = self.make_query(
  322. environments=[self.environments["production"]], sort_by="priority"
  323. )
  324. assert list(results) == [self.group2, self.group1]
  325. results = self.make_query(environments=[self.environments["production"]], sort_by="user")
  326. assert list(results) == [self.group1, self.group2]
  327. def test_status(self):
  328. results = self.make_query(search_filter_query="is:unresolved")
  329. assert set(results) == {self.group1}
  330. results = self.make_query(search_filter_query="is:resolved")
  331. assert set(results) == {self.group2}
  332. event_3 = self.store_event(
  333. data={
  334. "fingerprint": ["put-me-in-group3"],
  335. "event_id": "c" * 32,
  336. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  337. },
  338. project_id=self.project.id,
  339. )
  340. group_3 = event_3.group
  341. group_3.status = GroupStatus.MUTED
  342. group_3.substatus = None
  343. group_3.save()
  344. self.run_test_query_in_syntax(
  345. "status:[unresolved, resolved]", [self.group1, self.group2], [group_3]
  346. )
  347. self.run_test_query_in_syntax(
  348. "status:[resolved, muted]", [self.group2, group_3], [self.group1]
  349. )
  350. def test_category(self):
  351. results = self.make_query(search_filter_query="issue.category:error")
  352. assert set(results) == {self.group1, self.group2}
  353. event_3 = self.store_event(
  354. data={
  355. "fingerprint": ["put-me-in-group3"],
  356. "event_id": "c" * 32,
  357. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  358. },
  359. project_id=self.project.id,
  360. )
  361. group_3 = event_3.group
  362. group_3.update(type=PerformanceNPlusOneGroupType.type_id)
  363. results = self.make_query(search_filter_query="issue.category:performance")
  364. assert set(results) == {group_3}
  365. results = self.make_query(search_filter_query="issue.category:[error, performance]")
  366. assert set(results) == {self.group1, self.group2, group_3}
  367. with pytest.raises(InvalidSearchQuery):
  368. self.make_query(search_filter_query="issue.category:hellboy")
  369. def test_not_perf_category(self):
  370. results = self.make_query(search_filter_query="issue.category:error foo")
  371. assert set(results) == {self.group1}
  372. not_results = self.make_query(search_filter_query="!issue.category:performance foo")
  373. assert set(not_results) == {self.group1}
  374. def test_type(self):
  375. results = self.make_query(search_filter_query="issue.type:error")
  376. assert set(results) == {self.group1, self.group2}
  377. event_3 = self.store_event(
  378. data={
  379. "fingerprint": ["put-me-in-group3"],
  380. "event_id": "c" * 32,
  381. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  382. "type": PerformanceNPlusOneGroupType.type_id,
  383. },
  384. project_id=self.project.id,
  385. )
  386. group_3 = event_3.group
  387. group_3.update(type=PerformanceNPlusOneGroupType.type_id)
  388. results = self.make_query(
  389. search_filter_query="issue.type:performance_n_plus_one_db_queries"
  390. )
  391. assert set(results) == {group_3}
  392. event_4 = self.store_event(
  393. data={
  394. "fingerprint": ["put-me-in-group4"],
  395. "event_id": "d" * 32,
  396. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  397. },
  398. project_id=self.project.id,
  399. )
  400. group_4 = event_4.group
  401. group_4.update(type=PerformanceRenderBlockingAssetSpanGroupType.type_id)
  402. results = self.make_query(
  403. search_filter_query="issue.type:performance_render_blocking_asset_span"
  404. )
  405. assert set(results) == {group_4}
  406. results = self.make_query(
  407. search_filter_query="issue.type:[performance_render_blocking_asset_span, performance_n_plus_one_db_queries, error]"
  408. )
  409. assert set(results) == {self.group1, self.group2, group_3, group_4}
  410. with pytest.raises(InvalidSearchQuery):
  411. self.make_query(search_filter_query="issue.type:performance_i_dont_exist")
  412. def test_status_with_environment(self):
  413. results = self.make_query(
  414. environments=[self.environments["production"]], search_filter_query="is:unresolved"
  415. )
  416. assert set(results) == {self.group1}
  417. results = self.make_query(
  418. environments=[self.environments["staging"]], search_filter_query="is:resolved"
  419. )
  420. assert set(results) == {self.group2}
  421. results = self.make_query(
  422. environments=[self.environments["production"]], search_filter_query="is:resolved"
  423. )
  424. assert set(results) == set()
  425. def test_tags(self):
  426. results = self.make_query(search_filter_query="environment:staging")
  427. assert set(results) == {self.group2}
  428. results = self.make_query(search_filter_query="environment:example.com")
  429. assert set(results) == set()
  430. results = self.make_query(search_filter_query="has:environment")
  431. assert set(results) == {self.group2, self.group1}
  432. results = self.make_query(search_filter_query="environment:staging server:example.com")
  433. assert set(results) == {self.group2}
  434. results = self.make_query(search_filter_query='url:"http://example.com"')
  435. assert set(results) == {self.group2}
  436. results = self.make_query(search_filter_query="environment:staging has:server")
  437. assert set(results) == {self.group2}
  438. results = self.make_query(search_filter_query="environment:staging server:bar.example.com")
  439. assert set(results) == set()
  440. def test_tags_with_environment(self):
  441. results = self.make_query(
  442. environments=[self.environments["production"]], search_filter_query="server:example.com"
  443. )
  444. assert set(results) == {self.group1}
  445. results = self.make_query(
  446. environments=[self.environments["staging"]], search_filter_query="server:example.com"
  447. )
  448. assert set(results) == {self.group2}
  449. results = self.make_query(
  450. environments=[self.environments["staging"]], search_filter_query="has:server"
  451. )
  452. assert set(results) == {self.group2}
  453. results = self.make_query(
  454. environments=[self.environments["production"]],
  455. search_filter_query='url:"http://example.com"',
  456. )
  457. assert set(results) == set()
  458. results = self.make_query(
  459. environments=[self.environments["staging"]],
  460. search_filter_query='url:"http://example.com"',
  461. )
  462. assert set(results) == {self.group2}
  463. results = self.make_query(
  464. environments=[self.environments["staging"]],
  465. search_filter_query="server:bar.example.com",
  466. )
  467. assert set(results) == set()
  468. def test_bookmarked_by(self):
  469. results = self.make_query(search_filter_query="bookmarks:%s" % self.user.username)
  470. assert set(results) == {self.group2}
  471. def test_bookmarked_by_in_syntax(self):
  472. self.run_test_query_in_syntax(
  473. f"bookmarks:[{self.user.username}]", [self.group2], [self.group1]
  474. )
  475. user_2 = self.create_user()
  476. GroupBookmark.objects.create(
  477. user_id=user_2.id, group=self.group1, project=self.group2.project
  478. )
  479. self.run_test_query_in_syntax(
  480. f"bookmarks:[{self.user.username}, {user_2.username}]", [self.group2, self.group1], []
  481. )
  482. def test_bookmarked_by_with_environment(self):
  483. results = self.make_query(
  484. environments=[self.environments["staging"]],
  485. search_filter_query="bookmarks:%s" % self.user.username,
  486. )
  487. assert set(results) == {self.group2}
  488. results = self.make_query(
  489. environments=[self.environments["production"]],
  490. search_filter_query="bookmarks:%s" % self.user.username,
  491. )
  492. assert set(results) == set()
  493. def test_search_filter_query_with_custom_priority_tag(self):
  494. priority = "high"
  495. self.store_event(
  496. data={
  497. "fingerprint": ["put-me-in-group2"],
  498. "timestamp": iso_format(self.group2.first_seen + timedelta(days=1)),
  499. "stacktrace": {"frames": [{"module": "group2"}]},
  500. "message": "group2",
  501. "tags": {"priority": priority},
  502. },
  503. project_id=self.project.id,
  504. )
  505. results = self.make_query(search_filter_query="priority:%s" % priority)
  506. assert set(results) == {self.group2}
  507. def test_search_filter_query_with_custom_priority_tag_and_priority_sort(self):
  508. priority = "high"
  509. for i in range(1, 3):
  510. self.store_event(
  511. data={
  512. "fingerprint": ["put-me-in-group1"],
  513. "timestamp": iso_format(self.group2.last_seen + timedelta(days=i)),
  514. "stacktrace": {"frames": [{"module": "group1"}]},
  515. "message": "group1",
  516. "tags": {"priority": priority},
  517. },
  518. project_id=self.project.id,
  519. )
  520. self.store_event(
  521. data={
  522. "fingerprint": ["put-me-in-group2"],
  523. "timestamp": iso_format(self.group2.last_seen + timedelta(days=2)),
  524. "stacktrace": {"frames": [{"module": "group2"}]},
  525. "message": "group2",
  526. "tags": {"priority": priority},
  527. },
  528. project_id=self.project.id,
  529. )
  530. results = self.make_query(search_filter_query="priority:%s" % priority, sort_by="priority")
  531. assert list(results) == [self.group1, self.group2]
  532. def test_search_tag_overlapping_with_internal_fields(self):
  533. # Using a tag of email overlaps with the promoted user.email column in events.
  534. # We don't want to bypass public schema limits in issue search.
  535. self.store_event(
  536. data={
  537. "fingerprint": ["put-me-in-group2"],
  538. "timestamp": iso_format(self.group2.first_seen + timedelta(days=1)),
  539. "stacktrace": {"frames": [{"module": "group2"}]},
  540. "message": "group2",
  541. "tags": {"email": "tags@example.com"},
  542. },
  543. project_id=self.project.id,
  544. )
  545. results = self.make_query(search_filter_query="email:tags@example.com")
  546. assert set(results) == {self.group2}
  547. def test_project(self):
  548. results = self.make_query([self.create_project(name="other")])
  549. assert set(results) == set()
  550. def test_pagination(self):
  551. for options_set in [
  552. {"snuba.search.min-pre-snuba-candidates": None},
  553. {"snuba.search.min-pre-snuba-candidates": 500},
  554. ]:
  555. with self.options(options_set):
  556. results = self.backend.query([self.project], limit=1, sort_by="date")
  557. assert set(results) == {self.group1}
  558. assert not results.prev.has_results
  559. assert results.next.has_results
  560. results = self.backend.query(
  561. [self.project], cursor=results.next, limit=1, sort_by="date"
  562. )
  563. assert set(results) == {self.group2}
  564. assert results.prev.has_results
  565. assert not results.next.has_results
  566. # note: previous cursor
  567. results = self.backend.query(
  568. [self.project], cursor=results.prev, limit=1, sort_by="date"
  569. )
  570. assert set(results) == {self.group1}
  571. assert results.prev.has_results
  572. assert results.next.has_results
  573. # note: previous cursor, paging too far into 0 results
  574. results = self.backend.query(
  575. [self.project], cursor=results.prev, limit=1, sort_by="date"
  576. )
  577. assert set(results) == set()
  578. assert not results.prev.has_results
  579. assert results.next.has_results
  580. results = self.backend.query(
  581. [self.project], cursor=results.next, limit=1, sort_by="date"
  582. )
  583. assert set(results) == {self.group1}
  584. assert results.prev.has_results
  585. assert results.next.has_results
  586. results = self.backend.query(
  587. [self.project], cursor=results.next, limit=1, sort_by="date"
  588. )
  589. assert set(results) == {self.group2}
  590. assert results.prev.has_results
  591. assert not results.next.has_results
  592. results = self.backend.query(
  593. [self.project], cursor=results.next, limit=1, sort_by="date"
  594. )
  595. assert set(results) == set()
  596. assert results.prev.has_results
  597. assert not results.next.has_results
  598. def test_pagination_with_environment(self):
  599. for dt in [
  600. self.group1.first_seen + timedelta(days=1),
  601. self.group1.first_seen + timedelta(days=2),
  602. self.group1.last_seen + timedelta(days=1),
  603. ]:
  604. self.store_event(
  605. data={
  606. "fingerprint": ["put-me-in-group2"],
  607. "timestamp": iso_format(dt),
  608. "environment": "production",
  609. "message": "group2",
  610. "stacktrace": {"frames": [{"module": "group2"}]},
  611. },
  612. project_id=self.project.id,
  613. )
  614. results = self.backend.query(
  615. [self.project],
  616. environments=[self.environments["production"]],
  617. sort_by="date",
  618. limit=1,
  619. count_hits=True,
  620. )
  621. assert list(results) == [self.group2]
  622. assert results.hits == 2
  623. results = self.backend.query(
  624. [self.project],
  625. environments=[self.environments["production"]],
  626. sort_by="date",
  627. limit=1,
  628. cursor=results.next,
  629. count_hits=True,
  630. )
  631. assert list(results) == [self.group1]
  632. assert results.hits == 2
  633. results = self.backend.query(
  634. [self.project],
  635. environments=[self.environments["production"]],
  636. sort_by="date",
  637. limit=1,
  638. cursor=results.next,
  639. count_hits=True,
  640. )
  641. assert list(results) == []
  642. assert results.hits == 2
  643. def test_age_filter(self):
  644. results = self.make_query(
  645. search_filter_query="firstSeen:>=%s" % date_to_query_format(self.group2.first_seen)
  646. )
  647. assert set(results) == {self.group2}
  648. results = self.make_query(
  649. search_filter_query="firstSeen:<=%s"
  650. % date_to_query_format(self.group1.first_seen + timedelta(minutes=1))
  651. )
  652. assert set(results) == {self.group1}
  653. results = self.make_query(
  654. search_filter_query="firstSeen:>=%s firstSeen:<=%s"
  655. % (
  656. date_to_query_format(self.group1.first_seen),
  657. date_to_query_format(self.group1.first_seen + timedelta(minutes=1)),
  658. )
  659. )
  660. assert set(results) == {self.group1}
  661. def test_age_filter_with_environment(self):
  662. # add time instead to make it greater than or less than as needed.
  663. group1_first_seen = GroupEnvironment.objects.get(
  664. environment=self.environments["production"], group=self.group1
  665. ).first_seen
  666. results = self.make_query(
  667. environments=[self.environments["production"]],
  668. search_filter_query="firstSeen:>=%s" % date_to_query_format(group1_first_seen),
  669. )
  670. assert set(results) == {self.group1}
  671. results = self.make_query(
  672. environments=[self.environments["production"]],
  673. search_filter_query="firstSeen:<=%s" % date_to_query_format(group1_first_seen),
  674. )
  675. assert set(results) == {self.group1}
  676. results = self.make_query(
  677. environments=[self.environments["production"]],
  678. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  679. )
  680. assert set(results) == set()
  681. self.store_event(
  682. data={
  683. "fingerprint": ["put-me-in-group1"],
  684. "timestamp": iso_format(group1_first_seen + timedelta(days=1)),
  685. "message": "group1",
  686. "stacktrace": {"frames": [{"module": "group1"}]},
  687. "environment": "development",
  688. },
  689. project_id=self.project.id,
  690. )
  691. results = self.make_query(
  692. environments=[self.environments["production"]],
  693. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  694. )
  695. assert set(results) == set()
  696. results = self.make_query(
  697. environments=[Environment.objects.get(name="development")],
  698. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  699. )
  700. assert set(results) == {self.group1}
  701. def test_times_seen_filter(self):
  702. results = self.make_query([self.project], search_filter_query="times_seen:2")
  703. assert set(results) == {self.group1}
  704. results = self.make_query([self.project], search_filter_query="times_seen:>=2")
  705. assert set(results) == {self.group1}
  706. results = self.make_query([self.project], search_filter_query="times_seen:<=1")
  707. assert set(results) == {self.group2}
  708. def test_last_seen_filter(self):
  709. results = self.make_query(
  710. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen)
  711. )
  712. assert set(results) == {self.group1}
  713. results = self.make_query(
  714. search_filter_query="lastSeen:>=%s lastSeen:<=%s"
  715. % (
  716. date_to_query_format(self.group1.last_seen),
  717. date_to_query_format(self.group1.last_seen + timedelta(minutes=1)),
  718. )
  719. )
  720. assert set(results) == {self.group1}
  721. def test_last_seen_filter_with_environment(self):
  722. results = self.make_query(
  723. environments=[self.environments["production"]],
  724. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen),
  725. )
  726. assert set(results) == {self.group1}
  727. results = self.make_query(
  728. environments=[self.environments["production"]],
  729. search_filter_query="lastSeen:<=%s" % date_to_query_format(self.group1.last_seen),
  730. )
  731. assert set(results) == {self.group1}
  732. results = self.make_query(
  733. environments=[self.environments["production"]],
  734. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  735. )
  736. assert set(results) == set()
  737. self.store_event(
  738. data={
  739. "fingerprint": ["put-me-in-group1"],
  740. "timestamp": iso_format(self.group1.last_seen + timedelta(days=1)),
  741. "message": "group1",
  742. "stacktrace": {"frames": [{"module": "group1"}]},
  743. "environment": "development",
  744. },
  745. project_id=self.project.id,
  746. )
  747. self.group1.update(last_seen=self.group1.last_seen + timedelta(days=1))
  748. results = self.make_query(
  749. environments=[self.environments["production"]],
  750. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  751. )
  752. assert set(results) == set()
  753. results = self.make_query(
  754. environments=[Environment.objects.get(name="development")],
  755. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  756. )
  757. assert set(results) == set()
  758. results = self.make_query(
  759. environments=[Environment.objects.get(name="development")],
  760. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen),
  761. )
  762. assert set(results) == {self.group1}
  763. def test_date_filter(self):
  764. results = self.make_query(
  765. date_from=self.event2.datetime,
  766. search_filter_query="timestamp:>=%s" % date_to_query_format(self.event2.datetime),
  767. )
  768. assert set(results) == {self.group1, self.group2}
  769. results = self.make_query(
  770. date_to=self.event1.datetime + timedelta(minutes=1),
  771. search_filter_query="timestamp:<=%s"
  772. % date_to_query_format(self.event1.datetime + timedelta(minutes=1)),
  773. )
  774. assert set(results) == {self.group1}
  775. results = self.make_query(
  776. date_from=self.event1.datetime,
  777. date_to=self.event2.datetime + timedelta(minutes=1),
  778. search_filter_query="timestamp:>=%s timestamp:<=%s"
  779. % (
  780. date_to_query_format(self.event1.datetime),
  781. date_to_query_format(self.event2.datetime + timedelta(minutes=1)),
  782. ),
  783. )
  784. assert set(results) == {self.group1, self.group2}
  785. # Test with `Z` utc marker, should be equivalent
  786. results = self.make_query(
  787. date_from=self.event1.datetime,
  788. date_to=self.event2.datetime + timedelta(minutes=1),
  789. search_filter_query="timestamp:>=%s timestamp:<=%s"
  790. % (
  791. date_to_query_format(self.event1.datetime) + "Z",
  792. date_to_query_format(self.event2.datetime + timedelta(minutes=1)) + "Z",
  793. ),
  794. )
  795. assert set(results) == {self.group1, self.group2}
  796. def test_date_filter_with_environment(self):
  797. results = self.backend.query(
  798. [self.project],
  799. environments=[self.environments["production"]],
  800. date_from=self.event2.datetime,
  801. )
  802. assert set(results) == {self.group1}
  803. results = self.backend.query(
  804. [self.project],
  805. environments=[self.environments["production"]],
  806. date_to=self.event1.datetime + timedelta(minutes=1),
  807. )
  808. assert set(results) == {self.group1}
  809. results = self.backend.query(
  810. [self.project],
  811. environments=[self.environments["staging"]],
  812. date_from=self.event1.datetime,
  813. date_to=self.event2.datetime + timedelta(minutes=1),
  814. )
  815. assert set(results) == {self.group2}
  816. def test_linked(self):
  817. linked_group1 = self.create_group_with_integration_external_issue()
  818. linked_group2 = self.create_group_with_platform_external_issue()
  819. results = self.make_query(search_filter_query="is:unlinked")
  820. assert set(results) == {self.group1, self.group2}
  821. results = self.make_query(search_filter_query="is:linked")
  822. assert set(results) == {linked_group1, linked_group2}
  823. def test_linked_with_only_integration_external_issue(self):
  824. linked_group = self.create_group_with_integration_external_issue()
  825. results = self.make_query(search_filter_query="is:unlinked")
  826. assert set(results) == {self.group1, self.group2}
  827. results = self.make_query(search_filter_query="is:linked")
  828. assert set(results) == {linked_group}
  829. def test_linked_with_only_platform_external_issue(self):
  830. linked_group = self.create_group_with_platform_external_issue()
  831. results = self.make_query(search_filter_query="is:unlinked")
  832. assert set(results) == {self.group1, self.group2}
  833. results = self.make_query(search_filter_query="is:linked")
  834. assert set(results) == {linked_group}
  835. def test_linked_with_environment(self):
  836. linked_group1 = self.create_group_with_integration_external_issue(environment="production")
  837. linked_group2 = self.create_group_with_platform_external_issue(environment="staging")
  838. results = self.make_query(
  839. environments=[self.environments["production"]], search_filter_query="is:unlinked"
  840. )
  841. assert set(results) == {self.group1}
  842. results = self.make_query(
  843. environments=[self.environments["staging"]], search_filter_query="is:unlinked"
  844. )
  845. assert set(results) == {self.group2}
  846. results = self.make_query(
  847. environments=[self.environments["production"]], search_filter_query="is:linked"
  848. )
  849. assert set(results) == {linked_group1}
  850. results = self.make_query(
  851. environments=[self.environments["staging"]], search_filter_query="is:linked"
  852. )
  853. assert set(results) == {linked_group2}
  854. def test_unassigned(self):
  855. results = self.make_query(search_filter_query="is:unassigned")
  856. assert set(results) == {self.group1}
  857. results = self.make_query(search_filter_query="is:assigned")
  858. assert set(results) == {self.group2}
  859. def test_unassigned_with_environment(self):
  860. results = self.make_query(
  861. environments=[self.environments["production"]], search_filter_query="is:unassigned"
  862. )
  863. assert set(results) == {self.group1}
  864. results = self.make_query(
  865. environments=[self.environments["staging"]], search_filter_query="is:assigned"
  866. )
  867. assert set(results) == {self.group2}
  868. results = self.make_query(
  869. environments=[self.environments["production"]], search_filter_query="is:assigned"
  870. )
  871. assert set(results) == set()
  872. def test_assigned_to(self):
  873. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  874. assert set(results) == {self.group2}
  875. # test team assignee
  876. ga = GroupAssignee.objects.get(
  877. user_id=self.user.id, group=self.group2, project=self.group2.project
  878. )
  879. ga.update(team=self.team, user_id=None)
  880. assert GroupAssignee.objects.get(id=ga.id).user_id is None
  881. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  882. assert set(results) == {self.group2}
  883. # test when there should be no results
  884. other_user = self.create_user()
  885. results = self.make_query(search_filter_query="assigned:%s" % other_user.username)
  886. assert set(results) == set()
  887. owner = self.create_user()
  888. self.create_member(
  889. organization=self.project.organization, user=owner, role="owner", teams=[]
  890. )
  891. # test that owners don't see results for all teams
  892. results = self.make_query(search_filter_query="assigned:%s" % owner.username)
  893. assert set(results) == set()
  894. def test_assigned_to_in_syntax(self):
  895. group_3 = self.store_event(
  896. data={
  897. "fingerprint": ["put-me-in-group3"],
  898. "event_id": "c" * 32,
  899. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  900. },
  901. project_id=self.project.id,
  902. ).group
  903. group_3.status = GroupStatus.MUTED
  904. group_3.substatus = None
  905. group_3.save()
  906. other_user = self.create_user()
  907. self.run_test_query_in_syntax(
  908. f"assigned:[{self.user.username}, {other_user.username}]",
  909. [self.group2],
  910. [self.group1, group_3],
  911. )
  912. GroupAssignee.objects.create(project=self.project, group=group_3, user_id=other_user.id)
  913. self.run_test_query_in_syntax(
  914. f"assigned:[{self.user.username}, {other_user.username}]",
  915. [self.group2, group_3],
  916. [self.group1],
  917. )
  918. self.run_test_query_in_syntax(
  919. f"assigned:[#{self.team.slug}, {other_user.username}]",
  920. [group_3],
  921. [self.group1, self.group2],
  922. )
  923. ga_2 = GroupAssignee.objects.get(
  924. user_id=self.user.id, group=self.group2, project=self.group2.project
  925. )
  926. ga_2.update(team=self.team, user_id=None)
  927. self.run_test_query_in_syntax(
  928. f"assigned:[{self.user.username}, {other_user.username}]",
  929. [self.group2, group_3],
  930. [self.group1],
  931. )
  932. self.run_test_query_in_syntax(
  933. f"assigned:[#{self.team.slug}, {other_user.username}]",
  934. [self.group2, group_3],
  935. [self.group1],
  936. )
  937. self.run_test_query_in_syntax(
  938. f"assigned:[me, none, {other_user.username}]",
  939. [self.group1, self.group2, group_3],
  940. [],
  941. )
  942. def test_assigned_or_suggested_in_syntax(self):
  943. Group.objects.all().delete()
  944. group = self.store_event(
  945. data={
  946. "timestamp": iso_format(before_now(seconds=180)),
  947. "fingerprint": ["group-1"],
  948. },
  949. project_id=self.project.id,
  950. ).group
  951. group1 = self.store_event(
  952. data={
  953. "timestamp": iso_format(before_now(seconds=185)),
  954. "fingerprint": ["group-2"],
  955. },
  956. project_id=self.project.id,
  957. ).group
  958. group2 = self.store_event(
  959. data={
  960. "timestamp": iso_format(before_now(seconds=190)),
  961. "fingerprint": ["group-3"],
  962. },
  963. project_id=self.project.id,
  964. ).group
  965. assigned_group = self.store_event(
  966. data={
  967. "timestamp": iso_format(before_now(seconds=195)),
  968. "fingerprint": ["group-4"],
  969. },
  970. project_id=self.project.id,
  971. ).group
  972. assigned_to_other_group = self.store_event(
  973. data={
  974. "timestamp": iso_format(before_now(seconds=195)),
  975. "fingerprint": ["group-5"],
  976. },
  977. project_id=self.project.id,
  978. ).group
  979. self.run_test_query_in_syntax(
  980. "assigned_or_suggested:[me]",
  981. [],
  982. [group, group1, group2, assigned_group, assigned_to_other_group],
  983. )
  984. GroupOwner.objects.create(
  985. group=assigned_to_other_group,
  986. project=self.project,
  987. organization=self.organization,
  988. type=0,
  989. team_id=None,
  990. user_id=self.user.id,
  991. )
  992. GroupOwner.objects.create(
  993. group=group,
  994. project=self.project,
  995. organization=self.organization,
  996. type=0,
  997. team_id=None,
  998. user_id=self.user.id,
  999. )
  1000. self.run_test_query_in_syntax(
  1001. "assigned_or_suggested:[me]",
  1002. [group, assigned_to_other_group],
  1003. [group1, group2, assigned_group],
  1004. )
  1005. # Because assigned_to_other_event is assigned to self.other_user, it should not show up in assigned_or_suggested search for anyone but self.other_user. (aka. they are now the only owner)
  1006. other_user = self.create_user("other@user.com", is_superuser=False)
  1007. GroupAssignee.objects.create(
  1008. group=assigned_to_other_group,
  1009. project=self.project,
  1010. user_id=other_user.id,
  1011. )
  1012. self.run_test_query_in_syntax(
  1013. "assigned_or_suggested:[me]",
  1014. [group],
  1015. [group1, group2, assigned_group, assigned_to_other_group],
  1016. )
  1017. self.run_test_query_in_syntax(
  1018. f"assigned_or_suggested:[{other_user.email}]",
  1019. [assigned_to_other_group],
  1020. [group, group1, group2, assigned_group],
  1021. )
  1022. GroupAssignee.objects.create(
  1023. group=assigned_group, project=self.project, user_id=self.user.id
  1024. )
  1025. self.run_test_query_in_syntax(
  1026. f"assigned_or_suggested:[{self.user.email}]",
  1027. [assigned_group, group],
  1028. )
  1029. GroupOwner.objects.create(
  1030. group=group,
  1031. project=self.project,
  1032. organization=self.organization,
  1033. type=0,
  1034. team_id=self.team.id,
  1035. user_id=None,
  1036. )
  1037. self.run_test_query_in_syntax(
  1038. f"assigned_or_suggested:[#{self.team.slug}]",
  1039. [group],
  1040. )
  1041. self.run_test_query_in_syntax(
  1042. "assigned_or_suggested:[me, none]",
  1043. [group, group1, group2, assigned_group],
  1044. [assigned_to_other_group],
  1045. )
  1046. not_me = self.create_user(email="notme@sentry.io")
  1047. GroupOwner.objects.create(
  1048. group=group2,
  1049. project=self.project,
  1050. organization=self.organization,
  1051. type=0,
  1052. team_id=None,
  1053. user_id=not_me.id,
  1054. )
  1055. self.run_test_query_in_syntax(
  1056. "assigned_or_suggested:[me, none]",
  1057. [group, group1, assigned_group],
  1058. [assigned_to_other_group, group2],
  1059. )
  1060. GroupOwner.objects.filter(group=group, user_id=self.user.id).delete()
  1061. self.run_test_query_in_syntax(
  1062. f"assigned_or_suggested:[me, none, #{self.team.slug}]",
  1063. [group, group1, assigned_group],
  1064. [assigned_to_other_group, group2],
  1065. )
  1066. self.run_test_query_in_syntax(
  1067. f"assigned_or_suggested:[me, none, #{self.team.slug}, {not_me.email}]",
  1068. [group, group1, assigned_group, group2],
  1069. [assigned_to_other_group],
  1070. )
  1071. def test_assigned_to_with_environment(self):
  1072. results = self.make_query(
  1073. environments=[self.environments["staging"]],
  1074. search_filter_query="assigned:%s" % self.user.username,
  1075. )
  1076. assert set(results) == {self.group2}
  1077. results = self.make_query(
  1078. environments=[self.environments["production"]],
  1079. search_filter_query="assigned:%s" % self.user.username,
  1080. )
  1081. assert set(results) == set()
  1082. def test_subscribed_by(self):
  1083. results = self.make_query(
  1084. [self.group1.project], search_filter_query="subscribed:%s" % self.user.username
  1085. )
  1086. assert set(results) == {self.group1}
  1087. def test_subscribed_by_in_syntax(self):
  1088. self.run_test_query_in_syntax(
  1089. f"subscribed:[{self.user.username}]", [self.group1], [self.group2]
  1090. )
  1091. user_2 = self.create_user()
  1092. GroupSubscription.objects.create(
  1093. user_id=user_2.id, group=self.group2, project=self.project, is_active=True
  1094. )
  1095. self.run_test_query_in_syntax(
  1096. f"subscribed:[{self.user.username}, {user_2.username}]", [self.group1, self.group2], []
  1097. )
  1098. def test_subscribed_by_with_environment(self):
  1099. results = self.make_query(
  1100. [self.group1.project],
  1101. environments=[self.environments["production"]],
  1102. search_filter_query="subscribed:%s" % self.user.username,
  1103. )
  1104. assert set(results) == {self.group1}
  1105. results = self.make_query(
  1106. [self.group1.project],
  1107. environments=[self.environments["staging"]],
  1108. search_filter_query="subscribed:%s" % self.user.username,
  1109. )
  1110. assert set(results) == set()
  1111. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1112. def test_snuba_not_called_optimization(self, query_mock):
  1113. assert self.make_query(search_filter_query="status:unresolved").results == [self.group1]
  1114. assert not query_mock.called
  1115. assert (
  1116. self.make_query(
  1117. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1118. sort_by="date",
  1119. ).results
  1120. == []
  1121. )
  1122. assert query_mock.called
  1123. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1124. def test_reduce_bulk_results_none_total(self, bulk_raw_query_mock):
  1125. bulk_raw_query_mock.return_value = [
  1126. {"data": [], "totals": {"total": None}},
  1127. {"data": [], "totals": {"total": None}},
  1128. ]
  1129. assert (
  1130. self.make_query(
  1131. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1132. sort_by="date",
  1133. ).results
  1134. == []
  1135. )
  1136. assert bulk_raw_query_mock.called
  1137. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1138. def test_reduce_bulk_results_none_data(self, bulk_raw_query_mock):
  1139. bulk_raw_query_mock.return_value = [
  1140. {"data": None, "totals": {"total": 0}},
  1141. {"data": None, "totals": {"total": 0}},
  1142. ]
  1143. assert (
  1144. self.make_query(
  1145. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1146. sort_by="date",
  1147. ).results
  1148. == []
  1149. )
  1150. assert bulk_raw_query_mock.called
  1151. def test_pre_and_post_filtering(self):
  1152. prev_max_pre = options.get("snuba.search.max-pre-snuba-candidates")
  1153. options.set("snuba.search.max-pre-snuba-candidates", 1)
  1154. try:
  1155. # normal queries work as expected
  1156. results = self.make_query(search_filter_query="foo")
  1157. assert set(results) == {self.group1}
  1158. results = self.make_query(search_filter_query="bar")
  1159. assert set(results) == {self.group2}
  1160. # no candidate matches in Sentry, immediately return empty paginator
  1161. results = self.make_query(search_filter_query="NO MATCHES IN SENTRY")
  1162. assert set(results) == set()
  1163. # too many candidates, skip pre-filter, requires >1 postfilter queries
  1164. results = self.make_query()
  1165. assert set(results) == {self.group1, self.group2}
  1166. finally:
  1167. options.set("snuba.search.max-pre-snuba-candidates", prev_max_pre)
  1168. def test_optimizer_enabled(self):
  1169. prev_optimizer_enabled = options.get("snuba.search.pre-snuba-candidates-optimizer")
  1170. options.set("snuba.search.pre-snuba-candidates-optimizer", True)
  1171. try:
  1172. results = self.make_query(
  1173. search_filter_query="server:example.com",
  1174. environments=[self.environments["production"]],
  1175. )
  1176. assert set(results) == {self.group1}
  1177. finally:
  1178. options.set("snuba.search.pre-snuba-candidates-optimizer", prev_optimizer_enabled)
  1179. def test_search_out_of_range(self):
  1180. the_date = datetime(2000, 1, 1, 0, 0, 0, tzinfo=pytz.utc)
  1181. results = self.make_query(
  1182. search_filter_query=f"event.timestamp:>{the_date} event.timestamp:<{the_date}",
  1183. date_from=the_date,
  1184. date_to=the_date,
  1185. )
  1186. assert set(results) == set()
  1187. def test_regressed_in_release(self):
  1188. # expect no groups within the results since there are no releases
  1189. results = self.make_query(search_filter_query="regressed_in_release:fake")
  1190. assert set(results) == set()
  1191. # expect no groups even though there is a release; since no group regressed in this release
  1192. release_1 = self.create_release()
  1193. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1194. assert set(results) == set()
  1195. # Create a new event so that we get a group in this release
  1196. group = self.store_event(
  1197. data={
  1198. "release": release_1.version,
  1199. },
  1200. project_id=self.project.id,
  1201. ).group
  1202. # # Should still be no group since we didn't regress in this release
  1203. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1204. assert set(results) == set()
  1205. record_group_history(group, GroupHistoryStatus.REGRESSED, release=release_1)
  1206. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1207. assert set(results) == {group}
  1208. # Make sure this works correctly with multiple releases
  1209. release_2 = self.create_release()
  1210. group_2 = self.store_event(
  1211. data={
  1212. "fingerprint": ["put-me-in-group9001"],
  1213. "event_id": "a" * 32,
  1214. "release": release_2.version,
  1215. },
  1216. project_id=self.project.id,
  1217. ).group
  1218. record_group_history(group_2, GroupHistoryStatus.REGRESSED, release=release_2)
  1219. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1220. assert set(results) == {group}
  1221. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_2.version)
  1222. assert set(results) == {group_2}
  1223. def test_first_release(self):
  1224. # expect no groups within the results since there are no releases
  1225. results = self.make_query(search_filter_query="first_release:%s" % "fake")
  1226. assert set(results) == set()
  1227. # expect no groups even though there is a release; since no group
  1228. # is attached to a release
  1229. release_1 = self.create_release(self.project)
  1230. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1231. assert set(results) == set()
  1232. # Create a new event so that we get a group in this release
  1233. group = self.store_event(
  1234. data={
  1235. "fingerprint": ["put-me-in-group9001"],
  1236. "event_id": "a" * 32,
  1237. "message": "hello",
  1238. "environment": "production",
  1239. "tags": {"server": "example.com"},
  1240. "release": release_1.version,
  1241. "stacktrace": {"frames": [{"module": "group1"}]},
  1242. },
  1243. project_id=self.project.id,
  1244. ).group
  1245. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1246. assert set(results) == {group}
  1247. def test_first_release_in_syntax(self):
  1248. # expect no groups within the results since there are no releases
  1249. self.run_test_query_in_syntax("first_release:[fake, fake2]", [])
  1250. # expect no groups even though there is a release; since no group
  1251. # is attached to a release
  1252. release_1 = self.create_release(self.project)
  1253. release_2 = self.create_release(self.project)
  1254. self.run_test_query_in_syntax(
  1255. f"first_release:[{release_1.version}, {release_2.version}]", []
  1256. )
  1257. # Create a new event so that we get a group in this release
  1258. group = self.store_event(
  1259. data={
  1260. "fingerprint": ["put-me-in-group9001"],
  1261. "event_id": "a" * 32,
  1262. "message": "hello",
  1263. "environment": "production",
  1264. "tags": {"server": "example.com"},
  1265. "release": release_1.version,
  1266. "stacktrace": {"frames": [{"module": "group1"}]},
  1267. },
  1268. project_id=self.project.id,
  1269. ).group
  1270. self.run_test_query_in_syntax(
  1271. f"first_release:[{release_1.version}, {release_2.version}]",
  1272. [group],
  1273. [self.group1, self.group2],
  1274. )
  1275. # Create a new event so that we get a group in this release
  1276. group_2 = self.store_event(
  1277. data={
  1278. "fingerprint": ["put-me-in-group9002"],
  1279. "event_id": "a" * 32,
  1280. "message": "hello",
  1281. "environment": "production",
  1282. "tags": {"server": "example.com"},
  1283. "release": release_2.version,
  1284. "stacktrace": {"frames": [{"module": "group1"}]},
  1285. },
  1286. project_id=self.project.id,
  1287. ).group
  1288. self.run_test_query_in_syntax(
  1289. f"first_release:[{release_1.version}, {release_2.version}]",
  1290. [group, group_2],
  1291. )
  1292. def test_first_release_environments(self):
  1293. results = self.make_query(
  1294. environments=[self.environments["production"]],
  1295. search_filter_query="first_release:fake",
  1296. )
  1297. assert set(results) == set()
  1298. release = self.create_release(self.project)
  1299. group_env = GroupEnvironment.get_or_create(
  1300. group_id=self.group1.id, environment_id=self.environments["production"].id
  1301. )[0]
  1302. results = self.make_query(
  1303. environments=[self.environments["production"]],
  1304. search_filter_query=f"first_release:{release.version}",
  1305. )
  1306. assert set(results) == set()
  1307. group_env.first_release = release
  1308. group_env.save()
  1309. results = self.make_query(
  1310. environments=[self.environments["production"]],
  1311. search_filter_query=f"first_release:{release.version}",
  1312. )
  1313. assert set(results) == {self.group1}
  1314. def test_first_release_environments_in_syntax(self):
  1315. self.run_test_query_in_syntax(
  1316. "first_release:[fake, fake2]",
  1317. [],
  1318. [self.group1, self.group2],
  1319. environments=[self.environments["production"]],
  1320. )
  1321. release = self.create_release(self.project)
  1322. group_1_env = GroupEnvironment.objects.get(
  1323. group_id=self.group1.id, environment_id=self.environments["production"].id
  1324. )
  1325. group_1_env.update(first_release=release)
  1326. self.run_test_query_in_syntax(
  1327. f"first_release:[{release.version}, fake2]",
  1328. [self.group1],
  1329. [self.group2],
  1330. environments=[self.environments["production"]],
  1331. )
  1332. group_2_env = GroupEnvironment.objects.get(
  1333. group_id=self.group2.id, environment_id=self.environments["staging"].id
  1334. )
  1335. group_2_env.update(first_release=release)
  1336. self.run_test_query_in_syntax(
  1337. f"first_release:[{release.version}, fake2]",
  1338. [self.group1, self.group2],
  1339. [],
  1340. environments=[self.environments["production"], self.environments["staging"]],
  1341. )
  1342. # Make sure we don't get duplicate groups
  1343. GroupEnvironment.objects.create(
  1344. group_id=self.group1.id,
  1345. environment_id=self.environments["staging"].id,
  1346. first_release=release,
  1347. )
  1348. self.run_test_query_in_syntax(
  1349. f"first_release:[{release.version}, fake2]",
  1350. [self.group1, self.group2],
  1351. [],
  1352. environments=[self.environments["production"], self.environments["staging"]],
  1353. )
  1354. def test_query_enclosed_in_quotes(self):
  1355. results = self.make_query(search_filter_query='"foo"')
  1356. assert set(results) == {self.group1}
  1357. results = self.make_query(search_filter_query='"bar"')
  1358. assert set(results) == {self.group2}
  1359. @xfail_if_not_postgres("Wildcard searching only supported in Postgres")
  1360. def test_wildcard(self):
  1361. escaped_event = self.store_event(
  1362. data={
  1363. "fingerprint": ["hello-there"],
  1364. "event_id": "f" * 32,
  1365. "message": "somet[hing]",
  1366. "environment": "production",
  1367. "tags": {"server": "example.net"},
  1368. "timestamp": iso_format(self.base_datetime),
  1369. "stacktrace": {"frames": [{"module": "group1"}]},
  1370. },
  1371. project_id=self.project.id,
  1372. )
  1373. # Note: Adding in `environment:production` so that we make sure we query
  1374. # in both snuba and postgres
  1375. results = self.make_query(search_filter_query="environment:production so*t")
  1376. assert set(results) == {escaped_event.group}
  1377. # Make sure it's case insensitive
  1378. results = self.make_query(search_filter_query="environment:production SO*t")
  1379. assert set(results) == {escaped_event.group}
  1380. results = self.make_query(search_filter_query="environment:production so*zz")
  1381. assert set(results) == set()
  1382. results = self.make_query(search_filter_query="environment:production [hing]")
  1383. assert set(results) == {escaped_event.group}
  1384. results = self.make_query(search_filter_query="environment:production s*]")
  1385. assert set(results) == {escaped_event.group}
  1386. results = self.make_query(search_filter_query="environment:production server:example.*")
  1387. assert set(results) == {self.group1, escaped_event.group}
  1388. results = self.make_query(search_filter_query="environment:production !server:*net")
  1389. assert set(results) == {self.group1}
  1390. # TODO: Disabling tests that use [] syntax for the moment. Re-enable
  1391. # these if we decide to add back in, or remove if this comment has been
  1392. # here a while.
  1393. # results = self.make_query(
  1394. # search_filter_query='environment:production [s][of][mz]',
  1395. # )
  1396. # assert set(results) == set([escaped_event.group])
  1397. # results = self.make_query(
  1398. # search_filter_query='environment:production [z][of][mz]',
  1399. # )
  1400. # assert set(results) == set()
  1401. def test_null_tags(self):
  1402. tag_event = self.store_event(
  1403. data={
  1404. "fingerprint": ["hello-there"],
  1405. "event_id": "f" * 32,
  1406. "message": "something",
  1407. "environment": "production",
  1408. "tags": {"server": "example.net"},
  1409. "timestamp": iso_format(self.base_datetime),
  1410. "stacktrace": {"frames": [{"module": "group1"}]},
  1411. },
  1412. project_id=self.project.id,
  1413. )
  1414. no_tag_event = self.store_event(
  1415. data={
  1416. "fingerprint": ["hello-there-2"],
  1417. "event_id": "5" * 32,
  1418. "message": "something",
  1419. "environment": "production",
  1420. "timestamp": iso_format(self.base_datetime),
  1421. "stacktrace": {"frames": [{"module": "group2"}]},
  1422. },
  1423. project_id=self.project.id,
  1424. )
  1425. results = self.make_query(search_filter_query="environment:production !server:*net")
  1426. assert set(results) == {self.group1, no_tag_event.group}
  1427. results = self.make_query(search_filter_query="environment:production server:*net")
  1428. assert set(results) == {tag_event.group}
  1429. results = self.make_query(search_filter_query="environment:production !server:example.net")
  1430. assert set(results) == {self.group1, no_tag_event.group}
  1431. results = self.make_query(search_filter_query="environment:production server:example.net")
  1432. assert set(results) == {tag_event.group}
  1433. results = self.make_query(search_filter_query="environment:production has:server")
  1434. assert set(results) == {self.group1, tag_event.group}
  1435. results = self.make_query(search_filter_query="environment:production !has:server")
  1436. assert set(results) == {no_tag_event.group}
  1437. def test_null_promoted_tags(self):
  1438. tag_event = self.store_event(
  1439. data={
  1440. "fingerprint": ["hello-there"],
  1441. "event_id": "f" * 32,
  1442. "message": "something",
  1443. "environment": "production",
  1444. "tags": {"logger": "csp"},
  1445. "timestamp": iso_format(self.base_datetime),
  1446. "stacktrace": {"frames": [{"module": "group1"}]},
  1447. },
  1448. project_id=self.project.id,
  1449. )
  1450. no_tag_event = self.store_event(
  1451. data={
  1452. "fingerprint": ["hello-there-2"],
  1453. "event_id": "5" * 32,
  1454. "message": "something",
  1455. "environment": "production",
  1456. "timestamp": iso_format(self.base_datetime),
  1457. "stacktrace": {"frames": [{"module": "group2"}]},
  1458. },
  1459. project_id=self.project.id,
  1460. )
  1461. results = self.make_query(search_filter_query="environment:production !logger:*sp")
  1462. assert set(results) == {self.group1, no_tag_event.group}
  1463. results = self.make_query(search_filter_query="environment:production logger:*sp")
  1464. assert set(results) == {tag_event.group}
  1465. results = self.make_query(search_filter_query="environment:production !logger:csp")
  1466. assert set(results) == {self.group1, no_tag_event.group}
  1467. results = self.make_query(search_filter_query="environment:production logger:csp")
  1468. assert set(results) == {tag_event.group}
  1469. results = self.make_query(search_filter_query="environment:production has:logger")
  1470. assert set(results) == {tag_event.group}
  1471. results = self.make_query(search_filter_query="environment:production !has:logger")
  1472. assert set(results) == {self.group1, no_tag_event.group}
  1473. def test_sort_multi_project(self):
  1474. self.set_up_multi_project()
  1475. results = self.make_query([self.project, self.project2], sort_by="date")
  1476. assert list(results) == [self.group1, self.group_p2, self.group2]
  1477. results = self.make_query([self.project, self.project2], sort_by="new")
  1478. assert list(results) == [self.group2, self.group_p2, self.group1]
  1479. results = self.make_query([self.project, self.project2], sort_by="freq")
  1480. assert list(results) == [self.group1, self.group_p2, self.group2]
  1481. results = self.make_query([self.project, self.project2], sort_by="priority")
  1482. assert list(results) == [self.group1, self.group2, self.group_p2]
  1483. results = self.make_query([self.project, self.project2], sort_by="user")
  1484. assert list(results) == [self.group1, self.group2, self.group_p2]
  1485. def test_sort_trend(self):
  1486. start = self.group1.first_seen - timedelta(days=1)
  1487. end = before_now(days=1).replace(tzinfo=pytz.utc)
  1488. middle = start + ((end - start) / 2)
  1489. self.store_event(
  1490. data={
  1491. "fingerprint": ["put-me-in-group1"],
  1492. "event_id": "2" * 32,
  1493. "message": "something",
  1494. "timestamp": iso_format(self.base_datetime),
  1495. },
  1496. project_id=self.project.id,
  1497. )
  1498. self.store_event(
  1499. data={
  1500. "fingerprint": ["put-me-in-group1"],
  1501. "event_id": "3" * 32,
  1502. "message": "something",
  1503. "timestamp": iso_format(self.base_datetime),
  1504. },
  1505. project_id=self.project.id,
  1506. )
  1507. fewer_events_group = self.store_event(
  1508. data={
  1509. "fingerprint": ["put-me-in-group4"],
  1510. "event_id": "4" * 32,
  1511. "message": "something",
  1512. "timestamp": iso_format(middle - timedelta(days=1)),
  1513. },
  1514. project_id=self.project.id,
  1515. ).group
  1516. self.store_event(
  1517. data={
  1518. "fingerprint": ["put-me-in-group4"],
  1519. "event_id": "5" * 32,
  1520. "message": "something",
  1521. "timestamp": iso_format(middle - timedelta(days=1)),
  1522. },
  1523. project_id=self.project.id,
  1524. )
  1525. self.store_event(
  1526. data={
  1527. "fingerprint": ["put-me-in-group4"],
  1528. "event_id": "6" * 32,
  1529. "message": "something",
  1530. "timestamp": iso_format(self.base_datetime),
  1531. },
  1532. project_id=self.project.id,
  1533. )
  1534. no_before_group = self.store_event(
  1535. data={
  1536. "fingerprint": ["put-me-in-group5"],
  1537. "event_id": "3" * 32,
  1538. "message": "something",
  1539. "timestamp": iso_format(self.base_datetime),
  1540. },
  1541. project_id=self.project.id,
  1542. ).group
  1543. no_after_group = self.store_event(
  1544. data={
  1545. "fingerprint": ["put-me-in-group6"],
  1546. "event_id": "4" * 32,
  1547. "message": "something",
  1548. "timestamp": iso_format(middle - timedelta(days=1)),
  1549. },
  1550. project_id=self.project.id,
  1551. ).group
  1552. self.set_up_multi_project()
  1553. results = self.make_query([self.project], sort_by="trend", date_from=start, date_to=end)
  1554. assert results[:2] == [self.group1, fewer_events_group]
  1555. # These will be arbitrarily ordered since their trend values are all 0
  1556. assert set(results[2:]) == {self.group2, no_before_group, no_after_group}
  1557. def test_in_syntax_is_invalid(self):
  1558. with pytest.raises(InvalidSearchQuery, match='"in" syntax invalid for "is" search'):
  1559. self.make_query(search_filter_query="is:[unresolved, resolved]")
  1560. def test_first_release_any_or_no_environments(self):
  1561. # test scenarios for tickets:
  1562. # SEN-571
  1563. # ISSUE-432
  1564. # given the following setup:
  1565. #
  1566. # groups table:
  1567. # group first_release
  1568. # A 1
  1569. # B 1
  1570. # C 2
  1571. #
  1572. # groupenvironments table:
  1573. # group environment first_release
  1574. # A staging 1
  1575. # A production 2
  1576. #
  1577. # when querying by first release, the appropriate set of groups should be displayed:
  1578. #
  1579. # first_release: 1
  1580. # env=[]: A, B
  1581. # env=[production, staging]: A
  1582. # env=[staging]: A
  1583. # env=[production]: nothing
  1584. #
  1585. # first_release: 2
  1586. # env=[]: A, C
  1587. # env=[production, staging]: A
  1588. # env=[staging]: nothing
  1589. # env=[production]: A
  1590. # create an issue/group whose events that occur in 2 distinct environments
  1591. group_a_event_1 = self.store_event(
  1592. data={
  1593. "fingerprint": ["group_a"],
  1594. "event_id": "aaa" + ("1" * 29),
  1595. "environment": "example_staging",
  1596. "release": "release_1",
  1597. },
  1598. project_id=self.project.id,
  1599. )
  1600. group_a_event_2 = self.store_event(
  1601. data={
  1602. "fingerprint": ["group_a"],
  1603. "event_id": "aaa" + ("2" * 29),
  1604. "environment": "example_production",
  1605. "release": "release_2",
  1606. },
  1607. project_id=self.project.id,
  1608. )
  1609. group_a = group_a_event_1.group
  1610. # get the environments for group_a
  1611. prod_env = group_a_event_2.get_environment()
  1612. staging_env = group_a_event_1.get_environment()
  1613. # create an issue/group whose event that occur in no environments
  1614. # but will be tied to release release_1
  1615. group_b_event_1 = self.store_event(
  1616. data={
  1617. "fingerprint": ["group_b"],
  1618. "event_id": "bbb" + ("1" * 29),
  1619. "release": "release_1",
  1620. },
  1621. project_id=self.project.id,
  1622. )
  1623. assert group_b_event_1.get_environment().name == "" # has no environment
  1624. group_b = group_b_event_1.group
  1625. # create an issue/group whose event that occur in no environments
  1626. # but will be tied to release release_2
  1627. group_c_event_1 = self.store_event(
  1628. data={
  1629. "fingerprint": ["group_c"],
  1630. "event_id": "ccc" + ("1" * 29),
  1631. "release": "release_2",
  1632. },
  1633. project_id=self.project.id,
  1634. )
  1635. assert group_c_event_1.get_environment().name == "" # has no environment
  1636. group_c = group_c_event_1.group
  1637. # query by release release_1
  1638. results = self.make_query(search_filter_query="first_release:%s" % "release_1")
  1639. assert set(results) == {group_a, group_b}
  1640. results = self.make_query(
  1641. environments=[staging_env, prod_env],
  1642. search_filter_query="first_release:%s" % "release_1",
  1643. )
  1644. assert set(results) == {group_a}
  1645. results = self.make_query(
  1646. environments=[staging_env], search_filter_query="first_release:%s" % "release_1"
  1647. )
  1648. assert set(results) == {group_a}
  1649. results = self.make_query(
  1650. environments=[prod_env], search_filter_query="first_release:%s" % "release_1"
  1651. )
  1652. assert set(results) == set()
  1653. # query by release release_2
  1654. results = self.make_query(search_filter_query="first_release:%s" % "release_2")
  1655. assert set(results) == {group_a, group_c}
  1656. results = self.make_query(
  1657. environments=[staging_env, prod_env],
  1658. search_filter_query="first_release:%s" % "release_2",
  1659. )
  1660. assert set(results) == {group_a}
  1661. results = self.make_query(
  1662. environments=[staging_env], search_filter_query="first_release:%s" % "release_2"
  1663. )
  1664. assert set(results) == set()
  1665. results = self.make_query(
  1666. environments=[prod_env], search_filter_query="first_release:%s" % "release_2"
  1667. )
  1668. assert set(results) == {group_a}
  1669. def test_all_fields_do_not_error(self):
  1670. # Just a sanity check to make sure that all fields can be successfully
  1671. # searched on without returning type errors and other schema related
  1672. # issues.
  1673. def test_query(query):
  1674. try:
  1675. self.make_query(search_filter_query=query)
  1676. except SnubaError as e:
  1677. self.fail(f"Query {query} errored. Error info: {e}")
  1678. for key in SENTRY_SNUBA_MAP:
  1679. if key in ["project.id", "issue.id", "performance.issue_ids"]:
  1680. continue
  1681. test_query("has:%s" % key)
  1682. test_query("!has:%s" % key)
  1683. if key == "error.handled":
  1684. val = 1
  1685. elif key in issue_search_config.numeric_keys:
  1686. val = "123"
  1687. elif key in issue_search_config.date_keys:
  1688. val = self.base_datetime.isoformat()
  1689. elif key in issue_search_config.boolean_keys:
  1690. val = "true"
  1691. elif key in {"trace.span", "trace.parent_span"}:
  1692. val = "abcdef1234abcdef"
  1693. test_query(f"!{key}:{val}")
  1694. else:
  1695. val = "abadcafedeadbeefdeaffeedabadfeed"
  1696. test_query(f"!{key}:{val}")
  1697. test_query(f"{key}:{val}")
  1698. def test_message_negation(self):
  1699. self.store_event(
  1700. data={
  1701. "fingerprint": ["put-me-in-group1"],
  1702. "event_id": "2" * 32,
  1703. "message": "something",
  1704. "timestamp": iso_format(self.base_datetime),
  1705. },
  1706. project_id=self.project.id,
  1707. )
  1708. results = self.make_query(search_filter_query="!message:else")
  1709. results2 = self.make_query(search_filter_query="!message:else")
  1710. assert list(results) == list(results2)
  1711. def test_error_main_thread_true(self):
  1712. myProject = self.create_project(
  1713. name="Foo", slug="foo", teams=[self.team], fire_project_created=True
  1714. )
  1715. event = self.store_event(
  1716. data={
  1717. "event_id": "1" * 32,
  1718. "message": "something",
  1719. "timestamp": iso_format(self.base_datetime),
  1720. "exception": {
  1721. "values": [
  1722. {
  1723. "type": "SyntaxError",
  1724. "value": "hello world",
  1725. "thread_id": 1,
  1726. },
  1727. ],
  1728. },
  1729. "threads": {
  1730. "values": [
  1731. {
  1732. "id": 1,
  1733. "main": True,
  1734. },
  1735. ],
  1736. },
  1737. },
  1738. project_id=myProject.id,
  1739. )
  1740. myGroup = event.groups[0]
  1741. results = self.make_query(
  1742. projects=[myProject],
  1743. search_filter_query="error.main_thread:1",
  1744. sort_by="date",
  1745. )
  1746. assert list(results) == [myGroup]
  1747. def test_error_main_thread_false(self):
  1748. myProject = self.create_project(
  1749. name="Foo2", slug="foo2", teams=[self.team], fire_project_created=True
  1750. )
  1751. event = self.store_event(
  1752. data={
  1753. "event_id": "2" * 32,
  1754. "message": "something",
  1755. "timestamp": iso_format(self.base_datetime),
  1756. "exception": {
  1757. "values": [
  1758. {
  1759. "type": "SyntaxError",
  1760. "value": "hello world",
  1761. "thread_id": 1,
  1762. },
  1763. ],
  1764. },
  1765. "threads": {
  1766. "values": [
  1767. {
  1768. "id": 1,
  1769. "main": False,
  1770. },
  1771. ],
  1772. },
  1773. },
  1774. project_id=myProject.id,
  1775. )
  1776. myGroup = event.groups[0]
  1777. results = self.make_query(
  1778. projects=[myProject],
  1779. search_filter_query="error.main_thread:0",
  1780. sort_by="date",
  1781. )
  1782. assert list(results) == [myGroup]
  1783. def test_error_main_thread_no_results(self):
  1784. myProject = self.create_project(
  1785. name="Foo3", slug="foo3", teams=[self.team], fire_project_created=True
  1786. )
  1787. self.store_event(
  1788. data={
  1789. "event_id": "3" * 32,
  1790. "message": "something",
  1791. "timestamp": iso_format(self.base_datetime),
  1792. "exception": {
  1793. "values": [
  1794. {
  1795. "type": "SyntaxError",
  1796. "value": "hello world",
  1797. "thread_id": 1,
  1798. },
  1799. ],
  1800. },
  1801. "threads": {
  1802. "values": [
  1803. {
  1804. "id": 1,
  1805. },
  1806. ],
  1807. },
  1808. },
  1809. project_id=myProject.id,
  1810. )
  1811. results = self.make_query(
  1812. projects=[myProject],
  1813. search_filter_query="error.main_thread:1",
  1814. sort_by="date",
  1815. )
  1816. assert len(results) == 0
  1817. class EventsTransactionsSnubaSearchTest(SharedSnubaTest):
  1818. @property
  1819. def backend(self):
  1820. return EventsDatasetSnubaSearchBackend()
  1821. def setUp(self):
  1822. super().setUp()
  1823. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  1824. transaction_event_data = {
  1825. "level": "info",
  1826. "message": "ayoo",
  1827. "type": "transaction",
  1828. "culprit": "app/components/events/eventEntries in map",
  1829. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  1830. }
  1831. with self.options({"performance.issues.send_to_issues_platform": True}), self.feature(
  1832. "organizations:issue-platform"
  1833. ):
  1834. transaction_event_1 = self.store_event(
  1835. data={
  1836. **transaction_event_data,
  1837. "event_id": "a" * 32,
  1838. "timestamp": iso_format(before_now(minutes=1)),
  1839. "start_timestamp": iso_format(before_now(minutes=1, seconds=5)),
  1840. "tags": {"my_tag": 1},
  1841. "fingerprint": [
  1842. f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group1"
  1843. ],
  1844. },
  1845. project_id=self.project.id,
  1846. )
  1847. self.perf_group_1 = transaction_event_1.groups[0]
  1848. transaction_event_2 = self.store_event(
  1849. data={
  1850. **transaction_event_data,
  1851. "event_id": "a" * 32,
  1852. "timestamp": iso_format(before_now(minutes=2)),
  1853. "start_timestamp": iso_format(before_now(minutes=2, seconds=5)),
  1854. "tags": {"my_tag": 1},
  1855. "fingerprint": [
  1856. f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group2"
  1857. ],
  1858. },
  1859. project_id=self.project.id,
  1860. )
  1861. self.perf_group_2 = transaction_event_2.groups[0]
  1862. error_event_data = {
  1863. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1864. "message": "bar",
  1865. "environment": "staging",
  1866. "tags": {
  1867. "server": "example.com",
  1868. "url": "http://example.com",
  1869. "sentry:user": "event2@example.com",
  1870. "my_tag": 1,
  1871. },
  1872. }
  1873. error_event = self.store_event(
  1874. data={
  1875. **error_event_data,
  1876. "fingerprint": ["put-me-in-error_group_1"],
  1877. "event_id": "c" * 32,
  1878. "stacktrace": {"frames": [{"module": "error_group_1"}]},
  1879. },
  1880. project_id=self.project.id,
  1881. )
  1882. self.error_group_1 = error_event.group
  1883. error_event_2 = self.store_event(
  1884. data={
  1885. **error_event_data,
  1886. "fingerprint": ["put-me-in-error_group_2"],
  1887. "event_id": "d" * 32,
  1888. "stacktrace": {"frames": [{"module": "error_group_2"}]},
  1889. },
  1890. project_id=self.project.id,
  1891. )
  1892. self.error_group_2 = error_event_2.group
  1893. def test_performance_query(self):
  1894. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  1895. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1896. results = self.make_query(
  1897. search_filter_query="issue.type:[performance_n_plus_one_db_queries, performance_render_blocking_asset_span] my_tag:1"
  1898. )
  1899. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1900. def test_performance_query_no_duplicates(self):
  1901. # Regression test to catch an issue we had with performance issues showing duplicated in the
  1902. # issue stream. This was caused by us dual writing perf issues to transactions and to the
  1903. # issue platform. We'd end up reading the same issue twice and duplicate it in the response.
  1904. with self.feature("organizations:issue-platform"), self.options(
  1905. {"performance.issues.send_to_issues_platform": True}
  1906. ):
  1907. results = self.make_query(search_filter_query="!issue.category:error my_tag:1")
  1908. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1909. def test_performance_issue_search_feature_off(self):
  1910. with Feature({"organizations:performance-issues-search": False}):
  1911. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  1912. assert list(results) == []
  1913. with Feature({"organizations:performance-issues-search": True}):
  1914. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  1915. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1916. def test_error_performance_query(self):
  1917. results = self.make_query(search_filter_query="my_tag:1")
  1918. assert list(results) == [
  1919. self.perf_group_1,
  1920. self.perf_group_2,
  1921. self.error_group_2,
  1922. self.error_group_1,
  1923. ]
  1924. results = self.make_query(
  1925. search_filter_query="issue.category:[performance, error] my_tag:1"
  1926. )
  1927. assert list(results) == [
  1928. self.perf_group_1,
  1929. self.perf_group_2,
  1930. self.error_group_2,
  1931. self.error_group_1,
  1932. ]
  1933. results = self.make_query(
  1934. search_filter_query="issue.type:[performance_render_blocking_asset_span, error] my_tag:1"
  1935. )
  1936. assert list(results) == [
  1937. self.perf_group_1,
  1938. self.perf_group_2,
  1939. self.error_group_2,
  1940. self.error_group_1,
  1941. ]
  1942. def test_cursor_performance_issues(self):
  1943. results = self.make_query(
  1944. projects=[self.project],
  1945. search_filter_query="issue.category:performance my_tag:1",
  1946. sort_by="date",
  1947. limit=1,
  1948. count_hits=True,
  1949. )
  1950. assert list(results) == [self.perf_group_1]
  1951. assert results.hits == 2
  1952. results = self.make_query(
  1953. projects=[self.project],
  1954. search_filter_query="issue.category:performance my_tag:1",
  1955. sort_by="date",
  1956. limit=1,
  1957. cursor=results.next,
  1958. count_hits=True,
  1959. )
  1960. assert list(results) == [self.perf_group_2]
  1961. assert results.hits == 2
  1962. results = self.make_query(
  1963. projects=[self.project],
  1964. search_filter_query="issue.category:performance my_tag:1",
  1965. sort_by="date",
  1966. limit=1,
  1967. cursor=results.next,
  1968. count_hits=True,
  1969. )
  1970. assert list(results) == []
  1971. assert results.hits == 2
  1972. def test_perf_issue_search_message_term_queries_postgres(self):
  1973. from django.db.models import Q
  1974. from sentry.utils import snuba
  1975. transaction_name = "im a little tea pot"
  1976. tx = self.store_event(
  1977. data={
  1978. "level": "info",
  1979. "culprit": "app/components/events/eventEntries in map",
  1980. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  1981. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  1982. "event_id": "e" * 32,
  1983. "timestamp": iso_format(self.base_datetime),
  1984. "start_timestamp": iso_format(self.base_datetime),
  1985. "type": "transaction",
  1986. "transaction": transaction_name,
  1987. },
  1988. project_id=self.project.id,
  1989. )
  1990. assert "tea" in tx.search_message
  1991. created_group = tx.groups[0]
  1992. find_group = Group.objects.filter(
  1993. Q(type=PerformanceRenderBlockingAssetSpanGroupType.type_id, message__icontains="tea")
  1994. ).first()
  1995. assert created_group == find_group
  1996. result = snuba.raw_query(
  1997. dataset=snuba.Dataset.Transactions,
  1998. start=self.base_datetime - timedelta(hours=1),
  1999. end=self.base_datetime + timedelta(hours=1),
  2000. selected_columns=[
  2001. "event_id",
  2002. "group_ids",
  2003. "transaction_name",
  2004. ],
  2005. groupby=None,
  2006. filter_keys={"project_id": [self.project.id], "event_id": [tx.event_id]},
  2007. referrer="_insert_transaction.verify_transaction",
  2008. )
  2009. assert result["data"][0]["transaction_name"] == transaction_name
  2010. assert result["data"][0]["group_ids"] == [created_group.id]
  2011. results = self.make_query(search_filter_query="issue.category:performance tea")
  2012. assert set(results) == {created_group}
  2013. results2 = self.make_query(search_filter_query="tea")
  2014. assert set(results2) == {created_group}
  2015. def test_search_message_error_and_perf_issues(self):
  2016. tx = self.store_event(
  2017. data={
  2018. "level": "info",
  2019. "culprit": "app/components/events/eventEntries in map",
  2020. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  2021. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  2022. "event_id": "e" * 32,
  2023. "timestamp": iso_format(self.base_datetime),
  2024. "start_timestamp": iso_format(self.base_datetime),
  2025. "type": "transaction",
  2026. "transaction": "/api/0/events",
  2027. },
  2028. project_id=self.project.id,
  2029. )
  2030. perf_issue = tx.groups[0]
  2031. assert perf_issue
  2032. error = self.store_event(
  2033. data={
  2034. "fingerprint": ["another-random-group"],
  2035. "event_id": "d" * 32,
  2036. "message": "Uncaught exception on api /api/0/events",
  2037. "environment": "production",
  2038. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  2039. "timestamp": iso_format(self.base_datetime),
  2040. "stacktrace": {"frames": [{"module": "group1"}]},
  2041. },
  2042. project_id=self.project.id,
  2043. )
  2044. error_issue = error.group
  2045. assert error_issue
  2046. assert error_issue != perf_issue
  2047. assert set(self.make_query(search_filter_query="is:unresolved /api/0/events")) == {
  2048. perf_issue,
  2049. error_issue,
  2050. }
  2051. assert set(self.make_query(search_filter_query="/api/0/events")) == {
  2052. error_issue,
  2053. perf_issue,
  2054. }
  2055. def test_compound_message_negation(self):
  2056. self.store_event(
  2057. data={
  2058. "fingerprint": ["put-me-in-group1"],
  2059. "event_id": "2" * 32,
  2060. "message": "something",
  2061. "timestamp": iso_format(self.base_datetime),
  2062. },
  2063. project_id=self.project.id,
  2064. )
  2065. self.store_event(
  2066. data={
  2067. "level": "info",
  2068. "culprit": "app/components/events/eventEntries in map",
  2069. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  2070. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  2071. "event_id": "e" * 32,
  2072. "timestamp": iso_format(self.base_datetime),
  2073. "start_timestamp": iso_format(self.base_datetime),
  2074. "type": "transaction",
  2075. "transaction": "something",
  2076. },
  2077. project_id=self.project.id,
  2078. )
  2079. error_issues_only = self.make_query(
  2080. search_filter_query="!message:else group.category:error"
  2081. )
  2082. error_and_perf_issues = self.make_query(search_filter_query="!message:else")
  2083. assert set(error_and_perf_issues) > set(error_issues_only)
  2084. class EventsGenericSnubaSearchTest(SharedSnubaTest, OccurrenceTestMixin):
  2085. @property
  2086. def backend(self):
  2087. return EventsDatasetSnubaSearchBackend()
  2088. def setUp(self):
  2089. super().setUp()
  2090. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2091. event_id_1 = uuid.uuid4().hex
  2092. _, group_info = process_event_and_issue_occurrence(
  2093. self.build_occurrence_data(event_id=event_id_1),
  2094. {
  2095. "event_id": event_id_1,
  2096. "project_id": self.project.id,
  2097. "title": "some problem",
  2098. "platform": "python",
  2099. "tags": {"my_tag": "1"},
  2100. "timestamp": before_now(minutes=1).isoformat(),
  2101. "received": before_now(minutes=1).isoformat(),
  2102. },
  2103. )
  2104. self.profile_group_1 = group_info.group
  2105. event_id_2 = uuid.uuid4().hex
  2106. _, group_info = process_event_and_issue_occurrence(
  2107. self.build_occurrence_data(event_id=event_id_2, fingerprint=["put-me-in-group-2"]),
  2108. {
  2109. "event_id": event_id_2,
  2110. "project_id": self.project.id,
  2111. "title": "some other problem",
  2112. "platform": "python",
  2113. "tags": {"my_tag": "1"},
  2114. "timestamp": before_now(minutes=2).isoformat(),
  2115. "received": before_now(minutes=2).isoformat(),
  2116. },
  2117. )
  2118. self.profile_group_2 = group_info.group
  2119. event_id_3 = uuid.uuid4().hex
  2120. process_event_and_issue_occurrence(
  2121. self.build_occurrence_data(event_id=event_id_3, fingerprint=["put-me-in-group-3"]),
  2122. {
  2123. "event_id": event_id_3,
  2124. "project_id": self.project.id,
  2125. "title": "some other problem",
  2126. "platform": "python",
  2127. "tags": {"my_tag": "2"},
  2128. "timestamp": before_now(minutes=2).isoformat(),
  2129. "message_timestamp": before_now(minutes=2).isoformat(),
  2130. },
  2131. )
  2132. error_event_data = {
  2133. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  2134. "message": "bar",
  2135. "environment": "staging",
  2136. "tags": {
  2137. "server": "example.com",
  2138. "url": "http://example.com",
  2139. "sentry:user": "event2@example.com",
  2140. "my_tag": 1,
  2141. },
  2142. }
  2143. error_event = self.store_event(
  2144. data={
  2145. **error_event_data,
  2146. "fingerprint": ["put-me-in-error_group_1"],
  2147. "event_id": "c" * 32,
  2148. "stacktrace": {"frames": [{"module": "error_group_1"}]},
  2149. },
  2150. project_id=self.project.id,
  2151. )
  2152. self.error_group_1 = error_event.group
  2153. error_event_2 = self.store_event(
  2154. data={
  2155. **error_event_data,
  2156. "fingerprint": ["put-me-in-error_group_2"],
  2157. "event_id": "d" * 32,
  2158. "stacktrace": {"frames": [{"module": "error_group_2"}]},
  2159. },
  2160. project_id=self.project.id,
  2161. )
  2162. self.error_group_2 = error_event_2.group
  2163. def test_no_feature(self):
  2164. results = self.make_query(search_filter_query="issue.category:profile my_tag:1")
  2165. assert list(results) == []
  2166. def test_generic_query(self):
  2167. with self.feature(
  2168. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  2169. ):
  2170. results = self.make_query(search_filter_query="issue.category:profile my_tag:1")
  2171. assert list(results) == [self.profile_group_1, self.profile_group_2]
  2172. results = self.make_query(
  2173. search_filter_query="issue.type:profile_file_io_main_thread my_tag:1"
  2174. )
  2175. assert list(results) == [self.profile_group_1, self.profile_group_2]
  2176. def test_generic_query_perf(self):
  2177. event_id = uuid.uuid4().hex
  2178. group_type = PerformanceNPlusOneGroupType
  2179. self.project.update_option("sentry:performance_issue_create_issue_through_platform", True)
  2180. with self.feature("organizations:issue-platform-search-perf-issues"), self.options(
  2181. {"performance.issues.create_issues_through_platform": True}
  2182. ), mock.patch.object(
  2183. PerformanceNPlusOneGroupType, "noise_config", new=NoiseConfig(0, timedelta(minutes=1))
  2184. ):
  2185. with self.feature(group_type.build_ingest_feature_name()):
  2186. _, group_info = process_event_and_issue_occurrence(
  2187. self.build_occurrence_data(
  2188. event_id=event_id, type=group_type.type_id, fingerprint=["some perf issue"]
  2189. ),
  2190. {
  2191. "event_id": event_id,
  2192. "project_id": self.project.id,
  2193. "title": "some problem",
  2194. "platform": "python",
  2195. "tags": {"my_tag": "2"},
  2196. "timestamp": before_now(minutes=1).isoformat(),
  2197. "received": before_now(minutes=1).isoformat(),
  2198. },
  2199. )
  2200. results = self.make_query(search_filter_query="issue.category:performance my_tag:2")
  2201. assert list(results) == []
  2202. with self.feature(
  2203. [
  2204. "organizations:issue-platform",
  2205. group_type.build_visible_feature_name(),
  2206. "organizations:performance-issues-search",
  2207. ]
  2208. ):
  2209. results = self.make_query(search_filter_query="issue.category:performance my_tag:2")
  2210. assert list(results) == [group_info.group]
  2211. def test_error_generic_query(self):
  2212. with self.feature(
  2213. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  2214. ):
  2215. results = self.make_query(search_filter_query="my_tag:1")
  2216. assert list(results) == [
  2217. self.profile_group_1,
  2218. self.profile_group_2,
  2219. self.error_group_2,
  2220. self.error_group_1,
  2221. ]
  2222. results = self.make_query(
  2223. search_filter_query="issue.category:[profile, error] my_tag:1"
  2224. )
  2225. assert list(results) == [
  2226. self.profile_group_1,
  2227. self.profile_group_2,
  2228. self.error_group_2,
  2229. self.error_group_1,
  2230. ]
  2231. results = self.make_query(
  2232. search_filter_query="issue.type:[profile_file_io_main_thread, error] my_tag:1"
  2233. )
  2234. assert list(results) == [
  2235. self.profile_group_1,
  2236. self.profile_group_2,
  2237. self.error_group_2,
  2238. self.error_group_1,
  2239. ]
  2240. def test_cursor_profile_issues(self):
  2241. with self.feature(
  2242. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  2243. ):
  2244. results = self.make_query(
  2245. projects=[self.project],
  2246. search_filter_query="issue.category:profile my_tag:1",
  2247. sort_by="date",
  2248. limit=1,
  2249. count_hits=True,
  2250. )
  2251. assert list(results) == [self.profile_group_1]
  2252. assert results.hits == 2
  2253. results = self.make_query(
  2254. projects=[self.project],
  2255. search_filter_query="issue.category:profile my_tag:1",
  2256. sort_by="date",
  2257. limit=1,
  2258. cursor=results.next,
  2259. count_hits=True,
  2260. )
  2261. assert list(results) == [self.profile_group_2]
  2262. assert results.hits == 2
  2263. results = self.make_query(
  2264. projects=[self.project],
  2265. search_filter_query="issue.category:profile my_tag:1",
  2266. sort_by="date",
  2267. limit=1,
  2268. cursor=results.next,
  2269. count_hits=True,
  2270. )
  2271. assert list(results) == []
  2272. assert results.hits == 2
  2273. def test_rejected_filters(self):
  2274. """
  2275. Any queries with `error.handled` or `error.unhandled` filters querying the search_issues dataset
  2276. should be rejected and return empty results.
  2277. """
  2278. with self.feature(
  2279. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  2280. ):
  2281. results = self.make_query(
  2282. projects=[self.project],
  2283. search_filter_query="issue.category:profile error.unhandled:0",
  2284. sort_by="date",
  2285. limit=1,
  2286. count_hits=True,
  2287. )
  2288. results2 = self.make_query(
  2289. projects=[self.project],
  2290. search_filter_query="issue.category:profile error.unhandled:1",
  2291. sort_by="date",
  2292. limit=1,
  2293. count_hits=True,
  2294. )
  2295. result3 = self.make_query(
  2296. projects=[self.project],
  2297. search_filter_query="issue.category:profile error.handled:0",
  2298. sort_by="date",
  2299. limit=1,
  2300. count_hits=True,
  2301. )
  2302. results4 = self.make_query(
  2303. projects=[self.project],
  2304. search_filter_query="issue.category:profile error.handled:1",
  2305. sort_by="date",
  2306. limit=1,
  2307. count_hits=True,
  2308. )
  2309. results5 = self.make_query(
  2310. projects=[self.project],
  2311. search_filter_query="issue.category:profile error.main_thread:0",
  2312. sort_by="date",
  2313. limit=1,
  2314. count_hits=True,
  2315. )
  2316. results6 = self.make_query(
  2317. projects=[self.project],
  2318. search_filter_query="issue.category:profile error.main_thread:1",
  2319. sort_by="date",
  2320. limit=1,
  2321. count_hits=True,
  2322. )
  2323. assert (
  2324. list(results)
  2325. == list(results2)
  2326. == list(result3)
  2327. == list(results4)
  2328. == list(results5)
  2329. == list(results6)
  2330. == []
  2331. )
  2332. class CdcEventsSnubaSearchTest(SharedSnubaTest):
  2333. @property
  2334. def backend(self):
  2335. return CdcEventsDatasetSnubaSearchBackend()
  2336. def setUp(self):
  2337. super().setUp()
  2338. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2339. self.event1 = self.store_event(
  2340. data={
  2341. "fingerprint": ["put-me-in-group1"],
  2342. "event_id": "a" * 32,
  2343. "environment": "production",
  2344. "timestamp": iso_format(self.base_datetime - timedelta(days=21)),
  2345. "tags": {"sentry:user": "user1"},
  2346. },
  2347. project_id=self.project.id,
  2348. )
  2349. self.env1 = self.event1.get_environment()
  2350. self.group1 = self.event1.group
  2351. self.event3 = self.store_event(
  2352. data={
  2353. "fingerprint": ["put-me-in-group1"],
  2354. "environment": "staging",
  2355. "timestamp": iso_format(self.base_datetime),
  2356. "tags": {"sentry:user": "user2"},
  2357. },
  2358. project_id=self.project.id,
  2359. )
  2360. self.event2 = self.store_event(
  2361. data={
  2362. "fingerprint": ["put-me-in-group2"],
  2363. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  2364. "environment": "staging",
  2365. "tags": {"sentry:user": "user1"},
  2366. },
  2367. project_id=self.project.id,
  2368. )
  2369. self.group2 = self.event2.group
  2370. self.env2 = self.event2.get_environment()
  2371. def run_test(
  2372. self,
  2373. search_filter_query,
  2374. expected_groups,
  2375. expected_hits,
  2376. projects=None,
  2377. environments=None,
  2378. sort_by="date",
  2379. limit=None,
  2380. count_hits=False,
  2381. date_from=None,
  2382. date_to=None,
  2383. cursor=None,
  2384. ):
  2385. results = self.make_query(
  2386. projects=projects,
  2387. search_filter_query=search_filter_query,
  2388. environments=environments,
  2389. sort_by=sort_by,
  2390. limit=limit,
  2391. count_hits=count_hits,
  2392. date_from=date_from,
  2393. date_to=date_to,
  2394. cursor=cursor,
  2395. )
  2396. assert list(results) == expected_groups
  2397. assert results.hits == expected_hits
  2398. return results
  2399. def test(self):
  2400. self.run_test("is:unresolved", [self.group1, self.group2], None)
  2401. def test_invalid(self):
  2402. with pytest.raises(InvalidQueryForExecutor):
  2403. self.make_query(search_filter_query="is:unresolved abc:123")
  2404. def test_resolved_group(self):
  2405. self.group2.status = GroupStatus.RESOLVED
  2406. self.group2.substatus = None
  2407. self.group2.save()
  2408. self.store_group(self.group2)
  2409. self.run_test("is:unresolved", [self.group1], None)
  2410. self.run_test("is:resolved", [self.group2], None)
  2411. self.run_test("is:unresolved is:resolved", [], None)
  2412. def test_environment(self):
  2413. self.run_test("is:unresolved", [self.group1], None, environments=[self.env1])
  2414. self.run_test("is:unresolved", [self.group1, self.group2], None, environments=[self.env2])
  2415. def test_sort_times_seen(self):
  2416. self.run_test(
  2417. "is:unresolved",
  2418. [self.group1, self.group2],
  2419. None,
  2420. sort_by="freq",
  2421. date_from=self.base_datetime - timedelta(days=30),
  2422. )
  2423. self.store_event(
  2424. data={
  2425. "fingerprint": ["put-me-in-group2"],
  2426. "timestamp": iso_format(self.base_datetime - timedelta(days=15)),
  2427. },
  2428. project_id=self.project.id,
  2429. )
  2430. self.store_event(
  2431. data={
  2432. "fingerprint": ["put-me-in-group2"],
  2433. "timestamp": iso_format(self.base_datetime - timedelta(days=10)),
  2434. "tags": {"sentry:user": "user2"},
  2435. },
  2436. project_id=self.project.id,
  2437. )
  2438. self.run_test(
  2439. "is:unresolved",
  2440. [self.group2, self.group1],
  2441. None,
  2442. sort_by="freq",
  2443. # Change the date range to bust the
  2444. date_from=self.base_datetime - timedelta(days=29),
  2445. )
  2446. def test_sort_first_seen(self):
  2447. self.run_test(
  2448. "is:unresolved",
  2449. [self.group2, self.group1],
  2450. None,
  2451. sort_by="new",
  2452. date_from=self.base_datetime - timedelta(days=30),
  2453. )
  2454. group3 = self.store_event(
  2455. data={
  2456. "fingerprint": ["put-me-in-group3"],
  2457. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2458. },
  2459. project_id=self.project.id,
  2460. ).group
  2461. self.run_test(
  2462. "is:unresolved",
  2463. [group3, self.group2, self.group1],
  2464. None,
  2465. sort_by="new",
  2466. # Change the date range to bust the
  2467. date_from=self.base_datetime - timedelta(days=29),
  2468. )
  2469. def test_sort_user(self):
  2470. self.run_test(
  2471. "is:unresolved",
  2472. [self.group1, self.group2],
  2473. None,
  2474. sort_by="user",
  2475. date_from=self.base_datetime - timedelta(days=30),
  2476. )
  2477. self.store_event(
  2478. data={
  2479. "fingerprint": ["put-me-in-group2"],
  2480. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2481. "tags": {"sentry:user": "user2"},
  2482. },
  2483. project_id=self.project.id,
  2484. )
  2485. self.store_event(
  2486. data={
  2487. "fingerprint": ["put-me-in-group2"],
  2488. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2489. "tags": {"sentry:user": "user2"},
  2490. },
  2491. project_id=self.project.id,
  2492. )
  2493. self.store_event(
  2494. data={
  2495. "fingerprint": ["put-me-in-group1"],
  2496. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2497. "tags": {"sentry:user": "user1"},
  2498. },
  2499. project_id=self.project.id,
  2500. )
  2501. self.store_event(
  2502. data={
  2503. "fingerprint": ["put-me-in-group1"],
  2504. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2505. "tags": {"sentry:user": "user1"},
  2506. },
  2507. project_id=self.project.id,
  2508. )
  2509. # Test group with no users, which can return a null count
  2510. group3 = self.store_event(
  2511. data={
  2512. "fingerprint": ["put-me-in-group3"],
  2513. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2514. },
  2515. project_id=self.project.id,
  2516. ).group
  2517. self.run_test(
  2518. "is:unresolved",
  2519. [self.group2, self.group1, group3],
  2520. None,
  2521. sort_by="user",
  2522. # Change the date range to bust the
  2523. date_from=self.base_datetime - timedelta(days=29),
  2524. )
  2525. def test_sort_priority(self):
  2526. self.run_test(
  2527. "is:unresolved",
  2528. [self.group1, self.group2],
  2529. None,
  2530. sort_by="priority",
  2531. date_from=self.base_datetime - timedelta(days=30),
  2532. )
  2533. def test_cursor(self):
  2534. group3 = self.store_event(
  2535. data={
  2536. "fingerprint": ["put-me-in-group3"],
  2537. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2538. "tags": {"sentry:user": "user2"},
  2539. },
  2540. project_id=self.project.id,
  2541. ).group
  2542. group4 = self.store_event(
  2543. data={
  2544. "fingerprint": ["put-me-in-group7"],
  2545. "timestamp": iso_format(self.base_datetime + timedelta(days=2)),
  2546. "tags": {"sentry:user": "user2"},
  2547. },
  2548. project_id=self.project.id,
  2549. ).group
  2550. results = self.run_test("is:unresolved", [group4], 4, limit=1, count_hits=True)
  2551. results = self.run_test(
  2552. "is:unresolved", [group3], 4, limit=1, cursor=results.next, count_hits=True
  2553. )
  2554. results = self.run_test(
  2555. "is:unresolved", [group4], 4, limit=1, cursor=results.prev, count_hits=True
  2556. )
  2557. self.run_test(
  2558. "is:unresolved", [group3, self.group1], 4, limit=2, cursor=results.next, count_hits=True
  2559. )
  2560. def test_rechecking(self):
  2561. self.group2.status = GroupStatus.RESOLVED
  2562. self.group2.substatus = None
  2563. self.group2.save()
  2564. # Explicitly avoid calling `store_group` here. This means that Clickhouse will still see
  2565. # this group as `UNRESOLVED` and it will be returned in the snuba results. This group
  2566. # should still be filtered out by our recheck.
  2567. self.run_test("is:unresolved", [self.group1], None)