test_backend.py 88 KB

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