123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- from datetime import timedelta
- from django.urls import reverse
- from sentry.models.transaction_threshold import ProjectTransactionThreshold, TransactionMetric
- from sentry.testutils import APITestCase, SnubaTestCase
- from sentry.testutils.helpers.datetime import before_now, iso_format
- from sentry.utils.samples import load_data
- class OrganizationEventsVitalsEndpointTest(APITestCase, SnubaTestCase):
- def setUp(self):
- super().setUp()
- self.start = before_now(days=1).replace(hour=10, minute=0, second=0, microsecond=0)
- self.end = self.start + timedelta(hours=6)
- self.transaction_data = load_data("transaction", timestamp=self.start)
- self.query = {
- "start": iso_format(self.start),
- "end": iso_format(self.end),
- }
- self.features = {}
- def store_event(self, data, measurements=None, **kwargs):
- if measurements:
- for vital, value in measurements.items():
- data["measurements"][vital]["value"] = value
- return super().store_event(
- data.copy(),
- project_id=self.project.id,
- )
- def do_request(self, query=None, features=None):
- if features is None:
- features = {"organizations:discover-basic": True}
- features.update(self.features)
- if query is None:
- query = self.query
- self.login_as(user=self.user)
- url = reverse(
- "sentry-api-0-organization-events-vitals",
- kwargs={"organization_slug": self.organization.slug},
- )
- with self.feature(features):
- return self.client.get(url, query, format="json")
- def test_no_projects(self):
- response = self.do_request()
- assert response.status_code == 200, response.content
- assert len(response.data) == 0
- def test_no_vitals(self):
- self.store_event(
- self.transaction_data,
- project_id=self.project.id,
- )
- self.query.update({"vital": []})
- response = self.do_request()
- assert response.status_code == 400, response.content
- assert "Need to pass at least one vital" == response.data["detail"]
- def test_bad_vital(self):
- self.store_event(
- self.transaction_data,
- project_id=self.project.id,
- )
- self.query.update({"vital": ["foobar"]})
- response = self.do_request()
- assert response.status_code == 400, response.content
- assert "foobar is not a valid vital" == response.data["detail"]
- def test_simple(self):
- data = self.transaction_data.copy()
- for lcp in [2000, 3000, 5000]:
- self.store_event(
- data,
- {"lcp": lcp},
- project_id=self.project.id,
- )
- self.query.update({"vital": ["measurements.lcp"]})
- response = self.do_request()
- assert response.status_code == 200, response.content
- assert response.data["measurements.lcp"] == {
- "good": 1,
- "meh": 1,
- "poor": 1,
- "total": 3,
- "p75": 4000,
- }
- def test_simple_with_refining_user_misery_filter(self):
- project1 = self.create_project(organization=self.organization)
- project2 = self.create_project(organization=self.organization)
- ProjectTransactionThreshold.objects.create(
- project=project1,
- organization=project1.organization,
- threshold=100,
- metric=TransactionMetric.LCP.value,
- )
- ProjectTransactionThreshold.objects.create(
- project=project2,
- organization=project2.organization,
- threshold=1000,
- metric=TransactionMetric.LCP.value,
- )
- data = self.transaction_data.copy()
- for project in [project1, project2]:
- for lcp in [2000, 3000, 5000]:
- self.store_event(
- data,
- {"lcp": lcp},
- project_id=project.id,
- )
- self.query.update({"vital": ["measurements.lcp"]})
- response = self.do_request(
- features={"organizations:global-views": True, "organizations:discover-basic": True}
- )
- assert response.status_code == 200, response.content
- assert response.data["measurements.lcp"] == {
- "good": 0,
- "meh": 1,
- "poor": 1,
- "total": 2,
- "p75": 4500,
- }
- self.query.update({"query": "user_misery():<0.04"})
- response = self.do_request(
- features={"organizations:global-views": True, "organizations:discover-basic": True}
- )
- assert response.status_code == 200, response.content
- assert len(response.data) == 1
- assert response.data["measurements.lcp"] == {
- "good": 0,
- "meh": 1,
- "poor": 1,
- "total": 2,
- "p75": 4500,
- }
- def test_grouping(self):
- counts = [
- (100, 2),
- (3000, 3),
- (4500, 1),
- ]
- for duration, count in counts:
- for _ in range(count):
- self.store_event(
- load_data("transaction", timestamp=self.start),
- {"lcp": duration},
- project_id=self.project.id,
- )
- self.query.update({"vital": ["measurements.lcp"]})
- response = self.do_request()
- assert response.status_code == 200
- assert response.data["measurements.lcp"] == {
- "good": 2,
- "meh": 3,
- "poor": 1,
- "total": 6,
- "p75": 3000,
- }
- def test_multiple_vitals(self):
- vitals = {"lcp": 3000, "fid": 50, "cls": 0.15, "fcp": 5000, "fp": 4000}
- self.store_event(
- load_data("transaction", timestamp=self.start),
- vitals,
- project_id=self.project.id,
- )
- self.query.update(
- {
- "vital": [
- "measurements.lcp",
- "measurements.fid",
- "measurements.cls",
- "measurements.fcp",
- "measurements.fp",
- ]
- }
- )
- response = self.do_request()
- assert response.status_code == 200
- assert response.data["measurements.lcp"] == {
- "good": 0,
- "meh": 1,
- "poor": 0,
- "total": 1,
- "p75": 3000,
- }
- assert response.data["measurements.fid"] == {
- "good": 1,
- "meh": 0,
- "poor": 0,
- "total": 1,
- "p75": 50,
- }
- assert response.data["measurements.cls"] == {
- "good": 0,
- "meh": 1,
- "poor": 0,
- "total": 1,
- "p75": 0.15,
- }
- assert response.data["measurements.fcp"] == {
- "good": 0,
- "meh": 0,
- "poor": 1,
- "total": 1,
- "p75": 5000,
- }
- assert response.data["measurements.fp"] == {
- "good": 0,
- "meh": 0,
- "poor": 1,
- "total": 1,
- "p75": 4000,
- }
- def test_transactions_without_vitals(self):
- del self.transaction_data["measurements"]
- self.store_event(
- self.transaction_data,
- project_id=self.project.id,
- )
- self.query.update({"vital": ["measurements.lcp", "measurements.fcp"]})
- response = self.do_request()
- assert response.status_code == 200
- assert response.data["measurements.lcp"] == {
- "good": 0,
- "meh": 0,
- "poor": 0,
- "total": 0,
- "p75": None,
- }
- assert response.data["measurements.fcp"] == {
- "good": 0,
- "meh": 0,
- "poor": 0,
- "total": 0,
- "p75": None,
- }
- class OrganizationEventsVitalsEndpointTestWithSnql(OrganizationEventsVitalsEndpointTest):
- def setUp(self):
- super().setUp()
- self.features = {"organizations:performance-use-snql": True}
|