|
| 1 | +from sentry.incidents.grouptype import MetricAlertFire |
| 2 | +from sentry.incidents.models.alert_rule import AlertRule |
| 3 | +from sentry.snuba.models import QuerySubscription, SnubaQuery |
| 4 | +from sentry.users.services.user import RpcUser |
| 5 | +from sentry.workflow_engine.models import ( |
| 6 | + AlertRuleDetector, |
| 7 | + AlertRuleWorkflow, |
| 8 | + DataConditionGroup, |
| 9 | + DataSource, |
| 10 | + Detector, |
| 11 | + DetectorState, |
| 12 | + DetectorWorkflow, |
| 13 | + Workflow, |
| 14 | + WorkflowDataConditionGroup, |
| 15 | +) |
| 16 | +from sentry.workflow_engine.types import DetectorPriorityLevel |
| 17 | + |
| 18 | + |
| 19 | +def create_metric_alert_lookup_tables( |
| 20 | + alert_rule: AlertRule, |
| 21 | + detector: Detector, |
| 22 | + workflow: Workflow, |
| 23 | + data_source: DataSource, |
| 24 | + data_condition_group: DataConditionGroup, |
| 25 | +) -> tuple[AlertRuleDetector, AlertRuleWorkflow, DetectorWorkflow, WorkflowDataConditionGroup]: |
| 26 | + alert_rule_detector = AlertRuleDetector.objects.create(alert_rule=alert_rule, detector=detector) |
| 27 | + alert_rule_workflow = AlertRuleWorkflow.objects.create(alert_rule=alert_rule, workflow=workflow) |
| 28 | + detector_workflow = DetectorWorkflow.objects.create(detector=detector, workflow=workflow) |
| 29 | + workflow_data_condition_group = WorkflowDataConditionGroup.objects.create( |
| 30 | + condition_group=data_condition_group, workflow=workflow |
| 31 | + ) |
| 32 | + return ( |
| 33 | + alert_rule_detector, |
| 34 | + alert_rule_workflow, |
| 35 | + detector_workflow, |
| 36 | + workflow_data_condition_group, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def create_data_source( |
| 41 | + organization_id: int, snuba_query: SnubaQuery | None = None |
| 42 | +) -> DataSource | None: |
| 43 | + if not snuba_query: |
| 44 | + return None |
| 45 | + |
| 46 | + try: |
| 47 | + query_subscription = QuerySubscription.objects.get(snuba_query=snuba_query.id) |
| 48 | + except QuerySubscription.DoesNotExist: |
| 49 | + return None |
| 50 | + |
| 51 | + return DataSource.objects.create( |
| 52 | + organization_id=organization_id, |
| 53 | + query_id=query_subscription.id, |
| 54 | + type="snuba_query_subscription", |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def create_data_condition_group(organization_id: int) -> DataConditionGroup: |
| 59 | + return DataConditionGroup.objects.create( |
| 60 | + logic_type=DataConditionGroup.Type.ANY, |
| 61 | + organization_id=organization_id, |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +def create_workflow( |
| 66 | + name: str, |
| 67 | + organization_id: int, |
| 68 | + data_condition_group: DataConditionGroup, |
| 69 | + user: RpcUser | None = None, |
| 70 | +) -> Workflow: |
| 71 | + return Workflow.objects.create( |
| 72 | + name=name, |
| 73 | + organization_id=organization_id, |
| 74 | + when_condition_group=data_condition_group, |
| 75 | + enabled=True, |
| 76 | + created_by_id=user.id if user else None, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def create_detector( |
| 81 | + alert_rule: AlertRule, |
| 82 | + project_id: int, |
| 83 | + data_condition_group: DataConditionGroup, |
| 84 | + user: RpcUser | None = None, |
| 85 | +) -> Detector: |
| 86 | + return Detector.objects.create( |
| 87 | + project_id=project_id, |
| 88 | + enabled=True, |
| 89 | + created_by_id=user.id if user else None, |
| 90 | + name=alert_rule.name, |
| 91 | + workflow_condition_group=data_condition_group, |
| 92 | + type=MetricAlertFire.slug, |
| 93 | + description=alert_rule.description, |
| 94 | + owner_user_id=alert_rule.user_id, |
| 95 | + owner_team=alert_rule.team, |
| 96 | + config={ # TODO create a schema |
| 97 | + "threshold_period": alert_rule.threshold_period, |
| 98 | + "sensitivity": alert_rule.sensitivity, |
| 99 | + "seasonality": alert_rule.seasonality, |
| 100 | + "comparison_delta": alert_rule.comparison_delta, |
| 101 | + }, |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def migrate_alert_rule( |
| 106 | + alert_rule: AlertRule, |
| 107 | + user: RpcUser | None = None, |
| 108 | +) -> ( |
| 109 | + tuple[ |
| 110 | + DataSource, |
| 111 | + DataConditionGroup, |
| 112 | + Workflow, |
| 113 | + Detector, |
| 114 | + DetectorState, |
| 115 | + AlertRuleDetector, |
| 116 | + AlertRuleWorkflow, |
| 117 | + DetectorWorkflow, |
| 118 | + WorkflowDataConditionGroup, |
| 119 | + ] |
| 120 | + | None |
| 121 | +): |
| 122 | + organization_id = alert_rule.organization_id |
| 123 | + project = alert_rule.projects.first() |
| 124 | + if not project: |
| 125 | + return None |
| 126 | + |
| 127 | + data_source = create_data_source(organization_id, alert_rule.snuba_query) |
| 128 | + if not data_source: |
| 129 | + return None |
| 130 | + |
| 131 | + data_condition_group = create_data_condition_group(organization_id) |
| 132 | + workflow = create_workflow(alert_rule.name, organization_id, data_condition_group, user) |
| 133 | + detector = create_detector(alert_rule, project.id, data_condition_group, user) |
| 134 | + |
| 135 | + data_source.detectors.set([detector]) |
| 136 | + detector_state = DetectorState.objects.create( |
| 137 | + detector=detector, |
| 138 | + active=False, |
| 139 | + state=DetectorPriorityLevel.OK, |
| 140 | + ) |
| 141 | + alert_rule_detector, alert_rule_workflow, detector_workflow, workflow_data_condition_group = ( |
| 142 | + create_metric_alert_lookup_tables( |
| 143 | + alert_rule, detector, workflow, data_source, data_condition_group |
| 144 | + ) |
| 145 | + ) |
| 146 | + return ( |
| 147 | + data_source, |
| 148 | + data_condition_group, |
| 149 | + workflow, |
| 150 | + detector, |
| 151 | + detector_state, |
| 152 | + alert_rule_detector, |
| 153 | + alert_rule_workflow, |
| 154 | + detector_workflow, |
| 155 | + workflow_data_condition_group, |
| 156 | + ) |
0 commit comments