Skip to content

Commit 2de49ac

Browse files
authored
fix(anomaly detection): get aggregation key from snuba data (#77498)
We can't assume that the aggregation key of a snuba timeseries data point will be `"count"`, so we should set the aggregation key when we see a data point with an aggregation value.
1 parent 730f6d5 commit 2de49ac

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/sentry/seer/anomaly_detection/utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,21 @@ def format_historical_data(data: SnubaTSResult, dataset: Any) -> list[TimeSeries
102102
ts_point = TimeSeriesPoint(timestamp=date.timestamp(), value=count)
103103
formatted_data.append(ts_point)
104104
else:
105+
# we don't know what the aggregation key of the query is
106+
# so we should see it when we see a data point that has a value
107+
agg_key = ""
105108
for datum in nested_data:
106-
ts_point = TimeSeriesPoint(timestamp=datum.get("time"), value=datum.get("count", 0))
109+
if len(datum) == 1:
110+
# this data point has no value
111+
ts_point = TimeSeriesPoint(timestamp=datum.get("time"), value=0)
112+
else:
113+
# if we don't know the aggregation key yet, we should set it
114+
if not agg_key:
115+
for key in datum: # only two keys in this dict
116+
if key != "time":
117+
agg_key = key
118+
break
119+
ts_point = TimeSeriesPoint(timestamp=datum.get("time"), value=datum.get(agg_key, 0))
107120
formatted_data.append(ts_point)
108121
return formatted_data
109122

tests/sentry/seer/anomaly_detection/test_store_data.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ def test_anomaly_detection_format_historical_data(self):
5858
result = format_historical_data(data, errors)
5959
assert result == expected_return_value
6060

61+
def test_anomaly_detection_format_historical_data_two(self):
62+
"""
63+
Test a different aggregation key.
64+
"""
65+
expected_return_value = [
66+
{"timestamp": self.time_1_ts, "value": 0},
67+
{"timestamp": self.time_2_ts, "value": 1},
68+
]
69+
snuba_raw_data = [
70+
{"time": self.time_1_ts},
71+
{"count_unique_tags_sentry_user": 1, "time": self.time_2_ts},
72+
]
73+
data = SnubaTSResult({"data": snuba_raw_data}, self.time_1_ts, self.time_2_ts, 3600)
74+
result = format_historical_data(data, errors)
75+
assert result == expected_return_value
76+
6177
def test_anomaly_detection_fetch_historical_data(self):
6278
alert_rule = self.create_alert_rule(organization=self.organization, projects=[self.project])
6379
snuba_query = SnubaQuery.objects.get(id=alert_rule.snuba_query_id)

0 commit comments

Comments
 (0)