test_backend.py 94 KB

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