test_backend.py 127 KB

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