test_backend.py 146 KB

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