1
- # Generated by Django 5.1.1 on 2024-11-08 23:16
1
+ # Generated by Django 5.1.1 on 2024-11-25 20:59
2
2
3
3
from enum import Enum , IntEnum
4
4
@@ -54,34 +54,57 @@ class AlertRuleStatus(Enum):
54
54
NOT_ENOUGH_DATA = 6
55
55
56
56
57
+ class AlertRuleActivityType (Enum ):
58
+ CREATED = 1
59
+ DELETED = 2
60
+ UPDATED = 3
61
+ ENABLED = 4
62
+ DISABLED = 5
63
+ SNAPSHOT = 6
64
+ ACTIVATED = 7
65
+ DEACTIVATED = 8
66
+
67
+
57
68
def migrate_metric_alerts (apps : Apps , schema_editor : BaseDatabaseSchemaEditor ) -> None :
58
69
AlertRule = apps .get_model ("sentry" , "AlertRule" )
59
70
AlertRuleProjects = apps .get_model ("sentry" , "AlertRuleProjects" )
60
71
AlertRuleTrigger = apps .get_model ("sentry" , "AlertRuleTrigger" )
61
72
AlertRuleTriggerAction = apps .get_model ("sentry" , "AlertRuleTriggerAction" )
62
- # AlertRuleActivity = apps.get_model("sentry", "AlertRuleActivity")
73
+ AlertRuleActivity = apps .get_model ("sentry" , "AlertRuleActivity" )
74
+ RuleSnooze = apps .get_model ("sentry" , "RuleSnooze" )
63
75
QuerySubscription = apps .get_model ("sentry" , "QuerySubscription" )
64
76
Incident = apps .get_model ("sentry" , "Incident" )
65
77
66
- DataSource = apps .get_model ("sentry " , "DataSource" )
67
- DataConditionGroup = apps .get_model ("sentry " , "DataConditionGroup" )
68
- Workflow = apps .get_model ("sentry " , "Workflow" )
69
- Detector = apps .get_model ("sentry " , "Detector" )
70
- DetectorState = apps .get_model ("sentry " , "DetectorState" )
71
- DataCondition = apps .get_model ("sentry " , "DataCondition" )
72
- Action = apps .get_model ("sentry " , "Action" )
73
- DataConditionGroupAction = apps .get_model ("sentry " , "DataConditionGroupAction" )
74
- DataSourceDetector = apps .get_model ("sentry " , "DataSourceDetector" )
75
- DetectorWorkflow = apps .get_model ("sentry " , "DetectorWorkflow" )
76
- WorkflowDataConditionGroup = apps .get_model ("sentry " , "WorkflowDataConditionGroup" )
77
-
78
- for rule in RangeQuerySetWrapperWithProgressBarApprox (AlertRule .objects .all ()):
78
+ DataSource = apps .get_model ("workflow_engine " , "DataSource" )
79
+ DataConditionGroup = apps .get_model ("workflow_engine " , "DataConditionGroup" )
80
+ Workflow = apps .get_model ("workflow_engine " , "Workflow" )
81
+ Detector = apps .get_model ("workflow_engine " , "Detector" )
82
+ DetectorState = apps .get_model ("workflow_engine " , "DetectorState" )
83
+ DataCondition = apps .get_model ("workflow_engine " , "DataCondition" )
84
+ Action = apps .get_model ("workflow_engine " , "Action" )
85
+ DataConditionGroupAction = apps .get_model ("workflow_engine " , "DataConditionGroupAction" )
86
+ DataSourceDetector = apps .get_model ("workflow_engine " , "DataSourceDetector" )
87
+ DetectorWorkflow = apps .get_model ("workflow_engine " , "DetectorWorkflow" )
88
+ WorkflowDataConditionGroup = apps .get_model ("workflow_engine " , "WorkflowDataConditionGroup" )
89
+
90
+ for rule in RangeQuerySetWrapperWithProgressBarApprox (AlertRule .objects_with_snapshots .all ()):
79
91
if rule .status in [AlertRuleStatus .DISABLED , AlertRuleStatus .SNAPSHOT ]:
80
92
continue
81
- # what about the date_added and date_updated fields? can I write to those to preserve history?
82
- # description column will be added to the Detector model, but at the time of writing it isn't there yet. will need to update this
83
- # same for threshold_period, will likely be added to the Detector model but isn't there yet
84
- # AlertRuleActivity creation rows need to be backfilled into the Detector model, needs new created_by and created_at columns
93
+ # TODO: need to modify the DefaultFieldsModel auto_now and auto_now_add
94
+ # so we can write to the date_added and date_updated fields
95
+
96
+ snoozed = None
97
+ try :
98
+ snoozed = RuleSnooze .objects .get (alert_rule_id = rule .id , user_id = None )
99
+ except RuleSnooze .DoesNotExist :
100
+ pass
101
+
102
+ enabled = True if snoozed is not None else False
103
+
104
+ create_activity = AlertRuleActivity .objects .get (
105
+ alert_rule_id = rule .id , type = AlertRuleActivityType .CREATED .value
106
+ )
107
+ alert_rule_project = AlertRuleProjects .objects .get (alert_rule_id = rule .id )
85
108
86
109
query_subscription = QuerySubscription .objects .get (snuba_query = rule .snuba_query_id )
87
110
data_source = DataSource .update_or_create (
@@ -97,15 +120,21 @@ def migrate_metric_alerts(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -
97
120
name = rule .name ,
98
121
organization_id = rule .organization_id ,
99
122
when_condition_group = data_condition_group .id ,
123
+ enabled = enabled ,
124
+ created_by_id = create_activity .user_id ,
100
125
)
101
126
detector = Detector .update_or_create (
102
- organization_id = rule .organization_id ,
127
+ project_id = alert_rule_project .project_id ,
128
+ enabled = enabled ,
129
+ created_by_id = create_activity .user_id ,
103
130
name = rule .name ,
104
131
data_sources = data_source ,
105
132
workflow_condition_group = data_condition_group .id ,
106
133
type = MetricAlertFire .slug ,
134
+ description = rule .description ,
107
135
owner_user_id = rule .user_id ,
108
136
owner_team = rule .team ,
137
+ config = {"threshold_type" : rule .threshold_type }, # schema to come
109
138
)
110
139
111
140
# state is based on incident status
@@ -194,7 +223,7 @@ class Migration(CheckedMigration):
194
223
is_post_deployment = True
195
224
196
225
dependencies = [
197
- ("workflow_engine" , "0011_action_updates " ),
226
+ ("workflow_engine" , "0014_model_additions_for_milestones " ),
198
227
]
199
228
200
229
operations = [
0 commit comments