22
22
Workflow ,
23
23
WorkflowDataConditionGroup ,
24
24
)
25
- from sentry .workflow_engine .models .data_condition import Condition
25
+ from sentry .workflow_engine .models .data_condition import Condition , ConditionType
26
26
from sentry .workflow_engine .types import ActionType , DataSourceType , DetectorPriorityLevel
27
27
28
28
@@ -46,12 +46,17 @@ def create_metric_action(alert_rule_trigger_action: AlertRuleTriggerAction) -> N
46
46
)
47
47
48
48
DataConditionGroupAction .objects .update_or_create (
49
- condition_group_id = alert_rule_trigger_data_condition .data_condition .condition_group .id , # getting dataconditiongroup
49
+ condition_group_id = alert_rule_trigger_data_condition .data_condition .condition_group .id ,
50
50
action_id = action .id ,
51
51
)
52
52
53
53
54
54
def create_metric_data_condition (alert_rule_trigger : AlertRuleTrigger ) -> None :
55
+ alert_rule_detector = AlertRuleDetector .objects .get (alert_rule = alert_rule_trigger .alert_rule )
56
+ data_condition_group = alert_rule_detector .detector .workflow_condition_group
57
+ if not data_condition_group :
58
+ return None
59
+
55
60
condition_result = (
56
61
DetectorPriorityLevel .MEDIUM
57
62
if alert_rule_trigger .label == "warning"
@@ -62,13 +67,12 @@ def create_metric_data_condition(alert_rule_trigger: AlertRuleTrigger) -> None:
62
67
Condition .GREATER if threshold_type == AlertRuleThresholdType .ABOVE else Condition .LESS
63
68
)
64
69
65
- alert_rule_detector = AlertRuleDetector .objects .get (alert_rule = alert_rule_trigger .alert_rule )
66
70
data_condition = DataCondition .objects .create (
67
71
condition = condition ,
68
72
comparison = alert_rule_trigger .alert_threshold ,
69
73
condition_result = condition_result ,
70
- type = "MetricCondition" , # ??
71
- condition_group = alert_rule_detector . detector . workflow_condition_group ,
74
+ type = ConditionType . METRIC_CONDITION ,
75
+ condition_group = data_condition_group ,
72
76
)
73
77
AlertRuleTriggerDataCondition .objects .create (
74
78
alert_rule_trigger = alert_rule_trigger , data_condition = data_condition
@@ -94,7 +98,15 @@ def create_metric_detector_and_workflow(
94
98
alert_rule : AlertRule ,
95
99
user : RpcUser | None = None ,
96
100
) -> None :
97
- query_subscription = QuerySubscription .objects .get (snuba_query = alert_rule .snuba_query .id )
101
+ snuba_query = alert_rule .snuba_query
102
+ if not snuba_query :
103
+ return None
104
+
105
+ project = alert_rule .projects .first ()
106
+ if not project :
107
+ return None
108
+
109
+ query_subscription = QuerySubscription .objects .get (snuba_query = snuba_query .id )
98
110
data_source = DataSource .objects .create (
99
111
organization_id = alert_rule .organization_id ,
100
112
query_id = query_subscription .id ,
@@ -111,10 +123,11 @@ def create_metric_detector_and_workflow(
111
123
enabled = True ,
112
124
created_by_id = user .id if user else None ,
113
125
)
126
+
114
127
detector = Detector .objects .create (
115
- project_id = alert_rule . projects . first () .id ,
128
+ project_id = project .id ,
116
129
enabled = True ,
117
- created_by_id = user .id ,
130
+ created_by_id = user .id if user else None ,
118
131
name = alert_rule .name ,
119
132
workflow_condition_group = data_condition_group ,
120
133
type = MetricAlertFire .slug ,
0 commit comments