4141)
4242
4343
44- def update_detector_data (
45- detector : Detector ,
46- updated_fields : dict [str , Any ],
47- ) -> None :
48- try :
49- data_source = DataSourceDetector .objects .get (detector_id = detector .id ).data_source
50- except DataSourceDetector .DoesNotExist :
51- raise Exception ("Could not update detector, data source detector not found." )
44+ def _fetch_related_models (
45+ detector : Detector , method : str
46+ ) -> tuple [DataSource , DataCondition , SnubaQuery ] | None :
47+ # XXX: it is technically possible (though not used today) that a detector could have multiple data sources
48+ data_source_detector = DataSourceDetector .objects .filter (detector_id = detector .id ).first ()
49+ if not data_source_detector :
50+ raise DetectorException (f"Could not { method } detector, data source not found." )
51+ data_source = data_source_detector .data_source
52+
5253 try :
53- query_subscription = QuerySubscription .objects .get (id = data_source .source_id )
54+ query_subscription = QuerySubscription .objects .get (id = int ( data_source .source_id ) )
5455 except QuerySubscription .DoesNotExist :
55- raise Exception ("Could not update detector, query subscription not found." )
56+ raise DetectorException (
57+ f"Could not { method } detector, query subscription { data_source .source_id } not found."
58+ )
5659 try :
5760 snuba_query = SnubaQuery .objects .get (id = query_subscription .snuba_query_id )
5861 except SnubaQuery .DoesNotExist :
59- raise Exception ("Could not update detector, snuba query not found." )
62+ raise DetectorException (
63+ f"Could not { method } detector, snuba query { query_subscription .snuba_query_id } not found."
64+ )
6065 try :
6166 data_condition = DataCondition .objects .get (
6267 condition_group = detector .workflow_condition_group
@@ -69,8 +74,16 @@ def update_detector_data(
6974 else None
7075 )
7176 raise DetectorException (
72- f"Could not create detector, data condition { dcg_id } not found or too many found."
77+ f"Could not { method } detector, data condition { dcg_id } not found or too many found."
7378 )
79+ return data_source , data_condition , snuba_query
80+
81+
82+ def update_detector_data (
83+ detector : Detector ,
84+ updated_fields : dict [str , Any ],
85+ ) -> None :
86+ data_source , data_condition , snuba_query = _fetch_related_models (detector , "update" )
7487 # use setattr to avoid saving the models until the Seer call has successfully finished,
7588 # otherwise they would be in a bad state
7689 updated_data_condition_data = updated_fields .get ("condition_group" , {}).get ("conditions" )
@@ -113,38 +126,7 @@ def send_new_detector_data(detector: Detector) -> None:
113126 """
114127 Send historical data for a new Detector to Seer.
115128 """
116- # XXX: it is technically possible (though not used today) that a detector could have multiple data sources
117- data_source_detector = DataSourceDetector .objects .filter (detector_id = detector .id ).first ()
118- if not data_source_detector :
119- raise DetectorException ("Could not create detector, data source not found." )
120- data_source = data_source_detector .data_source
121-
122- try :
123- query_subscription = QuerySubscription .objects .get (id = int (data_source .source_id ))
124- except QuerySubscription .DoesNotExist :
125- raise DetectorException (
126- f"Could not create detector, query subscription { data_source .source_id } not found."
127- )
128- try :
129- snuba_query = SnubaQuery .objects .get (id = query_subscription .snuba_query_id )
130- except SnubaQuery .DoesNotExist :
131- raise DetectorException (
132- f"Could not create detector, snuba query { query_subscription .snuba_query_id } not found."
133- )
134- try :
135- data_condition = DataCondition .objects .get (
136- condition_group = detector .workflow_condition_group
137- )
138- except (DataCondition .DoesNotExist , DataCondition .MultipleObjectsReturned ):
139- # there should only ever be one data condition for a dynamic metric detector, we dont actually expect a MultipleObjectsReturned
140- dcg_id = (
141- detector .workflow_condition_group .id
142- if detector .workflow_condition_group is not None
143- else None
144- )
145- raise DetectorException (
146- f"Could not create detector, data condition { dcg_id } not found or too many found."
147- )
129+ data_source , data_condition , snuba_query = _fetch_related_models (detector , "create" )
148130 try :
149131 handle_send_historical_data_to_seer (
150132 detector , data_source , data_condition , snuba_query , detector .project , SeerMethod .CREATE
0 commit comments