123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373 |
- 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
- from sentry.testutils.helpers.datetime import freeze_time
- from sentry.testutils.helpers.link_header import parse_link_header
- from sentry.utils.cursors import Cursor
- 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.UTC) - datetime.timedelta(days=1)
- TWO_DAYS_AGO = datetime.datetime.now(tz=datetime.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 = MOCK_DATETIME.timestamp()
- 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, BaseMetricsTestCase):
- 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_id_or_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]},
- },
- ]
- @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},
- }
- ]
- @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_id_or_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]},
- },
- ]
|