test_backend.py 150 KB

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