test_backend.py 148 KB

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