test_backend.py 85 KB

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