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.group1, self.group2]
  313. results = self.make_query(sort_by="user")
  314. assert list(results) == [self.group1, self.group2]
  315. def test_better_priority_sort(self):
  316. with self.feature("organizations:issue-list-better-priority-sort"):
  317. weights: PrioritySortWeights = {
  318. "log_level": 5,
  319. "has_stacktrace": 5,
  320. "relative_volume": 1,
  321. "event_halflife_hours": 4,
  322. "issue_halflife_hours": 24 * 7,
  323. "v2": False,
  324. "norm": False,
  325. }
  326. results = self.make_query(
  327. sort_by="betterPriority",
  328. aggregate_kwargs=weights,
  329. )
  330. assert list(results) == [self.group2, self.group1]
  331. def test_sort_with_environment(self):
  332. for dt in [
  333. self.group1.first_seen + timedelta(days=1),
  334. self.group1.first_seen + timedelta(days=2),
  335. self.group1.last_seen + timedelta(days=1),
  336. ]:
  337. self.store_event(
  338. data={
  339. "fingerprint": ["put-me-in-group2"],
  340. "timestamp": iso_format(dt),
  341. "stacktrace": {"frames": [{"module": "group2"}]},
  342. "environment": "production",
  343. "message": "group2",
  344. },
  345. project_id=self.project.id,
  346. )
  347. results = self.make_query(environments=[self.environments["production"]], sort_by="date")
  348. assert list(results) == [self.group2, self.group1]
  349. results = self.make_query(environments=[self.environments["production"]], sort_by="new")
  350. assert list(results) == [self.group2, self.group1]
  351. results = self.make_query(environments=[self.environments["production"]], sort_by="freq")
  352. assert list(results) == [self.group2, self.group1]
  353. results = self.make_query(
  354. environments=[self.environments["production"]], sort_by="priority"
  355. )
  356. assert list(results) == [self.group2, self.group1]
  357. results = self.make_query(environments=[self.environments["production"]], sort_by="user")
  358. assert list(results) == [self.group1, self.group2]
  359. def test_status(self):
  360. results = self.make_query(search_filter_query="is:unresolved")
  361. assert set(results) == {self.group1}
  362. results = self.make_query(search_filter_query="is:resolved")
  363. assert set(results) == {self.group2}
  364. event_3 = self.store_event(
  365. data={
  366. "fingerprint": ["put-me-in-group3"],
  367. "event_id": "c" * 32,
  368. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  369. },
  370. project_id=self.project.id,
  371. )
  372. group_3 = event_3.group
  373. group_3.status = GroupStatus.MUTED
  374. group_3.substatus = None
  375. group_3.save()
  376. self.run_test_query("status:[unresolved, resolved]", [self.group1, self.group2], [group_3])
  377. self.run_test_query("status:[resolved, muted]", [self.group2, group_3], [self.group1])
  378. def test_substatus(self):
  379. with Feature("organizations:escalating-issues"):
  380. results = self.make_query(search_filter_query="is:ongoing")
  381. assert set(results) == {self.group1}
  382. with pytest.raises(
  383. InvalidSearchQuery, match="The substatus filter is not supported for this organization"
  384. ):
  385. self.make_query(search_filter_query="is:ongoing")
  386. def test_category(self):
  387. results = self.make_query(search_filter_query="issue.category:error")
  388. assert set(results) == {self.group1, self.group2}
  389. event_3 = self.store_event(
  390. data={
  391. "fingerprint": ["put-me-in-group3"],
  392. "event_id": "c" * 32,
  393. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  394. },
  395. project_id=self.project.id,
  396. )
  397. group_3 = event_3.group
  398. group_3.update(type=PerformanceNPlusOneGroupType.type_id)
  399. results = self.make_query(search_filter_query="issue.category:performance")
  400. assert set(results) == {group_3}
  401. results = self.make_query(search_filter_query="issue.category:[error, performance]")
  402. assert set(results) == {self.group1, self.group2, group_3}
  403. with pytest.raises(InvalidSearchQuery):
  404. self.make_query(search_filter_query="issue.category:hellboy")
  405. def test_not_perf_category(self):
  406. results = self.make_query(search_filter_query="issue.category:error foo")
  407. assert set(results) == {self.group1}
  408. not_results = self.make_query(search_filter_query="!issue.category:performance foo")
  409. assert set(not_results) == {self.group1}
  410. def test_type(self):
  411. results = self.make_query(search_filter_query="issue.type:error")
  412. assert set(results) == {self.group1, self.group2}
  413. event_3 = self.store_event(
  414. data={
  415. "fingerprint": ["put-me-in-group3"],
  416. "event_id": "c" * 32,
  417. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  418. "type": PerformanceNPlusOneGroupType.type_id,
  419. },
  420. project_id=self.project.id,
  421. )
  422. group_3 = event_3.group
  423. group_3.update(type=PerformanceNPlusOneGroupType.type_id)
  424. results = self.make_query(
  425. search_filter_query="issue.type:performance_n_plus_one_db_queries"
  426. )
  427. assert set(results) == {group_3}
  428. event_4 = self.store_event(
  429. data={
  430. "fingerprint": ["put-me-in-group4"],
  431. "event_id": "d" * 32,
  432. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  433. },
  434. project_id=self.project.id,
  435. )
  436. group_4 = event_4.group
  437. group_4.update(type=PerformanceRenderBlockingAssetSpanGroupType.type_id)
  438. results = self.make_query(
  439. search_filter_query="issue.type:performance_render_blocking_asset_span"
  440. )
  441. assert set(results) == {group_4}
  442. results = self.make_query(
  443. search_filter_query="issue.type:[performance_render_blocking_asset_span, performance_n_plus_one_db_queries, error]"
  444. )
  445. assert set(results) == {self.group1, self.group2, group_3, group_4}
  446. with pytest.raises(InvalidSearchQuery):
  447. self.make_query(search_filter_query="issue.type:performance_i_dont_exist")
  448. def test_status_with_environment(self):
  449. results = self.make_query(
  450. environments=[self.environments["production"]], search_filter_query="is:unresolved"
  451. )
  452. assert set(results) == {self.group1}
  453. results = self.make_query(
  454. environments=[self.environments["staging"]], search_filter_query="is:resolved"
  455. )
  456. assert set(results) == {self.group2}
  457. results = self.make_query(
  458. environments=[self.environments["production"]], search_filter_query="is:resolved"
  459. )
  460. assert set(results) == set()
  461. def test_tags(self):
  462. results = self.make_query(search_filter_query="environment:staging")
  463. assert set(results) == {self.group2}
  464. results = self.make_query(search_filter_query="environment:example.com")
  465. assert set(results) == set()
  466. results = self.make_query(search_filter_query="has:environment")
  467. assert set(results) == {self.group2, self.group1}
  468. results = self.make_query(search_filter_query="environment:staging server:example.com")
  469. assert set(results) == {self.group2}
  470. results = self.make_query(search_filter_query='url:"http://example.com"')
  471. assert set(results) == {self.group2}
  472. results = self.make_query(search_filter_query="environment:staging has:server")
  473. assert set(results) == {self.group2}
  474. results = self.make_query(search_filter_query="environment:staging server:bar.example.com")
  475. assert set(results) == set()
  476. def test_tags_with_environment(self):
  477. results = self.make_query(
  478. environments=[self.environments["production"]], search_filter_query="server:example.com"
  479. )
  480. assert set(results) == {self.group1}
  481. results = self.make_query(
  482. environments=[self.environments["staging"]], search_filter_query="server:example.com"
  483. )
  484. assert set(results) == {self.group2}
  485. results = self.make_query(
  486. environments=[self.environments["staging"]], search_filter_query="has:server"
  487. )
  488. assert set(results) == {self.group2}
  489. results = self.make_query(
  490. environments=[self.environments["production"]],
  491. search_filter_query='url:"http://example.com"',
  492. )
  493. assert set(results) == set()
  494. results = self.make_query(
  495. environments=[self.environments["staging"]],
  496. search_filter_query='url:"http://example.com"',
  497. )
  498. assert set(results) == {self.group2}
  499. results = self.make_query(
  500. environments=[self.environments["staging"]],
  501. search_filter_query="server:bar.example.com",
  502. )
  503. assert set(results) == set()
  504. def test_bookmarked_by(self):
  505. results = self.make_query(search_filter_query="bookmarks:%s" % self.user.username)
  506. assert set(results) == {self.group2}
  507. def test_bookmarked_by_in_syntax(self):
  508. self.run_test_query(f"bookmarks:[{self.user.username}]", [self.group2], [self.group1])
  509. user_2 = self.create_user()
  510. GroupBookmark.objects.create(
  511. user_id=user_2.id, group=self.group1, project=self.group2.project
  512. )
  513. self.run_test_query(
  514. f"bookmarks:[{self.user.username}, {user_2.username}]", [self.group2, self.group1], []
  515. )
  516. def test_bookmarked_by_with_environment(self):
  517. results = self.make_query(
  518. environments=[self.environments["staging"]],
  519. search_filter_query="bookmarks:%s" % self.user.username,
  520. )
  521. assert set(results) == {self.group2}
  522. results = self.make_query(
  523. environments=[self.environments["production"]],
  524. search_filter_query="bookmarks:%s" % self.user.username,
  525. )
  526. assert set(results) == set()
  527. def test_search_filter_query_with_custom_priority_tag(self):
  528. priority = "high"
  529. self.store_event(
  530. data={
  531. "fingerprint": ["put-me-in-group2"],
  532. "timestamp": iso_format(self.group2.first_seen + timedelta(days=1)),
  533. "stacktrace": {"frames": [{"module": "group2"}]},
  534. "message": "group2",
  535. "tags": {"priority": priority},
  536. },
  537. project_id=self.project.id,
  538. )
  539. results = self.make_query(search_filter_query="priority:%s" % priority)
  540. assert set(results) == {self.group2}
  541. def test_search_filter_query_with_custom_priority_tag_and_priority_sort(self):
  542. priority = "high"
  543. for i in range(1, 3):
  544. self.store_event(
  545. data={
  546. "fingerprint": ["put-me-in-group1"],
  547. "timestamp": iso_format(self.group2.last_seen + timedelta(days=i)),
  548. "stacktrace": {"frames": [{"module": "group1"}]},
  549. "message": "group1",
  550. "tags": {"priority": priority},
  551. },
  552. project_id=self.project.id,
  553. )
  554. self.store_event(
  555. data={
  556. "fingerprint": ["put-me-in-group2"],
  557. "timestamp": iso_format(self.group2.last_seen + timedelta(days=2)),
  558. "stacktrace": {"frames": [{"module": "group2"}]},
  559. "message": "group2",
  560. "tags": {"priority": priority},
  561. },
  562. project_id=self.project.id,
  563. )
  564. results = self.make_query(search_filter_query="priority:%s" % priority, sort_by="priority")
  565. assert list(results) == [self.group1, self.group2]
  566. def test_search_tag_overlapping_with_internal_fields(self):
  567. # Using a tag of email overlaps with the promoted user.email column in events.
  568. # We don't want to bypass public schema limits in issue search.
  569. self.store_event(
  570. data={
  571. "fingerprint": ["put-me-in-group2"],
  572. "timestamp": iso_format(self.group2.first_seen + timedelta(days=1)),
  573. "stacktrace": {"frames": [{"module": "group2"}]},
  574. "message": "group2",
  575. "tags": {"email": "tags@example.com"},
  576. },
  577. project_id=self.project.id,
  578. )
  579. results = self.make_query(search_filter_query="email:tags@example.com")
  580. assert set(results) == {self.group2}
  581. def test_project(self):
  582. results = self.make_query([self.create_project(name="other")])
  583. assert set(results) == set()
  584. def test_pagination(self):
  585. for options_set in [
  586. {"snuba.search.min-pre-snuba-candidates": None},
  587. {"snuba.search.min-pre-snuba-candidates": 500},
  588. ]:
  589. with self.options(options_set):
  590. results = self.backend.query([self.project], limit=1, sort_by="date")
  591. assert set(results) == {self.group1}
  592. assert not results.prev.has_results
  593. assert results.next.has_results
  594. results = self.backend.query(
  595. [self.project], cursor=results.next, limit=1, sort_by="date"
  596. )
  597. assert set(results) == {self.group2}
  598. assert results.prev.has_results
  599. assert not results.next.has_results
  600. # note: previous cursor
  601. results = self.backend.query(
  602. [self.project], cursor=results.prev, limit=1, sort_by="date"
  603. )
  604. assert set(results) == {self.group1}
  605. assert results.prev.has_results
  606. assert results.next.has_results
  607. # note: previous cursor, paging too far into 0 results
  608. results = self.backend.query(
  609. [self.project], cursor=results.prev, limit=1, sort_by="date"
  610. )
  611. assert set(results) == set()
  612. assert not results.prev.has_results
  613. assert results.next.has_results
  614. results = self.backend.query(
  615. [self.project], cursor=results.next, limit=1, sort_by="date"
  616. )
  617. assert set(results) == {self.group1}
  618. assert results.prev.has_results
  619. assert results.next.has_results
  620. results = self.backend.query(
  621. [self.project], cursor=results.next, limit=1, sort_by="date"
  622. )
  623. assert set(results) == {self.group2}
  624. assert results.prev.has_results
  625. assert not results.next.has_results
  626. results = self.backend.query(
  627. [self.project], cursor=results.next, limit=1, sort_by="date"
  628. )
  629. assert set(results) == set()
  630. assert results.prev.has_results
  631. assert not results.next.has_results
  632. def test_pagination_with_environment(self):
  633. for dt in [
  634. self.group1.first_seen + timedelta(days=1),
  635. self.group1.first_seen + timedelta(days=2),
  636. self.group1.last_seen + timedelta(days=1),
  637. ]:
  638. self.store_event(
  639. data={
  640. "fingerprint": ["put-me-in-group2"],
  641. "timestamp": iso_format(dt),
  642. "environment": "production",
  643. "message": "group2",
  644. "stacktrace": {"frames": [{"module": "group2"}]},
  645. },
  646. project_id=self.project.id,
  647. )
  648. results = self.backend.query(
  649. [self.project],
  650. environments=[self.environments["production"]],
  651. sort_by="date",
  652. limit=1,
  653. count_hits=True,
  654. )
  655. assert list(results) == [self.group2]
  656. assert results.hits == 2
  657. results = self.backend.query(
  658. [self.project],
  659. environments=[self.environments["production"]],
  660. sort_by="date",
  661. limit=1,
  662. cursor=results.next,
  663. count_hits=True,
  664. )
  665. assert list(results) == [self.group1]
  666. assert results.hits == 2
  667. results = self.backend.query(
  668. [self.project],
  669. environments=[self.environments["production"]],
  670. sort_by="date",
  671. limit=1,
  672. cursor=results.next,
  673. count_hits=True,
  674. )
  675. assert list(results) == []
  676. assert results.hits == 2
  677. def test_age_filter(self):
  678. results = self.make_query(
  679. search_filter_query="firstSeen:>=%s" % date_to_query_format(self.group2.first_seen)
  680. )
  681. assert set(results) == {self.group2}
  682. results = self.make_query(
  683. search_filter_query="firstSeen:<=%s"
  684. % date_to_query_format(self.group1.first_seen + timedelta(minutes=1))
  685. )
  686. assert set(results) == {self.group1}
  687. results = self.make_query(
  688. search_filter_query="firstSeen:>=%s firstSeen:<=%s"
  689. % (
  690. date_to_query_format(self.group1.first_seen),
  691. date_to_query_format(self.group1.first_seen + timedelta(minutes=1)),
  692. )
  693. )
  694. assert set(results) == {self.group1}
  695. def test_age_filter_with_environment(self):
  696. # add time instead to make it greater than or less than as needed.
  697. group1_first_seen = GroupEnvironment.objects.get(
  698. environment=self.environments["production"], group=self.group1
  699. ).first_seen
  700. results = self.make_query(
  701. environments=[self.environments["production"]],
  702. search_filter_query="firstSeen:>=%s" % date_to_query_format(group1_first_seen),
  703. )
  704. assert set(results) == {self.group1}
  705. results = self.make_query(
  706. environments=[self.environments["production"]],
  707. search_filter_query="firstSeen:<=%s" % date_to_query_format(group1_first_seen),
  708. )
  709. assert set(results) == {self.group1}
  710. results = self.make_query(
  711. environments=[self.environments["production"]],
  712. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  713. )
  714. assert set(results) == set()
  715. self.store_event(
  716. data={
  717. "fingerprint": ["put-me-in-group1"],
  718. "timestamp": iso_format(group1_first_seen + timedelta(days=1)),
  719. "message": "group1",
  720. "stacktrace": {"frames": [{"module": "group1"}]},
  721. "environment": "development",
  722. },
  723. project_id=self.project.id,
  724. )
  725. results = self.make_query(
  726. environments=[self.environments["production"]],
  727. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  728. )
  729. assert set(results) == set()
  730. results = self.make_query(
  731. environments=[Environment.objects.get(name="development")],
  732. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  733. )
  734. assert set(results) == {self.group1}
  735. def test_times_seen_filter(self):
  736. results = self.make_query([self.project], search_filter_query="times_seen:2")
  737. assert set(results) == {self.group1}
  738. results = self.make_query([self.project], search_filter_query="times_seen:>=2")
  739. assert set(results) == {self.group1}
  740. results = self.make_query([self.project], search_filter_query="times_seen:<=1")
  741. assert set(results) == {self.group2}
  742. def test_last_seen_filter(self):
  743. results = self.make_query(
  744. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen)
  745. )
  746. assert set(results) == {self.group1}
  747. results = self.make_query(
  748. search_filter_query="lastSeen:>=%s lastSeen:<=%s"
  749. % (
  750. date_to_query_format(self.group1.last_seen),
  751. date_to_query_format(self.group1.last_seen + timedelta(minutes=1)),
  752. )
  753. )
  754. assert set(results) == {self.group1}
  755. def test_last_seen_filter_with_environment(self):
  756. results = self.make_query(
  757. environments=[self.environments["production"]],
  758. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen),
  759. )
  760. assert set(results) == {self.group1}
  761. results = self.make_query(
  762. environments=[self.environments["production"]],
  763. search_filter_query="lastSeen:<=%s" % date_to_query_format(self.group1.last_seen),
  764. )
  765. assert set(results) == {self.group1}
  766. results = self.make_query(
  767. environments=[self.environments["production"]],
  768. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  769. )
  770. assert set(results) == set()
  771. self.store_event(
  772. data={
  773. "fingerprint": ["put-me-in-group1"],
  774. "timestamp": iso_format(self.group1.last_seen + timedelta(days=1)),
  775. "message": "group1",
  776. "stacktrace": {"frames": [{"module": "group1"}]},
  777. "environment": "development",
  778. },
  779. project_id=self.project.id,
  780. )
  781. self.group1.update(last_seen=self.group1.last_seen + timedelta(days=1))
  782. results = self.make_query(
  783. environments=[self.environments["production"]],
  784. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  785. )
  786. assert set(results) == set()
  787. results = self.make_query(
  788. environments=[Environment.objects.get(name="development")],
  789. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  790. )
  791. assert set(results) == set()
  792. results = self.make_query(
  793. environments=[Environment.objects.get(name="development")],
  794. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen),
  795. )
  796. assert set(results) == {self.group1}
  797. def test_date_filter(self):
  798. results = self.make_query(
  799. date_from=self.event2.datetime,
  800. search_filter_query="timestamp:>=%s" % date_to_query_format(self.event2.datetime),
  801. )
  802. assert set(results) == {self.group1, self.group2}
  803. results = self.make_query(
  804. date_to=self.event1.datetime + timedelta(minutes=1),
  805. search_filter_query="timestamp:<=%s"
  806. % date_to_query_format(self.event1.datetime + timedelta(minutes=1)),
  807. )
  808. assert set(results) == {self.group1}
  809. results = self.make_query(
  810. date_from=self.event1.datetime,
  811. date_to=self.event2.datetime + timedelta(minutes=1),
  812. search_filter_query="timestamp:>=%s timestamp:<=%s"
  813. % (
  814. date_to_query_format(self.event1.datetime),
  815. date_to_query_format(self.event2.datetime + timedelta(minutes=1)),
  816. ),
  817. )
  818. assert set(results) == {self.group1, self.group2}
  819. # Test with `Z` utc marker, should be equivalent
  820. results = self.make_query(
  821. date_from=self.event1.datetime,
  822. date_to=self.event2.datetime + timedelta(minutes=1),
  823. search_filter_query="timestamp:>=%s timestamp:<=%s"
  824. % (
  825. date_to_query_format(self.event1.datetime) + "Z",
  826. date_to_query_format(self.event2.datetime + timedelta(minutes=1)) + "Z",
  827. ),
  828. )
  829. assert set(results) == {self.group1, self.group2}
  830. def test_date_filter_with_environment(self):
  831. results = self.backend.query(
  832. [self.project],
  833. environments=[self.environments["production"]],
  834. date_from=self.event2.datetime,
  835. )
  836. assert set(results) == {self.group1}
  837. results = self.backend.query(
  838. [self.project],
  839. environments=[self.environments["production"]],
  840. date_to=self.event1.datetime + timedelta(minutes=1),
  841. )
  842. assert set(results) == {self.group1}
  843. results = self.backend.query(
  844. [self.project],
  845. environments=[self.environments["staging"]],
  846. date_from=self.event1.datetime,
  847. date_to=self.event2.datetime + timedelta(minutes=1),
  848. )
  849. assert set(results) == {self.group2}
  850. def test_linked(self):
  851. linked_group1 = self.create_group_with_integration_external_issue()
  852. linked_group2 = self.create_group_with_platform_external_issue()
  853. results = self.make_query(search_filter_query="is:unlinked")
  854. assert set(results) == {self.group1, self.group2}
  855. results = self.make_query(search_filter_query="is:linked")
  856. assert set(results) == {linked_group1, linked_group2}
  857. def test_linked_with_only_integration_external_issue(self):
  858. linked_group = self.create_group_with_integration_external_issue()
  859. results = self.make_query(search_filter_query="is:unlinked")
  860. assert set(results) == {self.group1, self.group2}
  861. results = self.make_query(search_filter_query="is:linked")
  862. assert set(results) == {linked_group}
  863. def test_linked_with_only_platform_external_issue(self):
  864. linked_group = self.create_group_with_platform_external_issue()
  865. results = self.make_query(search_filter_query="is:unlinked")
  866. assert set(results) == {self.group1, self.group2}
  867. results = self.make_query(search_filter_query="is:linked")
  868. assert set(results) == {linked_group}
  869. def test_linked_with_environment(self):
  870. linked_group1 = self.create_group_with_integration_external_issue(environment="production")
  871. linked_group2 = self.create_group_with_platform_external_issue(environment="staging")
  872. results = self.make_query(
  873. environments=[self.environments["production"]], search_filter_query="is:unlinked"
  874. )
  875. assert set(results) == {self.group1}
  876. results = self.make_query(
  877. environments=[self.environments["staging"]], search_filter_query="is:unlinked"
  878. )
  879. assert set(results) == {self.group2}
  880. results = self.make_query(
  881. environments=[self.environments["production"]], search_filter_query="is:linked"
  882. )
  883. assert set(results) == {linked_group1}
  884. results = self.make_query(
  885. environments=[self.environments["staging"]], search_filter_query="is:linked"
  886. )
  887. assert set(results) == {linked_group2}
  888. def test_unassigned(self):
  889. results = self.make_query(search_filter_query="is:unassigned")
  890. assert set(results) == {self.group1}
  891. results = self.make_query(search_filter_query="is:assigned")
  892. assert set(results) == {self.group2}
  893. def test_unassigned_with_environment(self):
  894. results = self.make_query(
  895. environments=[self.environments["production"]], search_filter_query="is:unassigned"
  896. )
  897. assert set(results) == {self.group1}
  898. results = self.make_query(
  899. environments=[self.environments["staging"]], search_filter_query="is:assigned"
  900. )
  901. assert set(results) == {self.group2}
  902. results = self.make_query(
  903. environments=[self.environments["production"]], search_filter_query="is:assigned"
  904. )
  905. assert set(results) == set()
  906. def test_assigned_to(self):
  907. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  908. assert set(results) == {self.group2}
  909. # test team assignee
  910. ga = GroupAssignee.objects.get(
  911. user_id=self.user.id, group=self.group2, project=self.group2.project
  912. )
  913. ga.update(team=self.team, user_id=None)
  914. assert GroupAssignee.objects.get(id=ga.id).user_id is None
  915. with Feature({"organizations:assign-to-me": False}):
  916. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  917. assert set(results) == {self.group2}
  918. # test when there should be no results
  919. other_user = self.create_user()
  920. results = self.make_query(search_filter_query="assigned:%s" % other_user.username)
  921. assert set(results) == set()
  922. owner = self.create_user()
  923. self.create_member(
  924. organization=self.project.organization, user=owner, role="owner", teams=[]
  925. )
  926. # test that owners don't see results for all teams
  927. results = self.make_query(search_filter_query="assigned:%s" % owner.username)
  928. assert set(results) == set()
  929. def test_assigned_to_me_my_teams(self):
  930. my_team_group = self.store_event(
  931. data={
  932. "fingerprint": ["put-me-in-group-my-teams"],
  933. "event_id": "f" * 32,
  934. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  935. "message": "baz",
  936. "environment": "staging",
  937. "tags": {
  938. "server": "example.com",
  939. "url": "http://example.com",
  940. "sentry:user": "event2@example.com",
  941. },
  942. "level": "error",
  943. },
  944. project_id=self.project.id,
  945. ).group
  946. # assign the issue to my team instead of me
  947. GroupAssignee.objects.create(
  948. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  949. )
  950. with Feature({"organizations:assign-to-me": False}):
  951. self.run_test_query(
  952. "assigned:me",
  953. [my_team_group, self.group2],
  954. user=self.user,
  955. )
  956. assert not GroupAssignee.objects.filter(
  957. user_id=self.user.id, group=my_team_group
  958. ).exists()
  959. with self.feature("organizations:assign-to-me"):
  960. self.run_test_query(
  961. "assigned:me",
  962. [self.group2],
  963. user=self.user,
  964. )
  965. self.run_test_query(
  966. "assigned:my_teams",
  967. [my_team_group],
  968. user=self.user,
  969. )
  970. def test_assigned_to_me_my_teams_in_syntax(self):
  971. my_team_group = self.store_event(
  972. data={
  973. "fingerprint": ["put-me-in-group-my-teams"],
  974. "event_id": "f" * 32,
  975. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  976. "message": "baz",
  977. "environment": "staging",
  978. "tags": {
  979. "server": "example.com",
  980. "url": "http://example.com",
  981. "sentry:user": "event2@example.com",
  982. },
  983. "level": "error",
  984. },
  985. project_id=self.project.id,
  986. ).group
  987. # assign the issue to my team instead of me
  988. GroupAssignee.objects.create(
  989. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  990. )
  991. with Feature({"organizations:assign-to-me": False}):
  992. self.run_test_query(
  993. "assigned:[me]",
  994. [my_team_group, self.group2],
  995. user=self.user,
  996. )
  997. assert not GroupAssignee.objects.filter(
  998. user_id=self.user.id, group=my_team_group
  999. ).exists()
  1000. with self.feature("organizations:assign-to-me"):
  1001. self.run_test_query(
  1002. "assigned:[me]",
  1003. [self.group2],
  1004. user=self.user,
  1005. )
  1006. self.run_test_query(
  1007. "assigned:[my_teams]",
  1008. [my_team_group],
  1009. user=self.user,
  1010. )
  1011. self.run_test_query(
  1012. "assigned:[me, my_teams]",
  1013. [self.group2, my_team_group],
  1014. user=self.user,
  1015. )
  1016. def test_assigned_to_in_syntax(self):
  1017. group_3 = self.store_event(
  1018. data={
  1019. "fingerprint": ["put-me-in-group3"],
  1020. "event_id": "c" * 32,
  1021. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1022. },
  1023. project_id=self.project.id,
  1024. ).group
  1025. group_3.status = GroupStatus.MUTED
  1026. group_3.substatus = None
  1027. group_3.save()
  1028. other_user = self.create_user()
  1029. self.run_test_query(
  1030. f"assigned:[{self.user.username}, {other_user.username}]",
  1031. [self.group2],
  1032. [self.group1, group_3],
  1033. )
  1034. GroupAssignee.objects.create(project=self.project, group=group_3, user_id=other_user.id)
  1035. self.run_test_query(
  1036. f"assigned:[{self.user.username}, {other_user.username}]",
  1037. [self.group2, group_3],
  1038. [self.group1],
  1039. )
  1040. self.run_test_query(
  1041. f"assigned:[#{self.team.slug}, {other_user.username}]",
  1042. [group_3],
  1043. [self.group1, self.group2],
  1044. )
  1045. ga_2 = GroupAssignee.objects.get(
  1046. user_id=self.user.id, group=self.group2, project=self.group2.project
  1047. )
  1048. ga_2.update(team=self.team, user_id=None)
  1049. with Feature({"organizations:assign-to-me": False}):
  1050. self.run_test_query(
  1051. f"assigned:[{self.user.username}, {other_user.username}]",
  1052. [self.group2, group_3],
  1053. [self.group1],
  1054. )
  1055. self.run_test_query(
  1056. f"assigned:[#{self.team.slug}, {other_user.username}]",
  1057. [self.group2, group_3],
  1058. [self.group1],
  1059. )
  1060. self.run_test_query(
  1061. f"assigned:[me, none, {other_user.username}]",
  1062. [self.group1, self.group2, group_3],
  1063. [],
  1064. )
  1065. def test_assigned_or_suggested_in_syntax(self):
  1066. Group.objects.all().delete()
  1067. group = self.store_event(
  1068. data={
  1069. "timestamp": iso_format(before_now(seconds=180)),
  1070. "fingerprint": ["group-1"],
  1071. },
  1072. project_id=self.project.id,
  1073. ).group
  1074. group1 = self.store_event(
  1075. data={
  1076. "timestamp": iso_format(before_now(seconds=185)),
  1077. "fingerprint": ["group-2"],
  1078. },
  1079. project_id=self.project.id,
  1080. ).group
  1081. group2 = self.store_event(
  1082. data={
  1083. "timestamp": iso_format(before_now(seconds=190)),
  1084. "fingerprint": ["group-3"],
  1085. },
  1086. project_id=self.project.id,
  1087. ).group
  1088. assigned_group = self.store_event(
  1089. data={
  1090. "timestamp": iso_format(before_now(seconds=195)),
  1091. "fingerprint": ["group-4"],
  1092. },
  1093. project_id=self.project.id,
  1094. ).group
  1095. assigned_to_other_group = self.store_event(
  1096. data={
  1097. "timestamp": iso_format(before_now(seconds=195)),
  1098. "fingerprint": ["group-5"],
  1099. },
  1100. project_id=self.project.id,
  1101. ).group
  1102. self.run_test_query(
  1103. "assigned_or_suggested:[me]",
  1104. [],
  1105. [group, group1, group2, assigned_group, assigned_to_other_group],
  1106. )
  1107. GroupOwner.objects.create(
  1108. group=assigned_to_other_group,
  1109. project=self.project,
  1110. organization=self.organization,
  1111. type=0,
  1112. team_id=None,
  1113. user_id=self.user.id,
  1114. )
  1115. GroupOwner.objects.create(
  1116. group=group,
  1117. project=self.project,
  1118. organization=self.organization,
  1119. type=0,
  1120. team_id=None,
  1121. user_id=self.user.id,
  1122. )
  1123. self.run_test_query(
  1124. "assigned_or_suggested:[me]",
  1125. [group, assigned_to_other_group],
  1126. [group1, group2, assigned_group],
  1127. )
  1128. # 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)
  1129. other_user = self.create_user("other@user.com", is_superuser=False)
  1130. GroupAssignee.objects.create(
  1131. group=assigned_to_other_group,
  1132. project=self.project,
  1133. user_id=other_user.id,
  1134. )
  1135. self.run_test_query(
  1136. "assigned_or_suggested:[me]",
  1137. [group],
  1138. [group1, group2, assigned_group, assigned_to_other_group],
  1139. )
  1140. self.run_test_query(
  1141. f"assigned_or_suggested:[{other_user.email}]",
  1142. [assigned_to_other_group],
  1143. [group, group1, group2, assigned_group],
  1144. )
  1145. GroupAssignee.objects.create(
  1146. group=assigned_group, project=self.project, user_id=self.user.id
  1147. )
  1148. self.run_test_query(
  1149. f"assigned_or_suggested:[{self.user.email}]",
  1150. [assigned_group, group],
  1151. )
  1152. GroupOwner.objects.create(
  1153. group=group,
  1154. project=self.project,
  1155. organization=self.organization,
  1156. type=0,
  1157. team_id=self.team.id,
  1158. user_id=None,
  1159. )
  1160. self.run_test_query(
  1161. f"assigned_or_suggested:[#{self.team.slug}]",
  1162. [group],
  1163. )
  1164. self.run_test_query(
  1165. "assigned_or_suggested:[me, none]",
  1166. [group, group1, group2, assigned_group],
  1167. [assigned_to_other_group],
  1168. )
  1169. not_me = self.create_user(email="notme@sentry.io")
  1170. GroupOwner.objects.create(
  1171. group=group2,
  1172. project=self.project,
  1173. organization=self.organization,
  1174. type=0,
  1175. team_id=None,
  1176. user_id=not_me.id,
  1177. )
  1178. self.run_test_query(
  1179. "assigned_or_suggested:[me, none]",
  1180. [group, group1, assigned_group],
  1181. [assigned_to_other_group, group2],
  1182. )
  1183. GroupOwner.objects.filter(group=group, user_id=self.user.id).delete()
  1184. self.run_test_query(
  1185. f"assigned_or_suggested:[me, none, #{self.team.slug}]",
  1186. [group, group1, assigned_group],
  1187. [assigned_to_other_group, group2],
  1188. )
  1189. self.run_test_query(
  1190. f"assigned_or_suggested:[me, none, #{self.team.slug}, {not_me.email}]",
  1191. [group, group1, assigned_group, group2],
  1192. [assigned_to_other_group],
  1193. )
  1194. def test_assigned_or_suggested_my_teams(self):
  1195. Group.objects.all().delete()
  1196. group = self.store_event(
  1197. data={
  1198. "timestamp": iso_format(before_now(seconds=180)),
  1199. "fingerprint": ["group-1"],
  1200. },
  1201. project_id=self.project.id,
  1202. ).group
  1203. group1 = self.store_event(
  1204. data={
  1205. "timestamp": iso_format(before_now(seconds=185)),
  1206. "fingerprint": ["group-2"],
  1207. },
  1208. project_id=self.project.id,
  1209. ).group
  1210. group2 = self.store_event(
  1211. data={
  1212. "timestamp": iso_format(before_now(seconds=190)),
  1213. "fingerprint": ["group-3"],
  1214. },
  1215. project_id=self.project.id,
  1216. ).group
  1217. assigned_group = self.store_event(
  1218. data={
  1219. "timestamp": iso_format(before_now(seconds=195)),
  1220. "fingerprint": ["group-4"],
  1221. },
  1222. project_id=self.project.id,
  1223. ).group
  1224. assigned_to_other_group = self.store_event(
  1225. data={
  1226. "timestamp": iso_format(before_now(seconds=195)),
  1227. "fingerprint": ["group-5"],
  1228. },
  1229. project_id=self.project.id,
  1230. ).group
  1231. my_team_group = self.store_event(
  1232. data={
  1233. "fingerprint": ["put-me-in-group-my-teams"],
  1234. "event_id": "f" * 32,
  1235. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1236. "message": "baz",
  1237. "environment": "staging",
  1238. "tags": {
  1239. "server": "example.com",
  1240. "url": "http://example.com",
  1241. "sentry:user": "event2@example.com",
  1242. },
  1243. "level": "error",
  1244. },
  1245. project_id=self.project.id,
  1246. ).group
  1247. # after the change to me -> (me + my_teams)
  1248. with self.feature("organizations:assign-to-me"):
  1249. self.run_test_query(
  1250. "assigned_or_suggested:me",
  1251. [],
  1252. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1253. user=self.user,
  1254. )
  1255. self.run_test_query(
  1256. "assigned_or_suggested:my_teams",
  1257. [],
  1258. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1259. user=self.user,
  1260. )
  1261. GroupOwner.objects.create(
  1262. group=assigned_to_other_group,
  1263. project=self.project,
  1264. organization=self.organization,
  1265. type=0,
  1266. team_id=None,
  1267. user_id=self.user.id,
  1268. )
  1269. GroupOwner.objects.create(
  1270. group=group,
  1271. project=self.project,
  1272. organization=self.organization,
  1273. type=0,
  1274. team_id=None,
  1275. user_id=self.user.id,
  1276. )
  1277. GroupAssignee.objects.create(
  1278. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  1279. )
  1280. # after the change to me -> (me + my_teams)
  1281. with self.feature("organizations:assign-to-me"):
  1282. self.run_test_query(
  1283. "assigned_or_suggested:me",
  1284. [group, assigned_to_other_group],
  1285. [group1, group2, assigned_group, my_team_group],
  1286. user=self.user,
  1287. )
  1288. self.run_test_query(
  1289. "assigned_or_suggested:my_teams",
  1290. [my_team_group],
  1291. [group, group1, group2, assigned_group, assigned_to_other_group],
  1292. user=self.user,
  1293. )
  1294. # 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)
  1295. other_user = self.create_user("other@user.com", is_superuser=False)
  1296. GroupAssignee.objects.create(
  1297. group=assigned_to_other_group,
  1298. project=self.project,
  1299. user_id=other_user.id,
  1300. )
  1301. # after the change to me -> (me + my_teams)
  1302. with self.feature("organizations:assign-to-me"):
  1303. self.run_test_query(
  1304. "assigned_or_suggested:me",
  1305. [group],
  1306. [group1, group2, assigned_group, my_team_group, assigned_to_other_group],
  1307. user=self.user,
  1308. )
  1309. self.run_test_query(
  1310. "assigned_or_suggested:my_teams",
  1311. [my_team_group],
  1312. [group, group1, group2, assigned_group, assigned_to_other_group],
  1313. user=self.user,
  1314. )
  1315. self.run_test_query(
  1316. f"assigned_or_suggested:{other_user.email}",
  1317. [assigned_to_other_group],
  1318. [group, group1, group2, assigned_group, my_team_group],
  1319. user=self.user,
  1320. )
  1321. GroupAssignee.objects.create(
  1322. group=assigned_group, project=self.project, user_id=self.user.id
  1323. )
  1324. # after the change to me -> (me + my_teams)
  1325. with self.feature("organizations:assign-to-me"):
  1326. self.run_test_query(
  1327. f"assigned_or_suggested:{self.user.email}",
  1328. [assigned_group, group],
  1329. [group1, group2, my_team_group, assigned_to_other_group],
  1330. user=self.user,
  1331. )
  1332. GroupOwner.objects.create(
  1333. group=group,
  1334. project=self.project,
  1335. organization=self.organization,
  1336. type=0,
  1337. team_id=self.team.id,
  1338. user_id=None,
  1339. )
  1340. # after the change to me -> (me + my_teams)
  1341. with self.feature("organizations:assign-to-me"):
  1342. self.run_test_query(
  1343. f"assigned_or_suggested:#{self.team.slug}",
  1344. [group, my_team_group],
  1345. [group1, group2, assigned_group, assigned_to_other_group],
  1346. user=self.user,
  1347. )
  1348. def test_assigned_or_suggested_my_teams_in_syntax(self):
  1349. Group.objects.all().delete()
  1350. group = self.store_event(
  1351. data={
  1352. "timestamp": iso_format(before_now(seconds=180)),
  1353. "fingerprint": ["group-1"],
  1354. },
  1355. project_id=self.project.id,
  1356. ).group
  1357. group1 = self.store_event(
  1358. data={
  1359. "timestamp": iso_format(before_now(seconds=185)),
  1360. "fingerprint": ["group-2"],
  1361. },
  1362. project_id=self.project.id,
  1363. ).group
  1364. group2 = self.store_event(
  1365. data={
  1366. "timestamp": iso_format(before_now(seconds=190)),
  1367. "fingerprint": ["group-3"],
  1368. },
  1369. project_id=self.project.id,
  1370. ).group
  1371. assigned_group = self.store_event(
  1372. data={
  1373. "timestamp": iso_format(before_now(seconds=195)),
  1374. "fingerprint": ["group-4"],
  1375. },
  1376. project_id=self.project.id,
  1377. ).group
  1378. assigned_to_other_group = self.store_event(
  1379. data={
  1380. "timestamp": iso_format(before_now(seconds=195)),
  1381. "fingerprint": ["group-5"],
  1382. },
  1383. project_id=self.project.id,
  1384. ).group
  1385. my_team_group = self.store_event(
  1386. data={
  1387. "fingerprint": ["put-me-in-group-my-teams"],
  1388. "event_id": "f" * 32,
  1389. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1390. "message": "baz",
  1391. "environment": "staging",
  1392. "tags": {
  1393. "server": "example.com",
  1394. "url": "http://example.com",
  1395. "sentry:user": "event2@example.com",
  1396. },
  1397. "level": "error",
  1398. },
  1399. project_id=self.project.id,
  1400. ).group
  1401. # after the change to me -> (me + my_teams)
  1402. with self.feature("organizations:assign-to-me"):
  1403. self.run_test_query(
  1404. "assigned_or_suggested:[me]",
  1405. [],
  1406. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1407. user=self.user,
  1408. )
  1409. self.run_test_query(
  1410. "assigned_or_suggested:[my_teams]",
  1411. [],
  1412. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1413. user=self.user,
  1414. )
  1415. self.run_test_query(
  1416. "assigned_or_suggested:[me, my_teams]",
  1417. [],
  1418. [group, group1, group2, assigned_group, assigned_to_other_group, my_team_group],
  1419. user=self.user,
  1420. )
  1421. GroupOwner.objects.create(
  1422. group=assigned_to_other_group,
  1423. project=self.project,
  1424. organization=self.organization,
  1425. type=0,
  1426. team_id=None,
  1427. user_id=self.user.id,
  1428. )
  1429. GroupOwner.objects.create(
  1430. group=group,
  1431. project=self.project,
  1432. organization=self.organization,
  1433. type=0,
  1434. team_id=None,
  1435. user_id=self.user.id,
  1436. )
  1437. GroupAssignee.objects.create(
  1438. user_id=None, team_id=self.team.id, group=my_team_group, project=my_team_group.project
  1439. )
  1440. # after the change to me -> (me + my_teams)
  1441. with self.feature("organizations:assign-to-me"):
  1442. self.run_test_query(
  1443. "assigned_or_suggested:[me]",
  1444. [group, assigned_to_other_group],
  1445. [group1, group2, assigned_group, my_team_group],
  1446. user=self.user,
  1447. )
  1448. self.run_test_query(
  1449. "assigned_or_suggested:[my_teams]",
  1450. [my_team_group],
  1451. [group, group1, group2, assigned_group, assigned_to_other_group],
  1452. user=self.user,
  1453. )
  1454. self.run_test_query(
  1455. "assigned_or_suggested:[me, my_teams]",
  1456. [group, assigned_to_other_group, my_team_group],
  1457. [group1, group2, assigned_group],
  1458. user=self.user,
  1459. )
  1460. # 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)
  1461. other_user = self.create_user("other@user.com", is_superuser=False)
  1462. GroupAssignee.objects.create(
  1463. group=assigned_to_other_group,
  1464. project=self.project,
  1465. user_id=other_user.id,
  1466. )
  1467. # after the change to me -> (me + my_teams)
  1468. with self.feature("organizations:assign-to-me"):
  1469. self.run_test_query(
  1470. "assigned_or_suggested:[me]",
  1471. [group],
  1472. [group1, group2, assigned_group, my_team_group, assigned_to_other_group],
  1473. user=self.user,
  1474. )
  1475. self.run_test_query(
  1476. "assigned_or_suggested:[my_teams]",
  1477. [my_team_group],
  1478. [group, group1, group2, assigned_group, assigned_to_other_group],
  1479. user=self.user,
  1480. )
  1481. self.run_test_query(
  1482. "assigned_or_suggested:[me, my_teams]",
  1483. [group, my_team_group],
  1484. [group1, group2, assigned_group, assigned_to_other_group],
  1485. user=self.user,
  1486. )
  1487. self.run_test_query(
  1488. f"assigned_or_suggested:[{other_user.email}]",
  1489. [assigned_to_other_group],
  1490. [group, group1, group2, assigned_group, my_team_group],
  1491. user=self.user,
  1492. )
  1493. GroupAssignee.objects.create(
  1494. group=assigned_group, project=self.project, user_id=self.user.id
  1495. )
  1496. # after the change to me -> (me + my_teams)
  1497. with self.feature("organizations:assign-to-me"):
  1498. self.run_test_query(
  1499. f"assigned_or_suggested:[{self.user.email}]",
  1500. [assigned_group, group],
  1501. [group1, group2, my_team_group, assigned_to_other_group],
  1502. user=self.user,
  1503. )
  1504. GroupOwner.objects.create(
  1505. group=group,
  1506. project=self.project,
  1507. organization=self.organization,
  1508. type=0,
  1509. team_id=self.team.id,
  1510. user_id=None,
  1511. )
  1512. # after the change to me -> (me + my_teams)
  1513. with self.feature("organizations:assign-to-me"):
  1514. self.run_test_query(
  1515. f"assigned_or_suggested:[#{self.team.slug}]",
  1516. [group, my_team_group],
  1517. [group1, group2, assigned_group, assigned_to_other_group],
  1518. user=self.user,
  1519. )
  1520. self.run_test_query(
  1521. "assigned_or_suggested:[me, none]",
  1522. [group, group1, group2, assigned_group],
  1523. [my_team_group, assigned_to_other_group],
  1524. user=self.user,
  1525. )
  1526. self.run_test_query(
  1527. "assigned_or_suggested:[my_teams, none]",
  1528. [group, group1, group2, my_team_group],
  1529. [assigned_to_other_group, assigned_group],
  1530. user=self.user,
  1531. )
  1532. self.run_test_query(
  1533. "assigned_or_suggested:[me, my_teams, none]",
  1534. [group, group1, group2, my_team_group, assigned_group],
  1535. [assigned_to_other_group],
  1536. user=self.user,
  1537. )
  1538. not_me = self.create_user(email="notme@sentry.io")
  1539. GroupOwner.objects.create(
  1540. group=group2,
  1541. project=self.project,
  1542. organization=self.organization,
  1543. type=0,
  1544. team_id=None,
  1545. user_id=not_me.id,
  1546. )
  1547. # after the change to me -> (me + my_teams)
  1548. with self.feature("organizations:assign-to-me"):
  1549. self.run_test_query(
  1550. "assigned_or_suggested:[me, none]",
  1551. [group, group1, assigned_group],
  1552. [group2, my_team_group, assigned_to_other_group],
  1553. user=self.user,
  1554. )
  1555. self.run_test_query(
  1556. "assigned_or_suggested:[my_teams, none]",
  1557. [group, group1, my_team_group],
  1558. [group2, assigned_group, assigned_to_other_group],
  1559. user=self.user,
  1560. )
  1561. self.run_test_query(
  1562. "assigned_or_suggested:[me, my_teams, none]",
  1563. [group, group1, my_team_group, assigned_group],
  1564. [group2, assigned_to_other_group],
  1565. user=self.user,
  1566. )
  1567. GroupOwner.objects.filter(group=group, user_id=self.user.id).delete()
  1568. # after the change to me -> (me + my_teams)
  1569. with self.feature("organizations:assign-to-me"):
  1570. self.run_test_query(
  1571. f"assigned_or_suggested:[me, none, #{self.team.slug}]",
  1572. [group, group1, assigned_group, my_team_group],
  1573. [assigned_to_other_group, group2],
  1574. user=self.user,
  1575. )
  1576. self.run_test_query(
  1577. f"assigned_or_suggested:[my_teams, none, #{self.team.slug}]",
  1578. [group, group1, my_team_group],
  1579. [assigned_to_other_group, group2, assigned_group],
  1580. user=self.user,
  1581. )
  1582. self.run_test_query(
  1583. f"assigned_or_suggested:[me, my_teams, none, #{self.team.slug}]",
  1584. [group, group1, my_team_group, assigned_group],
  1585. [assigned_to_other_group, group2],
  1586. user=self.user,
  1587. )
  1588. self.run_test_query(
  1589. f"assigned_or_suggested:[me, none, #{self.team.slug}, {not_me.email}]",
  1590. [group, group1, group2, assigned_group, my_team_group],
  1591. [assigned_to_other_group],
  1592. user=self.user,
  1593. )
  1594. self.run_test_query(
  1595. f"assigned_or_suggested:[my_teams, none, #{self.team.slug}, {not_me.email}]",
  1596. [group, group1, group2, my_team_group],
  1597. [assigned_to_other_group, assigned_group],
  1598. user=self.user,
  1599. )
  1600. self.run_test_query(
  1601. f"assigned_or_suggested:[me, my_teams, none, #{self.team.slug}, {not_me.email}]",
  1602. [group, group1, group2, my_team_group, assigned_group],
  1603. [assigned_to_other_group],
  1604. user=self.user,
  1605. )
  1606. def test_assigned_to_with_environment(self):
  1607. results = self.make_query(
  1608. environments=[self.environments["staging"]],
  1609. search_filter_query="assigned:%s" % self.user.username,
  1610. )
  1611. assert set(results) == {self.group2}
  1612. results = self.make_query(
  1613. environments=[self.environments["production"]],
  1614. search_filter_query="assigned:%s" % self.user.username,
  1615. )
  1616. assert set(results) == set()
  1617. def test_subscribed_by(self):
  1618. results = self.make_query(
  1619. [self.group1.project], search_filter_query="subscribed:%s" % self.user.username
  1620. )
  1621. assert set(results) == {self.group1}
  1622. def test_subscribed_by_in_syntax(self):
  1623. self.run_test_query(f"subscribed:[{self.user.username}]", [self.group1], [self.group2])
  1624. user_2 = self.create_user()
  1625. GroupSubscription.objects.create(
  1626. user_id=user_2.id, group=self.group2, project=self.project, is_active=True
  1627. )
  1628. self.run_test_query(
  1629. f"subscribed:[{self.user.username}, {user_2.username}]", [self.group1, self.group2], []
  1630. )
  1631. def test_subscribed_by_with_environment(self):
  1632. results = self.make_query(
  1633. [self.group1.project],
  1634. environments=[self.environments["production"]],
  1635. search_filter_query="subscribed:%s" % self.user.username,
  1636. )
  1637. assert set(results) == {self.group1}
  1638. results = self.make_query(
  1639. [self.group1.project],
  1640. environments=[self.environments["staging"]],
  1641. search_filter_query="subscribed:%s" % self.user.username,
  1642. )
  1643. assert set(results) == set()
  1644. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1645. def test_snuba_not_called_optimization(self, query_mock):
  1646. assert self.make_query(search_filter_query="status:unresolved").results == [self.group1]
  1647. assert not query_mock.called
  1648. assert (
  1649. self.make_query(
  1650. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1651. sort_by="date",
  1652. ).results
  1653. == []
  1654. )
  1655. assert query_mock.called
  1656. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1657. def test_reduce_bulk_results_none_total(self, bulk_raw_query_mock):
  1658. bulk_raw_query_mock.return_value = [
  1659. {"data": [], "totals": {"total": None}},
  1660. {"data": [], "totals": {"total": None}},
  1661. ]
  1662. assert (
  1663. self.make_query(
  1664. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1665. sort_by="date",
  1666. ).results
  1667. == []
  1668. )
  1669. assert bulk_raw_query_mock.called
  1670. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1671. def test_reduce_bulk_results_none_data(self, bulk_raw_query_mock):
  1672. bulk_raw_query_mock.return_value = [
  1673. {"data": None, "totals": {"total": 0}},
  1674. {"data": None, "totals": {"total": 0}},
  1675. ]
  1676. assert (
  1677. self.make_query(
  1678. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1679. sort_by="date",
  1680. ).results
  1681. == []
  1682. )
  1683. assert bulk_raw_query_mock.called
  1684. def test_pre_and_post_filtering(self):
  1685. prev_max_pre = options.get("snuba.search.max-pre-snuba-candidates")
  1686. options.set("snuba.search.max-pre-snuba-candidates", 1)
  1687. try:
  1688. # normal queries work as expected
  1689. results = self.make_query(search_filter_query="foo")
  1690. assert set(results) == {self.group1}
  1691. results = self.make_query(search_filter_query="bar")
  1692. assert set(results) == {self.group2}
  1693. # no candidate matches in Sentry, immediately return empty paginator
  1694. results = self.make_query(search_filter_query="NO MATCHES IN SENTRY")
  1695. assert set(results) == set()
  1696. # too many candidates, skip pre-filter, requires >1 postfilter queries
  1697. results = self.make_query()
  1698. assert set(results) == {self.group1, self.group2}
  1699. finally:
  1700. options.set("snuba.search.max-pre-snuba-candidates", prev_max_pre)
  1701. def test_optimizer_enabled(self):
  1702. prev_optimizer_enabled = options.get("snuba.search.pre-snuba-candidates-optimizer")
  1703. options.set("snuba.search.pre-snuba-candidates-optimizer", True)
  1704. try:
  1705. results = self.make_query(
  1706. search_filter_query="server:example.com",
  1707. environments=[self.environments["production"]],
  1708. )
  1709. assert set(results) == {self.group1}
  1710. finally:
  1711. options.set("snuba.search.pre-snuba-candidates-optimizer", prev_optimizer_enabled)
  1712. def test_search_out_of_range(self):
  1713. the_date = datetime(2000, 1, 1, 0, 0, 0, tzinfo=pytz.utc)
  1714. results = self.make_query(
  1715. search_filter_query=f"event.timestamp:>{the_date} event.timestamp:<{the_date}",
  1716. date_from=the_date,
  1717. date_to=the_date,
  1718. )
  1719. assert set(results) == set()
  1720. def test_regressed_in_release(self):
  1721. # expect no groups within the results since there are no releases
  1722. results = self.make_query(search_filter_query="regressed_in_release:fake")
  1723. assert set(results) == set()
  1724. # expect no groups even though there is a release; since no group regressed in this release
  1725. release_1 = self.create_release()
  1726. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1727. assert set(results) == set()
  1728. # Create a new event so that we get a group in this release
  1729. group = self.store_event(
  1730. data={
  1731. "release": release_1.version,
  1732. },
  1733. project_id=self.project.id,
  1734. ).group
  1735. # # Should still be no group since we didn't regress in this release
  1736. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1737. assert set(results) == set()
  1738. record_group_history(group, GroupHistoryStatus.REGRESSED, release=release_1)
  1739. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1740. assert set(results) == {group}
  1741. # Make sure this works correctly with multiple releases
  1742. release_2 = self.create_release()
  1743. group_2 = self.store_event(
  1744. data={
  1745. "fingerprint": ["put-me-in-group9001"],
  1746. "event_id": "a" * 32,
  1747. "release": release_2.version,
  1748. },
  1749. project_id=self.project.id,
  1750. ).group
  1751. record_group_history(group_2, GroupHistoryStatus.REGRESSED, release=release_2)
  1752. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1753. assert set(results) == {group}
  1754. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_2.version)
  1755. assert set(results) == {group_2}
  1756. def test_first_release(self):
  1757. # expect no groups within the results since there are no releases
  1758. results = self.make_query(search_filter_query="first_release:%s" % "fake")
  1759. assert set(results) == set()
  1760. # expect no groups even though there is a release; since no group
  1761. # is attached to a release
  1762. release_1 = self.create_release(self.project)
  1763. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1764. assert set(results) == set()
  1765. # Create a new event so that we get a group in this release
  1766. group = self.store_event(
  1767. data={
  1768. "fingerprint": ["put-me-in-group9001"],
  1769. "event_id": "a" * 32,
  1770. "message": "hello",
  1771. "environment": "production",
  1772. "tags": {"server": "example.com"},
  1773. "release": release_1.version,
  1774. "stacktrace": {"frames": [{"module": "group1"}]},
  1775. },
  1776. project_id=self.project.id,
  1777. ).group
  1778. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1779. assert set(results) == {group}
  1780. def test_first_release_in_syntax(self):
  1781. # expect no groups within the results since there are no releases
  1782. self.run_test_query("first_release:[fake, fake2]", [])
  1783. # expect no groups even though there is a release; since no group
  1784. # is attached to a release
  1785. release_1 = self.create_release(self.project)
  1786. release_2 = self.create_release(self.project)
  1787. self.run_test_query(f"first_release:[{release_1.version}, {release_2.version}]", [])
  1788. # Create a new event so that we get a group in this release
  1789. group = self.store_event(
  1790. data={
  1791. "fingerprint": ["put-me-in-group9001"],
  1792. "event_id": "a" * 32,
  1793. "message": "hello",
  1794. "environment": "production",
  1795. "tags": {"server": "example.com"},
  1796. "release": release_1.version,
  1797. "stacktrace": {"frames": [{"module": "group1"}]},
  1798. },
  1799. project_id=self.project.id,
  1800. ).group
  1801. self.run_test_query(
  1802. f"first_release:[{release_1.version}, {release_2.version}]",
  1803. [group],
  1804. [self.group1, self.group2],
  1805. )
  1806. # Create a new event so that we get a group in this release
  1807. group_2 = self.store_event(
  1808. data={
  1809. "fingerprint": ["put-me-in-group9002"],
  1810. "event_id": "a" * 32,
  1811. "message": "hello",
  1812. "environment": "production",
  1813. "tags": {"server": "example.com"},
  1814. "release": release_2.version,
  1815. "stacktrace": {"frames": [{"module": "group1"}]},
  1816. },
  1817. project_id=self.project.id,
  1818. ).group
  1819. self.run_test_query(
  1820. f"first_release:[{release_1.version}, {release_2.version}]",
  1821. [group, group_2],
  1822. )
  1823. def test_first_release_environments(self):
  1824. results = self.make_query(
  1825. environments=[self.environments["production"]],
  1826. search_filter_query="first_release:fake",
  1827. )
  1828. assert set(results) == set()
  1829. release = self.create_release(self.project)
  1830. group_env = GroupEnvironment.get_or_create(
  1831. group_id=self.group1.id, environment_id=self.environments["production"].id
  1832. )[0]
  1833. results = self.make_query(
  1834. environments=[self.environments["production"]],
  1835. search_filter_query=f"first_release:{release.version}",
  1836. )
  1837. assert set(results) == set()
  1838. group_env.first_release = release
  1839. group_env.save()
  1840. results = self.make_query(
  1841. environments=[self.environments["production"]],
  1842. search_filter_query=f"first_release:{release.version}",
  1843. )
  1844. assert set(results) == {self.group1}
  1845. def test_first_release_environments_in_syntax(self):
  1846. self.run_test_query(
  1847. "first_release:[fake, fake2]",
  1848. [],
  1849. [self.group1, self.group2],
  1850. environments=[self.environments["production"]],
  1851. )
  1852. release = self.create_release(self.project)
  1853. group_1_env = GroupEnvironment.objects.get(
  1854. group_id=self.group1.id, environment_id=self.environments["production"].id
  1855. )
  1856. group_1_env.update(first_release=release)
  1857. self.run_test_query(
  1858. f"first_release:[{release.version}, fake2]",
  1859. [self.group1],
  1860. [self.group2],
  1861. environments=[self.environments["production"]],
  1862. )
  1863. group_2_env = GroupEnvironment.objects.get(
  1864. group_id=self.group2.id, environment_id=self.environments["staging"].id
  1865. )
  1866. group_2_env.update(first_release=release)
  1867. self.run_test_query(
  1868. f"first_release:[{release.version}, fake2]",
  1869. [self.group1, self.group2],
  1870. [],
  1871. environments=[self.environments["production"], self.environments["staging"]],
  1872. )
  1873. # Make sure we don't get duplicate groups
  1874. GroupEnvironment.objects.create(
  1875. group_id=self.group1.id,
  1876. environment_id=self.environments["staging"].id,
  1877. first_release=release,
  1878. )
  1879. self.run_test_query(
  1880. f"first_release:[{release.version}, fake2]",
  1881. [self.group1, self.group2],
  1882. [],
  1883. environments=[self.environments["production"], self.environments["staging"]],
  1884. )
  1885. def test_query_enclosed_in_quotes(self):
  1886. results = self.make_query(search_filter_query='"foo"')
  1887. assert set(results) == {self.group1}
  1888. results = self.make_query(search_filter_query='"bar"')
  1889. assert set(results) == {self.group2}
  1890. @xfail_if_not_postgres("Wildcard searching only supported in Postgres")
  1891. def test_wildcard(self):
  1892. escaped_event = self.store_event(
  1893. data={
  1894. "fingerprint": ["hello-there"],
  1895. "event_id": "f" * 32,
  1896. "message": "somet[hing]",
  1897. "environment": "production",
  1898. "tags": {"server": "example.net"},
  1899. "timestamp": iso_format(self.base_datetime),
  1900. "stacktrace": {"frames": [{"module": "group1"}]},
  1901. },
  1902. project_id=self.project.id,
  1903. )
  1904. # Note: Adding in `environment:production` so that we make sure we query
  1905. # in both snuba and postgres
  1906. results = self.make_query(search_filter_query="environment:production so*t")
  1907. assert set(results) == {escaped_event.group}
  1908. # Make sure it's case insensitive
  1909. results = self.make_query(search_filter_query="environment:production SO*t")
  1910. assert set(results) == {escaped_event.group}
  1911. results = self.make_query(search_filter_query="environment:production so*zz")
  1912. assert set(results) == set()
  1913. results = self.make_query(search_filter_query="environment:production [hing]")
  1914. assert set(results) == {escaped_event.group}
  1915. results = self.make_query(search_filter_query="environment:production s*]")
  1916. assert set(results) == {escaped_event.group}
  1917. results = self.make_query(search_filter_query="environment:production server:example.*")
  1918. assert set(results) == {self.group1, escaped_event.group}
  1919. results = self.make_query(search_filter_query="environment:production !server:*net")
  1920. assert set(results) == {self.group1}
  1921. # TODO: Disabling tests that use [] syntax for the moment. Re-enable
  1922. # these if we decide to add back in, or remove if this comment has been
  1923. # here a while.
  1924. # results = self.make_query(
  1925. # search_filter_query='environment:production [s][of][mz]',
  1926. # )
  1927. # assert set(results) == set([escaped_event.group])
  1928. # results = self.make_query(
  1929. # search_filter_query='environment:production [z][of][mz]',
  1930. # )
  1931. # assert set(results) == set()
  1932. def test_null_tags(self):
  1933. tag_event = self.store_event(
  1934. data={
  1935. "fingerprint": ["hello-there"],
  1936. "event_id": "f" * 32,
  1937. "message": "something",
  1938. "environment": "production",
  1939. "tags": {"server": "example.net"},
  1940. "timestamp": iso_format(self.base_datetime),
  1941. "stacktrace": {"frames": [{"module": "group1"}]},
  1942. },
  1943. project_id=self.project.id,
  1944. )
  1945. no_tag_event = self.store_event(
  1946. data={
  1947. "fingerprint": ["hello-there-2"],
  1948. "event_id": "5" * 32,
  1949. "message": "something",
  1950. "environment": "production",
  1951. "timestamp": iso_format(self.base_datetime),
  1952. "stacktrace": {"frames": [{"module": "group2"}]},
  1953. },
  1954. project_id=self.project.id,
  1955. )
  1956. results = self.make_query(search_filter_query="environment:production !server:*net")
  1957. assert set(results) == {self.group1, no_tag_event.group}
  1958. results = self.make_query(search_filter_query="environment:production server:*net")
  1959. assert set(results) == {tag_event.group}
  1960. results = self.make_query(search_filter_query="environment:production !server:example.net")
  1961. assert set(results) == {self.group1, no_tag_event.group}
  1962. results = self.make_query(search_filter_query="environment:production server:example.net")
  1963. assert set(results) == {tag_event.group}
  1964. results = self.make_query(search_filter_query="environment:production has:server")
  1965. assert set(results) == {self.group1, tag_event.group}
  1966. results = self.make_query(search_filter_query="environment:production !has:server")
  1967. assert set(results) == {no_tag_event.group}
  1968. def test_null_promoted_tags(self):
  1969. tag_event = self.store_event(
  1970. data={
  1971. "fingerprint": ["hello-there"],
  1972. "event_id": "f" * 32,
  1973. "message": "something",
  1974. "environment": "production",
  1975. "tags": {"logger": "csp"},
  1976. "timestamp": iso_format(self.base_datetime),
  1977. "stacktrace": {"frames": [{"module": "group1"}]},
  1978. },
  1979. project_id=self.project.id,
  1980. )
  1981. no_tag_event = self.store_event(
  1982. data={
  1983. "fingerprint": ["hello-there-2"],
  1984. "event_id": "5" * 32,
  1985. "message": "something",
  1986. "environment": "production",
  1987. "timestamp": iso_format(self.base_datetime),
  1988. "stacktrace": {"frames": [{"module": "group2"}]},
  1989. },
  1990. project_id=self.project.id,
  1991. )
  1992. results = self.make_query(search_filter_query="environment:production !logger:*sp")
  1993. assert set(results) == {self.group1, no_tag_event.group}
  1994. results = self.make_query(search_filter_query="environment:production logger:*sp")
  1995. assert set(results) == {tag_event.group}
  1996. results = self.make_query(search_filter_query="environment:production !logger:csp")
  1997. assert set(results) == {self.group1, no_tag_event.group}
  1998. results = self.make_query(search_filter_query="environment:production logger:csp")
  1999. assert set(results) == {tag_event.group}
  2000. results = self.make_query(search_filter_query="environment:production has:logger")
  2001. assert set(results) == {tag_event.group}
  2002. results = self.make_query(search_filter_query="environment:production !has:logger")
  2003. assert set(results) == {self.group1, no_tag_event.group}
  2004. def test_sort_multi_project(self):
  2005. self.set_up_multi_project()
  2006. results = self.make_query([self.project, self.project2], sort_by="date")
  2007. assert list(results) == [self.group1, self.group_p2, self.group2]
  2008. results = self.make_query([self.project, self.project2], sort_by="new")
  2009. assert list(results) == [self.group2, self.group_p2, self.group1]
  2010. results = self.make_query([self.project, self.project2], sort_by="freq")
  2011. assert list(results) == [self.group1, self.group_p2, self.group2]
  2012. results = self.make_query([self.project, self.project2], sort_by="priority")
  2013. assert list(results) == [self.group1, self.group2, self.group_p2]
  2014. results = self.make_query([self.project, self.project2], sort_by="user")
  2015. assert list(results) == [self.group1, self.group2, self.group_p2]
  2016. def test_in_syntax_is_invalid(self):
  2017. with pytest.raises(InvalidSearchQuery, match='"in" syntax invalid for "is" search'):
  2018. self.make_query(search_filter_query="is:[unresolved, resolved]")
  2019. def test_first_release_any_or_no_environments(self):
  2020. # test scenarios for tickets:
  2021. # SEN-571
  2022. # ISSUE-432
  2023. # given the following setup:
  2024. #
  2025. # groups table:
  2026. # group first_release
  2027. # A 1
  2028. # B 1
  2029. # C 2
  2030. #
  2031. # groupenvironments table:
  2032. # group environment first_release
  2033. # A staging 1
  2034. # A production 2
  2035. #
  2036. # when querying by first release, the appropriate set of groups should be displayed:
  2037. #
  2038. # first_release: 1
  2039. # env=[]: A, B
  2040. # env=[production, staging]: A
  2041. # env=[staging]: A
  2042. # env=[production]: nothing
  2043. #
  2044. # first_release: 2
  2045. # env=[]: A, C
  2046. # env=[production, staging]: A
  2047. # env=[staging]: nothing
  2048. # env=[production]: A
  2049. # create an issue/group whose events that occur in 2 distinct environments
  2050. group_a_event_1 = self.store_event(
  2051. data={
  2052. "fingerprint": ["group_a"],
  2053. "event_id": "aaa" + ("1" * 29),
  2054. "environment": "example_staging",
  2055. "release": "release_1",
  2056. },
  2057. project_id=self.project.id,
  2058. )
  2059. group_a_event_2 = self.store_event(
  2060. data={
  2061. "fingerprint": ["group_a"],
  2062. "event_id": "aaa" + ("2" * 29),
  2063. "environment": "example_production",
  2064. "release": "release_2",
  2065. },
  2066. project_id=self.project.id,
  2067. )
  2068. group_a = group_a_event_1.group
  2069. # get the environments for group_a
  2070. prod_env = group_a_event_2.get_environment()
  2071. staging_env = group_a_event_1.get_environment()
  2072. # create an issue/group whose event that occur in no environments
  2073. # but will be tied to release release_1
  2074. group_b_event_1 = self.store_event(
  2075. data={
  2076. "fingerprint": ["group_b"],
  2077. "event_id": "bbb" + ("1" * 29),
  2078. "release": "release_1",
  2079. },
  2080. project_id=self.project.id,
  2081. )
  2082. assert group_b_event_1.get_environment().name == "" # has no environment
  2083. group_b = group_b_event_1.group
  2084. # create an issue/group whose event that occur in no environments
  2085. # but will be tied to release release_2
  2086. group_c_event_1 = self.store_event(
  2087. data={
  2088. "fingerprint": ["group_c"],
  2089. "event_id": "ccc" + ("1" * 29),
  2090. "release": "release_2",
  2091. },
  2092. project_id=self.project.id,
  2093. )
  2094. assert group_c_event_1.get_environment().name == "" # has no environment
  2095. group_c = group_c_event_1.group
  2096. # query by release release_1
  2097. results = self.make_query(search_filter_query="first_release:%s" % "release_1")
  2098. assert set(results) == {group_a, group_b}
  2099. results = self.make_query(
  2100. environments=[staging_env, prod_env],
  2101. search_filter_query="first_release:%s" % "release_1",
  2102. )
  2103. assert set(results) == {group_a}
  2104. results = self.make_query(
  2105. environments=[staging_env], search_filter_query="first_release:%s" % "release_1"
  2106. )
  2107. assert set(results) == {group_a}
  2108. results = self.make_query(
  2109. environments=[prod_env], search_filter_query="first_release:%s" % "release_1"
  2110. )
  2111. assert set(results) == set()
  2112. # query by release release_2
  2113. results = self.make_query(search_filter_query="first_release:%s" % "release_2")
  2114. assert set(results) == {group_a, group_c}
  2115. results = self.make_query(
  2116. environments=[staging_env, prod_env],
  2117. search_filter_query="first_release:%s" % "release_2",
  2118. )
  2119. assert set(results) == {group_a}
  2120. results = self.make_query(
  2121. environments=[staging_env], search_filter_query="first_release:%s" % "release_2"
  2122. )
  2123. assert set(results) == set()
  2124. results = self.make_query(
  2125. environments=[prod_env], search_filter_query="first_release:%s" % "release_2"
  2126. )
  2127. assert set(results) == {group_a}
  2128. def test_all_fields_do_not_error(self):
  2129. # Just a sanity check to make sure that all fields can be successfully
  2130. # searched on without returning type errors and other schema related
  2131. # issues.
  2132. def test_query(query):
  2133. try:
  2134. self.make_query(search_filter_query=query)
  2135. except SnubaError as e:
  2136. self.fail(f"Query {query} errored. Error info: {e}")
  2137. for key in SENTRY_SNUBA_MAP:
  2138. if key in ["project.id", "issue.id", "performance.issue_ids"]:
  2139. continue
  2140. test_query("has:%s" % key)
  2141. test_query("!has:%s" % key)
  2142. if key == "error.handled":
  2143. val = 1
  2144. elif key in issue_search_config.numeric_keys:
  2145. val = "123"
  2146. elif key in issue_search_config.date_keys:
  2147. val = self.base_datetime.isoformat()
  2148. elif key in issue_search_config.boolean_keys:
  2149. val = "true"
  2150. elif key in {"trace.span", "trace.parent_span"}:
  2151. val = "abcdef1234abcdef"
  2152. test_query(f"!{key}:{val}")
  2153. else:
  2154. val = "abadcafedeadbeefdeaffeedabadfeed"
  2155. test_query(f"!{key}:{val}")
  2156. test_query(f"{key}:{val}")
  2157. def test_message_negation(self):
  2158. self.store_event(
  2159. data={
  2160. "fingerprint": ["put-me-in-group1"],
  2161. "event_id": "2" * 32,
  2162. "message": "something",
  2163. "timestamp": iso_format(self.base_datetime),
  2164. },
  2165. project_id=self.project.id,
  2166. )
  2167. results = self.make_query(search_filter_query="!message:else")
  2168. results2 = self.make_query(search_filter_query="!message:else")
  2169. assert list(results) == list(results2)
  2170. def test_error_main_thread_true(self):
  2171. myProject = self.create_project(
  2172. name="Foo", slug="foo", teams=[self.team], fire_project_created=True
  2173. )
  2174. event = self.store_event(
  2175. data={
  2176. "event_id": "1" * 32,
  2177. "message": "something",
  2178. "timestamp": iso_format(self.base_datetime),
  2179. "exception": {
  2180. "values": [
  2181. {
  2182. "type": "SyntaxError",
  2183. "value": "hello world",
  2184. "thread_id": 1,
  2185. },
  2186. ],
  2187. },
  2188. "threads": {
  2189. "values": [
  2190. {
  2191. "id": 1,
  2192. "main": True,
  2193. },
  2194. ],
  2195. },
  2196. },
  2197. project_id=myProject.id,
  2198. )
  2199. myGroup = event.groups[0]
  2200. results = self.make_query(
  2201. projects=[myProject],
  2202. search_filter_query="error.main_thread:1",
  2203. sort_by="date",
  2204. )
  2205. assert list(results) == [myGroup]
  2206. def test_error_main_thread_false(self):
  2207. myProject = self.create_project(
  2208. name="Foo2", slug="foo2", teams=[self.team], fire_project_created=True
  2209. )
  2210. event = self.store_event(
  2211. data={
  2212. "event_id": "2" * 32,
  2213. "message": "something",
  2214. "timestamp": iso_format(self.base_datetime),
  2215. "exception": {
  2216. "values": [
  2217. {
  2218. "type": "SyntaxError",
  2219. "value": "hello world",
  2220. "thread_id": 1,
  2221. },
  2222. ],
  2223. },
  2224. "threads": {
  2225. "values": [
  2226. {
  2227. "id": 1,
  2228. "main": False,
  2229. },
  2230. ],
  2231. },
  2232. },
  2233. project_id=myProject.id,
  2234. )
  2235. myGroup = event.groups[0]
  2236. results = self.make_query(
  2237. projects=[myProject],
  2238. search_filter_query="error.main_thread:0",
  2239. sort_by="date",
  2240. )
  2241. assert list(results) == [myGroup]
  2242. def test_error_main_thread_no_results(self):
  2243. myProject = self.create_project(
  2244. name="Foo3", slug="foo3", teams=[self.team], fire_project_created=True
  2245. )
  2246. self.store_event(
  2247. data={
  2248. "event_id": "3" * 32,
  2249. "message": "something",
  2250. "timestamp": iso_format(self.base_datetime),
  2251. "exception": {
  2252. "values": [
  2253. {
  2254. "type": "SyntaxError",
  2255. "value": "hello world",
  2256. "thread_id": 1,
  2257. },
  2258. ],
  2259. },
  2260. "threads": {
  2261. "values": [
  2262. {
  2263. "id": 1,
  2264. },
  2265. ],
  2266. },
  2267. },
  2268. project_id=myProject.id,
  2269. )
  2270. results = self.make_query(
  2271. projects=[myProject],
  2272. search_filter_query="error.main_thread:1",
  2273. sort_by="date",
  2274. )
  2275. assert len(results) == 0
  2276. class EventsBetterPriorityTest(SharedSnubaTest, OccurrenceTestMixin):
  2277. @property
  2278. def backend(self):
  2279. return EventsDatasetSnubaSearchBackend()
  2280. def test_better_priority_sort_old_and_new_events(self):
  2281. """Test that an issue with only one old event is ranked lower than an issue with only one new event"""
  2282. new_project = self.create_project(organization=self.project.organization)
  2283. base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2284. recent_event = self.store_event(
  2285. data={
  2286. "fingerprint": ["put-me-in-recent-group"],
  2287. "event_id": "c" * 32,
  2288. "message": "group1",
  2289. "environment": "production",
  2290. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  2291. "timestamp": iso_format(base_datetime),
  2292. "stacktrace": {"frames": [{"module": "group1"}]},
  2293. },
  2294. project_id=new_project.id,
  2295. )
  2296. old_event = self.store_event(
  2297. data={
  2298. "fingerprint": ["put-me-in-old-group"],
  2299. "event_id": "a" * 32,
  2300. "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.",
  2301. "environment": "production",
  2302. "tags": {"server": "example.com", "sentry:user": "old_event@example.com"},
  2303. "timestamp": iso_format(base_datetime - timedelta(days=20)),
  2304. "stacktrace": {"frames": [{"module": "group1"}]},
  2305. },
  2306. project_id=new_project.id,
  2307. )
  2308. # datetime(2017, 9, 6, 0, 0)
  2309. old_event.data["timestamp"] = 1504656000.0
  2310. with self.feature("organizations:issue-list-better-priority-sort"):
  2311. weights: PrioritySortWeights = {
  2312. "log_level": 0,
  2313. "has_stacktrace": 0,
  2314. "relative_volume": 1,
  2315. "event_halflife_hours": 4,
  2316. "issue_halflife_hours": 24 * 7,
  2317. "v2": False,
  2318. "norm": False,
  2319. }
  2320. results = self.make_query(
  2321. sort_by="betterPriority",
  2322. projects=[new_project],
  2323. aggregate_kwargs=weights,
  2324. )
  2325. recent_group = Group.objects.get(id=recent_event.group.id)
  2326. old_group = Group.objects.get(id=old_event.group.id)
  2327. assert list(results) == [recent_group, old_group]
  2328. def test_better_priority_sort_v2(self):
  2329. """Test that the v2 formula works."""
  2330. new_project = self.create_project(organization=self.project.organization)
  2331. base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2332. recent_event = self.store_event(
  2333. data={
  2334. "fingerprint": ["put-me-in-recent-group"],
  2335. "event_id": "c" * 32,
  2336. "message": "group1",
  2337. "environment": "production",
  2338. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  2339. "timestamp": iso_format(base_datetime),
  2340. "stacktrace": {"frames": [{"module": "group1"}]},
  2341. },
  2342. project_id=new_project.id,
  2343. )
  2344. old_event = self.store_event(
  2345. data={
  2346. "fingerprint": ["put-me-in-old-group"],
  2347. "event_id": "a" * 32,
  2348. "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.",
  2349. "environment": "production",
  2350. "tags": {"server": "example.com", "sentry:user": "old_event@example.com"},
  2351. "timestamp": iso_format(base_datetime - timedelta(days=20)),
  2352. "stacktrace": {"frames": [{"module": "group1"}]},
  2353. },
  2354. project_id=new_project.id,
  2355. )
  2356. # datetime(2017, 9, 6, 0, 0)
  2357. old_event.data["timestamp"] = 1504656000.0
  2358. with self.feature("organizations:issue-list-better-priority-sort"):
  2359. weights: PrioritySortWeights = {
  2360. "log_level": 0,
  2361. "has_stacktrace": 0,
  2362. "relative_volume": 1,
  2363. "event_halflife_hours": 4,
  2364. "issue_halflife_hours": 24 * 7,
  2365. "v2": True,
  2366. "norm": False,
  2367. }
  2368. results = self.make_query(
  2369. sort_by="betterPriority",
  2370. projects=[new_project],
  2371. aggregate_kwargs=weights,
  2372. )
  2373. recent_group = Group.objects.get(id=recent_event.group.id)
  2374. old_group = Group.objects.get(id=old_event.group.id)
  2375. assert list(results) == [recent_group, old_group]
  2376. def test_better_priority_log_level_results(self):
  2377. """Test that the scoring results change when we pass in different log level weights"""
  2378. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2379. event1 = self.store_event(
  2380. data={
  2381. "fingerprint": ["put-me-in-group1"],
  2382. "event_id": "c" * 32,
  2383. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2384. "message": "foo",
  2385. "stacktrace": {"frames": [{"module": "group1"}]},
  2386. "environment": "staging",
  2387. "level": "fatal",
  2388. },
  2389. project_id=self.project.id,
  2390. )
  2391. event2 = self.store_event(
  2392. data={
  2393. "fingerprint": ["put-me-in-group2"],
  2394. "event_id": "d" * 32,
  2395. "timestamp": iso_format(base_datetime),
  2396. "message": "bar",
  2397. "stacktrace": {"frames": [{"module": "group2"}]},
  2398. "environment": "staging",
  2399. "level": "error",
  2400. },
  2401. project_id=self.project.id,
  2402. )
  2403. group1 = Group.objects.get(id=event1.group.id)
  2404. group2 = Group.objects.get(id=event2.group.id)
  2405. agg_kwargs = {
  2406. "better_priority": {
  2407. "log_level": 0,
  2408. "has_stacktrace": 0,
  2409. "relative_volume": 1,
  2410. "event_halflife_hours": 4,
  2411. "issue_halflife_hours": 24 * 7,
  2412. "v2": False,
  2413. "norm": False,
  2414. }
  2415. }
  2416. query_executor = self.backend._get_query_executor()
  2417. results_zero_log_level = query_executor.snuba_search(
  2418. start=None,
  2419. end=None,
  2420. project_ids=[self.project.id],
  2421. environment_ids=[],
  2422. sort_field="better_priority",
  2423. organization=self.organization,
  2424. group_ids=[group1.id, group2.id],
  2425. limit=150,
  2426. aggregate_kwargs=agg_kwargs,
  2427. )[0]
  2428. group1_score_before = results_zero_log_level[0][1]
  2429. group2_score_before = results_zero_log_level[1][1]
  2430. # initially group 2's score is higher since it has a more recent event
  2431. assert group2_score_before > group1_score_before
  2432. agg_kwargs["better_priority"].update({"log_level": 5})
  2433. results2 = query_executor.snuba_search(
  2434. start=None,
  2435. end=None,
  2436. project_ids=[self.project.id],
  2437. environment_ids=[],
  2438. sort_field="better_priority",
  2439. organization=self.organization,
  2440. group_ids=[group1.id, group2.id],
  2441. limit=150,
  2442. aggregate_kwargs=agg_kwargs,
  2443. )[0]
  2444. group1_score_after = results2[0][1]
  2445. group2_score_after = results2[1][1]
  2446. # ensure fatal has a higher score than error
  2447. assert group1_score_after > group2_score_after
  2448. def test_better_priority_has_stacktrace_results(self):
  2449. """Test that the scoring results change when we pass in different has_stacktrace weights"""
  2450. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2451. agg_kwargs = {
  2452. "better_priority": {
  2453. "log_level": 0,
  2454. "has_stacktrace": 0,
  2455. "relative_volume": 1,
  2456. "event_halflife_hours": 4,
  2457. "issue_halflife_hours": 24 * 7,
  2458. "v2": False,
  2459. "norm": False,
  2460. }
  2461. }
  2462. query_executor = self.backend._get_query_executor()
  2463. no_stacktrace_event = self.store_event(
  2464. data={
  2465. "event_id": "d" * 32,
  2466. "message": "oh no",
  2467. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2468. },
  2469. project_id=self.project.id,
  2470. )
  2471. group1 = Group.objects.get(id=no_stacktrace_event.group.id)
  2472. stacktrace_event = self.store_event(
  2473. data={
  2474. "event_id": "d" * 32,
  2475. "exception": {
  2476. "values": [
  2477. {
  2478. "type": "AnError",
  2479. "value": "Bad request",
  2480. "stacktrace": {
  2481. "frames": [
  2482. {
  2483. "module": "<my module>",
  2484. },
  2485. ]
  2486. },
  2487. }
  2488. ]
  2489. },
  2490. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2491. },
  2492. project_id=self.project.id,
  2493. )
  2494. group2 = Group.objects.get(id=stacktrace_event.group.id)
  2495. results = query_executor.snuba_search(
  2496. start=None,
  2497. end=None,
  2498. project_ids=[self.project.id],
  2499. environment_ids=[],
  2500. sort_field="better_priority",
  2501. organization=self.organization,
  2502. group_ids=[group1.id, group2.id],
  2503. limit=150,
  2504. aggregate_kwargs=agg_kwargs,
  2505. )[0]
  2506. group1_score = results[0][1]
  2507. group2_score = results[1][1]
  2508. assert group1_score == group2_score
  2509. agg_kwargs["better_priority"].update({"has_stacktrace": 3})
  2510. results = query_executor.snuba_search(
  2511. start=None,
  2512. end=None,
  2513. project_ids=[self.project.id],
  2514. environment_ids=[],
  2515. sort_field="better_priority",
  2516. organization=self.organization,
  2517. group_ids=[group1.id, group2.id],
  2518. limit=150,
  2519. aggregate_kwargs=agg_kwargs,
  2520. )[0]
  2521. group1_score = results[0][1]
  2522. group2_score = results[1][1]
  2523. # check that a group with an event with a stacktrace has a higher weight than one without
  2524. assert group1_score < group2_score
  2525. def test_better_priority_event_halflife_results(self):
  2526. """Test that the scoring results change when we pass in different event halflife weights"""
  2527. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2528. event1 = self.store_event(
  2529. data={
  2530. "fingerprint": ["put-me-in-group1"],
  2531. "event_id": "a" * 32,
  2532. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2533. "message": "foo",
  2534. "stacktrace": {"frames": [{"module": "group1"}]},
  2535. "environment": "staging",
  2536. "level": "fatal",
  2537. },
  2538. project_id=self.project.id,
  2539. )
  2540. event2 = self.store_event(
  2541. data={
  2542. "fingerprint": ["put-me-in-group2"],
  2543. "event_id": "b" * 32,
  2544. "timestamp": iso_format(base_datetime),
  2545. "message": "bar",
  2546. "stacktrace": {"frames": [{"module": "group2"}]},
  2547. "environment": "staging",
  2548. "level": "error",
  2549. },
  2550. project_id=self.project.id,
  2551. )
  2552. group1 = Group.objects.get(id=event1.group.id)
  2553. group2 = Group.objects.get(id=event2.group.id)
  2554. agg_kwargs = {
  2555. "better_priority": {
  2556. "log_level": 0,
  2557. "has_stacktrace": 0,
  2558. "relative_volume": 1,
  2559. "event_halflife_hours": 4,
  2560. "issue_halflife_hours": 24 * 7,
  2561. "v2": False,
  2562. "norm": False,
  2563. }
  2564. }
  2565. query_executor = self.backend._get_query_executor()
  2566. results = query_executor.snuba_search(
  2567. start=None,
  2568. end=None,
  2569. project_ids=[self.project.id],
  2570. environment_ids=[],
  2571. sort_field="better_priority",
  2572. organization=self.organization,
  2573. group_ids=[group1.id, group2.id],
  2574. limit=150,
  2575. aggregate_kwargs=agg_kwargs,
  2576. )[0]
  2577. group1_score_before = results[0][1]
  2578. group2_score_before = results[1][1]
  2579. # initially group 2's score is higher since it has a more recent event
  2580. assert group2_score_before > group1_score_before
  2581. agg_kwargs["better_priority"].update({"event_halflife_hours": 2})
  2582. results = query_executor.snuba_search(
  2583. start=None,
  2584. end=None,
  2585. project_ids=[self.project.id],
  2586. environment_ids=[],
  2587. sort_field="better_priority",
  2588. organization=self.organization,
  2589. group_ids=[group1.id, group2.id],
  2590. limit=150,
  2591. aggregate_kwargs=agg_kwargs,
  2592. )[0]
  2593. group1_score_after = results[0][1]
  2594. group2_score_after = results[1][1]
  2595. assert group1_score_after < group2_score_after
  2596. def test_better_priority_mixed_group_types(self):
  2597. base_datetime = (datetime.utcnow() - timedelta(hours=1)).replace(tzinfo=pytz.utc)
  2598. error_event = self.store_event(
  2599. data={
  2600. "fingerprint": ["put-me-in-group1"],
  2601. "event_id": "a" * 32,
  2602. "timestamp": iso_format(base_datetime - timedelta(hours=1)),
  2603. "message": "foo",
  2604. "stacktrace": {"frames": [{"module": "group1"}]},
  2605. "environment": "staging",
  2606. "level": "fatal",
  2607. },
  2608. project_id=self.project.id,
  2609. )
  2610. error_group = error_event.group
  2611. profile_event_id = uuid.uuid4().hex
  2612. _, group_info = process_event_and_issue_occurrence(
  2613. self.build_occurrence_data(event_id=profile_event_id),
  2614. {
  2615. "event_id": profile_event_id,
  2616. "project_id": self.project.id,
  2617. "title": "some problem",
  2618. "platform": "python",
  2619. "tags": {"my_tag": "1"},
  2620. "timestamp": before_now(minutes=1).isoformat(),
  2621. "received": before_now(minutes=1).isoformat(),
  2622. },
  2623. )
  2624. profile_group_1 = group_info.group
  2625. agg_kwargs = {
  2626. "better_priority": {
  2627. "log_level": 0,
  2628. "has_stacktrace": 0,
  2629. "relative_volume": 1,
  2630. "event_halflife_hours": 4,
  2631. "issue_halflife_hours": 24 * 7,
  2632. "v2": False,
  2633. "norm": False,
  2634. }
  2635. }
  2636. query_executor = self.backend._get_query_executor()
  2637. with self.feature(
  2638. [
  2639. "organizations:issue-platform",
  2640. ProfileFileIOGroupType.build_visible_feature_name(),
  2641. "organizations:issue-list-better-priority-sort",
  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)