test_backend.py 102 KB

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