test_backend.py 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  1. import uuid
  2. from datetime import datetime, timedelta
  3. from hashlib import md5
  4. from unittest import mock
  5. import pytest
  6. import pytz
  7. from django.utils import timezone
  8. from sentry import options
  9. from sentry.api.issue_search import convert_query_values, issue_search_config, parse_search_query
  10. from sentry.exceptions import InvalidSearchQuery
  11. from sentry.issues.grouptype import (
  12. ErrorGroupType,
  13. PerformanceNPlusOneGroupType,
  14. PerformanceRenderBlockingAssetSpanGroupType,
  15. )
  16. from sentry.issues.occurrence_consumer import process_event_and_issue_occurrence
  17. from sentry.models import (
  18. Environment,
  19. Group,
  20. GroupAssignee,
  21. GroupBookmark,
  22. GroupEnvironment,
  23. GroupHistoryStatus,
  24. GroupStatus,
  25. GroupSubscription,
  26. Integration,
  27. record_group_history,
  28. )
  29. from sentry.models.groupowner import GroupOwner
  30. from sentry.search.snuba.backend import (
  31. CdcEventsDatasetSnubaSearchBackend,
  32. EventsDatasetSnubaSearchBackend,
  33. )
  34. from sentry.search.snuba.executors import InvalidQueryForExecutor
  35. from sentry.testutils import SnubaTestCase, TestCase, xfail_if_not_postgres
  36. from sentry.testutils.helpers import Feature
  37. from sentry.testutils.helpers.datetime import before_now, iso_format
  38. from sentry.utils.snuba import SENTRY_SNUBA_MAP, SnubaError
  39. from tests.sentry.issues.test_utils import OccurrenceTestMixin
  40. def date_to_query_format(date):
  41. return date.strftime("%Y-%m-%dT%H:%M:%S")
  42. class SharedSnubaTest(TestCase, SnubaTestCase):
  43. def build_search_filter(self, query, projects=None, user=None, environments=None):
  44. user = user if user is not None else self.user
  45. projects = projects if projects is not None else [self.project]
  46. return convert_query_values(parse_search_query(query), projects, user, environments)
  47. def make_query(
  48. self,
  49. projects=None,
  50. search_filter_query=None,
  51. environments=None,
  52. sort_by="date",
  53. limit=None,
  54. count_hits=False,
  55. date_from=None,
  56. date_to=None,
  57. cursor=None,
  58. ):
  59. search_filters = []
  60. projects = projects if projects is not None else [self.project]
  61. if search_filter_query is not None:
  62. search_filters = self.build_search_filter(
  63. search_filter_query, projects, environments=environments
  64. )
  65. kwargs = {}
  66. if limit is not None:
  67. kwargs["limit"] = limit
  68. return self.backend.query(
  69. projects,
  70. search_filters=search_filters,
  71. environments=environments,
  72. count_hits=count_hits,
  73. sort_by=sort_by,
  74. date_from=date_from,
  75. date_to=date_to,
  76. cursor=cursor,
  77. **kwargs,
  78. )
  79. def store_event(self, data, *args, **kwargs):
  80. event = super().store_event(data, *args, **kwargs)
  81. environment_name = data.get("environment")
  82. if environment_name:
  83. GroupEnvironment.objects.filter(
  84. group_id=event.group_id,
  85. environment__name=environment_name,
  86. first_seen__gt=event.datetime,
  87. ).update(first_seen=event.datetime)
  88. return event
  89. class EventsSnubaSearchTest(SharedSnubaTest):
  90. @property
  91. def backend(self):
  92. return EventsDatasetSnubaSearchBackend()
  93. def setUp(self):
  94. super().setUp()
  95. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  96. event1_timestamp = iso_format(self.base_datetime - timedelta(days=21))
  97. self.event1 = self.store_event(
  98. data={
  99. "fingerprint": ["put-me-in-group1"],
  100. "event_id": "a" * 32,
  101. "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.",
  102. "environment": "production",
  103. "tags": {"server": "example.com", "sentry:user": "event1@example.com"},
  104. "timestamp": event1_timestamp,
  105. "stacktrace": {"frames": [{"module": "group1"}]},
  106. },
  107. project_id=self.project.id,
  108. )
  109. self.event3 = self.store_event(
  110. data={
  111. "fingerprint": ["put-me-in-group1"],
  112. "event_id": "c" * 32,
  113. "message": "group1",
  114. "environment": "production",
  115. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  116. "timestamp": iso_format(self.base_datetime),
  117. "stacktrace": {"frames": [{"module": "group1"}]},
  118. },
  119. project_id=self.project.id,
  120. )
  121. self.group1 = Group.objects.get(id=self.event1.group.id)
  122. assert self.group1.id == self.event1.group.id
  123. assert self.group1.id == self.event3.group.id
  124. assert self.group1.first_seen == self.event1.datetime
  125. assert self.group1.last_seen == self.event3.datetime
  126. self.group1.times_seen = 5
  127. self.group1.status = GroupStatus.UNRESOLVED
  128. self.group1.update(type=ErrorGroupType.type_id)
  129. self.group1.save()
  130. self.store_group(self.group1)
  131. self.event2 = self.store_event(
  132. data={
  133. "fingerprint": ["put-me-in-group2"],
  134. "event_id": "b" * 32,
  135. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  136. "message": "bar",
  137. "stacktrace": {"frames": [{"module": "group2"}]},
  138. "environment": "staging",
  139. "tags": {
  140. "server": "example.com",
  141. "url": "http://example.com",
  142. "sentry:user": "event2@example.com",
  143. },
  144. },
  145. project_id=self.project.id,
  146. )
  147. self.group2 = Group.objects.get(id=self.event2.group.id)
  148. assert self.group2.id == self.event2.group.id
  149. assert self.group2.first_seen == self.group2.last_seen == self.event2.datetime
  150. self.group2.status = GroupStatus.RESOLVED
  151. self.group2.times_seen = 10
  152. self.group2.update(type=ErrorGroupType.type_id)
  153. self.group2.save()
  154. self.store_group(self.group2)
  155. GroupBookmark.objects.create(
  156. user_id=self.user.id, group=self.group2, project=self.group2.project
  157. )
  158. GroupAssignee.objects.create(
  159. user_id=self.user.id, group=self.group2, project=self.group2.project
  160. )
  161. GroupSubscription.objects.create(
  162. user_id=self.user.id, group=self.group1, project=self.group1.project, is_active=True
  163. )
  164. GroupSubscription.objects.create(
  165. user_id=self.user.id, group=self.group2, project=self.group2.project, is_active=False
  166. )
  167. self.environments = {
  168. "production": self.event1.get_environment(),
  169. "staging": self.event2.get_environment(),
  170. }
  171. def set_up_multi_project(self):
  172. self.project2 = self.create_project(organization=self.project.organization)
  173. self.event_p2 = self.store_event(
  174. data={
  175. "event_id": "a" * 32,
  176. "fingerprint": ["put-me-in-groupP2"],
  177. "timestamp": iso_format(self.base_datetime - timedelta(days=21)),
  178. "message": "foo",
  179. "stacktrace": {"frames": [{"module": "group_p2"}]},
  180. "tags": {"server": "example.com"},
  181. "environment": "production",
  182. },
  183. project_id=self.project2.id,
  184. )
  185. self.group_p2 = Group.objects.get(id=self.event_p2.group.id)
  186. self.group_p2.times_seen = 6
  187. self.group_p2.last_seen = self.base_datetime - timedelta(days=1)
  188. self.group_p2.save()
  189. self.store_group(self.group_p2)
  190. def create_group_with_integration_external_issue(self, environment="production"):
  191. event = self.store_event(
  192. data={
  193. "fingerprint": ["linked_group1"],
  194. "event_id": uuid.uuid4().hex,
  195. "timestamp": iso_format(self.base_datetime),
  196. "environment": environment,
  197. },
  198. project_id=self.project.id,
  199. )
  200. integration = Integration.objects.create(provider="example", name="Example")
  201. integration.add_organization(event.group.organization, self.user)
  202. self.create_integration_external_issue(
  203. group=event.group,
  204. integration=integration,
  205. key="APP-123",
  206. )
  207. return event.group
  208. def create_group_with_platform_external_issue(self, environment="production"):
  209. event = self.store_event(
  210. data={
  211. "fingerprint": ["linked_group2"],
  212. "event_id": uuid.uuid4().hex,
  213. "timestamp": iso_format(self.base_datetime),
  214. "environment": environment,
  215. },
  216. project_id=self.project.id,
  217. )
  218. self.create_platform_external_issue(
  219. group=event.group,
  220. service_type="sentry-app",
  221. display_name="App#issue-1",
  222. web_url="https://example.com/app/issues/1",
  223. )
  224. return event.group
  225. def run_test_query_in_syntax(
  226. self, query, expected_groups, expected_negative_groups=None, environments=None
  227. ):
  228. results = self.make_query(search_filter_query=query, environments=environments)
  229. sort_key = lambda result: result.id
  230. assert sorted(results, key=sort_key) == sorted(expected_groups, key=sort_key)
  231. if expected_negative_groups is not None:
  232. results = self.make_query(search_filter_query=f"!{query}")
  233. assert sorted(results, key=sort_key) == sorted(expected_negative_groups, key=sort_key)
  234. def test_query(self):
  235. results = self.make_query(search_filter_query="foo")
  236. assert set(results) == {self.group1}
  237. results = self.make_query(search_filter_query="bar")
  238. assert set(results) == {self.group2}
  239. def test_query_multi_project(self):
  240. self.set_up_multi_project()
  241. results = self.make_query([self.project, self.project2], search_filter_query="foo")
  242. assert set(results) == {self.group1, self.group_p2}
  243. def test_query_with_environment(self):
  244. results = self.make_query(
  245. environments=[self.environments["production"]], search_filter_query="foo"
  246. )
  247. assert set(results) == {self.group1}
  248. results = self.make_query(
  249. environments=[self.environments["production"]], search_filter_query="bar"
  250. )
  251. assert set(results) == set()
  252. results = self.make_query(
  253. environments=[self.environments["staging"]], search_filter_query="bar"
  254. )
  255. assert set(results) == {self.group2}
  256. def test_query_for_text_in_long_message(self):
  257. results = self.make_query(
  258. [self.project],
  259. environments=[self.environments["production"]],
  260. search_filter_query="santryrox",
  261. )
  262. assert set(results) == {self.group1}
  263. def test_multi_environments(self):
  264. self.set_up_multi_project()
  265. results = self.make_query(
  266. [self.project, self.project2],
  267. environments=[self.environments["production"], self.environments["staging"]],
  268. )
  269. assert set(results) == {self.group1, self.group2, self.group_p2}
  270. def test_query_with_environment_multi_project(self):
  271. self.set_up_multi_project()
  272. results = self.make_query(
  273. [self.project, self.project2],
  274. environments=[self.environments["production"]],
  275. search_filter_query="foo",
  276. )
  277. assert set(results) == {self.group1, self.group_p2}
  278. results = self.make_query(
  279. [self.project, self.project2],
  280. environments=[self.environments["production"]],
  281. search_filter_query="bar",
  282. )
  283. assert set(results) == set()
  284. def test_sort(self):
  285. results = self.make_query(sort_by="date")
  286. assert list(results) == [self.group1, self.group2]
  287. results = self.make_query(sort_by="new")
  288. assert list(results) == [self.group2, self.group1]
  289. results = self.make_query(sort_by="freq")
  290. assert list(results) == [self.group1, self.group2]
  291. results = self.make_query(sort_by="priority")
  292. assert list(results) == [self.group1, self.group2]
  293. results = self.make_query(sort_by="user")
  294. assert list(results) == [self.group1, self.group2]
  295. def test_sort_with_environment(self):
  296. for dt in [
  297. self.group1.first_seen + timedelta(days=1),
  298. self.group1.first_seen + timedelta(days=2),
  299. self.group1.last_seen + timedelta(days=1),
  300. ]:
  301. self.store_event(
  302. data={
  303. "fingerprint": ["put-me-in-group2"],
  304. "timestamp": iso_format(dt),
  305. "stacktrace": {"frames": [{"module": "group2"}]},
  306. "environment": "production",
  307. "message": "group2",
  308. },
  309. project_id=self.project.id,
  310. )
  311. results = self.make_query(environments=[self.environments["production"]], sort_by="date")
  312. assert list(results) == [self.group2, self.group1]
  313. results = self.make_query(environments=[self.environments["production"]], sort_by="new")
  314. assert list(results) == [self.group2, self.group1]
  315. results = self.make_query(environments=[self.environments["production"]], sort_by="freq")
  316. assert list(results) == [self.group2, self.group1]
  317. results = self.make_query(
  318. environments=[self.environments["production"]], sort_by="priority"
  319. )
  320. assert list(results) == [self.group2, self.group1]
  321. results = self.make_query(environments=[self.environments["production"]], sort_by="user")
  322. assert list(results) == [self.group1, self.group2]
  323. def test_status(self):
  324. results = self.make_query(search_filter_query="is:unresolved")
  325. assert set(results) == {self.group1}
  326. results = self.make_query(search_filter_query="is:resolved")
  327. assert set(results) == {self.group2}
  328. event_3 = self.store_event(
  329. data={
  330. "fingerprint": ["put-me-in-group3"],
  331. "event_id": "c" * 32,
  332. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  333. },
  334. project_id=self.project.id,
  335. )
  336. group_3 = event_3.group
  337. group_3.status = GroupStatus.MUTED
  338. group_3.save()
  339. self.run_test_query_in_syntax(
  340. "status:[unresolved, resolved]", [self.group1, self.group2], [group_3]
  341. )
  342. self.run_test_query_in_syntax(
  343. "status:[resolved, muted]", [self.group2, group_3], [self.group1]
  344. )
  345. def test_category(self):
  346. results = self.make_query(search_filter_query="issue.category:error")
  347. assert set(results) == {self.group1, self.group2}
  348. event_3 = self.store_event(
  349. data={
  350. "fingerprint": ["put-me-in-group3"],
  351. "event_id": "c" * 32,
  352. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  353. },
  354. project_id=self.project.id,
  355. )
  356. group_3 = event_3.group
  357. group_3.update(type=PerformanceNPlusOneGroupType.type_id)
  358. results = self.make_query(search_filter_query="issue.category:performance")
  359. assert set(results) == {group_3}
  360. results = self.make_query(search_filter_query="issue.category:[error, performance]")
  361. assert set(results) == {self.group1, self.group2, group_3}
  362. with pytest.raises(InvalidSearchQuery):
  363. self.make_query(search_filter_query="issue.category:hellboy")
  364. def test_not_perf_category(self):
  365. results = self.make_query(search_filter_query="issue.category:error foo")
  366. assert set(results) == {self.group1}
  367. not_results = self.make_query(search_filter_query="!issue.category:performance foo")
  368. assert set(not_results) == {self.group1}
  369. def test_type(self):
  370. results = self.make_query(search_filter_query="issue.type:error")
  371. assert set(results) == {self.group1, self.group2}
  372. event_3 = self.store_event(
  373. data={
  374. "fingerprint": ["put-me-in-group3"],
  375. "event_id": "c" * 32,
  376. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  377. "type": PerformanceNPlusOneGroupType.type_id,
  378. },
  379. project_id=self.project.id,
  380. )
  381. group_3 = event_3.group
  382. group_3.update(type=PerformanceNPlusOneGroupType.type_id)
  383. results = self.make_query(
  384. search_filter_query="issue.type:performance_n_plus_one_db_queries"
  385. )
  386. assert set(results) == {group_3}
  387. event_4 = self.store_event(
  388. data={
  389. "fingerprint": ["put-me-in-group4"],
  390. "event_id": "d" * 32,
  391. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  392. },
  393. project_id=self.project.id,
  394. )
  395. group_4 = event_4.group
  396. group_4.update(type=PerformanceRenderBlockingAssetSpanGroupType.type_id)
  397. results = self.make_query(
  398. search_filter_query="issue.type:performance_render_blocking_asset_span"
  399. )
  400. assert set(results) == {group_4}
  401. results = self.make_query(
  402. search_filter_query="issue.type:[performance_render_blocking_asset_span, performance_n_plus_one_db_queries, error]"
  403. )
  404. assert set(results) == {self.group1, self.group2, group_3, group_4}
  405. with pytest.raises(InvalidSearchQuery):
  406. self.make_query(search_filter_query="issue.type:performance_i_dont_exist")
  407. def test_status_with_environment(self):
  408. results = self.make_query(
  409. environments=[self.environments["production"]], search_filter_query="is:unresolved"
  410. )
  411. assert set(results) == {self.group1}
  412. results = self.make_query(
  413. environments=[self.environments["staging"]], search_filter_query="is:resolved"
  414. )
  415. assert set(results) == {self.group2}
  416. results = self.make_query(
  417. environments=[self.environments["production"]], search_filter_query="is:resolved"
  418. )
  419. assert set(results) == set()
  420. def test_tags(self):
  421. results = self.make_query(search_filter_query="environment:staging")
  422. assert set(results) == {self.group2}
  423. results = self.make_query(search_filter_query="environment:example.com")
  424. assert set(results) == set()
  425. results = self.make_query(search_filter_query="has:environment")
  426. assert set(results) == {self.group2, self.group1}
  427. results = self.make_query(search_filter_query="environment:staging server:example.com")
  428. assert set(results) == {self.group2}
  429. results = self.make_query(search_filter_query='url:"http://example.com"')
  430. assert set(results) == {self.group2}
  431. results = self.make_query(search_filter_query="environment:staging has:server")
  432. assert set(results) == {self.group2}
  433. results = self.make_query(search_filter_query="environment:staging server:bar.example.com")
  434. assert set(results) == set()
  435. def test_tags_with_environment(self):
  436. results = self.make_query(
  437. environments=[self.environments["production"]], search_filter_query="server:example.com"
  438. )
  439. assert set(results) == {self.group1}
  440. results = self.make_query(
  441. environments=[self.environments["staging"]], search_filter_query="server:example.com"
  442. )
  443. assert set(results) == {self.group2}
  444. results = self.make_query(
  445. environments=[self.environments["staging"]], search_filter_query="has:server"
  446. )
  447. assert set(results) == {self.group2}
  448. results = self.make_query(
  449. environments=[self.environments["production"]],
  450. search_filter_query='url:"http://example.com"',
  451. )
  452. assert set(results) == set()
  453. results = self.make_query(
  454. environments=[self.environments["staging"]],
  455. search_filter_query='url:"http://example.com"',
  456. )
  457. assert set(results) == {self.group2}
  458. results = self.make_query(
  459. environments=[self.environments["staging"]],
  460. search_filter_query="server:bar.example.com",
  461. )
  462. assert set(results) == set()
  463. def test_bookmarked_by(self):
  464. results = self.make_query(search_filter_query="bookmarks:%s" % self.user.username)
  465. assert set(results) == {self.group2}
  466. def test_bookmarked_by_in_syntax(self):
  467. self.run_test_query_in_syntax(
  468. f"bookmarks:[{self.user.username}]", [self.group2], [self.group1]
  469. )
  470. user_2 = self.create_user()
  471. GroupBookmark.objects.create(
  472. user_id=user_2.id, group=self.group1, project=self.group2.project
  473. )
  474. self.run_test_query_in_syntax(
  475. f"bookmarks:[{self.user.username}, {user_2.username}]", [self.group2, self.group1], []
  476. )
  477. def test_bookmarked_by_with_environment(self):
  478. results = self.make_query(
  479. environments=[self.environments["staging"]],
  480. search_filter_query="bookmarks:%s" % self.user.username,
  481. )
  482. assert set(results) == {self.group2}
  483. results = self.make_query(
  484. environments=[self.environments["production"]],
  485. search_filter_query="bookmarks:%s" % self.user.username,
  486. )
  487. assert set(results) == set()
  488. def test_search_filter_query_with_custom_priority_tag(self):
  489. priority = "high"
  490. self.store_event(
  491. data={
  492. "fingerprint": ["put-me-in-group2"],
  493. "timestamp": iso_format(self.group2.first_seen + timedelta(days=1)),
  494. "stacktrace": {"frames": [{"module": "group2"}]},
  495. "message": "group2",
  496. "tags": {"priority": priority},
  497. },
  498. project_id=self.project.id,
  499. )
  500. results = self.make_query(search_filter_query="priority:%s" % priority)
  501. assert set(results) == {self.group2}
  502. def test_search_filter_query_with_custom_priority_tag_and_priority_sort(self):
  503. priority = "high"
  504. for i in range(1, 3):
  505. self.store_event(
  506. data={
  507. "fingerprint": ["put-me-in-group1"],
  508. "timestamp": iso_format(self.group2.last_seen + timedelta(days=i)),
  509. "stacktrace": {"frames": [{"module": "group1"}]},
  510. "message": "group1",
  511. "tags": {"priority": priority},
  512. },
  513. project_id=self.project.id,
  514. )
  515. self.store_event(
  516. data={
  517. "fingerprint": ["put-me-in-group2"],
  518. "timestamp": iso_format(self.group2.last_seen + timedelta(days=2)),
  519. "stacktrace": {"frames": [{"module": "group2"}]},
  520. "message": "group2",
  521. "tags": {"priority": priority},
  522. },
  523. project_id=self.project.id,
  524. )
  525. results = self.make_query(search_filter_query="priority:%s" % priority, sort_by="priority")
  526. assert list(results) == [self.group1, self.group2]
  527. def test_search_tag_overlapping_with_internal_fields(self):
  528. # Using a tag of email overlaps with the promoted user.email column in events.
  529. # We don't want to bypass public schema limits in issue search.
  530. self.store_event(
  531. data={
  532. "fingerprint": ["put-me-in-group2"],
  533. "timestamp": iso_format(self.group2.first_seen + timedelta(days=1)),
  534. "stacktrace": {"frames": [{"module": "group2"}]},
  535. "message": "group2",
  536. "tags": {"email": "tags@example.com"},
  537. },
  538. project_id=self.project.id,
  539. )
  540. results = self.make_query(search_filter_query="email:tags@example.com")
  541. assert set(results) == {self.group2}
  542. def test_project(self):
  543. results = self.make_query([self.create_project(name="other")])
  544. assert set(results) == set()
  545. def test_pagination(self):
  546. for options_set in [
  547. {"snuba.search.min-pre-snuba-candidates": None},
  548. {"snuba.search.min-pre-snuba-candidates": 500},
  549. ]:
  550. with self.options(options_set):
  551. results = self.backend.query([self.project], limit=1, sort_by="date")
  552. assert set(results) == {self.group1}
  553. assert not results.prev.has_results
  554. assert results.next.has_results
  555. results = self.backend.query(
  556. [self.project], cursor=results.next, limit=1, sort_by="date"
  557. )
  558. assert set(results) == {self.group2}
  559. assert results.prev.has_results
  560. assert not results.next.has_results
  561. # note: previous cursor
  562. results = self.backend.query(
  563. [self.project], cursor=results.prev, limit=1, sort_by="date"
  564. )
  565. assert set(results) == {self.group1}
  566. assert results.prev.has_results
  567. assert results.next.has_results
  568. # note: previous cursor, paging too far into 0 results
  569. results = self.backend.query(
  570. [self.project], cursor=results.prev, limit=1, sort_by="date"
  571. )
  572. assert set(results) == set()
  573. assert not results.prev.has_results
  574. assert results.next.has_results
  575. results = self.backend.query(
  576. [self.project], cursor=results.next, limit=1, sort_by="date"
  577. )
  578. assert set(results) == {self.group1}
  579. assert results.prev.has_results
  580. assert results.next.has_results
  581. results = self.backend.query(
  582. [self.project], cursor=results.next, limit=1, sort_by="date"
  583. )
  584. assert set(results) == {self.group2}
  585. assert results.prev.has_results
  586. assert not results.next.has_results
  587. results = self.backend.query(
  588. [self.project], cursor=results.next, limit=1, sort_by="date"
  589. )
  590. assert set(results) == set()
  591. assert results.prev.has_results
  592. assert not results.next.has_results
  593. def test_pagination_with_environment(self):
  594. for dt in [
  595. self.group1.first_seen + timedelta(days=1),
  596. self.group1.first_seen + timedelta(days=2),
  597. self.group1.last_seen + timedelta(days=1),
  598. ]:
  599. self.store_event(
  600. data={
  601. "fingerprint": ["put-me-in-group2"],
  602. "timestamp": iso_format(dt),
  603. "environment": "production",
  604. "message": "group2",
  605. "stacktrace": {"frames": [{"module": "group2"}]},
  606. },
  607. project_id=self.project.id,
  608. )
  609. results = self.backend.query(
  610. [self.project],
  611. environments=[self.environments["production"]],
  612. sort_by="date",
  613. limit=1,
  614. count_hits=True,
  615. )
  616. assert list(results) == [self.group2]
  617. assert results.hits == 2
  618. results = self.backend.query(
  619. [self.project],
  620. environments=[self.environments["production"]],
  621. sort_by="date",
  622. limit=1,
  623. cursor=results.next,
  624. count_hits=True,
  625. )
  626. assert list(results) == [self.group1]
  627. assert results.hits == 2
  628. results = self.backend.query(
  629. [self.project],
  630. environments=[self.environments["production"]],
  631. sort_by="date",
  632. limit=1,
  633. cursor=results.next,
  634. count_hits=True,
  635. )
  636. assert list(results) == []
  637. assert results.hits == 2
  638. def test_age_filter(self):
  639. results = self.make_query(
  640. search_filter_query="firstSeen:>=%s" % date_to_query_format(self.group2.first_seen)
  641. )
  642. assert set(results) == {self.group2}
  643. results = self.make_query(
  644. search_filter_query="firstSeen:<=%s"
  645. % date_to_query_format(self.group1.first_seen + timedelta(minutes=1))
  646. )
  647. assert set(results) == {self.group1}
  648. results = self.make_query(
  649. search_filter_query="firstSeen:>=%s firstSeen:<=%s"
  650. % (
  651. date_to_query_format(self.group1.first_seen),
  652. date_to_query_format(self.group1.first_seen + timedelta(minutes=1)),
  653. )
  654. )
  655. assert set(results) == {self.group1}
  656. def test_age_filter_with_environment(self):
  657. # add time instead to make it greater than or less than as needed.
  658. group1_first_seen = GroupEnvironment.objects.get(
  659. environment=self.environments["production"], group=self.group1
  660. ).first_seen
  661. results = self.make_query(
  662. environments=[self.environments["production"]],
  663. search_filter_query="firstSeen:>=%s" % date_to_query_format(group1_first_seen),
  664. )
  665. assert set(results) == {self.group1}
  666. results = self.make_query(
  667. environments=[self.environments["production"]],
  668. search_filter_query="firstSeen:<=%s" % date_to_query_format(group1_first_seen),
  669. )
  670. assert set(results) == {self.group1}
  671. results = self.make_query(
  672. environments=[self.environments["production"]],
  673. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  674. )
  675. assert set(results) == set()
  676. self.store_event(
  677. data={
  678. "fingerprint": ["put-me-in-group1"],
  679. "timestamp": iso_format(group1_first_seen + timedelta(days=1)),
  680. "message": "group1",
  681. "stacktrace": {"frames": [{"module": "group1"}]},
  682. "environment": "development",
  683. },
  684. project_id=self.project.id,
  685. )
  686. results = self.make_query(
  687. environments=[self.environments["production"]],
  688. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  689. )
  690. assert set(results) == set()
  691. results = self.make_query(
  692. environments=[Environment.objects.get(name="development")],
  693. search_filter_query="firstSeen:>%s" % date_to_query_format(group1_first_seen),
  694. )
  695. assert set(results) == {self.group1}
  696. def test_times_seen_filter(self):
  697. results = self.make_query([self.project], search_filter_query="times_seen:2")
  698. assert set(results) == {self.group1}
  699. results = self.make_query([self.project], search_filter_query="times_seen:>=2")
  700. assert set(results) == {self.group1}
  701. results = self.make_query([self.project], search_filter_query="times_seen:<=1")
  702. assert set(results) == {self.group2}
  703. def test_last_seen_filter(self):
  704. results = self.make_query(
  705. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen)
  706. )
  707. assert set(results) == {self.group1}
  708. results = self.make_query(
  709. search_filter_query="lastSeen:>=%s lastSeen:<=%s"
  710. % (
  711. date_to_query_format(self.group1.last_seen),
  712. date_to_query_format(self.group1.last_seen + timedelta(minutes=1)),
  713. )
  714. )
  715. assert set(results) == {self.group1}
  716. def test_last_seen_filter_with_environment(self):
  717. results = self.make_query(
  718. environments=[self.environments["production"]],
  719. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen),
  720. )
  721. assert set(results) == {self.group1}
  722. results = self.make_query(
  723. environments=[self.environments["production"]],
  724. search_filter_query="lastSeen:<=%s" % date_to_query_format(self.group1.last_seen),
  725. )
  726. assert set(results) == {self.group1}
  727. results = self.make_query(
  728. environments=[self.environments["production"]],
  729. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  730. )
  731. assert set(results) == set()
  732. self.store_event(
  733. data={
  734. "fingerprint": ["put-me-in-group1"],
  735. "timestamp": iso_format(self.group1.last_seen + timedelta(days=1)),
  736. "message": "group1",
  737. "stacktrace": {"frames": [{"module": "group1"}]},
  738. "environment": "development",
  739. },
  740. project_id=self.project.id,
  741. )
  742. self.group1.update(last_seen=self.group1.last_seen + timedelta(days=1))
  743. results = self.make_query(
  744. environments=[self.environments["production"]],
  745. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  746. )
  747. assert set(results) == set()
  748. results = self.make_query(
  749. environments=[Environment.objects.get(name="development")],
  750. search_filter_query="lastSeen:>%s" % date_to_query_format(self.group1.last_seen),
  751. )
  752. assert set(results) == set()
  753. results = self.make_query(
  754. environments=[Environment.objects.get(name="development")],
  755. search_filter_query="lastSeen:>=%s" % date_to_query_format(self.group1.last_seen),
  756. )
  757. assert set(results) == {self.group1}
  758. def test_date_filter(self):
  759. results = self.make_query(
  760. date_from=self.event2.datetime,
  761. search_filter_query="timestamp:>=%s" % date_to_query_format(self.event2.datetime),
  762. )
  763. assert set(results) == {self.group1, self.group2}
  764. results = self.make_query(
  765. date_to=self.event1.datetime + timedelta(minutes=1),
  766. search_filter_query="timestamp:<=%s"
  767. % date_to_query_format(self.event1.datetime + timedelta(minutes=1)),
  768. )
  769. assert set(results) == {self.group1}
  770. results = self.make_query(
  771. date_from=self.event1.datetime,
  772. date_to=self.event2.datetime + timedelta(minutes=1),
  773. search_filter_query="timestamp:>=%s timestamp:<=%s"
  774. % (
  775. date_to_query_format(self.event1.datetime),
  776. date_to_query_format(self.event2.datetime + timedelta(minutes=1)),
  777. ),
  778. )
  779. assert set(results) == {self.group1, self.group2}
  780. # Test with `Z` utc marker, should be equivalent
  781. results = self.make_query(
  782. date_from=self.event1.datetime,
  783. date_to=self.event2.datetime + timedelta(minutes=1),
  784. search_filter_query="timestamp:>=%s timestamp:<=%s"
  785. % (
  786. date_to_query_format(self.event1.datetime) + "Z",
  787. date_to_query_format(self.event2.datetime + timedelta(minutes=1)) + "Z",
  788. ),
  789. )
  790. assert set(results) == {self.group1, self.group2}
  791. def test_date_filter_with_environment(self):
  792. results = self.backend.query(
  793. [self.project],
  794. environments=[self.environments["production"]],
  795. date_from=self.event2.datetime,
  796. )
  797. assert set(results) == {self.group1}
  798. results = self.backend.query(
  799. [self.project],
  800. environments=[self.environments["production"]],
  801. date_to=self.event1.datetime + timedelta(minutes=1),
  802. )
  803. assert set(results) == {self.group1}
  804. results = self.backend.query(
  805. [self.project],
  806. environments=[self.environments["staging"]],
  807. date_from=self.event1.datetime,
  808. date_to=self.event2.datetime + timedelta(minutes=1),
  809. )
  810. assert set(results) == {self.group2}
  811. def test_linked(self):
  812. linked_group1 = self.create_group_with_integration_external_issue()
  813. linked_group2 = self.create_group_with_platform_external_issue()
  814. results = self.make_query(search_filter_query="is:unlinked")
  815. assert set(results) == {self.group1, self.group2}
  816. results = self.make_query(search_filter_query="is:linked")
  817. assert set(results) == {linked_group1, linked_group2}
  818. def test_linked_with_only_integration_external_issue(self):
  819. linked_group = self.create_group_with_integration_external_issue()
  820. results = self.make_query(search_filter_query="is:unlinked")
  821. assert set(results) == {self.group1, self.group2}
  822. results = self.make_query(search_filter_query="is:linked")
  823. assert set(results) == {linked_group}
  824. def test_linked_with_only_platform_external_issue(self):
  825. linked_group = self.create_group_with_platform_external_issue()
  826. results = self.make_query(search_filter_query="is:unlinked")
  827. assert set(results) == {self.group1, self.group2}
  828. results = self.make_query(search_filter_query="is:linked")
  829. assert set(results) == {linked_group}
  830. def test_linked_with_environment(self):
  831. linked_group1 = self.create_group_with_integration_external_issue(environment="production")
  832. linked_group2 = self.create_group_with_platform_external_issue(environment="staging")
  833. results = self.make_query(
  834. environments=[self.environments["production"]], search_filter_query="is:unlinked"
  835. )
  836. assert set(results) == {self.group1}
  837. results = self.make_query(
  838. environments=[self.environments["staging"]], search_filter_query="is:unlinked"
  839. )
  840. assert set(results) == {self.group2}
  841. results = self.make_query(
  842. environments=[self.environments["production"]], search_filter_query="is:linked"
  843. )
  844. assert set(results) == {linked_group1}
  845. results = self.make_query(
  846. environments=[self.environments["staging"]], search_filter_query="is:linked"
  847. )
  848. assert set(results) == {linked_group2}
  849. def test_unassigned(self):
  850. results = self.make_query(search_filter_query="is:unassigned")
  851. assert set(results) == {self.group1}
  852. results = self.make_query(search_filter_query="is:assigned")
  853. assert set(results) == {self.group2}
  854. def test_unassigned_with_environment(self):
  855. results = self.make_query(
  856. environments=[self.environments["production"]], search_filter_query="is:unassigned"
  857. )
  858. assert set(results) == {self.group1}
  859. results = self.make_query(
  860. environments=[self.environments["staging"]], search_filter_query="is:assigned"
  861. )
  862. assert set(results) == {self.group2}
  863. results = self.make_query(
  864. environments=[self.environments["production"]], search_filter_query="is:assigned"
  865. )
  866. assert set(results) == set()
  867. def test_assigned_to(self):
  868. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  869. assert set(results) == {self.group2}
  870. # test team assignee
  871. ga = GroupAssignee.objects.get(
  872. user_id=self.user.id, group=self.group2, project=self.group2.project
  873. )
  874. ga.update(team=self.team, user_id=None)
  875. assert GroupAssignee.objects.get(id=ga.id).user_id is None
  876. results = self.make_query(search_filter_query="assigned:%s" % self.user.username)
  877. assert set(results) == {self.group2}
  878. # test when there should be no results
  879. other_user = self.create_user()
  880. results = self.make_query(search_filter_query="assigned:%s" % other_user.username)
  881. assert set(results) == set()
  882. owner = self.create_user()
  883. self.create_member(
  884. organization=self.project.organization, user=owner, role="owner", teams=[]
  885. )
  886. # test that owners don't see results for all teams
  887. results = self.make_query(search_filter_query="assigned:%s" % owner.username)
  888. assert set(results) == set()
  889. def test_assigned_to_in_syntax(self):
  890. group_3 = self.store_event(
  891. data={
  892. "fingerprint": ["put-me-in-group3"],
  893. "event_id": "c" * 32,
  894. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  895. },
  896. project_id=self.project.id,
  897. ).group
  898. group_3.status = GroupStatus.MUTED
  899. group_3.save()
  900. other_user = self.create_user()
  901. self.run_test_query_in_syntax(
  902. f"assigned:[{self.user.username}, {other_user.username}]",
  903. [self.group2],
  904. [self.group1, group_3],
  905. )
  906. GroupAssignee.objects.create(project=self.project, group=group_3, user_id=other_user.id)
  907. self.run_test_query_in_syntax(
  908. f"assigned:[{self.user.username}, {other_user.username}]",
  909. [self.group2, group_3],
  910. [self.group1],
  911. )
  912. self.run_test_query_in_syntax(
  913. f"assigned:[#{self.team.slug}, {other_user.username}]",
  914. [group_3],
  915. [self.group1, self.group2],
  916. )
  917. ga_2 = GroupAssignee.objects.get(
  918. user_id=self.user.id, group=self.group2, project=self.group2.project
  919. )
  920. ga_2.update(team=self.team, user_id=None)
  921. self.run_test_query_in_syntax(
  922. f"assigned:[{self.user.username}, {other_user.username}]",
  923. [self.group2, group_3],
  924. [self.group1],
  925. )
  926. self.run_test_query_in_syntax(
  927. f"assigned:[#{self.team.slug}, {other_user.username}]",
  928. [self.group2, group_3],
  929. [self.group1],
  930. )
  931. self.run_test_query_in_syntax(
  932. f"assigned:[me, none, {other_user.username}]",
  933. [self.group1, self.group2, group_3],
  934. [],
  935. )
  936. def test_assigned_or_suggested_in_syntax(self):
  937. Group.objects.all().delete()
  938. group = self.store_event(
  939. data={
  940. "timestamp": iso_format(before_now(seconds=180)),
  941. "fingerprint": ["group-1"],
  942. },
  943. project_id=self.project.id,
  944. ).group
  945. group1 = self.store_event(
  946. data={
  947. "timestamp": iso_format(before_now(seconds=185)),
  948. "fingerprint": ["group-2"],
  949. },
  950. project_id=self.project.id,
  951. ).group
  952. group2 = self.store_event(
  953. data={
  954. "timestamp": iso_format(before_now(seconds=190)),
  955. "fingerprint": ["group-3"],
  956. },
  957. project_id=self.project.id,
  958. ).group
  959. assigned_group = self.store_event(
  960. data={
  961. "timestamp": iso_format(before_now(seconds=195)),
  962. "fingerprint": ["group-4"],
  963. },
  964. project_id=self.project.id,
  965. ).group
  966. assigned_to_other_group = self.store_event(
  967. data={
  968. "timestamp": iso_format(before_now(seconds=195)),
  969. "fingerprint": ["group-5"],
  970. },
  971. project_id=self.project.id,
  972. ).group
  973. self.run_test_query_in_syntax(
  974. "assigned_or_suggested:[me]",
  975. [],
  976. [group, group1, group2, assigned_group, assigned_to_other_group],
  977. )
  978. GroupOwner.objects.create(
  979. group=assigned_to_other_group,
  980. project=self.project,
  981. organization=self.organization,
  982. type=0,
  983. team_id=None,
  984. user_id=self.user.id,
  985. )
  986. GroupOwner.objects.create(
  987. group=group,
  988. project=self.project,
  989. organization=self.organization,
  990. type=0,
  991. team_id=None,
  992. user_id=self.user.id,
  993. )
  994. self.run_test_query_in_syntax(
  995. "assigned_or_suggested:[me]",
  996. [group, assigned_to_other_group],
  997. [group1, group2, assigned_group],
  998. )
  999. # 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)
  1000. other_user = self.create_user("other@user.com", is_superuser=False)
  1001. GroupAssignee.objects.create(
  1002. group=assigned_to_other_group,
  1003. project=self.project,
  1004. user_id=other_user.id,
  1005. )
  1006. self.run_test_query_in_syntax(
  1007. "assigned_or_suggested:[me]",
  1008. [group],
  1009. [group1, group2, assigned_group, assigned_to_other_group],
  1010. )
  1011. self.run_test_query_in_syntax(
  1012. f"assigned_or_suggested:[{other_user.email}]",
  1013. [assigned_to_other_group],
  1014. [group, group1, group2, assigned_group],
  1015. )
  1016. GroupAssignee.objects.create(
  1017. group=assigned_group, project=self.project, user_id=self.user.id
  1018. )
  1019. self.run_test_query_in_syntax(
  1020. f"assigned_or_suggested:[{self.user.email}]",
  1021. [assigned_group, group],
  1022. )
  1023. GroupOwner.objects.create(
  1024. group=group,
  1025. project=self.project,
  1026. organization=self.organization,
  1027. type=0,
  1028. team_id=self.team.id,
  1029. user_id=None,
  1030. )
  1031. self.run_test_query_in_syntax(
  1032. f"assigned_or_suggested:[#{self.team.slug}]",
  1033. [group],
  1034. )
  1035. self.run_test_query_in_syntax(
  1036. "assigned_or_suggested:[me, none]",
  1037. [group, group1, group2, assigned_group],
  1038. [assigned_to_other_group],
  1039. )
  1040. not_me = self.create_user(email="notme@sentry.io")
  1041. GroupOwner.objects.create(
  1042. group=group2,
  1043. project=self.project,
  1044. organization=self.organization,
  1045. type=0,
  1046. team_id=None,
  1047. user_id=not_me.id,
  1048. )
  1049. self.run_test_query_in_syntax(
  1050. "assigned_or_suggested:[me, none]",
  1051. [group, group1, assigned_group],
  1052. [assigned_to_other_group, group2],
  1053. )
  1054. GroupOwner.objects.filter(group=group, user_id=self.user.id).delete()
  1055. self.run_test_query_in_syntax(
  1056. f"assigned_or_suggested:[me, none, #{self.team.slug}]",
  1057. [group, group1, assigned_group],
  1058. [assigned_to_other_group, group2],
  1059. )
  1060. self.run_test_query_in_syntax(
  1061. f"assigned_or_suggested:[me, none, #{self.team.slug}, {not_me.email}]",
  1062. [group, group1, assigned_group, group2],
  1063. [assigned_to_other_group],
  1064. )
  1065. def test_assigned_to_with_environment(self):
  1066. results = self.make_query(
  1067. environments=[self.environments["staging"]],
  1068. search_filter_query="assigned:%s" % self.user.username,
  1069. )
  1070. assert set(results) == {self.group2}
  1071. results = self.make_query(
  1072. environments=[self.environments["production"]],
  1073. search_filter_query="assigned:%s" % self.user.username,
  1074. )
  1075. assert set(results) == set()
  1076. def test_subscribed_by(self):
  1077. results = self.make_query(
  1078. [self.group1.project], search_filter_query="subscribed:%s" % self.user.username
  1079. )
  1080. assert set(results) == {self.group1}
  1081. def test_subscribed_by_in_syntax(self):
  1082. self.run_test_query_in_syntax(
  1083. f"subscribed:[{self.user.username}]", [self.group1], [self.group2]
  1084. )
  1085. user_2 = self.create_user()
  1086. GroupSubscription.objects.create(
  1087. user_id=user_2.id, group=self.group2, project=self.project, is_active=True
  1088. )
  1089. self.run_test_query_in_syntax(
  1090. f"subscribed:[{self.user.username}, {user_2.username}]", [self.group1, self.group2], []
  1091. )
  1092. def test_subscribed_by_with_environment(self):
  1093. results = self.make_query(
  1094. [self.group1.project],
  1095. environments=[self.environments["production"]],
  1096. search_filter_query="subscribed:%s" % self.user.username,
  1097. )
  1098. assert set(results) == {self.group1}
  1099. results = self.make_query(
  1100. [self.group1.project],
  1101. environments=[self.environments["staging"]],
  1102. search_filter_query="subscribed:%s" % self.user.username,
  1103. )
  1104. assert set(results) == set()
  1105. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1106. def test_snuba_not_called_optimization(self, query_mock):
  1107. assert self.make_query(search_filter_query="status:unresolved").results == [self.group1]
  1108. assert not query_mock.called
  1109. assert (
  1110. self.make_query(
  1111. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1112. sort_by="date",
  1113. ).results
  1114. == []
  1115. )
  1116. assert query_mock.called
  1117. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1118. def test_reduce_bulk_results_none_total(self, bulk_raw_query_mock):
  1119. bulk_raw_query_mock.return_value = [
  1120. {"data": [], "totals": {"total": None}},
  1121. {"data": [], "totals": {"total": None}},
  1122. ]
  1123. assert (
  1124. self.make_query(
  1125. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1126. sort_by="date",
  1127. ).results
  1128. == []
  1129. )
  1130. assert bulk_raw_query_mock.called
  1131. @mock.patch("sentry.search.snuba.executors.bulk_raw_query")
  1132. def test_reduce_bulk_results_none_data(self, bulk_raw_query_mock):
  1133. bulk_raw_query_mock.return_value = [
  1134. {"data": None, "totals": {"total": 0}},
  1135. {"data": None, "totals": {"total": 0}},
  1136. ]
  1137. assert (
  1138. self.make_query(
  1139. search_filter_query="last_seen:>%s" % date_to_query_format(timezone.now()),
  1140. sort_by="date",
  1141. ).results
  1142. == []
  1143. )
  1144. assert bulk_raw_query_mock.called
  1145. def test_pre_and_post_filtering(self):
  1146. prev_max_pre = options.get("snuba.search.max-pre-snuba-candidates")
  1147. options.set("snuba.search.max-pre-snuba-candidates", 1)
  1148. try:
  1149. # normal queries work as expected
  1150. results = self.make_query(search_filter_query="foo")
  1151. assert set(results) == {self.group1}
  1152. results = self.make_query(search_filter_query="bar")
  1153. assert set(results) == {self.group2}
  1154. # no candidate matches in Sentry, immediately return empty paginator
  1155. results = self.make_query(search_filter_query="NO MATCHES IN SENTRY")
  1156. assert set(results) == set()
  1157. # too many candidates, skip pre-filter, requires >1 postfilter queries
  1158. results = self.make_query()
  1159. assert set(results) == {self.group1, self.group2}
  1160. finally:
  1161. options.set("snuba.search.max-pre-snuba-candidates", prev_max_pre)
  1162. def test_optimizer_enabled(self):
  1163. prev_optimizer_enabled = options.get("snuba.search.pre-snuba-candidates-optimizer")
  1164. options.set("snuba.search.pre-snuba-candidates-optimizer", True)
  1165. try:
  1166. results = self.make_query(
  1167. search_filter_query="server:example.com",
  1168. environments=[self.environments["production"]],
  1169. )
  1170. assert set(results) == {self.group1}
  1171. finally:
  1172. options.set("snuba.search.pre-snuba-candidates-optimizer", prev_optimizer_enabled)
  1173. def test_search_out_of_range(self):
  1174. the_date = datetime(2000, 1, 1, 0, 0, 0, tzinfo=pytz.utc)
  1175. results = self.make_query(
  1176. search_filter_query=f"event.timestamp:>{the_date} event.timestamp:<{the_date}",
  1177. date_from=the_date,
  1178. date_to=the_date,
  1179. )
  1180. assert set(results) == set()
  1181. def test_hits_estimate(self):
  1182. # 400 Groups/Events
  1183. # Every 3rd one is Unresolved
  1184. # Every 2nd one has tag match=1
  1185. for i in range(400):
  1186. event = self.store_event(
  1187. data={
  1188. "event_id": md5(f"event {i}".encode()).hexdigest(),
  1189. "fingerprint": [f"put-me-in-group{i}"],
  1190. "timestamp": iso_format(self.base_datetime - timedelta(days=21)),
  1191. "message": f"group {i} event",
  1192. "stacktrace": {"frames": [{"module": f"module {i}"}]},
  1193. "tags": {"match": f"{i % 2}"},
  1194. "environment": "production",
  1195. },
  1196. project_id=self.project.id,
  1197. )
  1198. group = event.group
  1199. group.times_seen = 5
  1200. group.status = GroupStatus.UNRESOLVED if i % 3 == 0 else GroupStatus.RESOLVED
  1201. group.save()
  1202. self.store_group(group)
  1203. # Sample should estimate there are roughly 66 overall matching groups
  1204. # based on a random sample of 100 (or $sample_size) of the total 200
  1205. # snuba matches, of which 33% should pass the postgres filter.
  1206. with self.options(
  1207. {
  1208. # Too small to pass all django candidates down to snuba
  1209. "snuba.search.max-pre-snuba-candidates": 5,
  1210. "snuba.search.hits-sample-size": 50,
  1211. }
  1212. ):
  1213. first_results = self.make_query(
  1214. search_filter_query="is:unresolved match:1", limit=10, count_hits=True
  1215. )
  1216. # Deliberately do not assert that the value is within some margin
  1217. # of error, as this will fail tests at some rate corresponding to
  1218. # our confidence interval.
  1219. assert first_results.hits > 10
  1220. # When searching for the same tags, we should get the same set of
  1221. # hits as the sampling is based on the hash of the query.
  1222. second_results = self.make_query(
  1223. search_filter_query="is:unresolved match:1", limit=10, count_hits=True
  1224. )
  1225. assert first_results.results == second_results.results
  1226. # When using a different search, we should get a different sample
  1227. # but still should have some hits.
  1228. third_results = self.make_query(
  1229. search_filter_query="is:unresolved match:0", limit=10, count_hits=True
  1230. )
  1231. assert third_results.hits > 10
  1232. assert third_results.results != second_results.results
  1233. def test_regressed_in_release(self):
  1234. # expect no groups within the results since there are no releases
  1235. results = self.make_query(search_filter_query="regressed_in_release:fake")
  1236. assert set(results) == set()
  1237. # expect no groups even though there is a release; since no group regressed in this release
  1238. release_1 = self.create_release()
  1239. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1240. assert set(results) == set()
  1241. # Create a new event so that we get a group in this release
  1242. group = self.store_event(
  1243. data={
  1244. "release": release_1.version,
  1245. },
  1246. project_id=self.project.id,
  1247. ).group
  1248. # # Should still be no group since we didn't regress in this release
  1249. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1250. assert set(results) == set()
  1251. record_group_history(group, GroupHistoryStatus.REGRESSED, release=release_1)
  1252. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1253. assert set(results) == {group}
  1254. # Make sure this works correctly with multiple releases
  1255. release_2 = self.create_release()
  1256. group_2 = self.store_event(
  1257. data={
  1258. "fingerprint": ["put-me-in-group9001"],
  1259. "event_id": "a" * 32,
  1260. "release": release_2.version,
  1261. },
  1262. project_id=self.project.id,
  1263. ).group
  1264. record_group_history(group_2, GroupHistoryStatus.REGRESSED, release=release_2)
  1265. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_1.version)
  1266. assert set(results) == {group}
  1267. results = self.make_query(search_filter_query="regressed_in_release:%s" % release_2.version)
  1268. assert set(results) == {group_2}
  1269. def test_first_release(self):
  1270. # expect no groups within the results since there are no releases
  1271. results = self.make_query(search_filter_query="first_release:%s" % "fake")
  1272. assert set(results) == set()
  1273. # expect no groups even though there is a release; since no group
  1274. # is attached to a release
  1275. release_1 = self.create_release(self.project)
  1276. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1277. assert set(results) == set()
  1278. # Create a new event so that we get a group in this release
  1279. group = self.store_event(
  1280. data={
  1281. "fingerprint": ["put-me-in-group9001"],
  1282. "event_id": "a" * 32,
  1283. "message": "hello",
  1284. "environment": "production",
  1285. "tags": {"server": "example.com"},
  1286. "release": release_1.version,
  1287. "stacktrace": {"frames": [{"module": "group1"}]},
  1288. },
  1289. project_id=self.project.id,
  1290. ).group
  1291. results = self.make_query(search_filter_query="first_release:%s" % release_1.version)
  1292. assert set(results) == {group}
  1293. def test_first_release_in_syntax(self):
  1294. # expect no groups within the results since there are no releases
  1295. self.run_test_query_in_syntax("first_release:[fake, fake2]", [])
  1296. # expect no groups even though there is a release; since no group
  1297. # is attached to a release
  1298. release_1 = self.create_release(self.project)
  1299. release_2 = self.create_release(self.project)
  1300. self.run_test_query_in_syntax(
  1301. f"first_release:[{release_1.version}, {release_2.version}]", []
  1302. )
  1303. # Create a new event so that we get a group in this release
  1304. group = self.store_event(
  1305. data={
  1306. "fingerprint": ["put-me-in-group9001"],
  1307. "event_id": "a" * 32,
  1308. "message": "hello",
  1309. "environment": "production",
  1310. "tags": {"server": "example.com"},
  1311. "release": release_1.version,
  1312. "stacktrace": {"frames": [{"module": "group1"}]},
  1313. },
  1314. project_id=self.project.id,
  1315. ).group
  1316. self.run_test_query_in_syntax(
  1317. f"first_release:[{release_1.version}, {release_2.version}]",
  1318. [group],
  1319. [self.group1, self.group2],
  1320. )
  1321. # Create a new event so that we get a group in this release
  1322. group_2 = self.store_event(
  1323. data={
  1324. "fingerprint": ["put-me-in-group9002"],
  1325. "event_id": "a" * 32,
  1326. "message": "hello",
  1327. "environment": "production",
  1328. "tags": {"server": "example.com"},
  1329. "release": release_2.version,
  1330. "stacktrace": {"frames": [{"module": "group1"}]},
  1331. },
  1332. project_id=self.project.id,
  1333. ).group
  1334. self.run_test_query_in_syntax(
  1335. f"first_release:[{release_1.version}, {release_2.version}]",
  1336. [group, group_2],
  1337. )
  1338. def test_first_release_environments(self):
  1339. results = self.make_query(
  1340. environments=[self.environments["production"]],
  1341. search_filter_query="first_release:fake",
  1342. )
  1343. assert set(results) == set()
  1344. release = self.create_release(self.project)
  1345. group_env = GroupEnvironment.get_or_create(
  1346. group_id=self.group1.id, environment_id=self.environments["production"].id
  1347. )[0]
  1348. results = self.make_query(
  1349. environments=[self.environments["production"]],
  1350. search_filter_query=f"first_release:{release.version}",
  1351. )
  1352. assert set(results) == set()
  1353. group_env.first_release = release
  1354. group_env.save()
  1355. results = self.make_query(
  1356. environments=[self.environments["production"]],
  1357. search_filter_query=f"first_release:{release.version}",
  1358. )
  1359. assert set(results) == {self.group1}
  1360. def test_first_release_environments_in_syntax(self):
  1361. self.run_test_query_in_syntax(
  1362. "first_release:[fake, fake2]",
  1363. [],
  1364. [self.group1, self.group2],
  1365. environments=[self.environments["production"]],
  1366. )
  1367. release = self.create_release(self.project)
  1368. group_1_env = GroupEnvironment.objects.get(
  1369. group_id=self.group1.id, environment_id=self.environments["production"].id
  1370. )
  1371. group_1_env.update(first_release=release)
  1372. self.run_test_query_in_syntax(
  1373. f"first_release:[{release.version}, fake2]",
  1374. [self.group1],
  1375. [self.group2],
  1376. environments=[self.environments["production"]],
  1377. )
  1378. group_2_env = GroupEnvironment.objects.get(
  1379. group_id=self.group2.id, environment_id=self.environments["staging"].id
  1380. )
  1381. group_2_env.update(first_release=release)
  1382. self.run_test_query_in_syntax(
  1383. f"first_release:[{release.version}, fake2]",
  1384. [self.group1, self.group2],
  1385. [],
  1386. environments=[self.environments["production"], self.environments["staging"]],
  1387. )
  1388. # Make sure we don't get duplicate groups
  1389. GroupEnvironment.objects.create(
  1390. group_id=self.group1.id,
  1391. environment_id=self.environments["staging"].id,
  1392. first_release=release,
  1393. )
  1394. self.run_test_query_in_syntax(
  1395. f"first_release:[{release.version}, fake2]",
  1396. [self.group1, self.group2],
  1397. [],
  1398. environments=[self.environments["production"], self.environments["staging"]],
  1399. )
  1400. def test_query_enclosed_in_quotes(self):
  1401. results = self.make_query(search_filter_query='"foo"')
  1402. assert set(results) == {self.group1}
  1403. results = self.make_query(search_filter_query='"bar"')
  1404. assert set(results) == {self.group2}
  1405. @xfail_if_not_postgres("Wildcard searching only supported in Postgres")
  1406. def test_wildcard(self):
  1407. escaped_event = self.store_event(
  1408. data={
  1409. "fingerprint": ["hello-there"],
  1410. "event_id": "f" * 32,
  1411. "message": "somet[hing]",
  1412. "environment": "production",
  1413. "tags": {"server": "example.net"},
  1414. "timestamp": iso_format(self.base_datetime),
  1415. "stacktrace": {"frames": [{"module": "group1"}]},
  1416. },
  1417. project_id=self.project.id,
  1418. )
  1419. # Note: Adding in `environment:production` so that we make sure we query
  1420. # in both snuba and postgres
  1421. results = self.make_query(search_filter_query="environment:production so*t")
  1422. assert set(results) == {escaped_event.group}
  1423. # Make sure it's case insensitive
  1424. results = self.make_query(search_filter_query="environment:production SO*t")
  1425. assert set(results) == {escaped_event.group}
  1426. results = self.make_query(search_filter_query="environment:production so*zz")
  1427. assert set(results) == set()
  1428. results = self.make_query(search_filter_query="environment:production [hing]")
  1429. assert set(results) == {escaped_event.group}
  1430. results = self.make_query(search_filter_query="environment:production s*]")
  1431. assert set(results) == {escaped_event.group}
  1432. results = self.make_query(search_filter_query="environment:production server:example.*")
  1433. assert set(results) == {self.group1, escaped_event.group}
  1434. results = self.make_query(search_filter_query="environment:production !server:*net")
  1435. assert set(results) == {self.group1}
  1436. # TODO: Disabling tests that use [] syntax for the moment. Re-enable
  1437. # these if we decide to add back in, or remove if this comment has been
  1438. # here a while.
  1439. # results = self.make_query(
  1440. # search_filter_query='environment:production [s][of][mz]',
  1441. # )
  1442. # assert set(results) == set([escaped_event.group])
  1443. # results = self.make_query(
  1444. # search_filter_query='environment:production [z][of][mz]',
  1445. # )
  1446. # assert set(results) == set()
  1447. def test_null_tags(self):
  1448. tag_event = self.store_event(
  1449. data={
  1450. "fingerprint": ["hello-there"],
  1451. "event_id": "f" * 32,
  1452. "message": "something",
  1453. "environment": "production",
  1454. "tags": {"server": "example.net"},
  1455. "timestamp": iso_format(self.base_datetime),
  1456. "stacktrace": {"frames": [{"module": "group1"}]},
  1457. },
  1458. project_id=self.project.id,
  1459. )
  1460. no_tag_event = self.store_event(
  1461. data={
  1462. "fingerprint": ["hello-there-2"],
  1463. "event_id": "5" * 32,
  1464. "message": "something",
  1465. "environment": "production",
  1466. "timestamp": iso_format(self.base_datetime),
  1467. "stacktrace": {"frames": [{"module": "group2"}]},
  1468. },
  1469. project_id=self.project.id,
  1470. )
  1471. results = self.make_query(search_filter_query="environment:production !server:*net")
  1472. assert set(results) == {self.group1, no_tag_event.group}
  1473. results = self.make_query(search_filter_query="environment:production server:*net")
  1474. assert set(results) == {tag_event.group}
  1475. results = self.make_query(search_filter_query="environment:production !server:example.net")
  1476. assert set(results) == {self.group1, no_tag_event.group}
  1477. results = self.make_query(search_filter_query="environment:production server:example.net")
  1478. assert set(results) == {tag_event.group}
  1479. results = self.make_query(search_filter_query="environment:production has:server")
  1480. assert set(results) == {self.group1, tag_event.group}
  1481. results = self.make_query(search_filter_query="environment:production !has:server")
  1482. assert set(results) == {no_tag_event.group}
  1483. def test_null_promoted_tags(self):
  1484. tag_event = self.store_event(
  1485. data={
  1486. "fingerprint": ["hello-there"],
  1487. "event_id": "f" * 32,
  1488. "message": "something",
  1489. "environment": "production",
  1490. "tags": {"logger": "csp"},
  1491. "timestamp": iso_format(self.base_datetime),
  1492. "stacktrace": {"frames": [{"module": "group1"}]},
  1493. },
  1494. project_id=self.project.id,
  1495. )
  1496. no_tag_event = self.store_event(
  1497. data={
  1498. "fingerprint": ["hello-there-2"],
  1499. "event_id": "5" * 32,
  1500. "message": "something",
  1501. "environment": "production",
  1502. "timestamp": iso_format(self.base_datetime),
  1503. "stacktrace": {"frames": [{"module": "group2"}]},
  1504. },
  1505. project_id=self.project.id,
  1506. )
  1507. results = self.make_query(search_filter_query="environment:production !logger:*sp")
  1508. assert set(results) == {self.group1, no_tag_event.group}
  1509. results = self.make_query(search_filter_query="environment:production logger:*sp")
  1510. assert set(results) == {tag_event.group}
  1511. results = self.make_query(search_filter_query="environment:production !logger:csp")
  1512. assert set(results) == {self.group1, no_tag_event.group}
  1513. results = self.make_query(search_filter_query="environment:production logger:csp")
  1514. assert set(results) == {tag_event.group}
  1515. results = self.make_query(search_filter_query="environment:production has:logger")
  1516. assert set(results) == {tag_event.group}
  1517. results = self.make_query(search_filter_query="environment:production !has:logger")
  1518. assert set(results) == {self.group1, no_tag_event.group}
  1519. def test_sort_multi_project(self):
  1520. self.set_up_multi_project()
  1521. results = self.make_query([self.project, self.project2], sort_by="date")
  1522. assert list(results) == [self.group1, self.group_p2, self.group2]
  1523. results = self.make_query([self.project, self.project2], sort_by="new")
  1524. assert list(results) == [self.group2, self.group_p2, self.group1]
  1525. results = self.make_query([self.project, self.project2], sort_by="freq")
  1526. assert list(results) == [self.group1, self.group_p2, self.group2]
  1527. results = self.make_query([self.project, self.project2], sort_by="priority")
  1528. assert list(results) == [self.group1, self.group2, self.group_p2]
  1529. results = self.make_query([self.project, self.project2], sort_by="user")
  1530. assert list(results) == [self.group1, self.group2, self.group_p2]
  1531. def test_sort_trend(self):
  1532. start = self.group1.first_seen - timedelta(days=1)
  1533. end = before_now(days=1).replace(tzinfo=pytz.utc)
  1534. middle = start + ((end - start) / 2)
  1535. self.store_event(
  1536. data={
  1537. "fingerprint": ["put-me-in-group1"],
  1538. "event_id": "2" * 32,
  1539. "message": "something",
  1540. "timestamp": iso_format(self.base_datetime),
  1541. },
  1542. project_id=self.project.id,
  1543. )
  1544. self.store_event(
  1545. data={
  1546. "fingerprint": ["put-me-in-group1"],
  1547. "event_id": "3" * 32,
  1548. "message": "something",
  1549. "timestamp": iso_format(self.base_datetime),
  1550. },
  1551. project_id=self.project.id,
  1552. )
  1553. fewer_events_group = self.store_event(
  1554. data={
  1555. "fingerprint": ["put-me-in-group4"],
  1556. "event_id": "4" * 32,
  1557. "message": "something",
  1558. "timestamp": iso_format(middle - timedelta(days=1)),
  1559. },
  1560. project_id=self.project.id,
  1561. ).group
  1562. self.store_event(
  1563. data={
  1564. "fingerprint": ["put-me-in-group4"],
  1565. "event_id": "5" * 32,
  1566. "message": "something",
  1567. "timestamp": iso_format(middle - timedelta(days=1)),
  1568. },
  1569. project_id=self.project.id,
  1570. )
  1571. self.store_event(
  1572. data={
  1573. "fingerprint": ["put-me-in-group4"],
  1574. "event_id": "6" * 32,
  1575. "message": "something",
  1576. "timestamp": iso_format(self.base_datetime),
  1577. },
  1578. project_id=self.project.id,
  1579. )
  1580. no_before_group = self.store_event(
  1581. data={
  1582. "fingerprint": ["put-me-in-group5"],
  1583. "event_id": "3" * 32,
  1584. "message": "something",
  1585. "timestamp": iso_format(self.base_datetime),
  1586. },
  1587. project_id=self.project.id,
  1588. ).group
  1589. no_after_group = self.store_event(
  1590. data={
  1591. "fingerprint": ["put-me-in-group6"],
  1592. "event_id": "4" * 32,
  1593. "message": "something",
  1594. "timestamp": iso_format(middle - timedelta(days=1)),
  1595. },
  1596. project_id=self.project.id,
  1597. ).group
  1598. self.set_up_multi_project()
  1599. results = self.make_query([self.project], sort_by="trend", date_from=start, date_to=end)
  1600. assert results[:2] == [self.group1, fewer_events_group]
  1601. # These will be arbitrarily ordered since their trend values are all 0
  1602. assert set(results[2:]) == {self.group2, no_before_group, no_after_group}
  1603. def test_in_syntax_is_invalid(self):
  1604. with pytest.raises(InvalidSearchQuery, match='"in" syntax invalid for "is" search'):
  1605. self.make_query(search_filter_query="is:[unresolved, resolved]")
  1606. def test_first_release_any_or_no_environments(self):
  1607. # test scenarios for tickets:
  1608. # SEN-571
  1609. # ISSUE-432
  1610. # given the following setup:
  1611. #
  1612. # groups table:
  1613. # group first_release
  1614. # A 1
  1615. # B 1
  1616. # C 2
  1617. #
  1618. # groupenvironments table:
  1619. # group environment first_release
  1620. # A staging 1
  1621. # A production 2
  1622. #
  1623. # when querying by first release, the appropriate set of groups should be displayed:
  1624. #
  1625. # first_release: 1
  1626. # env=[]: A, B
  1627. # env=[production, staging]: A
  1628. # env=[staging]: A
  1629. # env=[production]: nothing
  1630. #
  1631. # first_release: 2
  1632. # env=[]: A, C
  1633. # env=[production, staging]: A
  1634. # env=[staging]: nothing
  1635. # env=[production]: A
  1636. # create an issue/group whose events that occur in 2 distinct environments
  1637. group_a_event_1 = self.store_event(
  1638. data={
  1639. "fingerprint": ["group_a"],
  1640. "event_id": "aaa" + ("1" * 29),
  1641. "environment": "example_staging",
  1642. "release": "release_1",
  1643. },
  1644. project_id=self.project.id,
  1645. )
  1646. group_a_event_2 = self.store_event(
  1647. data={
  1648. "fingerprint": ["group_a"],
  1649. "event_id": "aaa" + ("2" * 29),
  1650. "environment": "example_production",
  1651. "release": "release_2",
  1652. },
  1653. project_id=self.project.id,
  1654. )
  1655. group_a = group_a_event_1.group
  1656. # get the environments for group_a
  1657. prod_env = group_a_event_2.get_environment()
  1658. staging_env = group_a_event_1.get_environment()
  1659. # create an issue/group whose event that occur in no environments
  1660. # but will be tied to release release_1
  1661. group_b_event_1 = self.store_event(
  1662. data={
  1663. "fingerprint": ["group_b"],
  1664. "event_id": "bbb" + ("1" * 29),
  1665. "release": "release_1",
  1666. },
  1667. project_id=self.project.id,
  1668. )
  1669. assert group_b_event_1.get_environment().name == "" # has no environment
  1670. group_b = group_b_event_1.group
  1671. # create an issue/group whose event that occur in no environments
  1672. # but will be tied to release release_2
  1673. group_c_event_1 = self.store_event(
  1674. data={
  1675. "fingerprint": ["group_c"],
  1676. "event_id": "ccc" + ("1" * 29),
  1677. "release": "release_2",
  1678. },
  1679. project_id=self.project.id,
  1680. )
  1681. assert group_c_event_1.get_environment().name == "" # has no environment
  1682. group_c = group_c_event_1.group
  1683. # query by release release_1
  1684. results = self.make_query(search_filter_query="first_release:%s" % "release_1")
  1685. assert set(results) == {group_a, group_b}
  1686. results = self.make_query(
  1687. environments=[staging_env, prod_env],
  1688. search_filter_query="first_release:%s" % "release_1",
  1689. )
  1690. assert set(results) == {group_a}
  1691. results = self.make_query(
  1692. environments=[staging_env], search_filter_query="first_release:%s" % "release_1"
  1693. )
  1694. assert set(results) == {group_a}
  1695. results = self.make_query(
  1696. environments=[prod_env], search_filter_query="first_release:%s" % "release_1"
  1697. )
  1698. assert set(results) == set()
  1699. # query by release release_2
  1700. results = self.make_query(search_filter_query="first_release:%s" % "release_2")
  1701. assert set(results) == {group_a, group_c}
  1702. results = self.make_query(
  1703. environments=[staging_env, prod_env],
  1704. search_filter_query="first_release:%s" % "release_2",
  1705. )
  1706. assert set(results) == {group_a}
  1707. results = self.make_query(
  1708. environments=[staging_env], search_filter_query="first_release:%s" % "release_2"
  1709. )
  1710. assert set(results) == set()
  1711. results = self.make_query(
  1712. environments=[prod_env], search_filter_query="first_release:%s" % "release_2"
  1713. )
  1714. assert set(results) == {group_a}
  1715. def test_all_fields_do_not_error(self):
  1716. # Just a sanity check to make sure that all fields can be successfully
  1717. # searched on without returning type errors and other schema related
  1718. # issues.
  1719. def test_query(query):
  1720. try:
  1721. self.make_query(search_filter_query=query)
  1722. except SnubaError as e:
  1723. self.fail(f"Query {query} errored. Error info: {e}")
  1724. for key in SENTRY_SNUBA_MAP:
  1725. if key in ["project.id", "issue.id", "performance.issue_ids"]:
  1726. continue
  1727. test_query("has:%s" % key)
  1728. test_query("!has:%s" % key)
  1729. if key == "error.handled":
  1730. val = 1
  1731. elif key in issue_search_config.numeric_keys:
  1732. val = "123"
  1733. elif key in issue_search_config.date_keys:
  1734. val = self.base_datetime.isoformat()
  1735. elif key in issue_search_config.boolean_keys:
  1736. val = "true"
  1737. elif key in {"trace.span", "trace.parent_span"}:
  1738. val = "abcdef1234abcdef"
  1739. test_query(f"!{key}:{val}")
  1740. else:
  1741. val = "abadcafedeadbeefdeaffeedabadfeed"
  1742. test_query(f"!{key}:{val}")
  1743. test_query(f"{key}:{val}")
  1744. def test_message_negation(self):
  1745. self.store_event(
  1746. data={
  1747. "fingerprint": ["put-me-in-group1"],
  1748. "event_id": "2" * 32,
  1749. "message": "something",
  1750. "timestamp": iso_format(self.base_datetime),
  1751. },
  1752. project_id=self.project.id,
  1753. )
  1754. results = self.make_query(search_filter_query="!message:else")
  1755. results2 = self.make_query(search_filter_query="!message:else")
  1756. assert list(results) == list(results2)
  1757. class EventsTransactionsSnubaSearchTest(SharedSnubaTest):
  1758. @property
  1759. def backend(self):
  1760. return EventsDatasetSnubaSearchBackend()
  1761. def setUp(self):
  1762. super().setUp()
  1763. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  1764. transaction_event_data = {
  1765. "level": "info",
  1766. "message": "ayoo",
  1767. "type": "transaction",
  1768. "culprit": "app/components/events/eventEntries in map",
  1769. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  1770. }
  1771. transaction_event_1 = self.store_event(
  1772. data={
  1773. **transaction_event_data,
  1774. "event_id": "a" * 32,
  1775. "timestamp": iso_format(before_now(minutes=1)),
  1776. "start_timestamp": iso_format(before_now(minutes=1, seconds=5)),
  1777. "tags": {"my_tag": 1},
  1778. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group1"],
  1779. },
  1780. project_id=self.project.id,
  1781. )
  1782. self.perf_group_1 = transaction_event_1.groups[0]
  1783. transaction_event_2 = self.store_event(
  1784. data={
  1785. **transaction_event_data,
  1786. "event_id": "a" * 32,
  1787. "timestamp": iso_format(before_now(minutes=2)),
  1788. "start_timestamp": iso_format(before_now(minutes=2, seconds=5)),
  1789. "tags": {"my_tag": 1},
  1790. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group2"],
  1791. },
  1792. project_id=self.project.id,
  1793. )
  1794. self.perf_group_2 = transaction_event_2.groups[0]
  1795. error_event_data = {
  1796. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  1797. "message": "bar",
  1798. "environment": "staging",
  1799. "tags": {
  1800. "server": "example.com",
  1801. "url": "http://example.com",
  1802. "sentry:user": "event2@example.com",
  1803. "my_tag": 1,
  1804. },
  1805. }
  1806. error_event = self.store_event(
  1807. data={
  1808. **error_event_data,
  1809. "fingerprint": ["put-me-in-error_group_1"],
  1810. "event_id": "c" * 32,
  1811. "stacktrace": {"frames": [{"module": "error_group_1"}]},
  1812. },
  1813. project_id=self.project.id,
  1814. )
  1815. self.error_group_1 = error_event.group
  1816. error_event_2 = self.store_event(
  1817. data={
  1818. **error_event_data,
  1819. "fingerprint": ["put-me-in-error_group_2"],
  1820. "event_id": "d" * 32,
  1821. "stacktrace": {"frames": [{"module": "error_group_2"}]},
  1822. },
  1823. project_id=self.project.id,
  1824. )
  1825. self.error_group_2 = error_event_2.group
  1826. def test_performance_query(self):
  1827. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  1828. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1829. results = self.make_query(
  1830. search_filter_query="issue.type:[performance_n_plus_one_db_queries, performance_render_blocking_asset_span] my_tag:1"
  1831. )
  1832. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1833. def test_performance_issue_search_feature_off(self):
  1834. with Feature({"organizations:performance-issues-search": False}):
  1835. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  1836. assert list(results) == []
  1837. with Feature({"organizations:performance-issues-search": True}):
  1838. results = self.make_query(search_filter_query="issue.category:performance my_tag:1")
  1839. assert list(results) == [self.perf_group_1, self.perf_group_2]
  1840. def test_error_performance_query(self):
  1841. results = self.make_query(search_filter_query="my_tag:1")
  1842. assert list(results) == [
  1843. self.perf_group_1,
  1844. self.perf_group_2,
  1845. self.error_group_2,
  1846. self.error_group_1,
  1847. ]
  1848. results = self.make_query(
  1849. search_filter_query="issue.category:[performance, error] my_tag:1"
  1850. )
  1851. assert list(results) == [
  1852. self.perf_group_1,
  1853. self.perf_group_2,
  1854. self.error_group_2,
  1855. self.error_group_1,
  1856. ]
  1857. results = self.make_query(
  1858. search_filter_query="issue.type:[performance_render_blocking_asset_span, error] my_tag:1"
  1859. )
  1860. assert list(results) == [
  1861. self.perf_group_1,
  1862. self.perf_group_2,
  1863. self.error_group_2,
  1864. self.error_group_1,
  1865. ]
  1866. def test_cursor_performance_issues(self):
  1867. results = self.make_query(
  1868. projects=[self.project],
  1869. search_filter_query="issue.category:performance my_tag:1",
  1870. sort_by="date",
  1871. limit=1,
  1872. count_hits=True,
  1873. )
  1874. assert list(results) == [self.perf_group_1]
  1875. assert results.hits == 2
  1876. results = self.make_query(
  1877. projects=[self.project],
  1878. search_filter_query="issue.category:performance my_tag:1",
  1879. sort_by="date",
  1880. limit=1,
  1881. cursor=results.next,
  1882. count_hits=True,
  1883. )
  1884. assert list(results) == [self.perf_group_2]
  1885. assert results.hits == 2
  1886. results = self.make_query(
  1887. projects=[self.project],
  1888. search_filter_query="issue.category:performance my_tag:1",
  1889. sort_by="date",
  1890. limit=1,
  1891. cursor=results.next,
  1892. count_hits=True,
  1893. )
  1894. assert list(results) == []
  1895. assert results.hits == 2
  1896. def test_perf_issue_search_message_term_queries_postgres(self):
  1897. from django.db.models import Q
  1898. from sentry.utils import snuba
  1899. transaction_name = "im a little tea pot"
  1900. tx = self.store_event(
  1901. data={
  1902. "level": "info",
  1903. "culprit": "app/components/events/eventEntries in map",
  1904. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  1905. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  1906. "event_id": "e" * 32,
  1907. "timestamp": iso_format(self.base_datetime),
  1908. "start_timestamp": iso_format(self.base_datetime),
  1909. "type": "transaction",
  1910. "transaction": transaction_name,
  1911. },
  1912. project_id=self.project.id,
  1913. )
  1914. assert "tea" in tx.search_message
  1915. created_group = tx.groups[0]
  1916. find_group = Group.objects.filter(
  1917. Q(type=PerformanceRenderBlockingAssetSpanGroupType.type_id, message__icontains="tea")
  1918. ).first()
  1919. assert created_group == find_group
  1920. result = snuba.raw_query(
  1921. dataset=snuba.Dataset.Transactions,
  1922. start=self.base_datetime - timedelta(hours=1),
  1923. end=self.base_datetime + timedelta(hours=1),
  1924. selected_columns=[
  1925. "event_id",
  1926. "group_ids",
  1927. "transaction_name",
  1928. ],
  1929. groupby=None,
  1930. filter_keys={"project_id": [self.project.id], "event_id": [tx.event_id]},
  1931. referrer="_insert_transaction.verify_transaction",
  1932. )
  1933. assert result["data"][0]["transaction_name"] == transaction_name
  1934. assert result["data"][0]["group_ids"] == [created_group.id]
  1935. results = self.make_query(search_filter_query="issue.category:performance tea")
  1936. assert set(results) == {created_group}
  1937. results2 = self.make_query(search_filter_query="tea")
  1938. assert set(results2) == {created_group}
  1939. def test_search_message_error_and_perf_issues(self):
  1940. tx = self.store_event(
  1941. data={
  1942. "level": "info",
  1943. "culprit": "app/components/events/eventEntries in map",
  1944. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  1945. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  1946. "event_id": "e" * 32,
  1947. "timestamp": iso_format(self.base_datetime),
  1948. "start_timestamp": iso_format(self.base_datetime),
  1949. "type": "transaction",
  1950. "transaction": "/api/0/events",
  1951. },
  1952. project_id=self.project.id,
  1953. )
  1954. perf_issue = tx.groups[0]
  1955. assert perf_issue
  1956. error = self.store_event(
  1957. data={
  1958. "fingerprint": ["another-random-group"],
  1959. "event_id": "d" * 32,
  1960. "message": "Uncaught exception on api /api/0/events",
  1961. "environment": "production",
  1962. "tags": {"server": "example.com", "sentry:user": "event3@example.com"},
  1963. "timestamp": iso_format(self.base_datetime),
  1964. "stacktrace": {"frames": [{"module": "group1"}]},
  1965. },
  1966. project_id=self.project.id,
  1967. )
  1968. error_issue = error.group
  1969. assert error_issue
  1970. assert error_issue != perf_issue
  1971. assert set(self.make_query(search_filter_query="is:unresolved /api/0/events")) == {
  1972. perf_issue,
  1973. error_issue,
  1974. }
  1975. assert set(self.make_query(search_filter_query="/api/0/events")) == {
  1976. error_issue,
  1977. perf_issue,
  1978. }
  1979. def test_compound_message_negation(self):
  1980. self.store_event(
  1981. data={
  1982. "fingerprint": ["put-me-in-group1"],
  1983. "event_id": "2" * 32,
  1984. "message": "something",
  1985. "timestamp": iso_format(self.base_datetime),
  1986. },
  1987. project_id=self.project.id,
  1988. )
  1989. self.store_event(
  1990. data={
  1991. "level": "info",
  1992. "culprit": "app/components/events/eventEntries in map",
  1993. "contexts": {"trace": {"trace_id": "b" * 32, "span_id": "c" * 16, "op": ""}},
  1994. "fingerprint": [f"{PerformanceRenderBlockingAssetSpanGroupType.type_id}-group12"],
  1995. "event_id": "e" * 32,
  1996. "timestamp": iso_format(self.base_datetime),
  1997. "start_timestamp": iso_format(self.base_datetime),
  1998. "type": "transaction",
  1999. "transaction": "something",
  2000. },
  2001. project_id=self.project.id,
  2002. )
  2003. error_issues_only = self.make_query(
  2004. search_filter_query="!message:else group.category:error"
  2005. )
  2006. error_and_perf_issues = self.make_query(search_filter_query="!message:else")
  2007. assert set(error_and_perf_issues) > set(error_issues_only)
  2008. class EventsGenericSnubaSearchTest(SharedSnubaTest, OccurrenceTestMixin):
  2009. @property
  2010. def backend(self):
  2011. return EventsDatasetSnubaSearchBackend()
  2012. def setUp(self):
  2013. super().setUp()
  2014. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2015. event_id_1 = uuid.uuid4().hex
  2016. _, group_info = process_event_and_issue_occurrence(
  2017. self.build_occurrence_data(event_id=event_id_1),
  2018. {
  2019. "event_id": event_id_1,
  2020. "project_id": self.project.id,
  2021. "title": "some problem",
  2022. "platform": "python",
  2023. "tags": {"my_tag": "1"},
  2024. "timestamp": before_now(minutes=1).isoformat(),
  2025. "received": before_now(minutes=1).isoformat(),
  2026. },
  2027. )
  2028. self.profile_group_1 = group_info.group
  2029. event_id_2 = uuid.uuid4().hex
  2030. _, group_info = process_event_and_issue_occurrence(
  2031. self.build_occurrence_data(event_id=event_id_2, fingerprint=["put-me-in-group-2"]),
  2032. {
  2033. "event_id": event_id_2,
  2034. "project_id": self.project.id,
  2035. "title": "some other problem",
  2036. "platform": "python",
  2037. "tags": {"my_tag": "1"},
  2038. "timestamp": before_now(minutes=2).isoformat(),
  2039. "received": before_now(minutes=2).isoformat(),
  2040. },
  2041. )
  2042. self.profile_group_2 = group_info.group
  2043. event_id_3 = uuid.uuid4().hex
  2044. process_event_and_issue_occurrence(
  2045. self.build_occurrence_data(event_id=event_id_3, fingerprint=["put-me-in-group-3"]),
  2046. {
  2047. "event_id": event_id_3,
  2048. "project_id": self.project.id,
  2049. "title": "some other problem",
  2050. "platform": "python",
  2051. "tags": {"my_tag": "2"},
  2052. "timestamp": before_now(minutes=2).isoformat(),
  2053. "message_timestamp": before_now(minutes=2).isoformat(),
  2054. },
  2055. )
  2056. error_event_data = {
  2057. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  2058. "message": "bar",
  2059. "environment": "staging",
  2060. "tags": {
  2061. "server": "example.com",
  2062. "url": "http://example.com",
  2063. "sentry:user": "event2@example.com",
  2064. "my_tag": 1,
  2065. },
  2066. }
  2067. error_event = self.store_event(
  2068. data={
  2069. **error_event_data,
  2070. "fingerprint": ["put-me-in-error_group_1"],
  2071. "event_id": "c" * 32,
  2072. "stacktrace": {"frames": [{"module": "error_group_1"}]},
  2073. },
  2074. project_id=self.project.id,
  2075. )
  2076. self.error_group_1 = error_event.group
  2077. error_event_2 = self.store_event(
  2078. data={
  2079. **error_event_data,
  2080. "fingerprint": ["put-me-in-error_group_2"],
  2081. "event_id": "d" * 32,
  2082. "stacktrace": {"frames": [{"module": "error_group_2"}]},
  2083. },
  2084. project_id=self.project.id,
  2085. )
  2086. self.error_group_2 = error_event_2.group
  2087. def test_no_feature(self):
  2088. results = self.make_query(search_filter_query="issue.category:profile my_tag:1")
  2089. assert list(results) == []
  2090. def test_generic_query(self):
  2091. with self.feature("organizations:issue-platform"):
  2092. results = self.make_query(search_filter_query="issue.category:profile my_tag:1")
  2093. assert list(results) == [self.profile_group_1, self.profile_group_2]
  2094. with self.feature("organizations:issue-platform"):
  2095. results = self.make_query(
  2096. search_filter_query="issue.type:profile_file_io_main_thread my_tag:1"
  2097. )
  2098. assert list(results) == [self.profile_group_1, self.profile_group_2]
  2099. def test_error_generic_query(self):
  2100. with self.feature("organizations:issue-platform"):
  2101. results = self.make_query(search_filter_query="my_tag:1")
  2102. assert list(results) == [
  2103. self.profile_group_1,
  2104. self.profile_group_2,
  2105. self.error_group_2,
  2106. self.error_group_1,
  2107. ]
  2108. with self.feature("organizations:issue-platform"):
  2109. results = self.make_query(
  2110. search_filter_query="issue.category:[profile, error] my_tag:1"
  2111. )
  2112. assert list(results) == [
  2113. self.profile_group_1,
  2114. self.profile_group_2,
  2115. self.error_group_2,
  2116. self.error_group_1,
  2117. ]
  2118. with self.feature("organizations:issue-platform"):
  2119. results = self.make_query(
  2120. search_filter_query="issue.type:[profile_file_io_main_thread, error] my_tag:1"
  2121. )
  2122. assert list(results) == [
  2123. self.profile_group_1,
  2124. self.profile_group_2,
  2125. self.error_group_2,
  2126. self.error_group_1,
  2127. ]
  2128. def test_cursor_performance_issues(self):
  2129. with self.feature("organizations:issue-platform"):
  2130. results = self.make_query(
  2131. projects=[self.project],
  2132. search_filter_query="issue.category:profile my_tag:1",
  2133. sort_by="date",
  2134. limit=1,
  2135. count_hits=True,
  2136. )
  2137. assert list(results) == [self.profile_group_1]
  2138. assert results.hits == 2
  2139. with self.feature("organizations:issue-platform"):
  2140. results = self.make_query(
  2141. projects=[self.project],
  2142. search_filter_query="issue.category:profile my_tag:1",
  2143. sort_by="date",
  2144. limit=1,
  2145. cursor=results.next,
  2146. count_hits=True,
  2147. )
  2148. assert list(results) == [self.profile_group_2]
  2149. assert results.hits == 2
  2150. with self.feature("organizations:issue-platform"):
  2151. results = self.make_query(
  2152. projects=[self.project],
  2153. search_filter_query="issue.category:profile my_tag:1",
  2154. sort_by="date",
  2155. limit=1,
  2156. cursor=results.next,
  2157. count_hits=True,
  2158. )
  2159. assert list(results) == []
  2160. assert results.hits == 2
  2161. def test_rejected_filters(self):
  2162. """
  2163. Any queries with `error.handled` or `error.unhandled` filters querying the search_issues dataset
  2164. should be rejected and return empty results.
  2165. """
  2166. with self.feature("organizations:issue-platform"):
  2167. results = self.make_query(
  2168. projects=[self.project],
  2169. search_filter_query="issue.category:profile error.unhandled:0",
  2170. sort_by="date",
  2171. limit=1,
  2172. count_hits=True,
  2173. )
  2174. results2 = self.make_query(
  2175. projects=[self.project],
  2176. search_filter_query="issue.category:profile error.unhandled:1",
  2177. sort_by="date",
  2178. limit=1,
  2179. count_hits=True,
  2180. )
  2181. result3 = self.make_query(
  2182. projects=[self.project],
  2183. search_filter_query="issue.category:profile error.handled:0",
  2184. sort_by="date",
  2185. limit=1,
  2186. count_hits=True,
  2187. )
  2188. results4 = self.make_query(
  2189. projects=[self.project],
  2190. search_filter_query="issue.category:profile error.handled:1",
  2191. sort_by="date",
  2192. limit=1,
  2193. count_hits=True,
  2194. )
  2195. assert list(results) == list(results2) == list(result3) == list(results4) == []
  2196. class CdcEventsSnubaSearchTest(SharedSnubaTest):
  2197. @property
  2198. def backend(self):
  2199. return CdcEventsDatasetSnubaSearchBackend()
  2200. def setUp(self):
  2201. super().setUp()
  2202. self.base_datetime = (datetime.utcnow() - timedelta(days=3)).replace(tzinfo=pytz.utc)
  2203. self.event1 = self.store_event(
  2204. data={
  2205. "fingerprint": ["put-me-in-group1"],
  2206. "event_id": "a" * 32,
  2207. "environment": "production",
  2208. "timestamp": iso_format(self.base_datetime - timedelta(days=21)),
  2209. "tags": {"sentry:user": "user1"},
  2210. },
  2211. project_id=self.project.id,
  2212. )
  2213. self.env1 = self.event1.get_environment()
  2214. self.group1 = self.event1.group
  2215. self.event3 = self.store_event(
  2216. data={
  2217. "fingerprint": ["put-me-in-group1"],
  2218. "environment": "staging",
  2219. "timestamp": iso_format(self.base_datetime),
  2220. "tags": {"sentry:user": "user2"},
  2221. },
  2222. project_id=self.project.id,
  2223. )
  2224. self.event2 = self.store_event(
  2225. data={
  2226. "fingerprint": ["put-me-in-group2"],
  2227. "timestamp": iso_format(self.base_datetime - timedelta(days=20)),
  2228. "environment": "staging",
  2229. "tags": {"sentry:user": "user1"},
  2230. },
  2231. project_id=self.project.id,
  2232. )
  2233. self.group2 = self.event2.group
  2234. self.env2 = self.event2.get_environment()
  2235. def run_test(
  2236. self,
  2237. search_filter_query,
  2238. expected_groups,
  2239. expected_hits,
  2240. projects=None,
  2241. environments=None,
  2242. sort_by="date",
  2243. limit=None,
  2244. count_hits=False,
  2245. date_from=None,
  2246. date_to=None,
  2247. cursor=None,
  2248. ):
  2249. results = self.make_query(
  2250. projects=projects,
  2251. search_filter_query=search_filter_query,
  2252. environments=environments,
  2253. sort_by=sort_by,
  2254. limit=limit,
  2255. count_hits=count_hits,
  2256. date_from=date_from,
  2257. date_to=date_to,
  2258. cursor=cursor,
  2259. )
  2260. assert list(results) == expected_groups
  2261. assert results.hits == expected_hits
  2262. return results
  2263. def test(self):
  2264. self.run_test("is:unresolved", [self.group1, self.group2], None)
  2265. def test_invalid(self):
  2266. with pytest.raises(InvalidQueryForExecutor):
  2267. self.make_query(search_filter_query="is:unresolved abc:123")
  2268. def test_resolved_group(self):
  2269. self.group2.status = GroupStatus.RESOLVED
  2270. self.group2.save()
  2271. self.store_group(self.group2)
  2272. self.run_test("is:unresolved", [self.group1], None)
  2273. self.run_test("is:resolved", [self.group2], None)
  2274. self.run_test("is:unresolved is:resolved", [], None)
  2275. def test_environment(self):
  2276. self.run_test("is:unresolved", [self.group1], None, environments=[self.env1])
  2277. self.run_test("is:unresolved", [self.group1, self.group2], None, environments=[self.env2])
  2278. def test_sort_times_seen(self):
  2279. self.run_test(
  2280. "is:unresolved",
  2281. [self.group1, self.group2],
  2282. None,
  2283. sort_by="freq",
  2284. date_from=self.base_datetime - timedelta(days=30),
  2285. )
  2286. self.store_event(
  2287. data={
  2288. "fingerprint": ["put-me-in-group2"],
  2289. "timestamp": iso_format(self.base_datetime - timedelta(days=15)),
  2290. },
  2291. project_id=self.project.id,
  2292. )
  2293. self.store_event(
  2294. data={
  2295. "fingerprint": ["put-me-in-group2"],
  2296. "timestamp": iso_format(self.base_datetime - timedelta(days=10)),
  2297. "tags": {"sentry:user": "user2"},
  2298. },
  2299. project_id=self.project.id,
  2300. )
  2301. self.run_test(
  2302. "is:unresolved",
  2303. [self.group2, self.group1],
  2304. None,
  2305. sort_by="freq",
  2306. # Change the date range to bust the
  2307. date_from=self.base_datetime - timedelta(days=29),
  2308. )
  2309. def test_sort_first_seen(self):
  2310. self.run_test(
  2311. "is:unresolved",
  2312. [self.group2, self.group1],
  2313. None,
  2314. sort_by="new",
  2315. date_from=self.base_datetime - timedelta(days=30),
  2316. )
  2317. group3 = self.store_event(
  2318. data={
  2319. "fingerprint": ["put-me-in-group3"],
  2320. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2321. },
  2322. project_id=self.project.id,
  2323. ).group
  2324. self.run_test(
  2325. "is:unresolved",
  2326. [group3, self.group2, self.group1],
  2327. None,
  2328. sort_by="new",
  2329. # Change the date range to bust the
  2330. date_from=self.base_datetime - timedelta(days=29),
  2331. )
  2332. def test_sort_user(self):
  2333. self.run_test(
  2334. "is:unresolved",
  2335. [self.group1, self.group2],
  2336. None,
  2337. sort_by="user",
  2338. date_from=self.base_datetime - timedelta(days=30),
  2339. )
  2340. self.store_event(
  2341. data={
  2342. "fingerprint": ["put-me-in-group2"],
  2343. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2344. "tags": {"sentry:user": "user2"},
  2345. },
  2346. project_id=self.project.id,
  2347. )
  2348. self.store_event(
  2349. data={
  2350. "fingerprint": ["put-me-in-group2"],
  2351. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2352. "tags": {"sentry:user": "user2"},
  2353. },
  2354. project_id=self.project.id,
  2355. )
  2356. self.store_event(
  2357. data={
  2358. "fingerprint": ["put-me-in-group1"],
  2359. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2360. "tags": {"sentry:user": "user1"},
  2361. },
  2362. project_id=self.project.id,
  2363. )
  2364. self.store_event(
  2365. data={
  2366. "fingerprint": ["put-me-in-group1"],
  2367. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2368. "tags": {"sentry:user": "user1"},
  2369. },
  2370. project_id=self.project.id,
  2371. )
  2372. # Test group with no users, which can return a null count
  2373. group3 = self.store_event(
  2374. data={
  2375. "fingerprint": ["put-me-in-group3"],
  2376. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2377. },
  2378. project_id=self.project.id,
  2379. ).group
  2380. self.run_test(
  2381. "is:unresolved",
  2382. [self.group2, self.group1, group3],
  2383. None,
  2384. sort_by="user",
  2385. # Change the date range to bust the
  2386. date_from=self.base_datetime - timedelta(days=29),
  2387. )
  2388. def test_sort_priority(self):
  2389. self.run_test(
  2390. "is:unresolved",
  2391. [self.group1, self.group2],
  2392. None,
  2393. sort_by="priority",
  2394. date_from=self.base_datetime - timedelta(days=30),
  2395. )
  2396. def test_cursor(self):
  2397. group3 = self.store_event(
  2398. data={
  2399. "fingerprint": ["put-me-in-group3"],
  2400. "timestamp": iso_format(self.base_datetime + timedelta(days=1)),
  2401. "tags": {"sentry:user": "user2"},
  2402. },
  2403. project_id=self.project.id,
  2404. ).group
  2405. group4 = self.store_event(
  2406. data={
  2407. "fingerprint": ["put-me-in-group7"],
  2408. "timestamp": iso_format(self.base_datetime + timedelta(days=2)),
  2409. "tags": {"sentry:user": "user2"},
  2410. },
  2411. project_id=self.project.id,
  2412. ).group
  2413. results = self.run_test("is:unresolved", [group4], 4, limit=1, count_hits=True)
  2414. results = self.run_test(
  2415. "is:unresolved", [group3], 4, limit=1, cursor=results.next, count_hits=True
  2416. )
  2417. results = self.run_test(
  2418. "is:unresolved", [group4], 4, limit=1, cursor=results.prev, count_hits=True
  2419. )
  2420. self.run_test(
  2421. "is:unresolved", [group3, self.group1], 4, limit=2, cursor=results.next, count_hits=True
  2422. )
  2423. def test_rechecking(self):
  2424. self.group2.status = GroupStatus.RESOLVED
  2425. self.group2.save()
  2426. # Explicitly avoid calling `store_group` here. This means that Clickhouse will still see
  2427. # this group as `UNRESOLVED` and it will be returned in the snuba results. This group
  2428. # should still be filtered out by our recheck.
  2429. self.run_test("is:unresolved", [self.group1], None)