123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915 |
- from datetime import timedelta
- from django.urls import reverse
- from sentry.testutils.cases import APITestCase, SnubaTestCase
- from sentry.testutils.helpers import parse_link_header
- from sentry.testutils.helpers.datetime import before_now, iso_format
- from sentry.utils.samples import load_data
- class OrganizationEventsTrendsBase(APITestCase, SnubaTestCase):
- def setUp(self):
- super().setUp()
- self.login_as(user=self.user)
- self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
- self.prototype = load_data("transaction")
- data = self.prototype.copy()
- data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=30))
- data["user"] = {"email": "foo@example.com"}
- data["timestamp"] = iso_format(self.day_ago + timedelta(minutes=30, seconds=2))
- data["measurements"]["lcp"]["value"] = 2000
- self.store_event(data, project_id=self.project.id)
- second = [0, 2, 10]
- for i in range(3):
- data = self.prototype.copy()
- data["start_timestamp"] = iso_format(self.day_ago + timedelta(hours=1, minutes=30 + i))
- data["timestamp"] = iso_format(
- self.day_ago + timedelta(hours=1, minutes=30 + i, seconds=second[i])
- )
- data["measurements"]["lcp"]["value"] = second[i] * 1000
- data["user"] = {"email": f"foo{i}@example.com"}
- self.store_event(data, project_id=self.project.id)
- self.expected_data = {
- "count_range_1": 1,
- "count_range_2": 3,
- "transaction": self.prototype["transaction"],
- "project": self.project.slug,
- }
- def assert_event(self, data):
- for key, value in self.expected_data.items():
- assert data[key] == value, key
- class OrganizationEventsTrendsEndpointTest(OrganizationEventsTrendsBase):
- def setUp(self):
- super().setUp()
- self.url = reverse(
- "sentry-api-0-organization-events-trends",
- kwargs={"organization_slug": self.project.organization.slug},
- )
- self.features = {"organizations:performance-view": True}
- def test_simple(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendType": "regression",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 2000,
- "count_percentage": 3.0,
- "trend_difference": 0.0,
- "trend_percentage": 1.0,
- }
- )
- self.assert_event(events["data"][0])
- def test_web_vital(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendType": "regression",
- "trendFunction": "p50(measurements.lcp)",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- # LCP values are identical to duration
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 2000,
- "count_percentage": 3.0,
- "trend_difference": 0.0,
- "trend_percentage": 1.0,
- }
- )
- self.assert_event(events["data"][0])
- def test_p75(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p75()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 6000,
- "count_percentage": 3.0,
- "trend_difference": 4000.0,
- "trend_percentage": 3.0,
- }
- )
- self.assert_event(events["data"][0])
- def test_p95(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p95()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 9200,
- "count_percentage": 3.0,
- "trend_difference": 7200.0,
- "trend_percentage": 4.6,
- }
- )
- self.assert_event(events["data"][0])
- def test_p99(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p99()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 9840,
- "count_percentage": 3.0,
- "trend_difference": 7840.0,
- "trend_percentage": 4.92,
- }
- )
- self.assert_event(events["data"][0])
- def test_trend_percentage_query_alias(self):
- queries = [
- ("trend_percentage():>0%", "regression", 1),
- ("trend_percentage():392%", "regression", 1),
- ("trend_percentage():>0%", "improved", 0),
- ("trend_percentage():392%", "improved", 0),
- ]
- for query_data in queries:
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": f"event.type:transaction {query_data[0]}",
- "trendType": query_data[1],
- # Use p99 since it has the most significant change
- "trendFunction": "p99()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == query_data[2], query_data
- def test_trend_percentage_query_alias_as_sort(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendType": "improved",
- "trendFunction": "p50()",
- "sort": "trend_percentage()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- def test_trend_difference_query_alias(self):
- queries = [
- ("trend_difference():>7s", "regression", 1),
- ("trend_difference():7.84s", "regression", 1),
- ("trend_difference():>7s", "improved", 0),
- ("trend_difference():7.84s", "improved", 0),
- ]
- for query_data in queries:
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": f"event.type:transaction {query_data[0]}",
- "trendType": query_data[1],
- # Use p99 since it has the most significant change
- "trendFunction": "p99()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == query_data[2], query_data
- def test_avg_trend_function(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "avg(transaction.duration)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 4000,
- "count_percentage": 3.0,
- "trend_difference": 2000.0,
- "trend_percentage": 2.0,
- }
- )
- self.assert_event(events["data"][0])
- def test_invalid_trend_function(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "apdex(450)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 400
- def test_divide_by_zero(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- # Set the timeframe to where the second range has no transactions so all the counts/percentile are 0
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago - timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "count_range_2": 4,
- "count_range_1": 0,
- "aggregate_range_1": 0,
- "aggregate_range_2": 2000.0,
- "count_percentage": None,
- "trend_difference": 0,
- "trend_percentage": None,
- }
- )
- self.assert_event(events["data"][0])
- def test_auto_aggregation(self):
- # absolute_correlation is automatically added, and not a part of data otherwise
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- # Set the timeframe to where the second range has no transactions so all the counts/percentile are 0
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago - timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction absolute_correlation():>0.2",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "count_range_2": 4,
- "count_range_1": 0,
- "aggregate_range_1": 0,
- "aggregate_range_2": 2000.0,
- "count_percentage": None,
- "trend_difference": 0,
- "trend_percentage": None,
- }
- )
- self.assert_event(events["data"][0])
- class OrganizationEventsTrendsStatsEndpointTest(OrganizationEventsTrendsBase):
- def setUp(self):
- super().setUp()
- self.url = reverse(
- "sentry-api-0-organization-events-trends-stats",
- kwargs={"organization_slug": self.project.organization.slug},
- )
- self.features = {"organizations:performance-view": True}
- def test_simple(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 2000,
- "count_percentage": 3.0,
- "trend_difference": 0.0,
- "trend_percentage": 1.0,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 2000}],
- ]
- def test_web_vital(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p50(measurements.lcp)",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 2000,
- "count_percentage": 3.0,
- "trend_difference": 0.0,
- "trend_percentage": 1.0,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 2000}],
- ]
- def test_p75(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p75()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 6000,
- "count_percentage": 3.0,
- "trend_difference": 4000.0,
- "trend_percentage": 3.0,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 6000}],
- ]
- def test_p95(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p95()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 9200,
- "count_percentage": 3.0,
- "trend_difference": 7200.0,
- "trend_percentage": 4.6,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 9200}],
- ]
- def test_p99(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p99()",
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 9840,
- "count_percentage": 3.0,
- "trend_difference": 7840.0,
- "trend_percentage": 4.92,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 9840}],
- ]
- def test_avg_trend_function(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "interval": "1h",
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "avg(transaction.duration)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 4000,
- "count_percentage": 3.0,
- "trend_difference": 2000.0,
- "trend_percentage": 2.0,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 4000}],
- ]
- def test_alias_in_conditions(self):
- query_parts = [
- "event.type:transaction",
- "count_percentage():>0.25",
- "count_percentage():<4",
- "trend_percentage():>0%",
- ]
- queries = [" ".join(query_parts), " AND ".join(query_parts)]
- for query in queries:
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "interval": "1h",
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": query,
- "trendFunction": "avg(transaction.duration)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "aggregate_range_1": 2000,
- "aggregate_range_2": 4000,
- "count_percentage": 3.0,
- "trend_difference": 2000.0,
- "trend_percentage": 2.0,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 4000}],
- ]
- def test_trend_with_middle(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "middle": iso_format(self.day_ago + timedelta(hours=1, minutes=31)),
- "start": iso_format(self.day_ago),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "avg(transaction.duration)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "count_range_2": 2,
- "count_range_1": 2,
- "aggregate_range_1": 1000,
- "aggregate_range_2": 6000,
- "count_percentage": 1.0,
- "trend_difference": 5000.0,
- "trend_percentage": 6.0,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 2000}],
- [{"count": 4000}],
- ]
- def test_invalid_middle_date(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "start": iso_format(self.day_ago),
- "middle": "blah",
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "p50()",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 400
- response = self.client.get(
- self.url,
- format="json",
- data={
- "start": iso_format(self.day_ago),
- "middle": iso_format(self.day_ago - timedelta(hours=2)),
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "apdex(450)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 400
- response = self.client.get(
- self.url,
- format="json",
- data={
- "start": iso_format(self.day_ago),
- "middle": iso_format(self.day_ago + timedelta(hours=4)),
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "apdex(450)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 400
- def test_invalid_trend_function(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "trendFunction": "apdex(450)",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 400
- def test_divide_by_zero(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- # Set the timeframe to where the second range has no transactions so all the counts/percentile are 0
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago - timedelta(hours=2)),
- "interval": "1h",
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- events = response.data["events"]
- result_stats = response.data["stats"]
- assert len(events["data"]) == 1
- self.expected_data.update(
- {
- "count_range_2": 4,
- "count_range_1": 0,
- "aggregate_range_1": 0,
- "aggregate_range_2": 2000.0,
- "count_percentage": None,
- "trend_difference": 0,
- "trend_percentage": None,
- }
- )
- self.assert_event(events["data"][0])
- stats = result_stats[f"{self.project.slug},{self.prototype['transaction']}"]
- assert [attrs for time, attrs in stats["data"]] == [
- [{"count": 0}],
- [{"count": 0}],
- [{"count": 2000}],
- [{"count": 2000}],
- ]
- class OrganizationEventsTrendsPagingTest(APITestCase, SnubaTestCase):
- def setUp(self):
- super().setUp()
- self.login_as(user=self.user)
- self.url = reverse(
- "sentry-api-0-organization-events-trends-stats",
- kwargs={"organization_slug": self.project.organization.slug},
- )
- self.day_ago = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
- self.prototype = load_data("transaction")
- self.features = {"organizations:performance-view": True}
- # Make 10 transactions for paging
- for i in range(10):
- for j in range(2):
- data = self.prototype.copy()
- data["user"] = {"email": "foo@example.com"}
- data["start_timestamp"] = iso_format(self.day_ago + timedelta(minutes=30))
- data["timestamp"] = iso_format(
- self.day_ago + timedelta(hours=j, minutes=30, seconds=2)
- )
- if i < 5:
- data["transaction"] = f"transaction_1{i}"
- else:
- data["transaction"] = f"transaction_2{i}"
- self.store_event(data, project_id=self.project.id)
- def _parse_links(self, header):
- # links come in {url: {...attrs}}, but we need {rel: {...attrs}}
- links = {}
- for url, attrs in parse_link_header(header).items():
- links[attrs["rel"]] = attrs
- attrs["href"] = url
- return links
- def test_pagination(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- # Set the timeframe to where the second range has no transactions so all the counts/percentile are 0
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago - timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- links = self._parse_links(response["Link"])
- assert links["previous"]["results"] == "false"
- assert links["next"]["results"] == "true"
- assert len(response.data["events"]["data"]) == 5
- response = self.client.get(links["next"]["href"], format="json")
- assert response.status_code == 200, response.content
- links = self._parse_links(response["Link"])
- assert links["previous"]["results"] == "true"
- assert links["next"]["results"] == "false"
- assert len(response.data["events"]["data"]) == 5
- def test_pagination_with_query(self):
- with self.feature(self.features):
- response = self.client.get(
- self.url,
- format="json",
- data={
- # Set the timeframe to where the second range has no transactions so all the counts/percentile are 0
- "end": iso_format(self.day_ago + timedelta(hours=2)),
- "start": iso_format(self.day_ago - timedelta(hours=2)),
- "field": ["project", "transaction"],
- "query": "event.type:transaction transaction:transaction_1*",
- "project": [self.project.id],
- },
- )
- assert response.status_code == 200, response.content
- links = self._parse_links(response["Link"])
- assert links["previous"]["results"] == "false"
- assert links["next"]["results"] == "false"
- assert len(response.data["events"]["data"]) == 5
|