test_backend.py 138 KB

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