diff --git a/snuba/web/rpc/v1/resolvers/R_eap_spans/common/aggregation.py b/snuba/web/rpc/v1/resolvers/R_eap_spans/common/aggregation.py index 9541452423..67d530f725 100644 --- a/snuba/web/rpc/v1/resolvers/R_eap_spans/common/aggregation.py +++ b/snuba/web/rpc/v1/resolvers/R_eap_spans/common/aggregation.py @@ -16,7 +16,7 @@ from snuba.query.dsl import CurriedFunctions as cf from snuba.query.dsl import Functions as f -from snuba.query.dsl import column, literal +from snuba.query.dsl import column from snuba.query.expressions import ( CurriedFunctionCall, Expression, @@ -548,7 +548,9 @@ def get_confidence_interval_column( get_field_existence_expression(aggregation), alias=f"{alias}_N", ), - f.minus(column(f"{alias}_N"), literal(1)), + f.sumIf( + sign_column, get_field_existence_expression(aggregation) + ), ), ) ), diff --git a/tests/web/rpc/v1/test_endpoint_time_series/test_endpoint_time_series_extrapolation.py b/tests/web/rpc/v1/test_endpoint_time_series/test_endpoint_time_series_extrapolation.py index b338a6780a..c71ce8c52e 100644 --- a/tests/web/rpc/v1/test_endpoint_time_series/test_endpoint_time_series_extrapolation.py +++ b/tests/web/rpc/v1/test_endpoint_time_series/test_endpoint_time_series_extrapolation.py @@ -474,14 +474,14 @@ def test_sum_unreliable(self) -> None: def test_avg_unreliable(self) -> None: # store a test metric with a value of 1, every second for an hour - granularity_secs = 60 + granularity_secs = 120 query_duration = 3600 store_timeseries( BASE_TIME, 1, 3600, # for each time interval we distribute the values from -55 to 64 to keep the avg close to 0 - metrics=[DummyMetric("test_metric", get_value=lambda x: (x % 60) - 55)], + metrics=[DummyMetric("test_metric", get_value=lambda x: (x % 120) - 55)], measurements=[ DummyMeasurement( "client_sample_rate", @@ -522,7 +522,7 @@ def test_avg_unreliable(self) -> None: buckets=expected_buckets, data_points=[ DataPoint( - data=-25.5, # (-55 + -54 + ... + -4) / 60 = -25.5 + data=4.5, # (-55 + -54 + ... + 64) / 60 = 4.5 data_present=True, reliability=Reliability.RELIABILITY_LOW, avg_sampling_rate=0.0001,