test_backend.py 146 KB

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