test_backend.py 146 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914
  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. def sort_key(result):
  242. return result.id
  243. assert sorted(results, key=sort_key) == sorted(expected_groups, key=sort_key)
  244. if expected_negative_groups is not None:
  245. results = self.make_query(search_filter_query=f"!{query}", user=user)
  246. assert sorted(results, key=sort_key) == sorted(expected_negative_groups, key=sort_key)
  247. def test_query(self):
  248. results = self.make_query(search_filter_query="foo")
  249. assert set(results) == {self.group1}
  250. results = self.make_query(search_filter_query="bar")
  251. assert set(results) == {self.group2}
  252. def test_query_multi_project(self):
  253. self.set_up_multi_project()
  254. results = self.make_query([self.project, self.project2], search_filter_query="foo")
  255. assert set(results) == {self.group1, self.group_p2}
  256. def test_query_with_environment(self):
  257. results = self.make_query(
  258. environments=[self.environments["production"]], search_filter_query="foo"
  259. )
  260. assert set(results) == {self.group1}
  261. results = self.make_query(
  262. environments=[self.environments["production"]], search_filter_query="bar"
  263. )
  264. assert set(results) == set()
  265. results = self.make_query(
  266. environments=[self.environments["staging"]], search_filter_query="bar"
  267. )
  268. assert set(results) == {self.group2}
  269. def test_query_for_text_in_long_message(self):
  270. results = self.make_query(
  271. [self.project],
  272. environments=[self.environments["production"]],
  273. search_filter_query="santryrox",
  274. )
  275. assert set(results) == {self.group1}
  276. def test_multi_environments(self):
  277. self.set_up_multi_project()
  278. results = self.make_query(
  279. [self.project, self.project2],
  280. environments=[self.environments["production"], self.environments["staging"]],
  281. )
  282. assert set(results) == {self.group1, self.group2, self.group_p2}
  283. def test_query_with_environment_multi_project(self):
  284. self.set_up_multi_project()
  285. results = self.make_query(
  286. [self.project, self.project2],
  287. environments=[self.environments["production"]],
  288. search_filter_query="foo",
  289. )
  290. assert set(results) == {self.group1, self.group_p2}
  291. results = self.make_query(
  292. [self.project, self.project2],
  293. environments=[self.environments["production"]],
  294. search_filter_query="bar",
  295. )
  296. assert set(results) == set()
  297. def test_query_timestamp(self):
  298. results = self.make_query(
  299. [self.project],
  300. environments=[self.environments["production"]],
  301. search_filter_query=f"timestamp:>{iso_format(self.event1.datetime)} timestamp:<{iso_format(self.event3.datetime)}",
  302. )
  303. assert set(results) == {self.group1}
  304. def test_sort(self):
  305. results = self.make_query(sort_by="date")
  306. assert list(results) == [self.group1, self.group2]
  307. results = self.make_query(sort_by="new")
  308. assert list(results) == [self.group2, self.group1]
  309. results = self.make_query(sort_by="freq")
  310. assert list(results) == [self.group1, self.group2]
  311. results = self.make_query(sort_by="priority")
  312. assert list(results) == [self.group2, self.group1]
  313. results = self.make_query(sort_by="user")
  314. assert list(results) == [self.group1, self.group2]
  315. def test_better_priority_sort(self):
  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.group2, self.group1]
  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. with Feature({"organizations:assign-to-me": False}):
  915. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  916. assert set(results) == {self.group2}
  917. # test when there should be no results
  918. other_user = self.create_user()
  919. results = self.make_query(search_filter_query="assigned:%s" % other_user.username)
  920. assert set(results) == set()
  921. owner = self.create_user()
  922. self.create_member(
  923. organization=self.project.organization, user=owner, role="owner", teams=[]
  924. )
  925. # test that owners don't see results for all teams
  926. results = self.make_query(search_filter_query="assigned:%s" % owner.username)
  927. assert set(results) == set()
  928. def test_assigned_to_me_my_teams(self):
  929. my_team_group = self.store_event(
  930. data={
  931. "fingerprint": ["put-me-in-group-my-teams"],
  932. "event_id": "f" * 32,
  933. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  934. "message": "baz",
  935. "environment": "staging",
  936. "tags": {
  937. "server": "example.com",
  938. "url": "http://example.com",
  939. "sentry:user": "event2@example.com",
  940. },
  941. "level": "error",
  942. },
  943. project_id=self.project.id,
  944. ).group
  945. # assign the issue to my team instead of me
  946. GroupAssignee.objects.create(
  947. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  948. )
  949. with Feature({"organizations:assign-to-me": False}):
  950. self.run_test_query(
  951. "assigned:me",
  952. [my_team_group, self.group2],
  953. user=self.user,
  954. )
  955. assert not GroupAssignee.objects.filter(
  956. user_id=self.user.id, group=my_team_group
  957. ).exists()
  958. with self.feature("organizations:assign-to-me"):
  959. self.run_test_query(
  960. "assigned:me",
  961. [self.group2],
  962. user=self.user,
  963. )
  964. self.run_test_query(
  965. "assigned:my_teams",
  966. [my_team_group],
  967. user=self.user,
  968. )
  969. def test_assigned_to_me_my_teams_in_syntax(self):
  970. my_team_group = self.store_event(
  971. data={
  972. "fingerprint": ["put-me-in-group-my-teams"],
  973. "event_id": "f" * 32,
  974. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  975. "message": "baz",
  976. "environment": "staging",
  977. "tags": {
  978. "server": "example.com",
  979. "url": "http://example.com",
  980. "sentry:user": "event2@example.com",
  981. },
  982. "level": "error",
  983. },
  984. project_id=self.project.id,
  985. ).group
  986. # assign the issue to my team instead of me
  987. GroupAssignee.objects.create(
  988. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  989. )
  990. with Feature({"organizations:assign-to-me": False}):
  991. self.run_test_query(
  992. "assigned:[me]",
  993. [my_team_group, self.group2],
  994. user=self.user,
  995. )
  996. assert not GroupAssignee.objects.filter(
  997. user_id=self.user.id, group=my_team_group
  998. ).exists()
  999. with self.feature("organizations:assign-to-me"):
  1000. self.run_test_query(
  1001. "assigned:[me]",
  1002. [self.group2],
  1003. user=self.user,
  1004. )
  1005. self.run_test_query(
  1006. "assigned:[my_teams]",
  1007. [my_team_group],
  1008. user=self.user,
  1009. )
  1010. self.run_test_query(
  1011. "assigned:[me, my_teams]",
  1012. [self.group2, my_team_group],
  1013. user=self.user,
  1014. )
  1015. def test_assigned_to_in_syntax(self):
  1016. group_3 = self.store_event(
  1017. data={
  1018. "fingerprint": ["put-me-in-group3"],
  1019. "event_id": "c" * 32,
  1020. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1021. },
  1022. project_id=self.project.id,
  1023. ).group
  1024. group_3.status = GroupStatus.MUTED
  1025. group_3.substatus = None
  1026. group_3.save()
  1027. other_user = self.create_user()
  1028. self.run_test_query(
  1029. f"assigned:[{self.user.username}, {other_user.username}]",
  1030. [self.group2],
  1031. [self.group1, group_3],
  1032. )
  1033. GroupAssignee.objects.create(project=self.project, group=group_3, user_id=other_user.id)
  1034. self.run_test_query(
  1035. f"assigned:[{self.user.username}, {other_user.username}]",
  1036. [self.group2, group_3],
  1037. [self.group1],
  1038. )
  1039. self.run_test_query(
  1040. f"assigned:[#{self.team.slug}, {other_user.username}]",
  1041. [group_3],
  1042. [self.group1, self.group2],
  1043. )
  1044. ga_2 = GroupAssignee.objects.get(
  1045. user_id=self.user.id, group=self.group2, project=self.group2.project
  1046. )
  1047. ga_2.update(team=self.team, user_id=None)
  1048. with Feature({"organizations:assign-to-me": False}):
  1049. self.run_test_query(
  1050. f"assigned:[{self.user.username}, {other_user.username}]",
  1051. [self.group2, group_3],
  1052. [self.group1],
  1053. )
  1054. self.run_test_query(
  1055. f"assigned:[#{self.team.slug}, {other_user.username}]",
  1056. [self.group2, group_3],
  1057. [self.group1],
  1058. )
  1059. self.run_test_query(
  1060. f"assigned:[me, none, {other_user.username}]",
  1061. [self.group1, self.group2, group_3],
  1062. [],
  1063. )
  1064. def test_assigned_or_suggested_in_syntax(self):
  1065. Group.objects.all().delete()
  1066. group = self.store_event(
  1067. data={
  1068. "timestamp": iso_format(before_now(seconds=180)),
  1069. "fingerprint": ["group-1"],
  1070. },
  1071. project_id=self.project.id,
  1072. ).group
  1073. group1 = self.store_event(
  1074. data={
  1075. "timestamp": iso_format(before_now(seconds=185)),
  1076. "fingerprint": ["group-2"],
  1077. },
  1078. project_id=self.project.id,
  1079. ).group
  1080. group2 = self.store_event(
  1081. data={
  1082. "timestamp": iso_format(before_now(seconds=190)),
  1083. "fingerprint": ["group-3"],
  1084. },
  1085. project_id=self.project.id,
  1086. ).group
  1087. assigned_group = self.store_event(
  1088. data={
  1089. "timestamp": iso_format(before_now(seconds=195)),
  1090. "fingerprint": ["group-4"],
  1091. },
  1092. project_id=self.project.id,
  1093. ).group
  1094. assigned_to_other_group = self.store_event(
  1095. data={
  1096. "timestamp": iso_format(before_now(seconds=195)),
  1097. "fingerprint": ["group-5"],
  1098. },
  1099. project_id=self.project.id,
  1100. ).group
  1101. self.run_test_query(
  1102. "assigned_or_suggested:[me]",
  1103. [],
  1104. [group, group1, group2, assigned_group, assigned_to_other_group],
  1105. )
  1106. GroupOwner.objects.create(
  1107. group=assigned_to_other_group,
  1108. project=self.project,
  1109. organization=self.organization,
  1110. type=0,
  1111. team_id=None,
  1112. user_id=self.user.id,
  1113. )
  1114. GroupOwner.objects.create(
  1115. group=group,
  1116. project=self.project,
  1117. organization=self.organization,
  1118. type=0,
  1119. team_id=None,
  1120. user_id=self.user.id,
  1121. )
  1122. self.run_test_query(
  1123. "assigned_or_suggested:[me]",
  1124. [group, assigned_to_other_group],
  1125. [group1, group2, assigned_group],
  1126. )
  1127. # 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)
  1128. other_user = self.create_user("other@user.com", is_superuser=False)
  1129. GroupAssignee.objects.create(
  1130. group=assigned_to_other_group,
  1131. project=self.project,
  1132. user_id=other_user.id,
  1133. )
  1134. self.run_test_query(
  1135. "assigned_or_suggested:[me]",
  1136. [group],
  1137. [group1, group2, assigned_group, assigned_to_other_group],
  1138. )
  1139. self.run_test_query(
  1140. f"assigned_or_suggested:[{other_user.email}]",
  1141. [assigned_to_other_group],
  1142. [group, group1, group2, assigned_group],
  1143. )
  1144. GroupAssignee.objects.create(
  1145. group=assigned_group, project=self.project, user_id=self.user.id
  1146. )
  1147. self.run_test_query(
  1148. f"assigned_or_suggested:[{self.user.email}]",
  1149. [assigned_group, group],
  1150. )
  1151. GroupOwner.objects.create(
  1152. group=group,
  1153. project=self.project,
  1154. organization=self.organization,
  1155. type=0,
  1156. team_id=self.team.id,
  1157. user_id=None,
  1158. )
  1159. self.run_test_query(
  1160. f"assigned_or_suggested:[#{self.team.slug}]",
  1161. [group],
  1162. )
  1163. self.run_test_query(
  1164. "assigned_or_suggested:[me, none]",
  1165. [group, group1, group2, assigned_group],
  1166. [assigned_to_other_group],
  1167. )
  1168. not_me = self.create_user(email="notme@sentry.io")
  1169. GroupOwner.objects.create(
  1170. group=group2,
  1171. project=self.project,
  1172. organization=self.organization,
  1173. type=0,
  1174. team_id=None,
  1175. user_id=not_me.id,
  1176. )
  1177. self.run_test_query(
  1178. "assigned_or_suggested:[me, none]",
  1179. [group, group1, assigned_group],
  1180. [assigned_to_other_group, group2],
  1181. )
  1182. GroupOwner.objects.filter(group=group, user_id=self.user.id).delete()
  1183. self.run_test_query(
  1184. f"assigned_or_suggested:[me, none, #{self.team.slug}]",
  1185. [group, group1, assigned_group],
  1186. [assigned_to_other_group, group2],
  1187. )
  1188. self.run_test_query(
  1189. f"assigned_or_suggested:[me, none, #{self.team.slug}, {not_me.email}]",
  1190. [group, group1, assigned_group, group2],
  1191. [assigned_to_other_group],
  1192. )
  1193. def test_assigned_or_suggested_my_teams(self):
  1194. Group.objects.all().delete()
  1195. group = self.store_event(
  1196. data={
  1197. "timestamp": iso_format(before_now(seconds=180)),
  1198. "fingerprint": ["group-1"],
  1199. },
  1200. project_id=self.project.id,
  1201. ).group
  1202. group1 = self.store_event(
  1203. data={
  1204. "timestamp": iso_format(before_now(seconds=185)),
  1205. "fingerprint": ["group-2"],
  1206. },
  1207. project_id=self.project.id,
  1208. ).group
  1209. group2 = self.store_event(
  1210. data={
  1211. "timestamp": iso_format(before_now(seconds=190)),
  1212. "fingerprint": ["group-3"],
  1213. },
  1214. project_id=self.project.id,
  1215. ).group
  1216. assigned_group = self.store_event(
  1217. data={
  1218. "timestamp": iso_format(before_now(seconds=195)),
  1219. "fingerprint": ["group-4"],
  1220. },
  1221. project_id=self.project.id,
  1222. ).group
  1223. assigned_to_other_group = self.store_event(
  1224. data={
  1225. "timestamp": iso_format(before_now(seconds=195)),
  1226. "fingerprint": ["group-5"],
  1227. },
  1228. project_id=self.project.id,
  1229. ).group
  1230. my_team_group = self.store_event(
  1231. data={
  1232. "fingerprint": ["put-me-in-group-my-teams"],
  1233. "event_id": "f" * 32,
  1234. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1235. "message": "baz",
  1236. "environment": "staging",
  1237. "tags": {
  1238. "server": "example.com",
  1239. "url": "http://example.com",
  1240. "sentry:user": "event2@example.com",
  1241. },
  1242. "level": "error",
  1243. },
  1244. project_id=self.project.id,
  1245. ).group
  1246. # after the change to me -> (me + my_teams)
  1247. with self.feature("organizations:assign-to-me"):
  1248. self.run_test_query(
  1249. "assigned_or_suggested:me",
  1250. [],
  1251. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1252. user=self.user,
  1253. )
  1254. self.run_test_query(
  1255. "assigned_or_suggested:my_teams",
  1256. [],
  1257. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1258. user=self.user,
  1259. )
  1260. GroupOwner.objects.create(
  1261. group=assigned_to_other_group,
  1262. project=self.project,
  1263. organization=self.organization,
  1264. type=0,
  1265. team_id=None,
  1266. user_id=self.user.id,
  1267. )
  1268. GroupOwner.objects.create(
  1269. group=group,
  1270. project=self.project,
  1271. organization=self.organization,
  1272. type=0,
  1273. team_id=None,
  1274. user_id=self.user.id,
  1275. )
  1276. GroupAssignee.objects.create(
  1277. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  1278. )
  1279. # after the change to me -> (me + my_teams)
  1280. with self.feature("organizations:assign-to-me"):
  1281. self.run_test_query(
  1282. "assigned_or_suggested:me",
  1283. [group, assigned_to_other_group],
  1284. [group1, group2, assigned_group, my_team_group],
  1285. user=self.user,
  1286. )
  1287. self.run_test_query(
  1288. "assigned_or_suggested:my_teams",
  1289. [my_team_group],
  1290. [group, group1, group2, assigned_group, assigned_to_other_group],
  1291. user=self.user,
  1292. )
  1293. # 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)
  1294. other_user = self.create_user("other@user.com", is_superuser=False)
  1295. GroupAssignee.objects.create(
  1296. group=assigned_to_other_group,
  1297. project=self.project,
  1298. user_id=other_user.id,
  1299. )
  1300. # after the change to me -> (me + my_teams)
  1301. with self.feature("organizations:assign-to-me"):
  1302. self.run_test_query(
  1303. "assigned_or_suggested:me",
  1304. [group],
  1305. [group1, group2, assigned_group, my_team_group, assigned_to_other_group],
  1306. user=self.user,
  1307. )
  1308. self.run_test_query(
  1309. "assigned_or_suggested:my_teams",
  1310. [my_team_group],
  1311. [group, group1, group2, assigned_group, assigned_to_other_group],
  1312. user=self.user,
  1313. )
  1314. self.run_test_query(
  1315. f"assigned_or_suggested:{other_user.email}",
  1316. [assigned_to_other_group],
  1317. [group, group1, group2, assigned_group, my_team_group],
  1318. user=self.user,
  1319. )
  1320. GroupAssignee.objects.create(
  1321. group=assigned_group, project=self.project, user_id=self.user.id
  1322. )
  1323. # after the change to me -> (me + my_teams)
  1324. with self.feature("organizations:assign-to-me"):
  1325. self.run_test_query(
  1326. f"assigned_or_suggested:{self.user.email}",
  1327. [assigned_group, group],
  1328. [group1, group2, my_team_group, assigned_to_other_group],
  1329. user=self.user,
  1330. )
  1331. GroupOwner.objects.create(
  1332. group=group,
  1333. project=self.project,
  1334. organization=self.organization,
  1335. type=0,
  1336. team_id=self.team.id,
  1337. user_id=None,
  1338. )
  1339. # after the change to me -> (me + my_teams)
  1340. with self.feature("organizations:assign-to-me"):
  1341. self.run_test_query(
  1342. f"assigned_or_suggested:#{self.team.slug}",
  1343. [group, my_team_group],
  1344. [group1, group2, assigned_group, assigned_to_other_group],
  1345. user=self.user,
  1346. )
  1347. def test_assigned_or_suggested_my_teams_in_syntax(self):
  1348. Group.objects.all().delete()
  1349. group = self.store_event(
  1350. data={
  1351. "timestamp": iso_format(before_now(seconds=180)),
  1352. "fingerprint": ["group-1"],
  1353. },
  1354. project_id=self.project.id,
  1355. ).group
  1356. group1 = self.store_event(
  1357. data={
  1358. "timestamp": iso_format(before_now(seconds=185)),
  1359. "fingerprint": ["group-2"],
  1360. },
  1361. project_id=self.project.id,
  1362. ).group
  1363. group2 = self.store_event(
  1364. data={
  1365. "timestamp": iso_format(before_now(seconds=190)),
  1366. "fingerprint": ["group-3"],
  1367. },
  1368. project_id=self.project.id,
  1369. ).group
  1370. assigned_group = self.store_event(
  1371. data={
  1372. "timestamp": iso_format(before_now(seconds=195)),
  1373. "fingerprint": ["group-4"],
  1374. },
  1375. project_id=self.project.id,
  1376. ).group
  1377. assigned_to_other_group = self.store_event(
  1378. data={
  1379. "timestamp": iso_format(before_now(seconds=195)),
  1380. "fingerprint": ["group-5"],
  1381. },
  1382. project_id=self.project.id,
  1383. ).group
  1384. my_team_group = self.store_event(
  1385. data={
  1386. "fingerprint": ["put-me-in-group-my-teams"],
  1387. "event_id": "f" * 32,
  1388. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1389. "message": "baz",
  1390. "environment": "staging",
  1391. "tags": {
  1392. "server": "example.com",
  1393. "url": "http://example.com",
  1394. "sentry:user": "event2@example.com",
  1395. },
  1396. "level": "error",
  1397. },
  1398. project_id=self.project.id,
  1399. ).group
  1400. # after the change to me -> (me + my_teams)
  1401. with self.feature("organizations:assign-to-me"):
  1402. self.run_test_query(
  1403. "assigned_or_suggested:[me]",
  1404. [],
  1405. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1406. user=self.user,
  1407. )
  1408. self.run_test_query(
  1409. "assigned_or_suggested:[my_teams]",
  1410. [],
  1411. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1412. user=self.user,
  1413. )
  1414. self.run_test_query(
  1415. "assigned_or_suggested:[me, my_teams]",
  1416. [],
  1417. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1418. user=self.user,
  1419. )
  1420. GroupOwner.objects.create(
  1421. group=assigned_to_other_group,
  1422. project=self.project,
  1423. organization=self.organization,
  1424. type=0,
  1425. team_id=None,
  1426. user_id=self.user.id,
  1427. )
  1428. GroupOwner.objects.create(
  1429. group=group,
  1430. project=self.project,
  1431. organization=self.organization,
  1432. type=0,
  1433. team_id=None,
  1434. user_id=self.user.id,
  1435. )
  1436. GroupAssignee.objects.create(
  1437. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  1438. )
  1439. # after the change to me -> (me + my_teams)
  1440. with self.feature("organizations:assign-to-me"):
  1441. self.run_test_query(
  1442. "assigned_or_suggested:[me]",
  1443. [group, assigned_to_other_group],
  1444. [group1, group2, assigned_group, my_team_group],
  1445. user=self.user,
  1446. )
  1447. self.run_test_query(
  1448. "assigned_or_suggested:[my_teams]",
  1449. [my_team_group],
  1450. [group, group1, group2, assigned_group, assigned_to_other_group],
  1451. user=self.user,
  1452. )
  1453. self.run_test_query(
  1454. "assigned_or_suggested:[me, my_teams]",
  1455. [group, assigned_to_other_group, my_team_group],
  1456. [group1, group2, assigned_group],
  1457. user=self.user,
  1458. )
  1459. # 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)
  1460. other_user = self.create_user("other@user.com", is_superuser=False)
  1461. GroupAssignee.objects.create(
  1462. group=assigned_to_other_group,
  1463. project=self.project,
  1464. user_id=other_user.id,
  1465. )
  1466. # after the change to me -> (me + my_teams)
  1467. with self.feature("organizations:assign-to-me"):
  1468. self.run_test_query(
  1469. "assigned_or_suggested:[me]",
  1470. [group],
  1471. [group1, group2, assigned_group, my_team_group, assigned_to_other_group],
  1472. user=self.user,
  1473. )
  1474. self.run_test_query(
  1475. "assigned_or_suggested:[my_teams]",
  1476. [my_team_group],
  1477. [group, group1, group2, assigned_group, assigned_to_other_group],
  1478. user=self.user,
  1479. )
  1480. self.run_test_query(
  1481. "assigned_or_suggested:[me, my_teams]",
  1482. [group, my_team_group],
  1483. [group1, group2, assigned_group, assigned_to_other_group],
  1484. user=self.user,
  1485. )
  1486. self.run_test_query(
  1487. f"assigned_or_suggested:[{other_user.email}]",
  1488. [assigned_to_other_group],
  1489. [group, group1, group2, assigned_group, my_team_group],
  1490. user=self.user,
  1491. )
  1492. GroupAssignee.objects.create(
  1493. group=assigned_group, project=self.project, user_id=self.user.id
  1494. )
  1495. # after the change to me -> (me + my_teams)
  1496. with self.feature("organizations:assign-to-me"):
  1497. self.run_test_query(
  1498. f"assigned_or_suggested:[{self.user.email}]",
  1499. [assigned_group, group],
  1500. [group1, group2, my_team_group, assigned_to_other_group],
  1501. user=self.user,
  1502. )
  1503. GroupOwner.objects.create(
  1504. group=group,
  1505. project=self.project,
  1506. organization=self.organization,
  1507. type=0,
  1508. team_id=self.team.id,
  1509. user_id=None,
  1510. )
  1511. # after the change to me -> (me + my_teams)
  1512. with self.feature("organizations:assign-to-me"):
  1513. self.run_test_query(
  1514. f"assigned_or_suggested:[#{self.team.slug}]",
  1515. [group, my_team_group],
  1516. [group1, group2, assigned_group, assigned_to_other_group],
  1517. user=self.user,
  1518. )
  1519. self.run_test_query(
  1520. "assigned_or_suggested:[me, none]",
  1521. [group, group1, group2, assigned_group],
  1522. [my_team_group, assigned_to_other_group],
  1523. user=self.user,
  1524. )
  1525. self.run_test_query(
  1526. "assigned_or_suggested:[my_teams, none]",
  1527. [group, group1, group2, my_team_group],
  1528. [assigned_to_other_group, assigned_group],
  1529. user=self.user,
  1530. )
  1531. self.run_test_query(
  1532. "assigned_or_suggested:[me, my_teams, none]",
  1533. [group, group1, group2, my_team_group, assigned_group],
  1534. [assigned_to_other_group],
  1535. user=self.user,
  1536. )
  1537. not_me = self.create_user(email="notme@sentry.io")
  1538. GroupOwner.objects.create(
  1539. group=group2,
  1540. project=self.project,
  1541. organization=self.organization,
  1542. type=0,
  1543. team_id=None,
  1544. user_id=not_me.id,
  1545. )
  1546. # after the change to me -> (me + my_teams)
  1547. with self.feature("organizations:assign-to-me"):
  1548. self.run_test_query(
  1549. "assigned_or_suggested:[me, none]",
  1550. [group, group1, assigned_group],
  1551. [group2, my_team_group, assigned_to_other_group],
  1552. user=self.user,
  1553. )
  1554. self.run_test_query(
  1555. "assigned_or_suggested:[my_teams, none]",
  1556. [group, group1, my_team_group],
  1557. [group2, assigned_group, assigned_to_other_group],
  1558. user=self.user,
  1559. )
  1560. self.run_test_query(
  1561. "assigned_or_suggested:[me, my_teams, none]",
  1562. [group, group1, my_team_group, assigned_group],
  1563. [group2, assigned_to_other_group],
  1564. user=self.user,
  1565. )
  1566. GroupOwner.objects.filter(group=group, user_id=self.user.id).delete()
  1567. # after the change to me -> (me + my_teams)
  1568. with self.feature("organizations:assign-to-me"):
  1569. self.run_test_query(
  1570. f"assigned_or_suggested:[me, none, #{self.team.slug}]",
  1571. [group, group1, assigned_group, my_team_group],
  1572. [assigned_to_other_group, group2],
  1573. user=self.user,
  1574. )
  1575. self.run_test_query(
  1576. f"assigned_or_suggested:[my_teams, none, #{self.team.slug}]",
  1577. [group, group1, my_team_group],
  1578. [assigned_to_other_group, group2, assigned_group],
  1579. user=self.user,
  1580. )
  1581. self.run_test_query(
  1582. f"assigned_or_suggested:[me, my_teams, none, #{self.team.slug}]",
  1583. [group, group1, my_team_group, assigned_group],
  1584. [assigned_to_other_group, group2],
  1585. user=self.user,
  1586. )
  1587. self.run_test_query(
  1588. f"assigned_or_suggested:[me, none, #{self.team.slug}, {not_me.email}]",
  1589. [group, group1, group2, assigned_group, my_team_group],
  1590. [assigned_to_other_group],
  1591. user=self.user,
  1592. )
  1593. self.run_test_query(
  1594. f"assigned_or_suggested:[my_teams, none, #{self.team.slug}, {not_me.email}]",
  1595. [group, group1, group2, my_team_group],
  1596. [assigned_to_other_group, assigned_group],
  1597. user=self.user,
  1598. )
  1599. self.run_test_query(
  1600. f"assigned_or_suggested:[me, my_teams, none, #{self.team.slug}, {not_me.email}]",
  1601. [group, group1, group2, my_team_group, assigned_group],
  1602. [assigned_to_other_group],
  1603. user=self.user,
  1604. )
  1605. def test_assigned_to_with_environment(self):
  1606. results = self.make_query(
  1607. environments=[self.environments["staging"]],
  1608. search_filter_query="assigned:%s" % self.user.username,
  1609. )
  1610. assert set(results) == {self.group2}
  1611. results = self.make_query(
  1612. environments=[self.environments["production"]],
  1613. search_filter_query="assigned:%s" % self.user.username,
  1614. )
  1615. assert set(results) == set()
  1616. def test_subscribed_by(self):
  1617. results = self.make_query(
  1618. [self.group1.project], search_filter_query="subscribed:%s" % self.user.username
  1619. )
  1620. assert set(results) == {self.group1}
  1621. def test_subscribed_by_in_syntax(self):
  1622. self.run_test_query(f"subscribed:[{self.user.username}]", [self.group1], [self.group2])
  1623. user_2 = self.create_user()
  1624. GroupSubscription.objects.create(
  1625. user_id=user_2.id, group=self.group2, project=self.project, is_active=True
  1626. )
  1627. self.run_test_query(
  1628. f"subscribed:[{self.user.username}, {user_2.username}]", [self.group1, self.group2], []
  1629. )
  1630. def test_subscribed_by_with_environment(self):
  1631. results = self.make_query(
  1632. [self.group1.project],
  1633. environments=[self.environments["production"]],
  1634. search_filter_query="subscribed:%s" % self.user.username,
  1635. )
  1636. assert set(results) == {self.group1}
  1637. results = self.make_query(
  1638. [self.group1.project],
  1639. environments=[self.environments["staging"]],
  1640. search_filter_query="subscribed:%s" % self.user.username,
  1641. )
  1642. assert set(results) == set()
  1643. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1644. def test_snuba_not_called_optimization(self, query_mock):
  1645. assert self.make_query(search_filter_query="status:unresolved").results == [self.group1]
  1646. assert not query_mock.called
  1647. assert (
  1648. self.make_query(
  1649. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1650. sort_by="date",
  1651. ).results
  1652. == []
  1653. )
  1654. assert query_mock.called
  1655. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1656. def test_reduce_bulk_results_none_total(self, bulk_raw_query_mock):
  1657. bulk_raw_query_mock.return_value = [
  1658. {"data": [], "totals": {"total": None}},
  1659. {"data": [], "totals": {"total": None}},
  1660. ]
  1661. assert (
  1662. self.make_query(
  1663. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1664. sort_by="date",
  1665. ).results
  1666. == []
  1667. )
  1668. assert bulk_raw_query_mock.called
  1669. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1670. def test_reduce_bulk_results_none_data(self, bulk_raw_query_mock):
  1671. bulk_raw_query_mock.return_value = [
  1672. {"data": None, "totals": {"total": 0}},
  1673. {"data": None, "totals": {"total": 0}},
  1674. ]
  1675. assert (
  1676. self.make_query(
  1677. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1678. sort_by="date",
  1679. ).results
  1680. == []
  1681. )
  1682. assert bulk_raw_query_mock.called
  1683. def test_pre_and_post_filtering(self):
  1684. prev_max_pre = options.get("snuba.search.max-pre-snuba-candidates")
  1685. options.set("snuba.search.max-pre-snuba-candidates", 1)
  1686. try:
  1687. # normal queries work as expected
  1688. results = self.make_query(search_filter_query="foo")
  1689. assert set(results) == {self.group1}
  1690. results = self.make_query(search_filter_query="bar")
  1691. assert set(results) == {self.group2}
  1692. # no candidate matches in Sentry, immediately return empty paginator
  1693. results = self.make_query(search_filter_query="NO MATCHES IN SENTRY")
  1694. assert set(results) == set()
  1695. # too many candidates, skip pre-filter, requires >1 postfilter queries
  1696. results = self.make_query()
  1697. assert set(results) == {self.group1, self.group2}
  1698. finally:
  1699. options.set("snuba.search.max-pre-snuba-candidates", prev_max_pre)
  1700. def test_optimizer_enabled(self):
  1701. prev_optimizer_enabled = options.get("snuba.search.pre-snuba-candidates-optimizer")
  1702. options.set("snuba.search.pre-snuba-candidates-optimizer", True)
  1703. try:
  1704. results = self.make_query(
  1705. search_filter_query="server:example.com",
  1706. environments=[self.environments["production"]],
  1707. )
  1708. assert set(results) == {self.group1}
  1709. finally:
  1710. options.set("snuba.search.pre-snuba-candidates-optimizer", prev_optimizer_enabled)
  1711. def test_search_out_of_range(self):
  1712. the_date = datetime(2000, 1, 1, 0, 0, 0, tzinfo=pytz.utc)
  1713. results = self.make_query(
  1714. search_filter_query=f"event.timestamp:>{the_date} event.timestamp:<{the_date}",
  1715. date_from=the_date,
  1716. date_to=the_date,
  1717. )
  1718. assert set(results) == set()
  1719. def test_regressed_in_release(self):
  1720. # expect no groups within the results since there are no releases
  1721. results = self.make_query(search_filter_query="regressed_in_release:fake")
  1722. assert set(results) == set()
  1723. # expect no groups even though there is a release; since no group regressed in this release
  1724. release_1 = self.create_release()
  1725. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1726. assert set(results) == set()
  1727. # Create a new event so that we get a group in this release
  1728. group = self.store_event(
  1729. data={
  1730. "release": release_1.version,
  1731. },
  1732. project_id=self.project.id,
  1733. ).group
  1734. # # Should still be no group since we didn't regress in this release
  1735. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1736. assert set(results) == set()
  1737. record_group_history(group, GroupHistoryStatus.REGRESSED, release=release_1)
  1738. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1739. assert set(results) == {group}
  1740. # Make sure this works correctly with multiple releases
  1741. release_2 = self.create_release()
  1742. group_2 = self.store_event(
  1743. data={
  1744. "fingerprint": ["put-me-in-group9001"],
  1745. "event_id": "a" * 32,
  1746. "release": release_2.version,
  1747. },
  1748. project_id=self.project.id,
  1749. ).group
  1750. record_group_history(group_2, GroupHistoryStatus.REGRESSED, release=release_2)
  1751. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1752. assert set(results) == {group}
  1753. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_2.version)
  1754. assert set(results) == {group_2}
  1755. def test_first_release(self):
  1756. # expect no groups within the results since there are no releases
  1757. results = self.make_query(search_filter_query="first_release:%s" % "fake")
  1758. assert set(results) == set()
  1759. # expect no groups even though there is a release; since no group
  1760. # is attached to a release
  1761. release_1 = self.create_release(self.project)
  1762. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1763. assert set(results) == set()
  1764. # Create a new event so that we get a group in this release
  1765. group = self.store_event(
  1766. data={
  1767. "fingerprint": ["put-me-in-group9001"],
  1768. "event_id": "a" * 32,
  1769. "message": "hello",
  1770. "environment": "production",
  1771. "tags": {"server": "example.com"},
  1772. "release": release_1.version,
  1773. "stacktrace": {"frames": [{"module": "group1"}]},
  1774. },
  1775. project_id=self.project.id,
  1776. ).group
  1777. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1778. assert set(results) == {group}
  1779. def test_first_release_in_syntax(self):
  1780. # expect no groups within the results since there are no releases
  1781. self.run_test_query("first_release:[fake, fake2]", [])
  1782. # expect no groups even though there is a release; since no group
  1783. # is attached to a release
  1784. release_1 = self.create_release(self.project)
  1785. release_2 = self.create_release(self.project)
  1786. self.run_test_query(f"first_release:[{release_1.version}, {release_2.version}]", [])
  1787. # Create a new event so that we get a group in this release
  1788. group = self.store_event(
  1789. data={
  1790. "fingerprint": ["put-me-in-group9001"],
  1791. "event_id": "a" * 32,
  1792. "message": "hello",
  1793. "environment": "production",
  1794. "tags": {"server": "example.com"},
  1795. "release": release_1.version,
  1796. "stacktrace": {"frames": [{"module": "group1"}]},
  1797. },
  1798. project_id=self.project.id,
  1799. ).group
  1800. self.run_test_query(
  1801. f"first_release:[{release_1.version}, {release_2.version}]",
  1802. [group],
  1803. [self.group1, self.group2],
  1804. )
  1805. # Create a new event so that we get a group in this release
  1806. group_2 = self.store_event(
  1807. data={
  1808. "fingerprint": ["put-me-in-group9002"],
  1809. "event_id": "a" * 32,
  1810. "message": "hello",
  1811. "environment": "production",
  1812. "tags": {"server": "example.com"},
  1813. "release": release_2.version,
  1814. "stacktrace": {"frames": [{"module": "group1"}]},
  1815. },
  1816. project_id=self.project.id,
  1817. ).group
  1818. self.run_test_query(
  1819. f"first_release:[{release_1.version}, {release_2.version}]",
  1820. [group, group_2],
  1821. )
  1822. def test_first_release_environments(self):
  1823. results = self.make_query(
  1824. environments=[self.environments["production"]],
  1825. search_filter_query="first_release:fake",
  1826. )
  1827. assert set(results) == set()
  1828. release = self.create_release(self.project)
  1829. group_env = GroupEnvironment.get_or_create(
  1830. group_id=self.group1.id, environment_id=self.environments["production"].id
  1831. )[0]
  1832. results = self.make_query(
  1833. environments=[self.environments["production"]],
  1834. search_filter_query=f"first_release:{release.version}",
  1835. )
  1836. assert set(results) == set()
  1837. group_env.first_release = release
  1838. group_env.save()
  1839. results = self.make_query(
  1840. environments=[self.environments["production"]],
  1841. search_filter_query=f"first_release:{release.version}",
  1842. )
  1843. assert set(results) == {self.group1}
  1844. def test_first_release_environments_in_syntax(self):
  1845. self.run_test_query(
  1846. "first_release:[fake, fake2]",
  1847. [],
  1848. [self.group1, self.group2],
  1849. environments=[self.environments["production"]],
  1850. )
  1851. release = self.create_release(self.project)
  1852. group_1_env = GroupEnvironment.objects.get(
  1853. group_id=self.group1.id, environment_id=self.environments["production"].id
  1854. )
  1855. group_1_env.update(first_release=release)
  1856. self.run_test_query(
  1857. f"first_release:[{release.version}, fake2]",
  1858. [self.group1],
  1859. [self.group2],
  1860. environments=[self.environments["production"]],
  1861. )
  1862. group_2_env = GroupEnvironment.objects.get(
  1863. group_id=self.group2.id, environment_id=self.environments["staging"].id
  1864. )
  1865. group_2_env.update(first_release=release)
  1866. self.run_test_query(
  1867. f"first_release:[{release.version}, fake2]",
  1868. [self.group1, self.group2],
  1869. [],
  1870. environments=[self.environments["production"], self.environments["staging"]],
  1871. )
  1872. # Make sure we don't get duplicate groups
  1873. GroupEnvironment.objects.create(
  1874. group_id=self.group1.id,
  1875. environment_id=self.environments["staging"].id,
  1876. first_release=release,
  1877. )
  1878. self.run_test_query(
  1879. f"first_release:[{release.version}, fake2]",
  1880. [self.group1, self.group2],
  1881. [],
  1882. environments=[self.environments["production"], self.environments["staging"]],
  1883. )
  1884. def test_query_enclosed_in_quotes(self):
  1885. results = self.make_query(search_filter_query='"foo"')
  1886. assert set(results) == {self.group1}
  1887. results = self.make_query(search_filter_query='"bar"')
  1888. assert set(results) == {self.group2}
  1889. @xfail_if_not_postgres("Wildcard searching only supported in Postgres")
  1890. def test_wildcard(self):
  1891. escaped_event = self.store_event(
  1892. data={
  1893. "fingerprint": ["hello-there"],
  1894. "event_id": "f" * 32,
  1895. "message": "somet[hing]",
  1896. "environment": "production",
  1897. "tags": {"server": "example.net"},
  1898. "timestamp": iso_format(self.base_datetime),
  1899. "stacktrace": {"frames": [{"module": "group1"}]},
  1900. },
  1901. project_id=self.project.id,
  1902. )
  1903. # Note: Adding in `environment:production` so that we make sure we query
  1904. # in both snuba and postgres
  1905. results = self.make_query(search_filter_query="environment:production so*t")
  1906. assert set(results) == {escaped_event.group}
  1907. # Make sure it's case insensitive
  1908. results = self.make_query(search_filter_query="environment:production SO*t")
  1909. assert set(results) == {escaped_event.group}
  1910. results = self.make_query(search_filter_query="environment:production so*zz")
  1911. assert set(results) == set()
  1912. results = self.make_query(search_filter_query="environment:production [hing]")
  1913. assert set(results) == {escaped_event.group}
  1914. results = self.make_query(search_filter_query="environment:production s*]")
  1915. assert set(results) == {escaped_event.group}
  1916. results = self.make_query(search_filter_query="environment:production server:example.*")
  1917. assert set(results) == {self.group1, escaped_event.group}
  1918. results = self.make_query(search_filter_query="environment:production !server:*net")
  1919. assert set(results) == {self.group1}
  1920. # TODO: Disabling tests that use [] syntax for the moment. Re-enable
  1921. # these if we decide to add back in, or remove if this comment has been
  1922. # here a while.
  1923. # results = self.make_query(
  1924. # search_filter_query='environment:production [s][of][mz]',
  1925. # )
  1926. # assert set(results) == set([escaped_event.group])
  1927. # results = self.make_query(
  1928. # search_filter_query='environment:production [z][of][mz]',
  1929. # )
  1930. # assert set(results) == set()
  1931. def test_null_tags(self):
  1932. tag_event = self.store_event(
  1933. data={
  1934. "fingerprint": ["hello-there"],
  1935. "event_id": "f" * 32,
  1936. "message": "something",
  1937. "environment": "production",
  1938. "tags": {"server": "example.net"},
  1939. "timestamp": iso_format(self.base_datetime),
  1940. "stacktrace": {"frames": [{"module": "group1"}]},
  1941. },
  1942. project_id=self.project.id,
  1943. )
  1944. no_tag_event = self.store_event(
  1945. data={
  1946. "fingerprint": ["hello-there-2"],
  1947. "event_id": "5" * 32,
  1948. "message": "something",
  1949. "environment": "production",
  1950. "timestamp": iso_format(self.base_datetime),
  1951. "stacktrace": {"frames": [{"module": "group2"}]},
  1952. },
  1953. project_id=self.project.id,
  1954. )
  1955. results = self.make_query(search_filter_query="environment:production !server:*net")
  1956. assert set(results) == {self.group1, no_tag_event.group}
  1957. results = self.make_query(search_filter_query="environment:production server:*net")
  1958. assert set(results) == {tag_event.group}
  1959. results = self.make_query(search_filter_query="environment:production !server:example.net")
  1960. assert set(results) == {self.group1, no_tag_event.group}
  1961. results = self.make_query(search_filter_query="environment:production server:example.net")
  1962. assert set(results) == {tag_event.group}
  1963. results = self.make_query(search_filter_query="environment:production has:server")
  1964. assert set(results) == {self.group1, tag_event.group}
  1965. results = self.make_query(search_filter_query="environment:production !has:server")
  1966. assert set(results) == {no_tag_event.group}
  1967. def test_null_promoted_tags(self):
  1968. tag_event = self.store_event(
  1969. data={
  1970. "fingerprint": ["hello-there"],
  1971. "event_id": "f" * 32,
  1972. "message": "something",
  1973. "environment": "production",
  1974. "tags": {"logger": "csp"},
  1975. "timestamp": iso_format(self.base_datetime),
  1976. "stacktrace": {"frames": [{"module": "group1"}]},
  1977. },
  1978. project_id=self.project.id,
  1979. )
  1980. no_tag_event = self.store_event(
  1981. data={
  1982. "fingerprint": ["hello-there-2"],
  1983. "event_id": "5" * 32,
  1984. "message": "something",
  1985. "environment": "production",
  1986. "timestamp": iso_format(self.base_datetime),
  1987. "stacktrace": {"frames": [{"module": "group2"}]},
  1988. },
  1989. project_id=self.project.id,
  1990. )
  1991. results = self.make_query(search_filter_query="environment:production !logger:*sp")
  1992. assert set(results) == {self.group1, no_tag_event.group}
  1993. results = self.make_query(search_filter_query="environment:production logger:*sp")
  1994. assert set(results) == {tag_event.group}
  1995. results = self.make_query(search_filter_query="environment:production !logger:csp")
  1996. assert set(results) == {self.group1, no_tag_event.group}
  1997. results = self.make_query(search_filter_query="environment:production logger:csp")
  1998. assert set(results) == {tag_event.group}
  1999. results = self.make_query(search_filter_query="environment:production has:logger")
  2000. assert set(results) == {tag_event.group}
  2001. results = self.make_query(search_filter_query="environment:production !has:logger")
  2002. assert set(results) == {self.group1, no_tag_event.group}
  2003. def test_sort_multi_project(self):
  2004. self.set_up_multi_project()
  2005. results = self.make_query([self.project, self.project2], sort_by="date")
  2006. assert list(results) == [self.group1, self.group_p2, self.group2]
  2007. results = self.make_query([self.project, self.project2], sort_by="new")
  2008. assert list(results) == [self.group2, self.group_p2, self.group1]
  2009. results = self.make_query([self.project, self.project2], sort_by="freq")
  2010. assert list(results) == [self.group1, self.group_p2, self.group2]
  2011. results = self.make_query([self.project, self.project2], sort_by="priority")
  2012. assert list(results) == [
  2013. self.group_p2,
  2014. self.group2,
  2015. self.group1,
  2016. ]
  2017. results = self.make_query([self.project, self.project2], sort_by="user")
  2018. assert list(results) == [self.group1, self.group2, self.group_p2]
  2019. def test_in_syntax_is_invalid(self):
  2020. with pytest.raises(InvalidSearchQuery, match='"in" syntax invalid for "is" search'):
  2021. self.make_query(search_filter_query="is:[unresolved, resolved]")
  2022. def test_first_release_any_or_no_environments(self):
  2023. # test scenarios for tickets:
  2024. # SEN-571
  2025. # ISSUE-432
  2026. # given the following setup:
  2027. #
  2028. # groups table:
  2029. # group first_release
  2030. # A 1
  2031. # B 1
  2032. # C 2
  2033. #
  2034. # groupenvironments table:
  2035. # group environment first_release
  2036. # A staging 1
  2037. # A production 2
  2038. #
  2039. # when querying by first release, the appropriate set of groups should be displayed:
  2040. #
  2041. # first_release: 1
  2042. # env=[]: A, B
  2043. # env=[production, staging]: A
  2044. # env=[staging]: A
  2045. # env=[production]: nothing
  2046. #
  2047. # first_release: 2
  2048. # env=[]: A, C
  2049. # env=[production, staging]: A
  2050. # env=[staging]: nothing
  2051. # env=[production]: A
  2052. # create an issue/group whose events that occur in 2 distinct environments
  2053. group_a_event_1 = self.store_event(
  2054. data={
  2055. "fingerprint": ["group_a"],
  2056. "event_id": "aaa" + ("1" * 29),
  2057. "environment": "example_staging",
  2058. "release": "release_1",
  2059. },
  2060. project_id=self.project.id,
  2061. )
  2062. group_a_event_2 = self.store_event(
  2063. data={
  2064. "fingerprint": ["group_a"],
  2065. "event_id": "aaa" + ("2" * 29),
  2066. "environment": "example_production",
  2067. "release": "release_2",
  2068. },
  2069. project_id=self.project.id,
  2070. )
  2071. group_a = group_a_event_1.group
  2072. # get the environments for group_a
  2073. prod_env = group_a_event_2.get_environment()
  2074. staging_env = group_a_event_1.get_environment()
  2075. # create an issue/group whose event that occur in no environments
  2076. # but will be tied to release release_1
  2077. group_b_event_1 = self.store_event(
  2078. data={
  2079. "fingerprint": ["group_b"],
  2080. "event_id": "bbb" + ("1" * 29),
  2081. "release": "release_1",
  2082. },
  2083. project_id=self.project.id,
  2084. )
  2085. assert group_b_event_1.get_environment().name == "" # has no environment
  2086. group_b = group_b_event_1.group
  2087. # create an issue/group whose event that occur in no environments
  2088. # but will be tied to release release_2
  2089. group_c_event_1 = self.store_event(
  2090. data={
  2091. "fingerprint": ["group_c"],
  2092. "event_id": "ccc" + ("1" * 29),
  2093. "release": "release_2",
  2094. },
  2095. project_id=self.project.id,
  2096. )
  2097. assert group_c_event_1.get_environment().name == "" # has no environment
  2098. group_c = group_c_event_1.group
  2099. # query by release release_1
  2100. results = self.make_query(search_filter_query="first_release:%s" % "release_1")
  2101. assert set(results) == {group_a, group_b}
  2102. results = self.make_query(
  2103. environments=[staging_env, prod_env],
  2104. search_filter_query="first_release:%s" % "release_1",
  2105. )
  2106. assert set(results) == {group_a}
  2107. results = self.make_query(
  2108. environments=[staging_env], search_filter_query="first_release:%s" % "release_1"
  2109. )
  2110. assert set(results) == {group_a}
  2111. results = self.make_query(
  2112. environments=[prod_env], search_filter_query="first_release:%s" % "release_1"
  2113. )
  2114. assert set(results) == set()
  2115. # query by release release_2
  2116. results = self.make_query(search_filter_query="first_release:%s" % "release_2")
  2117. assert set(results) == {group_a, group_c}
  2118. results = self.make_query(
  2119. environments=[staging_env, prod_env],
  2120. search_filter_query="first_release:%s" % "release_2",
  2121. )
  2122. assert set(results) == {group_a}
  2123. results = self.make_query(
  2124. environments=[staging_env], search_filter_query="first_release:%s" % "release_2"
  2125. )
  2126. assert set(results) == set()
  2127. results = self.make_query(
  2128. environments=[prod_env], search_filter_query="first_release:%s" % "release_2"
  2129. )
  2130. assert set(results) == {group_a}
  2131. def test_all_fields_do_not_error(self):
  2132. # Just a sanity check to make sure that all fields can be successfully
  2133. # searched on without returning type errors and other schema related
  2134. # issues.
  2135. def test_query(query):
  2136. try:
  2137. self.make_query(search_filter_query=query)
  2138. except SnubaError as e:
  2139. self.fail(f"Query {query} errored. Error info: {e}")
  2140. for key in SENTRY_SNUBA_MAP:
  2141. if key in ["project.id", "issue.id", "performance.issue_ids"]:
  2142. continue
  2143. test_query("has:%s" % key)
  2144. test_query("!has:%s" % key)
  2145. if key == "error.handled":
  2146. val = 1
  2147. elif key in issue_search_config.numeric_keys:
  2148. val = "123"
  2149. elif key in issue_search_config.date_keys:
  2150. val = self.base_datetime.isoformat()
  2151. elif key in issue_search_config.boolean_keys:
  2152. val = "true"
  2153. elif key in {"trace.span", "trace.parent_span"}:
  2154. val = "abcdef1234abcdef"
  2155. test_query(f"!{key}:{val}")
  2156. else:
  2157. val = "abadcafedeadbeefdeaffeedabadfeed"
  2158. test_query(f"!{key}:{val}")
  2159. test_query(f"{key}:{val}")
  2160. def test_message_negation(self):
  2161. self.store_event(
  2162. data={
  2163. "fingerprint": ["put-me-in-group1"],
  2164. "event_id": "2" * 32,
  2165. "message": "something",
  2166. "timestamp": iso_format(self.base_datetime),
  2167. },
  2168. project_id=self.project.id,
  2169. )
  2170. results = self.make_query(search_filter_query="!message:else")
  2171. results2 = self.make_query(search_filter_query="!message:else")
  2172. assert list(results) == list(results2)
  2173. def test_error_main_thread_true(self):
  2174. myProject = self.create_project(
  2175. name="Foo", slug="foo", teams=[self.team], fire_project_created=True
  2176. )
  2177. event = self.store_event(
  2178. data={
  2179. "event_id": "1" * 32,
  2180. "message": "something",
  2181. "timestamp": iso_format(self.base_datetime),
  2182. "exception": {
  2183. "values": [
  2184. {
  2185. "type": "SyntaxError",
  2186. "value": "hello world",
  2187. "thread_id": 1,
  2188. },
  2189. ],
  2190. },
  2191. "threads": {
  2192. "values": [
  2193. {
  2194. "id": 1,
  2195. "main": True,
  2196. },
  2197. ],
  2198. },
  2199. },
  2200. project_id=myProject.id,
  2201. )
  2202. myGroup = event.groups[0]
  2203. results = self.make_query(
  2204. projects=[myProject],
  2205. search_filter_query="error.main_thread:1",
  2206. sort_by="date",
  2207. )
  2208. assert list(results) == [myGroup]
  2209. def test_error_main_thread_false(self):
  2210. myProject = self.create_project(
  2211. name="Foo2", slug="foo2", teams=[self.team], fire_project_created=True
  2212. )
  2213. event = self.store_event(
  2214. data={
  2215. "event_id": "2" * 32,
  2216. "message": "something",
  2217. "timestamp": iso_format(self.base_datetime),
  2218. "exception": {
  2219. "values": [
  2220. {
  2221. "type": "SyntaxError",
  2222. "value": "hello world",
  2223. "thread_id": 1,
  2224. },
  2225. ],
  2226. },
  2227. "threads": {
  2228. "values": [
  2229. {
  2230. "id": 1,
  2231. "main": False,
  2232. },
  2233. ],
  2234. },
  2235. },
  2236. project_id=myProject.id,
  2237. )
  2238. myGroup = event.groups[0]
  2239. results = self.make_query(
  2240. projects=[myProject],
  2241. search_filter_query="error.main_thread:0",
  2242. sort_by="date",
  2243. )
  2244. assert list(results) == [myGroup]
  2245. def test_error_main_thread_no_results(self):
  2246. myProject = self.create_project(
  2247. name="Foo3", slug="foo3", teams=[self.team], fire_project_created=True
  2248. )
  2249. self.store_event(
  2250. data={
  2251. "event_id": "3" * 32,
  2252. "message": "something",
  2253. "timestamp": iso_format(self.base_datetime),
  2254. "exception": {
  2255. "values": [
  2256. {
  2257. "type": "SyntaxError",
  2258. "value": "hello world",
  2259. "thread_id": 1,
  2260. },
  2261. ],
  2262. },
  2263. "threads": {
  2264. "values": [
  2265. {
  2266. "id": 1,
  2267. },
  2268. ],
  2269. },
  2270. },
  2271. project_id=myProject.id,
  2272. )
  2273. results = self.make_query(
  2274. projects=[myProject],
  2275. search_filter_query="error.main_thread:1",
  2276. sort_by="date",
  2277. )
  2278. assert len(results) == 0
  2279. class EventsBetterPriorityTest(SharedSnubaTest, OccurrenceTestMixin):
  2280. @property
  2281. def backend(self):
  2282. return EventsDatasetSnubaSearchBackend()
  2283. def test_better_priority_sort_old_and_new_events(self):
  2284. """Test that an issue with only one old event is ranked lower than an issue with only one new event"""
  2285. new_project = self.create_project(organization=self.project.organization)
  2286. base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2287. recent_event = self.store_event(
  2288. data={
  2289. "fingerprint": ["put-me-in-recent-group"],
  2290. "event_id": "c" * 32,
  2291. "message": "group1",
  2292. "environment": "production",
  2293. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  2294. "timestamp": iso_format(base_datetime),
  2295. "stacktrace": {"frames": [{"module": "group1"}]},
  2296. },
  2297. project_id=new_project.id,
  2298. )
  2299. old_event = self.store_event(
  2300. data={
  2301. "fingerprint": ["put-me-in-old-group"],
  2302. "event_id": "a" * 32,
  2303. "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.",
  2304. "environment": "production",
  2305. "tags": {"server": "example.com", "sentry:user": "old_event@example.com"},
  2306. "timestamp": iso_format(base_datetime - timedelta(days=20)),
  2307. "stacktrace": {"frames": [{"module": "group1"}]},
  2308. },
  2309. project_id=new_project.id,
  2310. )
  2311. # datetime(2017, 9, 6, 0, 0)
  2312. old_event.data["timestamp"] = 1504656000.0
  2313. weights: PrioritySortWeights = {
  2314. "log_level": 0,
  2315. "has_stacktrace": 0,
  2316. "relative_volume": 1,
  2317. "event_halflife_hours": 4,
  2318. "issue_halflife_hours": 24 * 7,
  2319. "v2": False,
  2320. "norm": False,
  2321. }
  2322. results = self.make_query(
  2323. sort_by="betterPriority",
  2324. projects=[new_project],
  2325. aggregate_kwargs=weights,
  2326. )
  2327. recent_group = Group.objects.get(id=recent_event.group.id)
  2328. old_group = Group.objects.get(id=old_event.group.id)
  2329. assert list(results) == [recent_group, old_group]
  2330. def test_better_priority_sort_v2(self):
  2331. """Test that the v2 formula works."""
  2332. new_project = self.create_project(organization=self.project.organization)
  2333. base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2334. recent_event = self.store_event(
  2335. data={
  2336. "fingerprint": ["put-me-in-recent-group"],
  2337. "event_id": "c" * 32,
  2338. "message": "group1",
  2339. "environment": "production",
  2340. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  2341. "timestamp": iso_format(base_datetime),
  2342. "stacktrace": {"frames": [{"module": "group1"}]},
  2343. },
  2344. project_id=new_project.id,
  2345. )
  2346. old_event = self.store_event(
  2347. data={
  2348. "fingerprint": ["put-me-in-old-group"],
  2349. "event_id": "a" * 32,
  2350. "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.",
  2351. "environment": "production",
  2352. "tags": {"server": "example.com", "sentry:user": "old_event@example.com"},
  2353. "timestamp": iso_format(base_datetime - timedelta(days=20)),
  2354. "stacktrace": {"frames": [{"module": "group1"}]},
  2355. },
  2356. project_id=new_project.id,
  2357. )
  2358. # datetime(2017, 9, 6, 0, 0)
  2359. old_event.data["timestamp"] = 1504656000.0
  2360. weights: PrioritySortWeights = {
  2361. "log_level": 0,
  2362. "has_stacktrace": 0,
  2363. "relative_volume": 1,
  2364. "event_halflife_hours": 4,
  2365. "issue_halflife_hours": 24 * 7,
  2366. "v2": True,
  2367. "norm": False,
  2368. }
  2369. results = self.make_query(
  2370. sort_by="betterPriority",
  2371. projects=[new_project],
  2372. aggregate_kwargs=weights,
  2373. )
  2374. recent_group = Group.objects.get(id=recent_event.group.id)
  2375. old_group = Group.objects.get(id=old_event.group.id)
  2376. assert list(results) == [recent_group, old_group]
  2377. def test_better_priority_log_level_results(self):
  2378. """Test that the scoring results change when we pass in different log level weights"""
  2379. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2380. event1 = self.store_event(
  2381. data={
  2382. "fingerprint": ["put-me-in-group1"],
  2383. "event_id": "c" * 32,
  2384. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2385. "message": "foo",
  2386. "stacktrace": {"frames": [{"module": "group1"}]},
  2387. "environment": "staging",
  2388. "level": "fatal",
  2389. },
  2390. project_id=self.project.id,
  2391. )
  2392. event2 = self.store_event(
  2393. data={
  2394. "fingerprint": ["put-me-in-group2"],
  2395. "event_id": "d" * 32,
  2396. "timestamp": iso_format(base_datetime),
  2397. "message": "bar",
  2398. "stacktrace": {"frames": [{"module": "group2"}]},
  2399. "environment": "staging",
  2400. "level": "error",
  2401. },
  2402. project_id=self.project.id,
  2403. )
  2404. group1 = Group.objects.get(id=event1.group.id)
  2405. group2 = Group.objects.get(id=event2.group.id)
  2406. agg_kwargs = {
  2407. "better_priority": {
  2408. "log_level": 0,
  2409. "has_stacktrace": 0,
  2410. "relative_volume": 1,
  2411. "event_halflife_hours": 4,
  2412. "issue_halflife_hours": 24 * 7,
  2413. "v2": False,
  2414. "norm": False,
  2415. }
  2416. }
  2417. query_executor = self.backend._get_query_executor()
  2418. results_zero_log_level = query_executor.snuba_search(
  2419. start=None,
  2420. end=None,
  2421. project_ids=[self.project.id],
  2422. environment_ids=[],
  2423. sort_field="better_priority",
  2424. organization=self.organization,
  2425. group_ids=[group1.id, group2.id],
  2426. limit=150,
  2427. aggregate_kwargs=agg_kwargs,
  2428. )[0]
  2429. group1_score_before = results_zero_log_level[0][1]
  2430. group2_score_before = results_zero_log_level[1][1]
  2431. # initially group 2's score is higher since it has a more recent event
  2432. assert group2_score_before > group1_score_before
  2433. agg_kwargs["better_priority"].update({"log_level": 5})
  2434. results2 = query_executor.snuba_search(
  2435. start=None,
  2436. end=None,
  2437. project_ids=[self.project.id],
  2438. environment_ids=[],
  2439. sort_field="better_priority",
  2440. organization=self.organization,
  2441. group_ids=[group1.id, group2.id],
  2442. limit=150,
  2443. aggregate_kwargs=agg_kwargs,
  2444. )[0]
  2445. group1_score_after = results2[0][1]
  2446. group2_score_after = results2[1][1]
  2447. # ensure fatal has a higher score than error
  2448. assert group1_score_after > group2_score_after
  2449. def test_better_priority_has_stacktrace_results(self):
  2450. """Test that the scoring results change when we pass in different has_stacktrace weights"""
  2451. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2452. agg_kwargs = {
  2453. "better_priority": {
  2454. "log_level": 0,
  2455. "has_stacktrace": 0,
  2456. "relative_volume": 1,
  2457. "event_halflife_hours": 4,
  2458. "issue_halflife_hours": 24 * 7,
  2459. "v2": False,
  2460. "norm": False,
  2461. }
  2462. }
  2463. query_executor = self.backend._get_query_executor()
  2464. no_stacktrace_event = self.store_event(
  2465. data={
  2466. "event_id": "d" * 32,
  2467. "message": "oh no",
  2468. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2469. },
  2470. project_id=self.project.id,
  2471. )
  2472. group1 = Group.objects.get(id=no_stacktrace_event.group.id)
  2473. stacktrace_event = self.store_event(
  2474. data={
  2475. "event_id": "d" * 32,
  2476. "exception": {
  2477. "values": [
  2478. {
  2479. "type": "AnError",
  2480. "value": "Bad request",
  2481. "stacktrace": {
  2482. "frames": [
  2483. {
  2484. "module": "<my module>",
  2485. },
  2486. ]
  2487. },
  2488. }
  2489. ]
  2490. },
  2491. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2492. },
  2493. project_id=self.project.id,
  2494. )
  2495. group2 = Group.objects.get(id=stacktrace_event.group.id)
  2496. results = query_executor.snuba_search(
  2497. start=None,
  2498. end=None,
  2499. project_ids=[self.project.id],
  2500. environment_ids=[],
  2501. sort_field="better_priority",
  2502. organization=self.organization,
  2503. group_ids=[group1.id, group2.id],
  2504. limit=150,
  2505. aggregate_kwargs=agg_kwargs,
  2506. )[0]
  2507. group1_score = results[0][1]
  2508. group2_score = results[1][1]
  2509. assert group1_score == group2_score
  2510. agg_kwargs["better_priority"].update({"has_stacktrace": 3})
  2511. results = query_executor.snuba_search(
  2512. start=None,
  2513. end=None,
  2514. project_ids=[self.project.id],
  2515. environment_ids=[],
  2516. sort_field="better_priority",
  2517. organization=self.organization,
  2518. group_ids=[group1.id, group2.id],
  2519. limit=150,
  2520. aggregate_kwargs=agg_kwargs,
  2521. )[0]
  2522. group1_score = results[0][1]
  2523. group2_score = results[1][1]
  2524. # check that a group with an event with a stacktrace has a higher weight than one without
  2525. assert group1_score < group2_score
  2526. def test_better_priority_event_halflife_results(self):
  2527. """Test that the scoring results change when we pass in different event halflife weights"""
  2528. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2529. event1 = self.store_event(
  2530. data={
  2531. "fingerprint": ["put-me-in-group1"],
  2532. "event_id": "a" * 32,
  2533. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2534. "message": "foo",
  2535. "stacktrace": {"frames": [{"module": "group1"}]},
  2536. "environment": "staging",
  2537. "level": "fatal",
  2538. },
  2539. project_id=self.project.id,
  2540. )
  2541. event2 = self.store_event(
  2542. data={
  2543. "fingerprint": ["put-me-in-group2"],
  2544. "event_id": "b" * 32,
  2545. "timestamp": iso_format(base_datetime),
  2546. "message": "bar",
  2547. "stacktrace": {"frames": [{"module": "group2"}]},
  2548. "environment": "staging",
  2549. "level": "error",
  2550. },
  2551. project_id=self.project.id,
  2552. )
  2553. group1 = Group.objects.get(id=event1.group.id)
  2554. group2 = Group.objects.get(id=event2.group.id)
  2555. agg_kwargs = {
  2556. "better_priority": {
  2557. "log_level": 0,
  2558. "has_stacktrace": 0,
  2559. "relative_volume": 1,
  2560. "event_halflife_hours": 4,
  2561. "issue_halflife_hours": 24 * 7,
  2562. "v2": False,
  2563. "norm": False,
  2564. }
  2565. }
  2566. query_executor = self.backend._get_query_executor()
  2567. results = query_executor.snuba_search(
  2568. start=None,
  2569. end=None,
  2570. project_ids=[self.project.id],
  2571. environment_ids=[],
  2572. sort_field="better_priority",
  2573. organization=self.organization,
  2574. group_ids=[group1.id, group2.id],
  2575. limit=150,
  2576. aggregate_kwargs=agg_kwargs,
  2577. )[0]
  2578. group1_score_before = results[0][1]
  2579. group2_score_before = results[1][1]
  2580. # initially group 2's score is higher since it has a more recent event
  2581. assert group2_score_before > group1_score_before
  2582. agg_kwargs["better_priority"].update({"event_halflife_hours": 2})
  2583. results = query_executor.snuba_search(
  2584. start=None,
  2585. end=None,
  2586. project_ids=[self.project.id],
  2587. environment_ids=[],
  2588. sort_field="better_priority",
  2589. organization=self.organization,
  2590. group_ids=[group1.id, group2.id],
  2591. limit=150,
  2592. aggregate_kwargs=agg_kwargs,
  2593. )[0]
  2594. group1_score_after = results[0][1]
  2595. group2_score_after = results[1][1]
  2596. assert group1_score_after < group2_score_after
  2597. def test_better_priority_mixed_group_types(self):
  2598. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2599. error_event = self.store_event(
  2600. data={
  2601. "fingerprint": ["put-me-in-group1"],
  2602. "event_id": "a" * 32,
  2603. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2604. "message": "foo",
  2605. "stacktrace": {"frames": [{"module": "group1"}]},
  2606. "environment": "staging",
  2607. "level": "fatal",
  2608. },
  2609. project_id=self.project.id,
  2610. )
  2611. error_group = error_event.group
  2612. profile_event_id = uuid.uuid4().hex
  2613. _, group_info = process_event_and_issue_occurrence(
  2614. self.build_occurrence_data(event_id=profile_event_id),
  2615. {
  2616. "event_id": profile_event_id,
  2617. "project_id": self.project.id,
  2618. "title": "some problem",
  2619. "platform": "python",
  2620. "tags": {"my_tag": "1"},
  2621. "timestamp": before_now(minutes=1).isoformat(),
  2622. "received": before_now(minutes=1).isoformat(),
  2623. },
  2624. )
  2625. profile_group_1 = group_info.group
  2626. agg_kwargs = {
  2627. "better_priority": {
  2628. "log_level": 0,
  2629. "has_stacktrace": 0,
  2630. "relative_volume": 1,
  2631. "event_halflife_hours": 4,
  2632. "issue_halflife_hours": 24 * 7,
  2633. "v2": False,
  2634. "norm": False,
  2635. }
  2636. }
  2637. query_executor = self.backend._get_query_executor()
  2638. with self.feature(
  2639. [
  2640. "organizations:issue-platform",
  2641. ProfileFileIOGroupType.build_visible_feature_name(),
  2642. ]
  2643. ):
  2644. results = query_executor.snuba_search(
  2645. start=None,
  2646. end=None,
  2647. project_ids=[self.project.id],
  2648. environment_ids=[],
  2649. sort_field="better_priority",
  2650. organization=self.organization,
  2651. group_ids=[profile_group_1.id, error_group.id],
  2652. limit=150,
  2653. aggregate_kwargs=agg_kwargs,
  2654. )[0]
  2655. error_group_score = results[0][1]
  2656. profile_group_score = results[1][1]
  2657. assert error_group_score > 0
  2658. assert profile_group_score > 0
  2659. class EventsTransactionsSnubaSearchTest(SharedSnubaTest):
  2660. @property
  2661. def backend(self):
  2662. return EventsDatasetSnubaSearchBackend()
  2663. def setUp(self):
  2664. super().setUp()
  2665. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2666. transaction_event_data = {
  2667. "level": "info",
  2668. "message": "ayoo",
  2669. "type": "transaction",
  2670. "culprit": "app/components/events/eventEntries in map",
  2671. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  2672. }
  2673. with mock.patch(
  2674. "sentry.issues.ingest.send_issue_occurrence_to_eventstream",
  2675. side_effect=send_issue_occurrence_to_eventstream,
  2676. ) as mock_eventstream, mock.patch.object(
  2677. PerformanceRenderBlockingAssetSpanGroupType,
  2678. "noise_config",
  2679. new=NoiseConfig(0, timedelta(minutes=1)),
  2680. ), self.feature(
  2681. "organizations:issue-platform"
  2682. ):
  2683. self.store_event(
  2684. data={
  2685. **transaction_event_data,
  2686. "event_id": "a" * 32,
  2687. "timestamp": iso_format(before_now(minutes=1)),
  2688. "start_timestamp": iso_format(before_now(minutes=1, seconds=5)),
  2689. "tags": {"my_tag": 1},
  2690. "fingerprint": [
  2691. f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group1"
  2692. ],
  2693. },
  2694. project_id=self.project.id,
  2695. )
  2696. self.perf_group_1 = mock_eventstream.call_args[0][2].group
  2697. self.store_event(
  2698. data={
  2699. **transaction_event_data,
  2700. "event_id": "a" * 32,
  2701. "timestamp": iso_format(before_now(minutes=2)),
  2702. "start_timestamp": iso_format(before_now(minutes=2, seconds=5)),
  2703. "tags": {"my_tag": 1},
  2704. "fingerprint": [
  2705. f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group2"
  2706. ],
  2707. },
  2708. project_id=self.project.id,
  2709. )
  2710. self.perf_group_2 = mock_eventstream.call_args[0][2].group
  2711. error_event_data = {
  2712. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  2713. "message": "bar",
  2714. "environment": "staging",
  2715. "tags": {
  2716. "server": "example.com",
  2717. "url": "http://example.com",
  2718. "sentry:user": "event2@example.com",
  2719. "my_tag": 1,
  2720. },
  2721. }
  2722. error_event = self.store_event(
  2723. data={
  2724. **error_event_data,
  2725. "fingerprint": ["put-me-in-error_group_1"],
  2726. "event_id": "c" * 32,
  2727. "stacktrace": {"frames": [{"module": "error_group_1"}]},
  2728. },
  2729. project_id=self.project.id,
  2730. )
  2731. self.error_group_1 = error_event.group
  2732. error_event_2 = self.store_event(
  2733. data={
  2734. **error_event_data,
  2735. "fingerprint": ["put-me-in-error_group_2"],
  2736. "event_id": "d" * 32,
  2737. "stacktrace": {"frames": [{"module": "error_group_2"}]},
  2738. },
  2739. project_id=self.project.id,
  2740. )
  2741. self.error_group_2 = error_event_2.group
  2742. def test_performance_query(self):
  2743. with self.feature(
  2744. [
  2745. "organizations:issue-platform",
  2746. self.perf_group_1.issue_type.build_visible_feature_name(),
  2747. ]
  2748. ):
  2749. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  2750. assert list(results) == [self.perf_group_1, self.perf_group_2]
  2751. results = self.make_query(
  2752. search_filter_query="issue.type:[performance_n_plus_one_db_queries, performance_render_blocking_asset_span] my_tag:1"
  2753. )
  2754. assert list(results) == [self.perf_group_1, self.perf_group_2]
  2755. def test_performance_query_no_duplicates(self):
  2756. # Regression test to catch an issue we had with performance issues showing duplicated in the
  2757. # issue stream. This was caused by us dual writing perf issues to transactions and to the
  2758. # issue platform. We'd end up reading the same issue twice and duplicate it in the response.
  2759. with self.feature(
  2760. [
  2761. "organizations:issue-platform",
  2762. self.perf_group_1.issue_type.build_visible_feature_name(),
  2763. ]
  2764. ):
  2765. results = self.make_query(search_filter_query="!issue.category:error my_tag:1")
  2766. assert list(results) == [self.perf_group_1, self.perf_group_2]
  2767. def test_performance_issue_search_feature_off(self):
  2768. with Feature({"organizations:performance-issues-search": False}):
  2769. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  2770. assert list(results) == []
  2771. with self.feature(
  2772. [
  2773. "organizations:issue-platform",
  2774. self.perf_group_1.issue_type.build_visible_feature_name(),
  2775. ]
  2776. ):
  2777. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  2778. assert list(results) == [self.perf_group_1, self.perf_group_2]
  2779. def test_error_performance_query(self):
  2780. with self.feature(
  2781. [
  2782. "organizations:issue-platform",
  2783. self.perf_group_1.issue_type.build_visible_feature_name(),
  2784. ]
  2785. ):
  2786. results = self.make_query(search_filter_query="my_tag:1")
  2787. assert list(results) == [
  2788. self.perf_group_1,
  2789. self.perf_group_2,
  2790. self.error_group_2,
  2791. self.error_group_1,
  2792. ]
  2793. results = self.make_query(
  2794. search_filter_query="issue.category:[performance, error] my_tag:1"
  2795. )
  2796. assert list(results) == [
  2797. self.perf_group_1,
  2798. self.perf_group_2,
  2799. self.error_group_2,
  2800. self.error_group_1,
  2801. ]
  2802. results = self.make_query(
  2803. search_filter_query="issue.type:[performance_render_blocking_asset_span, error] my_tag:1"
  2804. )
  2805. assert list(results) == [
  2806. self.perf_group_1,
  2807. self.perf_group_2,
  2808. self.error_group_2,
  2809. self.error_group_1,
  2810. ]
  2811. def test_cursor_performance_issues(self):
  2812. with self.feature(
  2813. [
  2814. "organizations:issue-platform",
  2815. self.perf_group_1.issue_type.build_visible_feature_name(),
  2816. ]
  2817. ):
  2818. results = self.make_query(
  2819. projects=[self.project],
  2820. search_filter_query="issue.category:performance my_tag:1",
  2821. sort_by="date",
  2822. limit=1,
  2823. count_hits=True,
  2824. )
  2825. assert list(results) == [self.perf_group_1]
  2826. assert results.hits == 2
  2827. results = self.make_query(
  2828. projects=[self.project],
  2829. search_filter_query="issue.category:performance my_tag:1",
  2830. sort_by="date",
  2831. limit=1,
  2832. cursor=results.next,
  2833. count_hits=True,
  2834. )
  2835. assert list(results) == [self.perf_group_2]
  2836. assert results.hits == 2
  2837. results = self.make_query(
  2838. projects=[self.project],
  2839. search_filter_query="issue.category:performance my_tag:1",
  2840. sort_by="date",
  2841. limit=1,
  2842. cursor=results.next,
  2843. count_hits=True,
  2844. )
  2845. assert list(results) == []
  2846. assert results.hits == 2
  2847. def test_perf_issue_search_message_term_queries_postgres(self):
  2848. from django.db.models import Q
  2849. from sentry.utils import snuba
  2850. transaction_name = "im a little tea pot"
  2851. with mock.patch(
  2852. "sentry.issues.ingest.send_issue_occurrence_to_eventstream",
  2853. side_effect=send_issue_occurrence_to_eventstream,
  2854. ) as mock_eventstream, mock.patch.object(
  2855. PerformanceRenderBlockingAssetSpanGroupType,
  2856. "noise_config",
  2857. new=NoiseConfig(0, timedelta(minutes=1)),
  2858. ), self.feature(
  2859. "organizations:issue-platform"
  2860. ):
  2861. tx = self.store_event(
  2862. data={
  2863. "level": "info",
  2864. "culprit": "app/components/events/eventEntries in map",
  2865. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  2866. "fingerprint": [
  2867. f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"
  2868. ],
  2869. "event_id": "e" * 32,
  2870. "timestamp": iso_format(self.base_datetime),
  2871. "start_timestamp": iso_format(self.base_datetime),
  2872. "type": "transaction",
  2873. "transaction": transaction_name,
  2874. },
  2875. project_id=self.project.id,
  2876. )
  2877. assert "tea" in tx.search_message
  2878. created_group = mock_eventstream.call_args[0][2].group
  2879. find_group = Group.objects.filter(
  2880. Q(type=PerformanceRenderBlockingAssetSpanGroupType.type_id, message__icontains="tea")
  2881. ).first()
  2882. assert created_group == find_group
  2883. with self.feature(
  2884. [
  2885. "organizations:issue-platform",
  2886. created_group.issue_type.build_visible_feature_name(),
  2887. ]
  2888. ):
  2889. result = snuba.raw_query(
  2890. dataset=snuba.Dataset.IssuePlatform,
  2891. start=self.base_datetime - timedelta(hours=1),
  2892. end=self.base_datetime + timedelta(hours=1),
  2893. selected_columns=[
  2894. "event_id",
  2895. "group_id",
  2896. "transaction_name",
  2897. ],
  2898. groupby=None,
  2899. filter_keys={"project_id": [self.project.id], "event_id": [tx.event_id]},
  2900. referrer="_insert_transaction.verify_transaction",
  2901. )
  2902. assert result["data"][0]["transaction_name"] == transaction_name
  2903. assert result["data"][0]["group_id"] == created_group.id
  2904. results = self.make_query(search_filter_query="issue.category:performance tea")
  2905. assert set(results) == {created_group}
  2906. results2 = self.make_query(search_filter_query="tea")
  2907. assert set(results2) == {created_group}
  2908. def test_search_message_error_and_perf_issues(self):
  2909. with mock.patch(
  2910. "sentry.issues.ingest.send_issue_occurrence_to_eventstream",
  2911. side_effect=send_issue_occurrence_to_eventstream,
  2912. ) as mock_eventstream, mock.patch.object(
  2913. PerformanceRenderBlockingAssetSpanGroupType,
  2914. "noise_config",
  2915. new=NoiseConfig(0, timedelta(minutes=1)),
  2916. ), self.feature(
  2917. "organizations:issue-platform"
  2918. ):
  2919. self.store_event(
  2920. data={
  2921. "level": "info",
  2922. "culprit": "app/components/events/eventEntries in map",
  2923. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  2924. "fingerprint": [
  2925. f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"
  2926. ],
  2927. "event_id": "e" * 32,
  2928. "timestamp": iso_format(self.base_datetime),
  2929. "start_timestamp": iso_format(self.base_datetime),
  2930. "type": "transaction",
  2931. "transaction": "/api/0/events",
  2932. },
  2933. project_id=self.project.id,
  2934. )
  2935. perf_issue = mock_eventstream.call_args[0][2].group
  2936. assert perf_issue
  2937. error = self.store_event(
  2938. data={
  2939. "fingerprint": ["another-random-group"],
  2940. "event_id": "d" * 32,
  2941. "message": "Uncaught exception on api /api/0/events",
  2942. "environment": "production",
  2943. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  2944. "timestamp": iso_format(self.base_datetime),
  2945. "stacktrace": {"frames": [{"module": "group1"}]},
  2946. },
  2947. project_id=self.project.id,
  2948. )
  2949. error_issue = error.group
  2950. assert error_issue
  2951. assert error_issue != perf_issue
  2952. with self.feature(
  2953. [
  2954. "organizations:issue-platform",
  2955. perf_issue.issue_type.build_visible_feature_name(),
  2956. ]
  2957. ):
  2958. assert set(self.make_query(search_filter_query="is:unresolved /api/0/events")) == {
  2959. perf_issue,
  2960. error_issue,
  2961. }
  2962. assert set(self.make_query(search_filter_query="/api/0/events")) == {
  2963. error_issue,
  2964. perf_issue,
  2965. }
  2966. def test_compound_message_negation(self):
  2967. self.store_event(
  2968. data={
  2969. "fingerprint": ["put-me-in-group1"],
  2970. "event_id": "2" * 32,
  2971. "message": "something",
  2972. "timestamp": iso_format(self.base_datetime),
  2973. },
  2974. project_id=self.project.id,
  2975. )
  2976. self.store_event(
  2977. data={
  2978. "level": "info",
  2979. "culprit": "app/components/events/eventEntries in map",
  2980. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  2981. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  2982. "event_id": "e" * 32,
  2983. "timestamp": iso_format(self.base_datetime),
  2984. "start_timestamp": iso_format(self.base_datetime),
  2985. "type": "transaction",
  2986. "transaction": "something",
  2987. },
  2988. project_id=self.project.id,
  2989. )
  2990. error_issues_only = self.make_query(
  2991. search_filter_query="!message:else group.category:error"
  2992. )
  2993. error_and_perf_issues = self.make_query(search_filter_query="!message:else")
  2994. assert set(error_and_perf_issues) > set(error_issues_only)
  2995. class EventsGenericSnubaSearchTest(SharedSnubaTest, OccurrenceTestMixin):
  2996. @property
  2997. def backend(self):
  2998. return EventsDatasetSnubaSearchBackend()
  2999. def setUp(self):
  3000. super().setUp()
  3001. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  3002. event_id_1 = uuid.uuid4().hex
  3003. _, group_info = process_event_and_issue_occurrence(
  3004. self.build_occurrence_data(event_id=event_id_1, issue_title="File I/O on Main Thread"),
  3005. {
  3006. "event_id": event_id_1,
  3007. "project_id": self.project.id,
  3008. "title": "some problem",
  3009. "platform": "python",
  3010. "tags": {"my_tag": "1"},
  3011. "timestamp": before_now(minutes=1).isoformat(),
  3012. "received": before_now(minutes=1).isoformat(),
  3013. },
  3014. )
  3015. self.profile_group_1 = group_info.group
  3016. event_id_2 = uuid.uuid4().hex
  3017. _, group_info = process_event_and_issue_occurrence(
  3018. self.build_occurrence_data(
  3019. event_id=event_id_2,
  3020. fingerprint=["put-me-in-group-2"],
  3021. issue_title="File I/O on Main Thread",
  3022. ),
  3023. {
  3024. "event_id": event_id_2,
  3025. "project_id": self.project.id,
  3026. "title": "some other problem",
  3027. "platform": "python",
  3028. "tags": {"my_tag": "1"},
  3029. "timestamp": before_now(minutes=2).isoformat(),
  3030. "received": before_now(minutes=2).isoformat(),
  3031. },
  3032. )
  3033. self.profile_group_2 = group_info.group
  3034. event_id_3 = uuid.uuid4().hex
  3035. process_event_and_issue_occurrence(
  3036. self.build_occurrence_data(event_id=event_id_3, fingerprint=["put-me-in-group-3"]),
  3037. {
  3038. "event_id": event_id_3,
  3039. "project_id": self.project.id,
  3040. "title": "some other problem",
  3041. "platform": "python",
  3042. "tags": {"my_tag": "2"},
  3043. "timestamp": before_now(minutes=2).isoformat(),
  3044. "message_timestamp": before_now(minutes=2).isoformat(),
  3045. },
  3046. )
  3047. error_event_data = {
  3048. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  3049. "message": "bar",
  3050. "environment": "staging",
  3051. "tags": {
  3052. "server": "example.com",
  3053. "url": "http://example.com",
  3054. "sentry:user": "event2@example.com",
  3055. "my_tag": 1,
  3056. },
  3057. }
  3058. error_event = self.store_event(
  3059. data={
  3060. **error_event_data,
  3061. "fingerprint": ["put-me-in-error_group_1"],
  3062. "event_id": "c" * 32,
  3063. "stacktrace": {"frames": [{"module": "error_group_1"}]},
  3064. },
  3065. project_id=self.project.id,
  3066. )
  3067. self.error_group_1 = error_event.group
  3068. error_event_2 = self.store_event(
  3069. data={
  3070. **error_event_data,
  3071. "fingerprint": ["put-me-in-error_group_2"],
  3072. "event_id": "d" * 32,
  3073. "stacktrace": {"frames": [{"module": "error_group_2"}]},
  3074. },
  3075. project_id=self.project.id,
  3076. )
  3077. self.error_group_2 = error_event_2.group
  3078. def test_no_feature(self):
  3079. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  3080. assert list(results) == []
  3081. def test_generic_query(self):
  3082. with self.feature(
  3083. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  3084. ):
  3085. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  3086. assert list(results) == [self.profile_group_1, self.profile_group_2]
  3087. results = self.make_query(
  3088. search_filter_query="issue.type:profile_file_io_main_thread my_tag:1"
  3089. )
  3090. assert list(results) == [self.profile_group_1, self.profile_group_2]
  3091. def test_generic_query_message(self):
  3092. with self.feature(
  3093. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  3094. ):
  3095. results = self.make_query(search_filter_query="File I/O")
  3096. assert list(results) == [self.profile_group_1, self.profile_group_2]
  3097. def test_generic_query_perf(self):
  3098. event_id = uuid.uuid4().hex
  3099. group_type = PerformanceNPlusOneGroupType
  3100. with mock.patch.object(
  3101. PerformanceNPlusOneGroupType, "noise_config", new=NoiseConfig(0, timedelta(minutes=1))
  3102. ):
  3103. with self.feature(group_type.build_ingest_feature_name()):
  3104. _, group_info = process_event_and_issue_occurrence(
  3105. self.build_occurrence_data(
  3106. event_id=event_id, type=group_type.type_id, fingerprint=["some perf issue"]
  3107. ),
  3108. {
  3109. "event_id": event_id,
  3110. "project_id": self.project.id,
  3111. "title": "some problem",
  3112. "platform": "python",
  3113. "tags": {"my_tag": "2"},
  3114. "timestamp": before_now(minutes=1).isoformat(),
  3115. "received": before_now(minutes=1).isoformat(),
  3116. },
  3117. )
  3118. results = self.make_query(search_filter_query="issue.category:performance my_tag:2")
  3119. assert list(results) == []
  3120. with self.feature(
  3121. [
  3122. "organizations:issue-platform",
  3123. group_type.build_visible_feature_name(),
  3124. "organizations:performance-issues-search",
  3125. ]
  3126. ):
  3127. results = self.make_query(search_filter_query="issue.category:performance my_tag:2")
  3128. assert list(results) == [group_info.group]
  3129. def test_error_generic_query(self):
  3130. with self.feature(
  3131. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  3132. ):
  3133. results = self.make_query(search_filter_query="my_tag:1")
  3134. assert list(results) == [
  3135. self.profile_group_1,
  3136. self.profile_group_2,
  3137. self.error_group_2,
  3138. self.error_group_1,
  3139. ]
  3140. results = self.make_query(
  3141. search_filter_query="issue.category:[performance, error] my_tag:1"
  3142. )
  3143. assert list(results) == [
  3144. self.profile_group_1,
  3145. self.profile_group_2,
  3146. self.error_group_2,
  3147. self.error_group_1,
  3148. ]
  3149. results = self.make_query(
  3150. search_filter_query="issue.type:[profile_file_io_main_thread, error] my_tag:1"
  3151. )
  3152. assert list(results) == [
  3153. self.profile_group_1,
  3154. self.profile_group_2,
  3155. self.error_group_2,
  3156. self.error_group_1,
  3157. ]
  3158. def test_cursor_profile_issues(self):
  3159. with self.feature(
  3160. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  3161. ):
  3162. results = self.make_query(
  3163. projects=[self.project],
  3164. search_filter_query="issue.category:performance my_tag:1",
  3165. sort_by="date",
  3166. limit=1,
  3167. count_hits=True,
  3168. )
  3169. assert list(results) == [self.profile_group_1]
  3170. assert results.hits == 2
  3171. results = self.make_query(
  3172. projects=[self.project],
  3173. search_filter_query="issue.category:performance my_tag:1",
  3174. sort_by="date",
  3175. limit=1,
  3176. cursor=results.next,
  3177. count_hits=True,
  3178. )
  3179. assert list(results) == [self.profile_group_2]
  3180. assert results.hits == 2
  3181. results = self.make_query(
  3182. projects=[self.project],
  3183. search_filter_query="issue.category:performance my_tag:1",
  3184. sort_by="date",
  3185. limit=1,
  3186. cursor=results.next,
  3187. count_hits=True,
  3188. )
  3189. assert list(results) == []
  3190. assert results.hits == 2
  3191. def test_rejected_filters(self):
  3192. """
  3193. Any queries with `error.handled` or `error.unhandled` filters querying the search_issues dataset
  3194. should be rejected and return empty results.
  3195. """
  3196. with self.feature(
  3197. ["organizations:issue-platform", ProfileFileIOGroupType.build_visible_feature_name()]
  3198. ):
  3199. results = self.make_query(
  3200. projects=[self.project],
  3201. search_filter_query="issue.category:performance error.unhandled:0",
  3202. sort_by="date",
  3203. limit=1,
  3204. count_hits=True,
  3205. )
  3206. results2 = self.make_query(
  3207. projects=[self.project],
  3208. search_filter_query="issue.category:performance error.unhandled:1",
  3209. sort_by="date",
  3210. limit=1,
  3211. count_hits=True,
  3212. )
  3213. result3 = self.make_query(
  3214. projects=[self.project],
  3215. search_filter_query="issue.category:performance error.handled:0",
  3216. sort_by="date",
  3217. limit=1,
  3218. count_hits=True,
  3219. )
  3220. results4 = self.make_query(
  3221. projects=[self.project],
  3222. search_filter_query="issue.category:performance error.handled:1",
  3223. sort_by="date",
  3224. limit=1,
  3225. count_hits=True,
  3226. )
  3227. results5 = self.make_query(
  3228. projects=[self.project],
  3229. search_filter_query="issue.category:performance error.main_thread:0",
  3230. sort_by="date",
  3231. limit=1,
  3232. count_hits=True,
  3233. )
  3234. results6 = self.make_query(
  3235. projects=[self.project],
  3236. search_filter_query="issue.category:performance error.main_thread:1",
  3237. sort_by="date",
  3238. limit=1,
  3239. count_hits=True,
  3240. )
  3241. assert (
  3242. list(results)
  3243. == list(results2)
  3244. == list(result3)
  3245. == list(results4)
  3246. == list(results5)
  3247. == list(results6)
  3248. == []
  3249. )
  3250. class CdcEventsSnubaSearchTest(SharedSnubaTest):
  3251. @property
  3252. def backend(self):
  3253. return CdcEventsDatasetSnubaSearchBackend()
  3254. def setUp(self):
  3255. super().setUp()
  3256. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  3257. self.event1 = self.store_event(
  3258. data={
  3259. "fingerprint": ["put-me-in-group1"],
  3260. "event_id": "a" * 32,
  3261. "environment": "production",
  3262. "timestamp": iso_format(self.base_datetime - timedelta(days=21)),
  3263. "tags": {"sentry:user": "user1"},
  3264. },
  3265. project_id=self.project.id,
  3266. )
  3267. self.env1 = self.event1.get_environment()
  3268. self.group1 = self.event1.group
  3269. self.event3 = self.store_event(
  3270. data={
  3271. "fingerprint": ["put-me-in-group1"],
  3272. "environment": "staging",
  3273. "timestamp": iso_format(self.base_datetime),
  3274. "tags": {"sentry:user": "user2"},
  3275. },
  3276. project_id=self.project.id,
  3277. )
  3278. self.event2 = self.store_event(
  3279. data={
  3280. "fingerprint": ["put-me-in-group2"],
  3281. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  3282. "environment": "staging",
  3283. "tags": {"sentry:user": "user1"},
  3284. },
  3285. project_id=self.project.id,
  3286. )
  3287. self.group2 = self.event2.group
  3288. self.env2 = self.event2.get_environment()
  3289. def run_test(
  3290. self,
  3291. search_filter_query,
  3292. expected_groups,
  3293. expected_hits,
  3294. projects=None,
  3295. environments=None,
  3296. sort_by="date",
  3297. limit=None,
  3298. count_hits=False,
  3299. date_from=None,
  3300. date_to=None,
  3301. cursor=None,
  3302. ):
  3303. results = self.make_query(
  3304. projects=projects,
  3305. search_filter_query=search_filter_query,
  3306. environments=environments,
  3307. sort_by=sort_by,
  3308. limit=limit,
  3309. count_hits=count_hits,
  3310. date_from=date_from,
  3311. date_to=date_to,
  3312. cursor=cursor,
  3313. )
  3314. assert list(results) == expected_groups
  3315. assert results.hits == expected_hits
  3316. return results
  3317. def test(self):
  3318. self.run_test("is:unresolved", [self.group1, self.group2], None)
  3319. def test_invalid(self):
  3320. with pytest.raises(InvalidQueryForExecutor):
  3321. self.make_query(search_filter_query="is:unresolved abc:123")
  3322. def test_resolved_group(self):
  3323. self.group2.status = GroupStatus.RESOLVED
  3324. self.group2.substatus = None
  3325. self.group2.save()
  3326. self.store_group(self.group2)
  3327. self.run_test("is:unresolved", [self.group1], None)
  3328. self.run_test("is:resolved", [self.group2], None)
  3329. self.run_test("is:unresolved is:resolved", [], None)
  3330. def test_environment(self):
  3331. self.run_test("is:unresolved", [self.group1], None, environments=[self.env1])
  3332. self.run_test("is:unresolved", [self.group1, self.group2], None, environments=[self.env2])
  3333. def test_sort_times_seen(self):
  3334. self.run_test(
  3335. "is:unresolved",
  3336. [self.group1, self.group2],
  3337. None,
  3338. sort_by="freq",
  3339. date_from=self.base_datetime - timedelta(days=30),
  3340. )
  3341. self.store_event(
  3342. data={
  3343. "fingerprint": ["put-me-in-group2"],
  3344. "timestamp": iso_format(self.base_datetime - timedelta(days=15)),
  3345. },
  3346. project_id=self.project.id,
  3347. )
  3348. self.store_event(
  3349. data={
  3350. "fingerprint": ["put-me-in-group2"],
  3351. "timestamp": iso_format(self.base_datetime - timedelta(days=10)),
  3352. "tags": {"sentry:user": "user2"},
  3353. },
  3354. project_id=self.project.id,
  3355. )
  3356. self.run_test(
  3357. "is:unresolved",
  3358. [self.group2, self.group1],
  3359. None,
  3360. sort_by="freq",
  3361. # Change the date range to bust the
  3362. date_from=self.base_datetime - timedelta(days=29),
  3363. )
  3364. def test_sort_first_seen(self):
  3365. self.run_test(
  3366. "is:unresolved",
  3367. [self.group2, self.group1],
  3368. None,
  3369. sort_by="new",
  3370. date_from=self.base_datetime - timedelta(days=30),
  3371. )
  3372. group3 = self.store_event(
  3373. data={
  3374. "fingerprint": ["put-me-in-group3"],
  3375. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3376. },
  3377. project_id=self.project.id,
  3378. ).group
  3379. self.run_test(
  3380. "is:unresolved",
  3381. [group3, self.group2, self.group1],
  3382. None,
  3383. sort_by="new",
  3384. # Change the date range to bust the
  3385. date_from=self.base_datetime - timedelta(days=29),
  3386. )
  3387. def test_sort_user(self):
  3388. self.run_test(
  3389. "is:unresolved",
  3390. [self.group1, self.group2],
  3391. None,
  3392. sort_by="user",
  3393. date_from=self.base_datetime - timedelta(days=30),
  3394. )
  3395. self.store_event(
  3396. data={
  3397. "fingerprint": ["put-me-in-group2"],
  3398. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3399. "tags": {"sentry:user": "user2"},
  3400. },
  3401. project_id=self.project.id,
  3402. )
  3403. self.store_event(
  3404. data={
  3405. "fingerprint": ["put-me-in-group2"],
  3406. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3407. "tags": {"sentry:user": "user2"},
  3408. },
  3409. project_id=self.project.id,
  3410. )
  3411. self.store_event(
  3412. data={
  3413. "fingerprint": ["put-me-in-group1"],
  3414. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3415. "tags": {"sentry:user": "user1"},
  3416. },
  3417. project_id=self.project.id,
  3418. )
  3419. self.store_event(
  3420. data={
  3421. "fingerprint": ["put-me-in-group1"],
  3422. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3423. "tags": {"sentry:user": "user1"},
  3424. },
  3425. project_id=self.project.id,
  3426. )
  3427. # Test group with no users, which can return a null count
  3428. group3 = self.store_event(
  3429. data={
  3430. "fingerprint": ["put-me-in-group3"],
  3431. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3432. },
  3433. project_id=self.project.id,
  3434. ).group
  3435. self.run_test(
  3436. "is:unresolved",
  3437. [self.group2, self.group1, group3],
  3438. None,
  3439. sort_by="user",
  3440. # Change the date range to bust the
  3441. date_from=self.base_datetime - timedelta(days=29),
  3442. )
  3443. def test_sort_priority(self):
  3444. self.run_test(
  3445. "is:unresolved",
  3446. [self.group1, self.group2],
  3447. None,
  3448. sort_by="priority",
  3449. date_from=self.base_datetime - timedelta(days=30),
  3450. )
  3451. def test_cursor(self):
  3452. group3 = self.store_event(
  3453. data={
  3454. "fingerprint": ["put-me-in-group3"],
  3455. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  3456. "tags": {"sentry:user": "user2"},
  3457. },
  3458. project_id=self.project.id,
  3459. ).group
  3460. group4 = self.store_event(
  3461. data={
  3462. "fingerprint": ["put-me-in-group7"],
  3463. "timestamp": iso_format(self.base_datetime + timedelta(days=2)),
  3464. "tags": {"sentry:user": "user2"},
  3465. },
  3466. project_id=self.project.id,
  3467. ).group
  3468. results = self.run_test("is:unresolved", [group4], 4, limit=1, count_hits=True)
  3469. results = self.run_test(
  3470. "is:unresolved", [group3], 4, limit=1, cursor=results.next, count_hits=True
  3471. )
  3472. results = self.run_test(
  3473. "is:unresolved", [group4], 4, limit=1, cursor=results.prev, count_hits=True
  3474. )
  3475. self.run_test(
  3476. "is:unresolved", [group3, self.group1], 4, limit=2, cursor=results.next, count_hits=True
  3477. )
  3478. def test_rechecking(self):
  3479. self.group2.status = GroupStatus.RESOLVED
  3480. self.group2.substatus = None
  3481. self.group2.save()
  3482. # Explicitly avoid calling `store_group` here. This means that Clickhouse will still see
  3483. # this group as `UNRESOLVED` and it will be returned in the snuba results. This group
  3484. # should still be filtered out by our recheck.
  3485. self.run_test("is:unresolved", [self.group1], None)