test_backend.py 146 KB

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