test_backend.py 101 KB

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