12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384 |
- import datetime
- from unittest.mock import patch
- from uuid import uuid4
- import pytest
- from django.urls import reverse
- from django.utils import timezone
- from sentry import release_health
- from sentry.models.releaseprojectenvironment import ReleaseProjectEnvironment
- from sentry.release_health.metrics import MetricsReleaseHealthBackend
- from sentry.snuba.metrics import to_intervals
- from sentry.testutils.cases import APITestCase, BaseMetricsTestCase, SnubaTestCase
- from sentry.testutils.helpers.datetime import freeze_time
- from sentry.testutils.helpers.link_header import parse_link_header
- from sentry.testutils.silo import region_silo_test
- from sentry.utils.cursors import Cursor
- from sentry.utils.dates import to_timestamp
- pytestmark = pytest.mark.sentry_metrics
- def result_sorted(result):
- """sort the groups of the results array by the `by` object, ensuring a stable order"""
- def stable_dict(d):
- return tuple(sorted(d.items(), key=lambda t: t[0]))
- result["groups"].sort(key=lambda group: stable_dict(group["by"]))
- return result
- ONE_DAY_AGO = datetime.datetime.now(tz=datetime.timezone.utc) - datetime.timedelta(days=1)
- TWO_DAYS_AGO = datetime.datetime.now(tz=datetime.timezone.utc) - datetime.timedelta(days=2)
- MOCK_DATETIME = ONE_DAY_AGO.replace(hour=12, minute=27, second=28, microsecond=303000)
- MOCK_DATETIME_PLUS_TEN_MINUTES = MOCK_DATETIME + datetime.timedelta(minutes=10)
- MOCK_DATETIME_PLUS_ONE_HOUR = MOCK_DATETIME + datetime.timedelta(hours=1)
- SNUBA_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
- MOCK_DATETIME_START_OF_DAY = MOCK_DATETIME.replace(hour=0, minute=0, second=0)
- TIMESTAMP = to_timestamp(MOCK_DATETIME)
- RECEIVED = TIMESTAMP
- SESSION_STARTED = TIMESTAMP // 3600 * 3600 # round to the hour
- TEMPLATE = {
- "distinct_id": "00000000-0000-0000-0000-000000000000",
- "status": "exited",
- "seq": 0,
- "release": "foo@1.0.0",
- "environment": "production",
- "retention_days": 90,
- "duration": 123.4,
- "errors": 0,
- "started": SESSION_STARTED,
- "received": RECEIVED,
- }
- def make_duration(kwargs):
- """Randomish but deterministic duration"""
- return float(len(str(kwargs)))
- def make_session(project, **kwargs):
- return dict(
- dict(
- TEMPLATE,
- session_id=uuid4().hex,
- org_id=project.organization_id,
- project_id=project.id,
- duration=make_duration(kwargs),
- ),
- **kwargs,
- )
- def adjust_start(start: datetime.datetime, interval: int) -> datetime.datetime:
- # align start and end to the beginning of the intervals
- start, _end, _num_intervals = to_intervals(
- start, start + datetime.timedelta(minutes=1), interval
- )
- return start
- def adjust_end(end: datetime.datetime, interval: int) -> datetime.datetime:
- # align start and end to the beginning of the intervals
- _start, end, _num_intervals = to_intervals(end - datetime.timedelta(minutes=1), end, interval)
- return end
- class OrganizationSessionsEndpointTest(APITestCase, SnubaTestCase):
- def setUp(self):
- super().setUp()
- self.setup_fixture()
- def setup_fixture(self):
- self.organization1 = self.organization
- self.organization2 = self.create_organization()
- self.organization3 = self.create_organization()
- self.project1 = self.project
- self.project2 = self.create_project()
- self.project3 = self.create_project()
- self.project4 = self.create_project(organization=self.organization2)
- self.user2 = self.create_user(is_superuser=False)
- self.create_member(
- user=self.user2, organization=self.organization1, role="member", teams=[]
- )
- self.create_member(user=self.user, organization=self.organization3, role="admin", teams=[])
- self.create_environment(self.project2, name="development")
- self.bulk_store_sessions(
- [
- make_session(self.project1, started=SESSION_STARTED + 12 * 60),
- make_session(self.project1, started=SESSION_STARTED + 24 * 60, release="foo@1.1.0"),
- make_session(self.project1, started=SESSION_STARTED - 60 * 60),
- make_session(self.project1, started=SESSION_STARTED - 12 * 60 * 60),
- make_session(self.project2, status="crashed"),
- make_session(self.project2, environment="development"),
- make_session(self.project3, errors=1, release="foo@1.2.0"),
- make_session(
- self.project3,
- distinct_id="39887d89-13b2-4c84-8c23-5d13d2102664",
- started=SESSION_STARTED - 60 * 60,
- ),
- make_session(
- self.project3, distinct_id="39887d89-13b2-4c84-8c23-5d13d2102664", errors=1
- ),
- make_session(self.project4),
- ]
- )
- def do_request(self, query, user=None, org=None):
- self.login_as(user=user or self.user)
- url = reverse(
- "sentry-api-0-organization-sessions",
- kwargs={"organization_slug": (org or self.organization).slug},
- )
- return self.client.get(url, query, format="json")
- def test_empty_request(self):
- response = self.do_request({})
- assert response.status_code == 400, response.content
- assert response.data == {"detail": 'Request is missing a "field"'}
- def test_inaccessible_project(self):
- response = self.do_request({"project": [self.project4.id]})
- assert response.status_code == 403, response.content
- assert response.data == {"detail": "You do not have permission to perform this action."}
- def test_unknown_field(self):
- response = self.do_request({"field": ["summ(session)"]})
- assert response.status_code == 400, response.content
- assert response.data == {"detail": 'Invalid field: "summ(session)"'}
- def test_unknown_groupby(self):
- response = self.do_request({"field": ["sum(session)"], "groupBy": ["environment_"]})
- assert response.status_code == 400, response.content
- assert response.data == {"detail": 'Invalid groupBy: "environment_"'}
- def test_illegal_groupby(self):
- response = self.do_request({"field": ["sum(session)"], "groupBy": ["issue.id"]})
- assert response.status_code == 400, response.content
- assert response.data == {"detail": 'Invalid groupBy: "issue.id"'}
- def test_invalid_query(self):
- response = self.do_request(
- {"statsPeriod": "1d", "field": ["sum(session)"], "query": ["foo:bar"]}
- )
- assert response.status_code == 400, response.content
- assert response.data["detail"] == "Invalid search filter: foo"
- response = self.do_request(
- {
- "statsPeriod": "1d",
- "field": ["sum(session)"],
- "query": ["release:foo-bar@1.2.3 (123)"],
- }
- )
- assert response.status_code == 400, response.content
- # TODO: it would be good to provide a better error here,
- # since its not obvious where `message` comes from.
- assert response.data["detail"] == "Invalid search filter: message"
- def test_illegal_query(self):
- response = self.do_request(
- {"statsPeriod": "1d", "field": ["sum(session)"], "query": ["issue.id:123"]}
- )
- assert response.status_code == 400, response.content
- assert response.data["detail"] == "Invalid search filter: issue.id"
- def test_too_many_points(self):
- # default statsPeriod is 90d
- response = self.do_request({"field": ["sum(session)"], "interval": "1h"})
- assert response.status_code == 400, response.content
- assert response.data == {
- "detail": "Your interval and date range would create too many results. "
- "Use a larger interval, or a smaller date range."
- }
- @freeze_time(MOCK_DATETIME)
- def test_future_request(self):
- start = MOCK_DATETIME + datetime.timedelta(days=1)
- end = MOCK_DATETIME + datetime.timedelta(days=2)
- response = self.do_request(
- {
- "project": [-1],
- "interval": "1h",
- "field": ["sum(session)"],
- "start": start.strftime(SNUBA_TIME_FORMAT),
- "end": end.strftime(SNUBA_TIME_FORMAT),
- }
- )
- assert response.status_code == 200, response.content
- @freeze_time(MOCK_DATETIME)
- def test_timeseries_interval(self):
- response = self.do_request(
- {"project": [-1], "statsPeriod": "1d", "interval": "1d", "field": ["sum(session)"]}
- )
- start_of_day_snuba_format = MOCK_DATETIME_START_OF_DAY.strftime(SNUBA_TIME_FORMAT)
- previous_start_of_day_snuba_format = (
- MOCK_DATETIME_START_OF_DAY - datetime.timedelta(days=1)
- ).strftime(SNUBA_TIME_FORMAT)
- expected_start = MOCK_DATETIME_START_OF_DAY - datetime.timedelta(days=1)
- expected_end = MOCK_DATETIME_START_OF_DAY + datetime.timedelta(days=1)
- assert response.status_code == 200, response.content
- assert result_sorted(response.data) == {
- "start": expected_start.strftime(SNUBA_TIME_FORMAT),
- "end": expected_end.strftime(SNUBA_TIME_FORMAT),
- "query": "",
- "intervals": [previous_start_of_day_snuba_format, start_of_day_snuba_format],
- "groups": [
- {"by": {}, "series": {"sum(session)": [0, 9]}, "totals": {"sum(session)": 9}}
- ],
- }
- response = self.do_request(
- {"project": [-1], "statsPeriod": "1d", "interval": "6h", "field": ["sum(session)"]}
- )
- six_hours = 6 * 60 * 60
- expected_start = adjust_start(TWO_DAYS_AGO.replace(hour=12, minute=0, second=0), six_hours)
- expected_end = adjust_end(MOCK_DATETIME.replace(minute=28, second=0), six_hours)
- assert response.status_code == 200, response.content
- assert result_sorted(response.data) == {
- "start": expected_start.strftime(SNUBA_TIME_FORMAT),
- "end": expected_end.strftime(SNUBA_TIME_FORMAT),
- "query": "",
- "intervals": [
- TWO_DAYS_AGO.replace(hour=12, minute=0, second=0).strftime(SNUBA_TIME_FORMAT),
- TWO_DAYS_AGO.replace(hour=18, minute=0, second=0).strftime(SNUBA_TIME_FORMAT),
- MOCK_DATETIME.replace(hour=0, minute=0, second=0).strftime(SNUBA_TIME_FORMAT),
- MOCK_DATETIME.replace(hour=6, minute=0, second=0).strftime(SNUBA_TIME_FORMAT),
- MOCK_DATETIME.replace(hour=12, minute=0, second=0).strftime(SNUBA_TIME_FORMAT),
- ],
- "groups": [
- {
- "by": {},
- "series": {"sum(session)": [0, 0, 1, 2, 6]},
- "totals": {"sum(session)": 9},
- }
- ],
- }
- @freeze_time(MOCK_DATETIME)
- def test_user_all_accessible(self):
- response = self.do_request(
- {"project": [-1], "statsPeriod": "1d", "interval": "1d", "field": ["sum(session)"]},
- user=self.user2,
- )
- start_of_previous_day = MOCK_DATETIME_START_OF_DAY - datetime.timedelta(days=1)
- start_of_day_snuba_format = MOCK_DATETIME_START_OF_DAY.strftime(SNUBA_TIME_FORMAT)
- start_of_previous_day_snuba_format = start_of_previous_day.strftime(SNUBA_TIME_FORMAT)
- expected_start = MOCK_DATETIME_START_OF_DAY - datetime.timedelta(days=1)
- expected_end = MOCK_DATETIME_START_OF_DAY + datetime.timedelta(days=1)
- assert response.status_code == 200, response.content
- assert result_sorted(response.data) == {
- "start": expected_start.strftime(SNUBA_TIME_FORMAT),
- "end": expected_end.strftime(SNUBA_TIME_FORMAT),
- "query": "",
- "intervals": [start_of_previous_day_snuba_format, start_of_day_snuba_format],
- "groups": [
- {"by": {}, "series": {"sum(session)": [0, 9]}, "totals": {"sum(session)": 9}}
- ],
- }
- def test_no_projects(self):
- response = self.do_request(
- {"project": [-1], "statsPeriod": "1d", "interval": "1d", "field": ["sum(session)"]},
- org=self.organization3,
- )
- assert response.status_code == 400, response.content
- assert response.data == {"detail": "No projects available"}
- @freeze_time(MOCK_DATETIME_PLUS_TEN_MINUTES)
- def test_minute_resolution(self):
- with self.feature("organizations:minute-resolution-sessions"):
- response = self.do_request(
- {
- "project": [self.project1.id, self.project2.id],
- "statsPeriod": "30m",
- "interval": "10m",
- "field": ["sum(session)"],
- }
- )
- assert response.status_code == 200, response.content
- ten_min = 10 * 60
- expected_start = adjust_start(
- MOCK_DATETIME.replace(hour=12, minute=0, second=0), ten_min
- )
- expected_end = adjust_end(MOCK_DATETIME.replace(hour=12, minute=38, second=0), ten_min)
- assert result_sorted(response.data) == {
- "start": expected_start.strftime(SNUBA_TIME_FORMAT),
- "end": expected_end.strftime(SNUBA_TIME_FORMAT),
- "query": "",
- "intervals": [
- *[
- MOCK_DATETIME.replace(hour=12, minute=min, second=0).strftime(
- SNUBA_TIME_FORMAT
- )
- for min in [0, 10, 20, 30]
- ],
- ],
- "groups": [
- {
- "by": {},
- "series": {"sum(session)": [2, 1, 1, 0]},
- "totals": {"sum(session)": 4},
- }
- ],
- }
- @freeze_time(MOCK_DATETIME_PLUS_TEN_MINUTES)
- def test_10s_resolution(self):
- with self.feature("organizations:minute-resolution-sessions"):
- response = self.do_request(
- {
- "project": [self.project1.id],
- "statsPeriod": "1m",
- "interval": "10s",
- "field": ["sum(session)"],
- }
- )
- assert response.status_code == 200, response.content
- if release_health.backend.is_metrics_based():
- # With the metrics backend, we should get exactly what we asked for,
- # 6 intervals with 10 second length. However, since we add both the
- # starting and ending interval we get 7 intervals.
- assert len(response.data["intervals"]) == 7
- else:
- # With the sessions backend, the resolution will be 1m and will include starting and ending
- # minute so 2 intervals
- assert len(response.data["intervals"]) == 2
- @freeze_time(MOCK_DATETIME)
- def test_filter_projects(self):
- response = self.do_request(
- {
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "project": [self.project2.id, self.project3.id],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 5]}, "totals": {"sum(session)": 5}}
- ]
- @freeze_time(MOCK_DATETIME)
- def test_anr_invalid_aggregates(self):
- default_request = {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["anr_rate()", "crash_free_rate(user)"],
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- response = req()
- assert response.status_code == 200
- if release_health.backend.is_metrics_based():
- # Both these fields are supported by the metrics backend
- assert response.data["groups"] == [
- {
- "by": {},
- "totals": {"anr_rate()": 0.0, "crash_free_rate(user)": 1.0},
- "series": {"anr_rate()": [None, 0.0], "crash_free_rate(user)": [None, 1.0]},
- }
- ]
- else:
- # Both these fields are not supported by the sessions backend
- assert response.data["groups"] == []
- response = req(field=["anr_rate()", "sum(session)"])
- assert response.status_code == 200
- if release_health.backend.is_metrics_based():
- # Both these fields are supported by the metrics backend
- assert response.data["groups"] == [
- {
- "by": {},
- "totals": {"anr_rate()": 0.0, "sum(session)": 9},
- "series": {"anr_rate()": [None, 0.0], "sum(session)": [0, 9]},
- }
- ]
- else:
- # Only sum(session) is supported by the sessions backend
- assert response.data["groups"] == [
- {"by": {}, "totals": {"sum(session)": 9}, "series": {"sum(session)": [0, 9]}}
- ]
- @freeze_time(MOCK_DATETIME)
- def test_filter_environment(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": "environment:development",
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 1]}, "totals": {"sum(session)": 1}}
- ]
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "environment": ["development"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 1]}, "totals": {"sum(session)": 1}}
- ]
- @freeze_time(MOCK_DATETIME)
- def test_filter_release(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": "release:foo@1.1.0",
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 1]}, "totals": {"sum(session)": 1}}
- ]
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": 'release:"foo@1.1.0" or release:"foo@1.2.0"',
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 2]}, "totals": {"sum(session)": 2}}
- ]
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": 'release:"foo@1.1.0" or release:["foo@1.2.0", release:"foo@1.3.0"]',
- "groupBy": ["release"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"release": "foo@1.1.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"release": "foo@1.2.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_filter_unknown_release(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1h",
- "field": ["sum(session)"],
- "query": "release:foo@6.6.6",
- "groupBy": "session.status",
- }
- )
- assert response.status_code == 200, response.content
- @freeze_time(MOCK_DATETIME)
- def test_filter_unknown_release_in(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": "release:[foo@6.6.6]",
- "groupBy": "session.status",
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"session.status": status},
- "series": {"sum(session)": [0, 0]},
- "totals": {"sum(session)": 0},
- }
- for status in ("abnormal", "crashed", "errored", "healthy")
- ]
- @freeze_time(MOCK_DATETIME)
- def test_groupby_project(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["project"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"project": self.project1.id},
- "series": {"sum(session)": [0, 4]},
- "totals": {"sum(session)": 4},
- },
- {
- "by": {"project": self.project2.id},
- "series": {"sum(session)": [0, 2]},
- "totals": {"sum(session)": 2},
- },
- {
- "by": {"project": self.project3.id},
- "series": {"sum(session)": [0, 3]},
- "totals": {"sum(session)": 3},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_groupby_environment(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["environment"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"environment": "development"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"environment": "production"},
- "series": {"sum(session)": [0, 8]},
- "totals": {"sum(session)": 8},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_groupby_release(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"release": "foo@1.0.0"},
- "series": {"sum(session)": [0, 7]},
- "totals": {"sum(session)": 7},
- },
- {
- "by": {"release": "foo@1.1.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"release": "foo@1.2.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_groupby_status(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["session.status"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"session.status": "abnormal"},
- "series": {"sum(session)": [0, 0]},
- "totals": {"sum(session)": 0},
- },
- {
- "by": {"session.status": "crashed"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"session.status": "errored"},
- "series": {"sum(session)": [0, 2]},
- "totals": {"sum(session)": 2},
- },
- {
- "by": {"session.status": "healthy"},
- "series": {"sum(session)": [0, 6]},
- "totals": {"sum(session)": 6},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_groupby_cross(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release", "environment"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"environment": "development", "release": "foo@1.0.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"environment": "production", "release": "foo@1.0.0"},
- "series": {"sum(session)": [0, 6]},
- "totals": {"sum(session)": 6},
- },
- {
- "by": {"environment": "production", "release": "foo@1.1.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"environment": "production", "release": "foo@1.2.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_users_groupby(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {},
- "series": {"count_unique(user)": [0, 1]},
- "totals": {"count_unique(user)": 1},
- }
- ]
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)"],
- "groupBy": ["session.status"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"session.status": "abnormal"},
- "series": {"count_unique(user)": [0, 0]},
- "totals": {"count_unique(user)": 0},
- },
- {
- "by": {"session.status": "crashed"},
- "series": {"count_unique(user)": [0, 0]},
- "totals": {"count_unique(user)": 0},
- },
- {
- "by": {"session.status": "errored"},
- "series": {"count_unique(user)": [0, 1]},
- "totals": {"count_unique(user)": 1},
- },
- {
- "by": {"session.status": "healthy"},
- "series": {"count_unique(user)": [0, 0]},
- "totals": {"count_unique(user)": 0},
- },
- ]
- expected_duration_values = {
- "avg(session.duration)": 42375.0,
- "max(session.duration)": 80000.0,
- "p50(session.duration)": 33500.0,
- "p75(session.duration)": 53750.0,
- "p90(session.duration)": 71600.0,
- "p95(session.duration)": 75800.0,
- "p99(session.duration)": 79159.99999999999,
- }
- @freeze_time(MOCK_DATETIME)
- def test_users_groupby_status_advanced(self):
- project = self.create_project()
- user1 = uuid4().hex
- session1 = uuid4().hex
- user2 = uuid4().hex
- session2a = uuid4().hex
- session2b = uuid4().hex
- user3 = uuid4().hex
- session3 = uuid4().hex
- self.store_session(
- make_session(project, session_id=session1, distinct_id=user1, status="ok")
- )
- self.store_session(
- make_session(
- project, session_id=session1, distinct_id=user1, seq=1, errors=1, status="errored"
- )
- )
- self.store_session(
- make_session(project, session_id=session1, distinct_id=user1, seq=2, status="crashed")
- )
- self.store_session(
- make_session(project, session_id=session2a, distinct_id=user2, status="ok")
- )
- self.store_session(
- make_session(project, session_id=session2b, distinct_id=user2, status="ok")
- )
- self.store_session(
- make_session(project, session_id=session2b, distinct_id=user2, status="abnormal")
- )
- self.store_session(
- make_session(
- project, session_id=session3, distinct_id=user3, errors=123, status="errored"
- )
- )
- # Add some extra healthy users:
- for _ in range(3):
- user = uuid4().hex
- self.store_session(make_session(project, distinct_id=user))
- # First, check if totals make sense:
- response = self.do_request(
- {
- "project": [project.id],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {},
- "series": {"count_unique(user)": [0, 6]},
- "totals": {"count_unique(user)": 6},
- },
- ]
- # Then check if grouping makes sense:
- response = self.do_request(
- {
- "project": [project.id],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)"],
- "groupBy": ["session.status"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"session.status": "abnormal"},
- "series": {"count_unique(user)": [0, 1]},
- "totals": {"count_unique(user)": 1},
- },
- {
- "by": {"session.status": "crashed"},
- "series": {"count_unique(user)": [0, 1]},
- "totals": {"count_unique(user)": 1},
- },
- {
- "by": {"session.status": "errored"},
- "series": {"count_unique(user)": [0, 1]},
- "totals": {"count_unique(user)": 1},
- },
- {
- # user
- "by": {"session.status": "healthy"},
- "series": {"count_unique(user)": [0, 3]},
- "totals": {"count_unique(user)": 3},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_duration_percentiles(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": [
- "avg(session.duration)",
- "p50(session.duration)",
- "p75(session.duration)",
- "p90(session.duration)",
- "p95(session.duration)",
- "p99(session.duration)",
- "max(session.duration)",
- ],
- }
- )
- assert response.status_code == 200, response.content
- expected = self.expected_duration_values
- groups = result_sorted(response.data)["groups"]
- assert len(groups) == 1, groups
- group = groups[0]
- assert group["by"] == {}
- assert group["totals"] == pytest.approx(expected)
- for key, series in group["series"].items():
- assert series == pytest.approx([None, expected[key]])
- @freeze_time(MOCK_DATETIME)
- def test_duration_percentiles_groupby(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": [
- "avg(session.duration)",
- "p50(session.duration)",
- "p75(session.duration)",
- "p90(session.duration)",
- "p95(session.duration)",
- "p99(session.duration)",
- "max(session.duration)",
- ],
- "groupBy": "session.status",
- }
- )
- assert response.status_code == 200, response.content
- expected = self.expected_duration_values
- seen = set() # Make sure all session statuses are listed
- for group in result_sorted(response.data)["groups"]:
- seen.add(group["by"].get("session.status"))
- if group["by"] == {"session.status": "healthy"}:
- assert group["totals"] == pytest.approx(expected)
- for key, series in group["series"].items():
- assert series == pytest.approx([None, expected[key]])
- else:
- # Everything's none:
- assert group["totals"] == {key: None for key in expected}, group["by"]
- assert group["series"] == {key: [None, None] for key in expected}
- assert seen == {"abnormal", "crashed", "errored", "healthy"}
- @freeze_time(MOCK_DATETIME)
- def test_snuba_limit_exceeded(self):
- # 2 * 4 => only show two groups
- with patch("sentry.snuba.sessions_v2.SNUBA_LIMIT", 8), patch(
- "sentry.snuba.metrics.query.MAX_POINTS", 8
- ):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "3d",
- "interval": "1d",
- # "user" is the first field, but "session" always wins:
- "field": ["count_unique(user)", "sum(session)"],
- "groupBy": ["project", "release", "environment"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {
- "release": "foo@1.0.0",
- "environment": "production",
- "project": self.project1.id,
- },
- "totals": {"sum(session)": 3, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 3], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "release": "foo@1.0.0",
- "environment": "production",
- "project": self.project3.id,
- },
- "totals": {"sum(session)": 2, "count_unique(user)": 1},
- "series": {"sum(session)": [0, 0, 0, 2], "count_unique(user)": [0, 0, 0, 1]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_snuba_limit_exceeded_groupby_status(self):
- """Get consistent result when grouping by status"""
- # 2 * 4 => only show two groups
- with patch("sentry.snuba.sessions_v2.SNUBA_LIMIT", 8), patch(
- "sentry.snuba.metrics.query.MAX_POINTS", 8
- ):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "3d",
- "interval": "1d",
- "field": ["sum(session)", "count_unique(user)"],
- "groupBy": ["project", "release", "environment", "session.status"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {
- "project": self.project1.id,
- "release": "foo@1.0.0",
- "session.status": "abnormal",
- "environment": "production",
- },
- "totals": {"sum(session)": 0, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 0], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "project": self.project1.id,
- "release": "foo@1.0.0",
- "session.status": "crashed",
- "environment": "production",
- },
- "totals": {"sum(session)": 0, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 0], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "project": self.project1.id,
- "release": "foo@1.0.0",
- "environment": "production",
- "session.status": "errored",
- },
- "totals": {"sum(session)": 0, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 0], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "project": self.project1.id,
- "session.status": "healthy",
- "release": "foo@1.0.0",
- "environment": "production",
- },
- "totals": {"sum(session)": 3, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 3], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "session.status": "abnormal",
- "release": "foo@1.0.0",
- "project": self.project3.id,
- "environment": "production",
- },
- "totals": {"sum(session)": 0, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 0], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "release": "foo@1.0.0",
- "project": self.project3.id,
- "session.status": "crashed",
- "environment": "production",
- },
- "totals": {"sum(session)": 0, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 0], "count_unique(user)": [0, 0, 0, 0]},
- },
- {
- "by": {
- "release": "foo@1.0.0",
- "project": self.project3.id,
- "environment": "production",
- "session.status": "errored",
- },
- "totals": {"sum(session)": 1, "count_unique(user)": 1},
- "series": {"sum(session)": [0, 0, 0, 1], "count_unique(user)": [0, 0, 0, 1]},
- },
- {
- "by": {
- "session.status": "healthy",
- "release": "foo@1.0.0",
- "project": self.project3.id,
- "environment": "production",
- },
- "totals": {"sum(session)": 1, "count_unique(user)": 0},
- "series": {"sum(session)": [0, 0, 0, 1], "count_unique(user)": [0, 0, 0, 0]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_environment_filter_not_present_in_query(self):
- self.create_environment(name="abc")
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "environment": ["development", "abc"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 1]}, "totals": {"sum(session)": 1}}
- ]
- @freeze_time(MOCK_DATETIME)
- def test_sessions_without_users(self):
- # The first field defines by which groups additional queries are filtered
- # But if the first field is the user count, the series should still
- # contain the session counts even if the project does not track users
- response = self.do_request(
- {
- "project": self.project.id, # project without users
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)", "sum(session)"],
- "groupBy": "release",
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"release": "foo@1.0.0"},
- "series": {"count_unique(user)": [0, 0], "sum(session)": [0, 3]},
- "totals": {"count_unique(user)": 0, "sum(session)": 3},
- },
- {
- "by": {"release": "foo@1.1.0"},
- "series": {"count_unique(user)": [0, 0], "sum(session)": [0, 1]},
- "totals": {"count_unique(user)": 0, "sum(session)": 1},
- },
- ]
- @freeze_time(MOCK_DATETIME + datetime.timedelta(days=2))
- def test_groupby_no_data(self):
- # Empty results for everything
- response = self.do_request(
- {
- "project": self.project.id, # project without users
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)", "sum(session)"],
- "groupBy": "release",
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == []
- @freeze_time(MOCK_DATETIME)
- def test_mix_known_and_unknown_strings(self):
- response = self.do_request(
- {
- "project": self.project.id, # project without users
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)", "sum(session)"],
- "query": "environment:[production,foo]",
- }
- )
- assert response.status_code == 200, response.data
- @freeze_time(MOCK_DATETIME)
- def test_release_semver_filter(self):
- r1 = self.create_release(version="ahmed@1.0.0")
- r2 = self.create_release(version="ahmed@1.1.0")
- r3 = self.create_release(version="ahmed@2.0.0")
- for r in (r1, r2, r3):
- self.store_session(make_session(self.project, release=r.version))
- response = self.do_request(
- {
- "project": self.project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "query": "release.version:1.*",
- }
- )
- assert response.status_code == 200
- assert sorted(response.data["groups"], key=lambda x: x["by"]["release"]) == [
- {
- "by": {"release": "ahmed@1.0.0"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- {
- "by": {"release": "ahmed@1.1.0"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_release_package_filter(self):
- r1 = self.create_release(version="ahmed@1.2.4+124")
- r2 = self.create_release(version="ahmed2@1.2.5+125")
- r3 = self.create_release(version="ahmed2@1.2.6+126")
- for r in (r1, r2, r3):
- self.store_session(make_session(self.project, release=r.version))
- response = self.do_request(
- {
- "project": self.project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "query": "release.package:ahmed2",
- }
- )
- assert response.status_code == 200
- assert sorted(response.data["groups"], key=lambda x: x["by"]["release"]) == [
- {
- "by": {"release": "ahmed2@1.2.5+125"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- {
- "by": {"release": "ahmed2@1.2.6+126"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_release_build_filter(self):
- r1 = self.create_release(version="ahmed@1.2.4+124")
- r2 = self.create_release(version="ahmed@1.2.3+123")
- r3 = self.create_release(version="ahmed2@1.2.5+125")
- for r in (r1, r2, r3):
- self.store_session(make_session(self.project, release=r.version))
- response = self.do_request(
- {
- "project": self.project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "query": "release.build:<125",
- }
- )
- assert response.status_code == 200
- assert sorted(response.data["groups"], key=lambda x: x["by"]["release"]) == [
- {
- "by": {"release": "ahmed@1.2.3+123"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- {
- "by": {"release": "ahmed@1.2.4+124"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_release_stage_filter(self):
- new_env = self.create_environment(name="new_env")
- adopted_release = self.create_release(version="adopted_release")
- not_adopted_release = self.create_release(version="not_adopted_release")
- ReleaseProjectEnvironment.objects.create(
- project_id=self.project.id,
- release_id=adopted_release.id,
- environment_id=new_env.id,
- adopted=timezone.now(),
- )
- ReleaseProjectEnvironment.objects.create(
- project_id=self.project.id,
- release_id=not_adopted_release.id,
- environment_id=new_env.id,
- )
- for r in (adopted_release, not_adopted_release):
- self.store_session(
- make_session(self.project, release=r.version, environment=new_env.name)
- )
- response = self.do_request(
- {
- "project": self.project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "query": "release.stage:adopted",
- "environment": new_env.name,
- }
- )
- assert response.status_code == 200
- assert response.data["groups"] == [
- {
- "by": {"release": "adopted_release"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- @region_silo_test
- @patch("sentry.release_health.backend", MetricsReleaseHealthBackend())
- class OrganizationSessionsEndpointMetricsTest(
- BaseMetricsTestCase, OrganizationSessionsEndpointTest
- ):
- """Repeat all tests with metrics backend"""
- @freeze_time(MOCK_DATETIME)
- def test_orderby(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)"],
- "orderBy": "foobar",
- }
- )
- assert response.status_code == 400
- assert response.data == {"detail": "'orderBy' must be one of the provided 'fields'"}
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)"],
- "orderBy": "count_unique(user)", # wrong field
- }
- )
- assert response.status_code == 400
- assert response.data == {"detail": "'orderBy' must be one of the provided 'fields'"}
- # Cannot sort by more than one field
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)", "count_unique(user)"],
- "orderBy": ["sum(session)", "count_unique(user)"],
- }
- )
- assert response.status_code == 400
- assert response.data == {"detail": "Cannot order by multiple fields"}
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)"],
- "orderBy": "sum(session)", # misses group by, but why not
- }
- )
- assert response.status_code == 200
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)"],
- "orderBy": "sum(session)",
- "groupBy": ["session.status"],
- }
- )
- assert response.status_code == 400
- assert response.data == {"detail": "Cannot use 'orderBy' when grouping by sessions.status"}
- response = self.do_request(
- {
- "project": [self.project.id, self.project3.id],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)", "p95(session.duration)"],
- "orderBy": "p95(session.duration)",
- "groupBy": ["project", "release", "environment"],
- }
- )
- expected_groups = [
- {
- "by": {
- "project": self.project.id,
- "release": "foo@1.0.0",
- "environment": "production",
- },
- "totals": {"sum(session)": 3, "p95(session.duration)": 25000.0},
- "series": {
- "sum(session)": [0, 0, 3],
- "p95(session.duration)": [None, None, 25000.0],
- },
- },
- {
- "by": {
- "project": self.project3.id,
- "release": "foo@1.2.0",
- "environment": "production",
- },
- "totals": {"sum(session)": 1, "p95(session.duration)": 37000.0},
- "series": {
- "sum(session)": [0, 0, 1],
- "p95(session.duration)": [None, None, 37000.0],
- },
- },
- {
- "by": {
- "project": self.project.id,
- "release": "foo@1.1.0",
- "environment": "production",
- },
- "totals": {"sum(session)": 1, "p95(session.duration)": 49000.0},
- "series": {
- "sum(session)": [0, 0, 1],
- "p95(session.duration)": [None, None, 49000.0],
- },
- },
- {
- "by": {
- "project": self.project3.id,
- "release": "foo@1.0.0",
- "environment": "production",
- },
- "totals": {"sum(session)": 2, "p95(session.duration)": 79400.0},
- "series": {
- "sum(session)": [0, 0, 2],
- "p95(session.duration)": [None, None, 79400.0],
- },
- },
- ]
- # Not using `result_sorted` here, because we want to verify the order
- assert response.status_code == 200, response.data
- assert response.data["groups"] == expected_groups
- # Sort descending
- response = self.do_request(
- {
- "project": [self.project.id, self.project3.id],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": ["sum(session)", "p95(session.duration)"],
- "orderBy": "-p95(session.duration)",
- "groupBy": ["project", "release", "environment"],
- }
- )
- assert response.status_code == 200
- assert response.data["groups"] == list(reversed(expected_groups))
- # Add some more code coverage
- all_fields = [
- "sum(session)",
- "count_unique(user)",
- "avg(session.duration)",
- ]
- for field in all_fields:
- assert (
- self.do_request(
- {
- "project": [self.project.id, self.project3.id],
- "statsPeriod": "2d",
- "interval": "1d",
- "field": all_fields,
- "orderBy": field,
- "groupBy": ["project", "release", "environment"],
- }
- ).status_code
- == 200
- )
- @freeze_time(MOCK_DATETIME)
- def test_wildcard_search(self):
- default_request = {
- "project": [-1],
- "statsPeriod": "2d",
- "interval": "1d",
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- response = req(field=["sum(session)"], query="release:foo@*")
- assert response.status_code == 400
- assert response.data == {"detail": "Invalid condition: wildcard search is not supported"}
- response = req(field=["sum(session)"], query="release:foo@* AND release:bar@*")
- assert response.status_code == 400
- assert response.data == {"detail": "Invalid condition: wildcard search is not supported"}
- response = req(field=["sum(session)"], query="release:foo@* OR release:bar@*")
- assert response.status_code == 400
- assert response.data == {"detail": "Invalid condition: wildcard search is not supported"}
- response = req(field=["sum(session)"], query="(release:foo@* OR release:bar) OR project:1")
- assert response.status_code == 400
- assert response.data == {"detail": "Invalid condition: wildcard search is not supported"}
- @freeze_time(MOCK_DATETIME)
- def test_filter_by_session_status(self):
- default_request = {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- response = req(field=["sum(session)"], query="session.status:bogus")
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == []
- response = req(field=["sum(session)"], query="!session.status:healthy")
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 3]}, "totals": {"sum(session)": 3}}
- ]
- # sum(session) filtered by multiple statuses adds them
- response = req(field=["sum(session)"], query="session.status:[healthy, errored]")
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 8]}, "totals": {"sum(session)": 8}}
- ]
- response = req(
- field=["sum(session)"],
- query="session.status:[healthy, errored]",
- groupBy="session.status",
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"session.status": "errored"},
- "totals": {"sum(session)": 2},
- "series": {"sum(session)": [0, 2]},
- },
- {
- "by": {"session.status": "healthy"},
- "totals": {"sum(session)": 6},
- "series": {"sum(session)": [0, 6]},
- },
- ]
- response = req(field=["sum(session)"], query="session.status:healthy release:foo@1.1.0")
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {"by": {}, "series": {"sum(session)": [0, 1]}, "totals": {"sum(session)": 1}}
- ]
- response = req(field=["sum(session)"], query="session.status:healthy OR release:foo@1.1.0")
- assert response.status_code == 400, response.data
- assert response.data == {"detail": "Unable to parse condition with session.status"}
- # count_unique(user) does not work with multiple session statuses selected
- response = req(field=["count_unique(user)"], query="session.status:[healthy, errored]")
- assert response.status_code == 400, response.data
- assert response.data == {
- "detail": "Cannot filter count_unique by multiple session.status unless it is in groupBy"
- }
- response = req(field=["p95(session.duration)"], query="session.status:abnormal")
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == []
- @freeze_time(MOCK_DATETIME)
- def test_filter_by_session_status_with_groupby(self):
- default_request = {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "groupBy": "release",
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- response = req(field=["sum(session)"], query="session.status:healthy")
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"release": "foo@1.0.0"},
- "series": {"sum(session)": [0, 5]},
- "totals": {"sum(session)": 5},
- },
- {
- "by": {"release": "foo@1.1.0"},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- },
- {
- "by": {"release": "foo@1.2.0"},
- "series": {"sum(session)": [0, 0]},
- "totals": {"sum(session)": 0},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_filter_by_session_status_with_orderby(self):
- default_request = {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- response = req(
- field=["sum(session)"],
- query="session.status:[abnormal,crashed]",
- groupBy="release",
- orderBy="sum(session)",
- )
- assert response.status_code == 400, response.content
- assert response.data == {"detail": "Cannot order by sum(session) with the current filters"}
- response = req(
- field=["sum(session)"],
- query="session.status:healthy",
- groupBy="release",
- orderBy="sum(session)",
- )
- assert response.status_code == 400, response.content
- assert response.data == {"detail": "Cannot order by sum(session) with the current filters"}
- @freeze_time(MOCK_DATETIME)
- def test_anr_rate(self):
- def store_anr_session(user_id, mechanism):
- self.store_session(
- make_session(
- self.project2,
- distinct_id=user_id,
- errors=1,
- status="abnormal",
- abnormal_mechanism=mechanism,
- )
- )
- self.store_session(
- make_session(
- self.project2,
- distinct_id="610c480b-3c47-4871-8c03-05ea04595eb0",
- started=SESSION_STARTED - 60 * 60,
- )
- )
- store_anr_session("610c480b-3c47-4871-8c03-05ea04595eb0", "anr_foreground")
- self.store_session(
- make_session(
- self.project2,
- distinct_id="ac0b74a2-8ace-415a-82d2-0fdb0d81dec4",
- started=SESSION_STARTED - 60 * 60,
- )
- )
- store_anr_session("ac0b74a2-8ace-415a-82d2-0fdb0d81dec4", "anr_background")
- self.store_session(
- make_session(
- self.project2,
- distinct_id="5344c005-653b-48b7-bbaf-d362c2f268dd",
- started=SESSION_STARTED - 60 * 60,
- )
- )
- default_request = {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["anr_rate()"],
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- # basic test case
- response = req()
- assert response.status_code == 200
- assert response.data["groups"] == [
- {"by": {}, "totals": {"anr_rate()": 0.5}, "series": {"anr_rate()": [None, 0.5]}}
- ]
- # group by session.status
- response = req(
- groupBy="session.status",
- )
- assert response.status_code == 400, response.content
- assert response.data == {"detail": "Cannot group field anr_rate() by session.status"}
- # valid group by
- response = req(
- field=["anr_rate()", "foreground_anr_rate()"],
- groupBy=["release", "environment"],
- orderBy=["anr_rate()"],
- query="release:foo@1.0.0",
- )
- assert response.status_code == 200, response.content
- assert response.data["groups"] == [
- {
- "by": {"environment": "production", "release": "foo@1.0.0"},
- "series": {
- "anr_rate()": [None, 0.5],
- "foreground_anr_rate()": [None, 0.25],
- },
- "totals": {
- "anr_rate()": 0.5,
- "foreground_anr_rate()": 0.25,
- },
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_crash_rate(self):
- default_request = {
- "project": [-1],
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["crash_rate(session)"],
- }
- def req(**kwargs):
- return self.do_request(dict(default_request, **kwargs))
- # 1 - filter session.status
- response = req(
- query="session.status:[abnormal,crashed]",
- )
- assert response.status_code == 400, response.content
- assert response.data == {
- "detail": "Cannot filter field crash_rate(session) by session.status"
- }
- # 2 - group by session.status
- response = req(
- groupBy="session.status",
- )
- assert response.status_code == 400, response.content
- assert response.data == {
- "detail": "Cannot group field crash_rate(session) by session.status"
- }
- # 4 - fetch all
- response = req(
- field=[
- "crash_rate(session)",
- "crash_rate(user)",
- "crash_free_rate(session)",
- "crash_free_rate(user)",
- ],
- groupBy=["release", "environment"],
- orderBy=["crash_free_rate(session)"],
- query="release:foo@1.0.0",
- )
- assert response.status_code == 200, response.content
- assert response.data["groups"] == [
- {
- "by": {"environment": "production", "release": "foo@1.0.0"},
- "series": {
- "crash_free_rate(session)": [None, 0.8333333333333334],
- "crash_free_rate(user)": [None, 1.0],
- "crash_rate(session)": [None, 0.16666666666666666],
- "crash_rate(user)": [None, 0.0],
- },
- "totals": {
- "crash_free_rate(session)": 0.8333333333333334,
- "crash_free_rate(user)": 1.0,
- "crash_rate(session)": 0.16666666666666666,
- "crash_rate(user)": 0.0,
- },
- },
- {
- "by": {"environment": "development", "release": "foo@1.0.0"},
- "series": {
- "crash_free_rate(session)": [None, 1.0],
- "crash_free_rate(user)": [None, None],
- "crash_rate(session)": [None, 0.0],
- "crash_rate(user)": [None, None],
- },
- "totals": {
- "crash_free_rate(session)": 1.0,
- "crash_free_rate(user)": None,
- "crash_rate(session)": 0.0,
- "crash_rate(user)": None,
- },
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_pagination(self):
- def do_request(cursor):
- return self.do_request(
- {
- "project": self.project.id, # project without users
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["count_unique(user)", "sum(session)"],
- "query": "",
- "groupBy": "release",
- "orderBy": "sum(session)",
- "per_page": 1,
- **({"cursor": cursor} if cursor else {}),
- }
- )
- response = do_request(None)
- assert response.status_code == 200, response.data
- assert len(response.data["groups"]) == 1
- assert response.data["groups"] == [
- {
- "by": {"release": "foo@1.1.0"},
- "series": {"count_unique(user)": [0, 0], "sum(session)": [0, 1]},
- "totals": {"count_unique(user)": 0, "sum(session)": 1},
- }
- ]
- links = {link["rel"]: link for url, link in parse_link_header(response["Link"]).items()}
- assert links["previous"]["results"] == "false"
- assert links["next"]["results"] == "true"
- response = do_request(links["next"]["cursor"])
- assert response.status_code == 200, response.data
- assert len(response.data["groups"]) == 1
- assert response.data["groups"] == [
- {
- "by": {"release": "foo@1.0.0"},
- "series": {"count_unique(user)": [0, 0], "sum(session)": [0, 3]},
- "totals": {"count_unique(user)": 0, "sum(session)": 3},
- }
- ]
- links = {link["rel"]: link for url, link in parse_link_header(response["Link"]).items()}
- assert links["previous"]["results"] == "true"
- assert links["next"]["results"] == "false"
- def test_unrestricted_date_range(self):
- response = self.do_request(
- {
- "project": [-1],
- "statsPeriod": "7h",
- "interval": "5m",
- "field": ["sum(session)"],
- }
- )
- assert response.status_code == 200
- @freeze_time(MOCK_DATETIME)
- def test_release_is_empty(self):
- self.store_session(
- make_session(
- self.project1, started=SESSION_STARTED + 12 * 60, release="", environment=""
- )
- )
- for query in ('release:"" environment:""', 'release:"" OR environment:""'):
- # Empty strings are invalid values for releases and environments, but we should still handle those cases
- # correctly at the query layer
- response = self.do_request(
- {
- "project": self.project.id, # project without users
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": query,
- "groupBy": ["release", "environment"],
- }
- )
- assert response.status_code == 200, response.content
- assert result_sorted(response.data)["groups"] == [
- {
- "by": {"environment": "", "release": ""},
- "series": {"sum(session)": [0, 1]},
- "totals": {"sum(session)": 1},
- }
- ]
- @region_silo_test
- @patch("sentry.release_health.backend", MetricsReleaseHealthBackend())
- class SessionsMetricsSortReleaseTimestampTest(BaseMetricsTestCase, APITestCase):
- def do_request(self, query, user=None, org=None):
- self.login_as(user=user or self.user)
- url = reverse(
- "sentry-api-0-organization-sessions",
- kwargs={"organization_slug": (org or self.organization).slug},
- )
- return self.client.get(url, query, format="json")
- @freeze_time(MOCK_DATETIME)
- def test_order_by_with_no_releases(self):
- """
- Test that ensures if we have no releases in the preflight query when trying to order by
- `release.timestamp`, we get no groups.
- Essentially testing the empty preflight query filters branch.
- """
- project_random = self.create_project()
- for _ in range(0, 2):
- self.store_session(make_session(project_random))
- self.store_session(make_session(project_random, status="crashed"))
- response = self.do_request(
- {
- "project": project_random.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["crash_free_rate(session)"],
- "groupBy": ["release"],
- "orderBy": "-release.timestamp",
- "per_page": 3,
- }
- )
- assert response.data["groups"] == []
- def test_order_by_max_limit(self):
- response = self.do_request(
- {
- "project": self.project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["crash_free_rate(session)"],
- "groupBy": ["release"],
- "orderBy": "-release.timestamp",
- "per_page": 103,
- }
- )
- assert response.data["detail"] == (
- "This limit is too high for queries that requests a preflight query. "
- "Please choose a limit below 100"
- )
- @freeze_time(MOCK_DATETIME)
- def test_order_by(self):
- """
- Test that ensures that we are able to get the crash_free_rate for the most 2 recent
- releases when grouping by release
- """
- # Step 1: Create 3 releases
- release1b = self.create_release(version="1B")
- release1c = self.create_release(version="1C")
- release1d = self.create_release(version="1D")
- # Step 2: Create crash free rate for each of those releases
- # Release 1c -> 66.7% Crash free rate
- for _ in range(0, 2):
- self.store_session(make_session(self.project, release=release1c.version))
- self.store_session(make_session(self.project, release=release1c.version, status="crashed"))
- # Release 1b -> 33.3% Crash free rate
- for _ in range(0, 2):
- self.store_session(
- make_session(self.project, release=release1b.version, status="crashed")
- )
- self.store_session(make_session(self.project, release=release1b.version))
- # Create Sessions in each of these releases
- # Release 1d -> 80% Crash free rate
- for _ in range(0, 4):
- self.store_session(make_session(self.project, release=release1d.version))
- self.store_session(make_session(self.project, release=release1d.version, status="crashed"))
- # Step 3: Make request
- response = self.do_request(
- {
- "project": self.project.id, # project without users
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["crash_free_rate(session)"],
- "groupBy": ["release"],
- "orderBy": "-release.timestamp",
- "per_page": 3,
- }
- )
- # Step 4: Validate Results
- assert response.data["groups"] == [
- {
- "by": {"release": "1D"},
- "totals": {"crash_free_rate(session)": 0.8},
- "series": {"crash_free_rate(session)": [None, 0.8]},
- },
- {
- "by": {"release": "1C"},
- "totals": {"crash_free_rate(session)": 0.6666666666666667},
- "series": {"crash_free_rate(session)": [None, 0.6666666666666667]},
- },
- {
- "by": {"release": "1B"},
- "totals": {"crash_free_rate(session)": 0.33333333333333337},
- "series": {"crash_free_rate(session)": [None, 0.33333333333333337]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_order_by_with_session_status_groupby(self):
- """
- Test that ensures we are able to group by session.status and order by `release.timestamp`
- since `release.timestamp` is generated from a preflight query
- """
- rando_project = self.create_project()
- release_1a = self.create_release(project=rando_project, version="1A")
- release_1b = self.create_release(project=rando_project, version="1B")
- # Release 1B sessions
- for _ in range(4):
- self.store_session(
- make_session(rando_project, release=release_1b.version, status="crashed")
- )
- for _ in range(10):
- self.store_session(make_session(rando_project, release=release_1b.version))
- for _ in range(3):
- self.store_session(make_session(rando_project, errors=1, release=release_1b.version))
- # Release 1A sessions
- for _ in range(0, 2):
- self.store_session(
- make_session(rando_project, release=release_1a.version, status="crashed")
- )
- self.store_session(make_session(rando_project, release=release_1a.version))
- for _ in range(3):
- self.store_session(make_session(rando_project, errors=1, release=release_1a.version))
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release", "session.status"],
- "orderBy": "-release.timestamp",
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1B", "session.status": "abnormal"},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1B", "session.status": "crashed"},
- "totals": {"sum(session)": 4},
- "series": {"sum(session)": [0, 4]},
- },
- {
- "by": {"release": "1B", "session.status": "errored"},
- "totals": {"sum(session)": 3},
- "series": {"sum(session)": [0, 3]},
- },
- {
- "by": {"release": "1B", "session.status": "healthy"},
- "totals": {"sum(session)": 10},
- "series": {"sum(session)": [0, 10]},
- },
- {
- "by": {"release": "1A", "session.status": "abnormal"},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1A", "session.status": "crashed"},
- "totals": {"sum(session)": 2},
- "series": {"sum(session)": [0, 2]},
- },
- {
- "by": {"release": "1A", "session.status": "errored"},
- "totals": {"sum(session)": 3},
- "series": {"sum(session)": [0, 3]},
- },
- {
- "by": {"release": "1A", "session.status": "healthy"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_order_by_with_limit(self):
- rando_project = self.create_project()
- # Create two releases with no metrics data and then two releases with metric data
- release_1a = self.create_release(project=rando_project, version="1A")
- release_1b = self.create_release(project=rando_project, version="1B")
- self.create_release(project=rando_project, version="1C")
- self.create_release(project=rando_project, version="1D")
- self.store_session(make_session(rando_project, release=release_1a.version))
- self.store_session(make_session(rando_project, release=release_1b.version))
- self.store_session(
- make_session(rando_project, release=release_1b.version, status="crashed")
- )
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "orderBy": "-release.timestamp",
- "per_page": 3,
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1D"},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1C"},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1B"},
- "totals": {"sum(session)": 2},
- "series": {"sum(session)": [0, 2]},
- },
- ]
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release", "session.status"],
- "orderBy": "-release.timestamp",
- "per_page": 4,
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1D", "session.status": None},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1C", "session.status": None},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1B", "session.status": "abnormal"},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1B", "session.status": "crashed"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release", "session.status", "project"],
- "orderBy": "-release.timestamp",
- "per_page": 2,
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1D", "session.status": None, "project": None},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- {
- "by": {"release": "1C", "session.status": None, "project": None},
- "totals": {"sum(session)": 0},
- "series": {"sum(session)": [0, 0]},
- },
- ]
- @freeze_time(MOCK_DATETIME)
- def test_order_by_with_limit_and_offset(self):
- rando_project = self.create_project()
- # Create two releases with no metrics data and then two releases with metric data
- release_1a = self.create_release(project=rando_project, version="1A")
- release_1b = self.create_release(project=rando_project, version="1B")
- self.create_release(project=rando_project, version="1C")
- self.create_release(project=rando_project, version="1D")
- self.store_session(make_session(rando_project, release=release_1a.version))
- self.store_session(make_session(rando_project, release=release_1b.version))
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "orderBy": "-release.timestamp",
- "per_page": 3,
- "cursor": Cursor(0, 1),
- }
- )
- assert response.data["detail"] == (
- "Passing an offset value greater than 0 when ordering by release.timestamp "
- "is not permitted"
- )
- @freeze_time(MOCK_DATETIME)
- def test_order_by_with_environment_filter_on_preflight(self):
- rando_project = self.create_project()
- rando_env = self.create_environment(name="rando_env", project=self.project)
- # Create two releases with no metrics data and then two releases with metric data
- release_1a = self.create_release(
- project=rando_project, version="1A", environments=[rando_env]
- )
- release_1b = self.create_release(
- project=rando_project, version="1B", environments=[rando_env]
- )
- release_1c = self.create_release(project=rando_project, version="1C")
- release_1d = self.create_release(project=rando_project, version="1D")
- self.store_session(
- make_session(rando_project, release=release_1a.version, environment="rando_env")
- )
- self.store_session(
- make_session(rando_project, release=release_1b.version, environment="rando_env")
- )
- self.store_session(make_session(rando_project, release=release_1c.version))
- self.store_session(make_session(rando_project, release=release_1d.version))
- # Test env condition with IN
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": "environment:[rando_env,rando_enc2]",
- "groupBy": ["release", "environment"],
- "orderBy": "-release.timestamp",
- "per_page": 4,
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1B", "environment": "rando_env"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- {
- "by": {"release": "1A", "environment": "rando_env"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- # Test env condition with NOT IN
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": "!environment:[rando_env,rando_enc2]",
- "groupBy": ["release", "environment"],
- "orderBy": "-release.timestamp",
- "per_page": 4,
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1D", "environment": "production"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- {
- "by": {"release": "1C", "environment": "production"},
- "totals": {"sum(session)": 1},
- "series": {"sum(session)": [0, 1]},
- },
- ]
- # Test env condition with invalid OR operation
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "field": ["sum(session)"],
- "query": "environment:rando_env OR environment:rando_enc2",
- "groupBy": ["release", "environment"],
- "orderBy": "-release.timestamp",
- "per_page": 4,
- }
- )
- assert response.json()["detail"] == "Unable to parse condition with environment"
- @freeze_time(MOCK_DATETIME)
- def test_order_by_without_release_groupby(self):
- rando_project = self.create_project()
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "query": "session.status:[crashed,errored]",
- "field": ["sum(session)"],
- "orderBy": "-release.timestamp",
- "per_page": 2,
- }
- )
- assert response.data["detail"] == (
- "To sort by release.timestamp, tag release must be in the groupBy"
- )
- @freeze_time(MOCK_DATETIME)
- def test_order_by_release_with_session_status_current_filter(self):
- rando_project = self.create_project()
- release_1a = self.create_release(project=rando_project, version="1A")
- release_1b = self.create_release(project=rando_project, version="1B")
- # Release 1B sessions
- for _ in range(4):
- self.store_session(
- make_session(rando_project, release=release_1b.version, status="crashed")
- )
- for _ in range(10):
- self.store_session(make_session(rando_project, release=release_1b.version))
- for _ in range(3):
- self.store_session(make_session(rando_project, errors=1, release=release_1b.version))
- # Release 1A sessions
- for _ in range(0, 2):
- self.store_session(
- make_session(rando_project, release=release_1a.version, status="crashed")
- )
- self.store_session(make_session(rando_project, release=release_1a.version))
- for _ in range(3):
- self.store_session(make_session(rando_project, errors=1, release=release_1a.version))
- response = self.do_request(
- {
- "project": rando_project.id,
- "statsPeriod": "1d",
- "interval": "1d",
- "query": "session.status:[crashed,errored]",
- "field": ["sum(session)"],
- "groupBy": ["release"],
- "orderBy": "-release.timestamp",
- }
- )
- assert response.data["groups"] == [
- {
- "by": {"release": "1B"},
- "totals": {"sum(session)": 7},
- "series": {"sum(session)": [0, 7]},
- },
- {
- "by": {"release": "1A"},
- "totals": {"sum(session)": 5},
- "series": {"sum(session)": [0, 5]},
- },
- ]
|