test_backend.py 102 KB

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