test_backend.py 126 KB

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