test_backend.py 124 KB

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