test_backend.py 147 KB

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