Skip to content
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

feat(ACI): Detector details PUT endpoint #84098

Merged
merged 30 commits into from
Feb 21, 2025
Merged

Conversation

ceorourke
Copy link
Member

Create a PUT endpoint for detector details.

Addresses https://getsentry.atlassian.net/browse/ACI-114

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 27, 2025
Copy link

codecov bot commented Jan 27, 2025

Codecov Report

Attention: Patch coverage is 93.86503% with 10 lines in your changes missing coverage. Please review.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...gine/endpoints/validators/metric_alert_detector.py 85.29% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #84098      +/-   ##
==========================================
+ Coverage   87.97%   87.99%   +0.01%     
==========================================
  Files        9631     9632       +1     
  Lines      543870   545756    +1886     
  Branches    21089    21089              
==========================================
+ Hits       478492   480241    +1749     
- Misses      65075    65212     +137     
  Partials      303      303              

def update_data_source(self, instance, data_source):
try:
source_instance = DataSource.objects.get(detector=instance)
except DataSource.DoesNotExist:
Copy link
Member

Choose a reason for hiding this comment

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

does the data source need to be created if you add it to an existing detector?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if that scenario would be possible in the UI, I believe you're always creating both a data source and a detector at the same time

def update(self, instance, validated_data):
instance.name = validated_data.get("name", instance.name)
instance.type = validated_data.get("detector_type", instance.group_type).slug
condition_group = validated_data.pop("condition_group")
Copy link
Member

Choose a reason for hiding this comment

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

nit: is there a reason this is pop instead of get?

Copy link
Member Author

Choose a reason for hiding this comment

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

🤔 I wanna say I had a reason at one point but I don't think it applies anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

oh wait the reason is because when I call instance.save() the instance here is a Detector and if I have the dict of condition group data that's bad cause it's not data that gets saved to that model

Copy link
Contributor

@saponifi3d saponifi3d Feb 19, 2025

Choose a reason for hiding this comment

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

🤔 should this be the Detector.workflow_condition_group or is this just because of the API -> models mapping?

Copy link
Member Author

Choose a reason for hiding this comment

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

What gets passed here are the data conditions, the condition group doesn't change

Copy link
Contributor

@saponifi3d saponifi3d left a comment

Choose a reason for hiding this comment

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

taking a step back, is it an anti-pattern to be creating new models in the validator?

just by the naming i would expect a validator to only ensure data exists as expected, not create new models. i'm not super familiar with this layer, so it's def more of a smell than anything for me.

404: RESPONSE_NOT_FOUND,
},
)
def put(self, request: Request, project: Project, detector: Detector) -> Response:
Copy link
Contributor

Choose a reason for hiding this comment

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

i really like how clean these api handlers are so clean and well documented 💯

@@ -1,12 +1,17 @@
from datetime import timedelta
Copy link
Contributor

Choose a reason for hiding this comment

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

once this pr is merged, mind if i move this validator to the incidents directory to colocate it with the detector it's validating?

Comment on lines 94 to 101
query_type=data_source.get("query_type", snuba_query.type),
dataset=data_source.get("dataset", snuba_query.dataset),
query=data_source.get("query", snuba_query.query),
aggregate=data_source.get("aggregate", snuba_query.aggregate),
time_window=timedelta(minutes=data_source.get("time_window", snuba_query.time_window)),
resolution=timedelta(seconds=data_source.get("resolution", snuba_query.resolution)),
environment=data_source.get("environment", snuba_query.environment),
event_types=data_source.get("event_types", [event_types]),
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 the data_source won't have these attributes, it's just the lookup like you have on line 87.

is this like new_snuba_query rather than data_source?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I based this off of https://github.com/getsentry/sentry/blob/master/tests/sentry/workflow_engine/endpoints/test_project_detector_index.py#L62-L70 and https://www.notion.so/sentry/UI-APIs-1888b10e4b5d80dfa47fcbffa2a7c9af?pvs=4#1928b10e4b5d801da849e25bc24915f3 - we'd need all of this information if the snuba query is changing. Do you feel strongly about renaming it? I'll have to check with Nate to make sure he's aware that it'll be changing.

def update(self, instance, validated_data):
instance.name = validated_data.get("name", instance.name)
instance.type = validated_data.get("detector_type", instance.group_type).slug
condition_group = validated_data.pop("condition_group")
Copy link
Contributor

@saponifi3d saponifi3d Feb 19, 2025

Choose a reason for hiding this comment

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

🤔 should this be the Detector.workflow_condition_group or is this just because of the API -> models mapping?

Copy link
Contributor

@saponifi3d saponifi3d left a comment

Choose a reason for hiding this comment

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

lgtm - we can update the nits in upcoming work.

Comment on lines +19 to +24
class DataConditionType(TypedDict):
id: int | None
comparison: int
type: Condition
condition_result: DetectorPriorityLevel
condition_group_id: int
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔 should this be in workflow_engine/types.py? (we can update later)

condition_group_id: int


class DataSourceType(TypedDict):
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this seems to be specific to snuba query's as the data source:

Suggested change
class DataSourceType(TypedDict):
class SnubaQueryDataSourceType(TypedDict):

@ceorourke ceorourke merged commit 4a76908 into master Feb 21, 2025
49 checks passed
@ceorourke ceorourke deleted the ceorourke/detector-details-put branch February 21, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants