test_backend.py 121 KB

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