12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049 |
- from datetime import timedelta
- from django.urls import reverse
- from sentry.api.endpoints.organization_events_trends import OrganizationEventsTrendsEndpointBase
- from sentry.search.events.filter import get_filter
- from sentry.testutils import APITestCase, SnubaTestCase
- from sentry.testutils.cases import TestCase
- 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
- class OrganizationEventsTrendsAliasTest(TestCase):
- def setUp(self):
- self.improved_aliases = OrganizationEventsTrendsEndpointBase.get_function_aliases(
- "improved"
- )
- self.regression_aliases = OrganizationEventsTrendsEndpointBase.get_function_aliases(
- "regression"
- )
- def test_simple(self):
- result = get_filter(
- "trend_percentage():>0% trend_difference():>0", {"aliases": self.improved_aliases}
- )
- assert result.having == [
- ["trend_percentage", "<", 1.0],
- ["trend_difference", "<", 0.0],
- ]
- result = get_filter(
- "trend_percentage():>0% trend_difference():>0", {"aliases": self.regression_aliases}
- )
- assert result.having == [
- ["trend_percentage", ">", 1.0],
- ["trend_difference", ">", 0.0],
- ]
- def test_and_query(self):
- result = get_filter(
- "trend_percentage():>0% AND trend_percentage():<100%",
- {"aliases": self.improved_aliases},
- )
- assert result.having == [["trend_percentage", "<", 1.0], ["trend_percentage", ">", 0.0]]
- result = get_filter(
- "trend_percentage():>0% AND trend_percentage():<100%",
- {"aliases": self.regression_aliases},
- )
- assert result.having == [["trend_percentage", ">", 1.0], ["trend_percentage", "<", 2.0]]
- def test_or_query(self):
- result = get_filter(
- "trend_percentage():>0% OR trend_percentage():<100%",
- {"aliases": self.improved_aliases},
- )
- assert result.having == [
- [
- [
- "or",
- [["less", ["trend_percentage", 1.0]], ["greater", ["trend_percentage", 0.0]]],
- ],
- "=",
- 1,
- ]
- ]
- result = get_filter(
- "trend_percentage():>0% OR trend_percentage():<100%",
- {"aliases": self.regression_aliases},
- )
- assert result.having == [
- [
- [
- "or",
- [["greater", ["trend_percentage", 1.0]], ["less", ["trend_percentage", 2.0]]],
- ],
- "=",
- 1,
- ]
- ]
- def test_greater_than(self):
- result = get_filter("trend_difference():>=0", {"aliases": self.improved_aliases})
- assert result.having == [["trend_difference", "<=", 0.0]]
- result = get_filter("trend_difference():>=0", {"aliases": self.regression_aliases})
- assert result.having == [["trend_difference", ">=", 0.0]]
- def test_negation(self):
- result = get_filter("!trend_difference():>=0", {"aliases": self.improved_aliases})
- assert result.having == [["trend_difference", ">", 0.0]]
- result = get_filter("!trend_difference():>=0", {"aliases": self.regression_aliases})
- assert result.having == [["trend_difference", "<", 0.0]]
- def test_confidence(self):
- result = get_filter("confidence():>6", {"aliases": self.improved_aliases})
- assert result.having == [["t_test", ">", 6.0]]
- result = get_filter("confidence():>6", {"aliases": self.regression_aliases})
- assert result.having == [["t_test", "<", -6.0]]
- class OrganizationEventsTrendsEndpointTestWithSnql(OrganizationEventsTrendsEndpointTest):
- def setUp(self):
- super().setUp()
- self.features = {
- "organizations:performance-view": True,
- "organizations:trends-use-snql": True,
- }
- class OrganizationEventsTrendsStatsEndpointTestWithSnql(OrganizationEventsTrendsStatsEndpointTest):
- def setUp(self):
- super().setUp()
- self.features = {
- "organizations:performance-view": True,
- "organizations:trends-use-snql": True,
- }
- class OrganizationEventsTrendsPagingTestWithSnql(OrganizationEventsTrendsPagingTest):
- def setUp(self):
- super().setUp()
- self.features = {
- "organizations:performance-view": True,
- "organizations:trends-use-snql": True,
- }
|