test_backend.py 143 KB

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