test_backend.py 146 KB

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