test_backend.py 147 KB

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