test_backend.py 149 KB

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