test_backend.py 145 KB

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