Skip to content

fix(anomaly detection): get aggregation key from snuba data #77498

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/sentry/seer/anomaly_detection/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,21 @@ def format_historical_data(data: SnubaTSResult, dataset: Any) -> list[TimeSeries
ts_point = TimeSeriesPoint(timestamp=date.timestamp(), value=count)
formatted_data.append(ts_point)
else:
# we don't know what the aggregation key of the query is
# so we should see it when we see a data point that has a value
agg_key = ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks good, but can we add some tests?

for datum in nested_data:
ts_point = TimeSeriesPoint(timestamp=datum.get("time"), value=datum.get("count", 0))
if len(datum) == 1:
# this data point has no value
ts_point = TimeSeriesPoint(timestamp=datum.get("time"), value=0)
else:
# if we don't know the aggregation key yet, we should set it
if not agg_key:
for key in datum: # only two keys in this dict
if key != "time":
agg_key = key
break
ts_point = TimeSeriesPoint(timestamp=datum.get("time"), value=datum.get(agg_key, 0))
formatted_data.append(ts_point)
return formatted_data

Expand Down
16 changes: 16 additions & 0 deletions tests/sentry/seer/anomaly_detection/test_store_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ def test_anomaly_detection_format_historical_data(self):
result = format_historical_data(data, errors)
assert result == expected_return_value

def test_anomaly_detection_format_historical_data_two(self):
"""
Test a different aggregation key.
"""
expected_return_value = [
{"timestamp": self.time_1_ts, "value": 0},
{"timestamp": self.time_2_ts, "value": 1},
]
snuba_raw_data = [
{"time": self.time_1_ts},
{"count_unique_tags_sentry_user": 1, "time": self.time_2_ts},
]
data = SnubaTSResult({"data": snuba_raw_data}, self.time_1_ts, self.time_2_ts, 3600)
result = format_historical_data(data, errors)
assert result == expected_return_value

def test_anomaly_detection_fetch_historical_data(self):
alert_rule = self.create_alert_rule(organization=self.organization, projects=[self.project])
snuba_query = SnubaQuery.objects.get(id=alert_rule.snuba_query_id)
Expand Down
Loading