1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027 |
- import random
- from collections import namedtuple
- from copy import deepcopy
- from datetime import timedelta
- import pytest
- from django.urls import reverse
- from rest_framework.exceptions import ErrorDetail
- from sentry.testutils import APITestCase, SnubaTestCase
- from sentry.testutils.helpers.datetime import before_now, iso_format
- from sentry.utils.samples import load_data
- from sentry.utils.snuba import get_array_column_alias
- HistogramSpec = namedtuple("HistogramSpec", ["start", "end", "fields"])
- ARRAY_COLUMNS = ["measurements", "span_op_breakdowns"]
- class OrganizationEventsHistogramEndpointTest(APITestCase, SnubaTestCase):
- def setUp(self):
- super().setUp()
- self.min_ago = iso_format(before_now(minutes=1))
- self.data = load_data("transaction")
- self.features = {}
- def populate_events(self, specs):
- start = before_now(minutes=5)
- for spec in specs:
- spec = HistogramSpec(*spec)
- for suffix_key, count in spec.fields:
- for i in range(count):
- data = deepcopy(self.data)
- measurement_name = suffix_key
- breakdown_name = f"ops.{suffix_key}"
- data["timestamp"] = iso_format(start)
- data["start_timestamp"] = iso_format(start - timedelta(seconds=i))
- value = random.random() * (spec.end - spec.start) + spec.start
- data["transaction"] = f"/measurement/{measurement_name}/value/{value}"
- data["measurements"] = {measurement_name: {"value": value}}
- data["breakdowns"] = {
- "span_ops": {
- breakdown_name: {"value": value},
- }
- }
- self.store_event(data, self.project.id)
- def as_response_data(self, specs):
- data = {}
- for spec in specs:
- spec = HistogramSpec(*spec)
- for measurement, count in sorted(spec.fields):
- if measurement not in data:
- data[measurement] = []
- data[measurement].append({"bin": spec.start, "count": count})
- return data
- def do_request(self, query, features=None):
- if features is None:
- features = {"organizations:performance-view": True}
- features.update(self.features)
- self.login_as(user=self.user)
- url = reverse(
- "sentry-api-0-organization-events-histogram",
- 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 response.data == {}
- def test_good_params(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "query": "event.type:transaction",
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- def test_good_params_with_optionals(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "query": "event.type:transaction",
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "precision": 0,
- "min": 0,
- "max": 10,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- def test_bad_params_reverse_min_max(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "query": "event.type:transaction",
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "precision": 0,
- "min": 10,
- "max": 5,
- }
- response = self.do_request(query)
- assert response.data == {"non_field_errors": ["min cannot be greater than max."]}
- def test_bad_params_missing_fields(self):
- query = {
- "project": [self.project.id],
- "numBuckets": 10,
- }
- response = self.do_request(query)
- assert response.status_code == 400
- assert response.data == {
- "field": [ErrorDetail(string="This field is required.", code="required")],
- }
- def test_bad_params_too_many_fields(self):
- query = {
- "project": [self.project.id],
- "field": ["foo", "bar", "baz", "qux", "quux"],
- "numBuckets": 10,
- "min": 0,
- "max": 100,
- "precision": 0,
- }
- response = self.do_request(query)
- assert response.status_code == 400
- assert response.data == {
- "field": ["Ensure this field has no more than 4 elements."],
- }
- def test_bad_params_mixed_fields(self):
- for array_column in ARRAY_COLUMNS:
- for other_array_column in ARRAY_COLUMNS:
- query = {
- "project": [self.project.id],
- "field": [
- "foo",
- f"{get_array_column_alias(array_column)}.foo",
- f"{get_array_column_alias(other_array_column)}.bar",
- ],
- "numBuckets": 10,
- "min": 0,
- "max": 100,
- "precision": 0,
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "field": [
- "You can only generate histogram for one column at a time unless they are all measurements or all span op breakdowns."
- ],
- }, f"failing for {array_column}"
- def test_bad_params_missing_num_buckets(self):
- query = {
- "project": [self.project.id],
- "field": ["foo"],
- }
- response = self.do_request(query)
- assert response.status_code == 400
- assert response.data == {
- "numBuckets": ["This field is required."],
- }
- def test_bad_params_invalid_num_buckets(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": "baz",
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "numBuckets": ["A valid integer is required."],
- }, f"failing for {array_column}"
- def test_bad_params_invalid_negative_num_buckets(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": -1,
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "numBuckets": ["Ensure this value is greater than or equal to 1."],
- }, f"failing for {array_column}"
- def test_bad_params_num_buckets_too_large(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 150,
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "numBuckets": ["Ensure this value is less than or equal to 100."],
- }, f"failing for {array_column}"
- def test_bad_params_invalid_precision_too_small(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "precision": -1,
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "precision": ["Ensure this value is greater than or equal to 0."],
- }, f"failing for {array_column}"
- def test_bad_params_invalid_precision_too_big(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "precision": 100,
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "precision": ["Ensure this value is less than or equal to 4."],
- }, f"failing for {array_column}"
- def test_bad_params_invalid_min(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "min": "qux",
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "min": ["A valid number is required."],
- }, f"failing for {array_column}"
- def test_bad_params_invalid_max(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "max": "qux",
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "max": ["A valid number is required."],
- }, f"failing for {array_column}"
- def test_histogram_empty(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [(i, i + 1, [(f"{alias}.foo", 0), (f"{alias}.bar", 0)]) for i in range(5)]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_simple(self):
- # range is [0, 5), so it is divided into 5 buckets of width 1
- specs = [
- (0, 1, [("foo", 1)]),
- (1, 2, [("foo", 1)]),
- (2, 3, [("foo", 1)]),
- (4, 5, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 1, [(f"{alias}.foo", 1)]),
- (1, 2, [(f"{alias}.foo", 1)]),
- (2, 3, [(f"{alias}.foo", 1)]),
- (3, 4, [(f"{alias}.foo", 0)]),
- (4, 5, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_simple_using_min_max(self):
- # range is [0, 5), so it is divided into 5 buckets of width 1
- specs = [
- (0, 1, [("foo", 1)]),
- (1, 2, [("foo", 1)]),
- (2, 3, [("foo", 1)]),
- (4, 5, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "min": 0,
- "max": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 1, [(f"{alias}.foo", 1)]),
- (1, 2, [(f"{alias}.foo", 1)]),
- (2, 3, [(f"{alias}.foo", 1)]),
- (3, 4, [(f"{alias}.foo", 0)]),
- (4, 5, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_simple_using_given_min_above_queried_max(self):
- # All these events are out of range of the query parameters,
- # and should not appear in the results.
- specs = [
- (0, 1, [("foo", 1)]),
- (1, 2, [("foo", 1)]),
- (2, 3, [("foo", 1)]),
- (4, 5, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "min": 6,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (6, 7, [(f"{alias}.foo", 0)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_simple_using_given_max_below_queried_min(self):
- # All these events are out of range of the query parameters,
- # and should not appear in the results.
- specs = [
- (6, 7, [("foo", 1)]),
- (8, 9, [("foo", 1)]),
- (10, 11, [("foo", 1)]),
- (12, 13, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "max": 6,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (5, 6, [(f"{alias}.foo", 0)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_large_buckets(self):
- # make sure that it works for large width buckets
- # range is [0, 99], so it is divided into 5 buckets of width 20
- specs = [
- (0, 0, [("foo", 2)]),
- (99, 99, [("foo", 2)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 20, [(f"{alias}.foo", 2)]),
- (20, 40, [(f"{alias}.foo", 0)]),
- (40, 60, [(f"{alias}.foo", 0)]),
- (60, 80, [(f"{alias}.foo", 0)]),
- (80, 100, [(f"{alias}.foo", 2)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_non_zero_offset(self):
- # range is [10, 15), so it is divided into 5 buckets of width 1
- specs = [
- (10, 11, [("foo", 1)]),
- (12, 13, [("foo", 1)]),
- (13, 14, [("foo", 1)]),
- (14, 15, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (10, 11, [(f"{alias}.foo", 1)]),
- (11, 12, [(f"{alias}.foo", 0)]),
- (12, 13, [(f"{alias}.foo", 1)]),
- (13, 14, [(f"{alias}.foo", 1)]),
- (14, 15, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_extra_data(self):
- # range is [11, 16), so it is divided into 5 buckets of width 1
- # make sure every bin has some value
- specs = [
- (10, 11, [("foo", 1)]),
- (11, 12, [("foo", 1)]),
- (12, 13, [("foo", 1)]),
- (13, 14, [("foo", 1)]),
- (14, 15, [("foo", 1)]),
- (15, 16, [("foo", 1)]),
- (16, 17, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "min": 11,
- "max": 16,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (11, 12, [(f"{alias}.foo", 1)]),
- (12, 13, [(f"{alias}.foo", 1)]),
- (13, 14, [(f"{alias}.foo", 1)]),
- (14, 15, [(f"{alias}.foo", 1)]),
- (15, 16, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_non_zero_min_large_buckets(self):
- # range is [10, 59], so it is divided into 5 buckets of width 10
- specs = [
- (10, 10, [("foo", 1)]),
- (40, 50, [("foo", 1)]),
- (59, 59, [("foo", 2)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (10, 20, [(f"{alias}.foo", 1)]),
- (20, 30, [(f"{alias}.foo", 0)]),
- (30, 40, [(f"{alias}.foo", 0)]),
- (40, 50, [(f"{alias}.foo", 1)]),
- (50, 60, [(f"{alias}.foo", 2)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- @pytest.mark.xfail(reason="snuba does not allow - in alias names")
- def test_histogram_negative_values(self):
- # range is [-9, -4), so it is divided into 5 buckets of width 1
- specs = [
- (-9, -8, [("foo", 3)]),
- (-5, -4, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (-9, -8, [(f"{alias}.foo", 3)]),
- (-8, -7, [(f"{alias}.foo", 0)]),
- (-7, -6, [(f"{alias}.foo", 0)]),
- (-6, -5, [(f"{alias}.foo", 0)]),
- (-5, -4, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- @pytest.mark.xfail(reason="snuba does not allow - in alias names")
- def test_histogram_positive_and_negative_values(self):
- # range is [-50, 49], so it is divided into 5 buckets of width 10
- specs = [
- (-50, -50, [("foo", 1)]),
- (-10, 10, [("foo", 2)]),
- (49, 49, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (-50, -30, [(f"{alias}.foo", 1)]),
- (-30, -10, [(f"{alias}.foo", 0)]),
- (-10, 10, [(f"{alias}.foo", 2)]),
- (10, 30, [(f"{alias}.foo", 0)]),
- (30, 50, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_increased_precision(self):
- # range is [1.00, 2.24], so it is divided into 5 buckets of width 0.25
- specs = [
- (1.00, 1.00, [("foo", 3)]),
- (2.24, 2.24, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "precision": 2,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (1.00, 1.25, [(f"{alias}.foo", 3)]),
- (1.25, 1.50, [(f"{alias}.foo", 0)]),
- (1.50, 1.75, [(f"{alias}.foo", 0)]),
- (1.75, 2.00, [(f"{alias}.foo", 0)]),
- (2.00, 2.25, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_increased_precision_with_min_max(self):
- # range is [1.25, 2.24], so it is divided into 5 buckets of width 0.25
- specs = [
- (1.00, 1.25, [("foo", 3)]),
- (2.00, 2.25, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 3,
- "precision": 2,
- "min": 1.25,
- "max": 2.00,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (1.25, 1.50, [(f"{alias}.foo", 0)]),
- (1.50, 1.75, [(f"{alias}.foo", 0)]),
- (1.75, 2.00, [(f"{alias}.foo", 0)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_increased_precision_large_buckets(self):
- # range is [10.0000, 59.9999] so it is divided into 5 buckets of width 10
- specs = [
- (10.0000, 10.0000, [("foo", 1)]),
- (30.0000, 40.0000, [("foo", 1)]),
- (59.9999, 59.9999, [("foo", 2)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "precision": 4,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (10.0000, 20.0000, [(f"{alias}.foo", 1)]),
- (20.0000, 30.0000, [(f"{alias}.foo", 0)]),
- (30.0000, 40.0000, [(f"{alias}.foo", 1)]),
- (40.0000, 50.0000, [(f"{alias}.foo", 0)]),
- (50.0000, 60.0000, [(f"{alias}.foo", 2)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_multiple_measures(self):
- # range is [10, 59] so it is divided into 5 buckets of width 10
- specs = [
- (10, 10, [("bar", 0), ("baz", 0), ("foo", 1)]),
- (30, 40, [("bar", 2), ("baz", 0), ("foo", 0)]),
- (59, 59, [("bar", 0), ("baz", 1), ("foo", 0)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.bar", f"{alias}.baz", f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (
- 10,
- 20,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 1),
- ],
- ),
- (
- 20,
- 30,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 0),
- ],
- ),
- (
- 30,
- 40,
- [
- (f"{alias}.bar", 2),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 0),
- ],
- ),
- (
- 40,
- 50,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 0),
- ],
- ),
- (
- 50,
- 60,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 1),
- (f"{alias}.foo", 0),
- ],
- ),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_max_value_on_edge(self):
- # range is [11, 21] so it is divided into 5 buckets of width 5
- # because using buckets of width 2 will exclude 21, and the next
- # nice number is 5
- specs = [
- (11, 11, [("bar", 0), ("baz", 0), ("foo", 1)]),
- (21, 21, [("bar", 1), ("baz", 1), ("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.bar", f"{alias}.baz", f"{alias}.foo"],
- "numBuckets": 5,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (
- 10,
- 15,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 1),
- ],
- ),
- (
- 15,
- 20,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 0),
- ],
- ),
- (
- 20,
- 25,
- [
- (f"{alias}.bar", 1),
- (f"{alias}.baz", 1),
- (f"{alias}.foo", 1),
- ],
- ),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_bins_exceed_max(self):
- specs = [
- (10, 15, [("bar", 0), ("baz", 0), ("foo", 1)]),
- (30, 30, [("bar", 1), ("baz", 1), ("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.bar", f"{alias}.baz", f"{alias}.foo"],
- "numBuckets": 5,
- "min": 10,
- "max": 21,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (
- 10,
- 15,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 1),
- ],
- ),
- (
- 15,
- 20,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 0),
- ],
- ),
- (
- 20,
- 25,
- [
- (f"{alias}.bar", 0),
- (f"{alias}.baz", 0),
- (f"{alias}.foo", 0),
- ],
- ),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_bad_params_invalid_data_filter(self):
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo", f"{alias}.bar"],
- "numBuckets": 10,
- "dataFilter": "invalid",
- }
- response = self.do_request(query)
- assert response.status_code == 400, f"failing for {array_column}"
- assert response.data == {
- "dataFilter": ['"invalid" is not a valid choice.'],
- }, f"failing for {array_column}"
- def test_histogram_all_data_filter(self):
- specs = [
- (0, 1, [("foo", 4)]),
- (4000, 5000, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "dataFilter": "all",
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 1000, [(f"{alias}.foo", 4)]),
- (1000, 2000, [(f"{alias}.foo", 0)]),
- (2000, 3000, [(f"{alias}.foo", 0)]),
- (3000, 4000, [(f"{alias}.foo", 0)]),
- (4000, 5000, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_exclude_outliers_data_filter(self):
- specs = [
- (0, 0, [("foo", 4)]),
- (4000, 4001, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "dataFilter": "exclude_outliers",
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 1, [(f"{alias}.foo", 4)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_missing_measurement_data(self):
- # make sure there is at least one transaction
- specs = [
- (0, 1, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- # make sure to query a measurement that does not exist
- "field": [f"{alias}.bar"],
- "numBuckets": 5,
- "dataFilter": "exclude_outliers",
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 1, [(f"{alias}.bar", 0)]),
- (1, 1, [(f"{alias}.bar", 0)]),
- (2, 2, [(f"{alias}.bar", 0)]),
- (3, 3, [(f"{alias}.bar", 0)]),
- (4, 4, [(f"{alias}.bar", 0)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_missing_measurement_data_with_explicit_bounds(self):
- # make sure there is at least one transaction
- specs = [
- (0, 1, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- # make sure to query a measurement that does not exist
- "field": [f"{alias}.bar"],
- "numBuckets": 5,
- "dataFilter": "exclude_outliers",
- "min": 10,
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (10, 11, [(f"{alias}.bar", 0)]),
- (11, 11, [(f"{alias}.bar", 0)]),
- (12, 12, [(f"{alias}.bar", 0)]),
- (13, 13, [(f"{alias}.bar", 0)]),
- (14, 14, [(f"{alias}.bar", 0)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_ignores_aggregate_conditions(self):
- # range is [0, 5), so it is divided into 5 buckets of width 1
- specs = [
- (0, 1, [("foo", 1)]),
- (1, 2, [("foo", 1)]),
- (2, 3, [("foo", 1)]),
- (3, 4, [("foo", 0)]),
- (4, 5, [("foo", 1)]),
- ]
- self.populate_events(specs)
- for array_column in ARRAY_COLUMNS:
- alias = get_array_column_alias(array_column)
- query = {
- "project": [self.project.id],
- "field": [f"{alias}.foo"],
- "numBuckets": 5,
- "query": "tpm():>0.001",
- }
- response = self.do_request(query)
- assert response.status_code == 200, f"failing for {array_column}"
- expected = [
- (0, 1, [(f"{alias}.foo", 1)]),
- (1, 2, [(f"{alias}.foo", 1)]),
- (2, 3, [(f"{alias}.foo", 1)]),
- (3, 4, [(f"{alias}.foo", 0)]),
- (4, 5, [(f"{alias}.foo", 1)]),
- ]
- assert response.data == self.as_response_data(expected), f"failing for {array_column}"
- def test_histogram_outlier_filtering_with_no_rows(self):
- query = {
- "project": [self.project.id],
- "field": ["transaction.duration"],
- "numBuckets": 5,
- "dataFilter": "exclude_outliers",
- }
- response = self.do_request(query)
- assert response.status_code == 200
- expected = [
- (0, 1, [("transaction.duration", 0)]),
- ]
- assert response.data == self.as_response_data(expected)
- class OrganizationEventsHistogramEndpointTestWithSnql(OrganizationEventsHistogramEndpointTest):
- def setUp(self):
- super().setUp()
- self.min_ago = iso_format(before_now(minutes=1))
- self.data = load_data("transaction")
- self.features["organizations:performance-use-snql"] = True
|