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): Add lookup tables for rules #81704

Merged
merged 15 commits into from
Dec 9, 2024
97 changes: 97 additions & 0 deletions fixtures/backup/model_dependencies/detailed.json
Original file line number Diff line number Diff line change
Expand Up @@ -6320,6 +6320,103 @@
"table_name": "workflow_engine_action",
"uniques": []
},
"workflow_engine.alertruledetector": {
"dangling": false,
"foreign_keys": {
"alert_rule": {
"kind": "FlexibleForeignKey",
"model": "sentry.alertrule",
"nullable": true
},
"detector": {
"kind": "FlexibleForeignKey",
"model": "workflow_engine.detector",
"nullable": false
},
"rule": {
"kind": "FlexibleForeignKey",
"model": "sentry.rule",
"nullable": true
}
},
"model": "workflow_engine.alertruledetector",
"relocation_dependencies": [],
"relocation_scope": "Excluded",
"silos": [
"Region"
],
"table_name": "workflow_engine_alertruledetector",
"uniques": [
[
"alert_rule",
"detector"
],
[
"detector",
"rule"
]
]
},
"workflow_engine.alertruletriggerdatacondition": {
"dangling": false,
"foreign_keys": {
"alert_rule_trigger": {
"kind": "FlexibleForeignKey",
"model": "sentry.alertruletrigger",
"nullable": false
},
"data_condition": {
"kind": "FlexibleForeignKey",
"model": "workflow_engine.datacondition",
"nullable": false
}
},
"model": "workflow_engine.alertruletriggerdatacondition",
"relocation_dependencies": [],
"relocation_scope": "Excluded",
"silos": [
"Region"
],
"table_name": "workflow_engine_alertruletriggerdatacondition",
"uniques": []
},
"workflow_engine.alertruleworkflow": {
"dangling": false,
"foreign_keys": {
"alert_rule": {
"kind": "FlexibleForeignKey",
"model": "sentry.alertrule",
"nullable": true
},
"rule": {
"kind": "FlexibleForeignKey",
"model": "sentry.rule",
"nullable": true
},
"workflow": {
"kind": "FlexibleForeignKey",
"model": "workflow_engine.workflow",
"nullable": false
}
},
"model": "workflow_engine.alertruleworkflow",
"relocation_dependencies": [],
"relocation_scope": "Excluded",
"silos": [
"Region"
],
"table_name": "workflow_engine_alertruleworkflow",
"uniques": [
[
"alert_rule",
"workflow"
],
[
"rule",
"workflow"
]
]
},
"workflow_engine.datacondition": {
"dangling": false,
"foreign_keys": {
Expand Down
14 changes: 14 additions & 0 deletions fixtures/backup/model_dependencies/flat.json
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,20 @@
"workflow_engine.action": [
"sentry.integration"
],
"workflow_engine.alertruledetector": [
"sentry.alertrule",
"sentry.rule",
"workflow_engine.detector"
],
"workflow_engine.alertruletriggerdatacondition": [
"sentry.alertruletrigger",
"workflow_engine.datacondition"
],
"workflow_engine.alertruleworkflow": [
"sentry.alertrule",
"sentry.rule",
"workflow_engine.workflow"
],
"workflow_engine.datacondition": [
"workflow_engine.dataconditiongroup"
],
Expand Down
3 changes: 3 additions & 0 deletions fixtures/backup/model_dependencies/sorted.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@
"workflow_engine.workflowdataconditiongroup",
"workflow_engine.detectorworkflow",
"workflow_engine.detectorstate",
"workflow_engine.alertruleworkflow",
"workflow_engine.alertruledetector",
"sentry.teamkeytransaction",
"sentry.snubaqueryeventtype",
"sentry.sentryappinstallation",
Expand All @@ -229,6 +231,7 @@
"sentry.alertruleactivity",
"sentry.alertruleactivations",
"sentry.alertruleactivationcondition",
"workflow_engine.alertruletriggerdatacondition",
"sentry.servicehook",
"sentry.sentryappinstallationtoken",
"sentry.sentryappinstallationforprovider",
Expand Down
3 changes: 3 additions & 0 deletions fixtures/backup/model_dependencies/truncate.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@
"workflow_engine_workflowdataconditiongroup",
"workflow_engine_detectorworkflow",
"workflow_engine_detectorstate",
"workflow_engine_alertruleworkflow",
"workflow_engine_alertruledetector",
"sentry_performanceteamkeytransaction",
"sentry_snubaqueryeventtype",
"sentry_sentryappinstallation",
Expand All @@ -229,6 +231,7 @@
"sentry_alertruleactivity",
"sentry_alertruleactivations",
"sentry_alertruleactivationcondition",
"workflow_engine_alertruletriggerdatacondition",
"sentry_servicehook",
"sentry_sentryappinstallationtoken",
"sentry_sentryappinstallationforprovider",
Expand Down
2 changes: 1 addition & 1 deletion migrations_lockfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ social_auth: 0002_default_auto_field

uptime: 0018_add_trace_sampling_field_to_uptime

workflow_engine: 0014_model_additions_for_milestones
workflow_engine: 0015_create_rule_lookup_tables
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Generated by Django 5.1.1 on 2024-12-05 18:15

import django.db.models.deletion
from django.db import migrations, models

import sentry.db.models.fields.bounded
import sentry.db.models.fields.foreignkey
from sentry.new_migrations.migrations import CheckedMigration


class Migration(CheckedMigration):
# This flag is used to mark that a migration shouldn't be automatically run in production.
# This should only be used for operations where it's safe to run the migration after your
# code has deployed. So this should not be used for most operations that alter the schema
# of a table.
# Here are some things that make sense to mark as post deployment:
# - Large data migrations. Typically we want these to be run manually so that they can be
# monitored and not block the deploy for a long period of time while they run.
# - Adding indexes to large tables. Since this can take a long time, we'd generally prefer to
# run this outside deployments so that we don't block them. Note that while adding an index
# is a schema change, it's completely safe to run the operation after the code has deployed.
# Once deployed, run these manually via: https://develop.sentry.dev/database-migrations/#migration-deployment

is_post_deployment = False

dependencies = [
("sentry", "0802_remove_grouping_auto_update_option"),
("workflow_engine", "0014_model_additions_for_milestones"),
]

operations = [
migrations.CreateModel(
name="AlertRuleTriggerDataCondition",
fields=[
(
"id",
sentry.db.models.fields.bounded.BoundedBigAutoField(
primary_key=True, serialize=False
),
),
("date_updated", models.DateTimeField(auto_now=True)),
("date_added", models.DateTimeField(auto_now_add=True)),
(
"alert_rule_trigger",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="sentry.alertruletrigger"
),
),
(
"data_condition",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="workflow_engine.datacondition",
),
),
],
options={
"abstract": False,
},
),
migrations.CreateModel(
name="AlertRuleDetector",
fields=[
(
"id",
sentry.db.models.fields.bounded.BoundedBigAutoField(
primary_key=True, serialize=False
),
),
("date_updated", models.DateTimeField(auto_now=True)),
("date_added", models.DateTimeField(auto_now_add=True)),
(
"alert_rule",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="sentry.alertrule",
),
),
(
"detector",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="workflow_engine.detector"
),
),
(
"rule",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
null=True, on_delete=django.db.models.deletion.CASCADE, to="sentry.rule"
),
),
],
options={
"db_table": "workflow_engine_alertruledetector",
"constraints": [
models.CheckConstraint(
condition=models.Q(
models.Q(("alert_rule__isnull", True), ("rule__isnull", False)),
models.Q(("alert_rule__isnull", False), ("rule__isnull", True)),
_connector="OR",
),
ceorourke marked this conversation as resolved.
Show resolved Hide resolved
name="rule_or_alert_rule_detector",
)
],
"unique_together": {("detector", "alert_rule"), ("detector", "rule")},
},
),
migrations.CreateModel(
name="AlertRuleWorkflow",
fields=[
(
"id",
sentry.db.models.fields.bounded.BoundedBigAutoField(
primary_key=True, serialize=False
),
),
("date_updated", models.DateTimeField(auto_now=True)),
("date_added", models.DateTimeField(auto_now_add=True)),
(
"alert_rule",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="sentry.alertrule",
),
),
(
"rule",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
null=True, on_delete=django.db.models.deletion.CASCADE, to="sentry.rule"
),
),
(
"workflow",
sentry.db.models.fields.foreignkey.FlexibleForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="workflow_engine.workflow"
),
),
],
options={
"db_table": "workflow_engine_alertruleworkflow",
"constraints": [
models.CheckConstraint(
condition=models.Q(
models.Q(("alert_rule__isnull", True), ("rule__isnull", False)),
models.Q(("alert_rule__isnull", False), ("rule__isnull", True)),
_connector="OR",
),
ceorourke marked this conversation as resolved.
Show resolved Hide resolved
name="rule_or_alert_rule_workflow",
)
],
"unique_together": {("workflow", "alert_rule"), ("workflow", "rule")},
},
),
]
6 changes: 6 additions & 0 deletions src/sentry/workflow_engine/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
__all__ = [
"Action",
"AlertRuleDetector",
"AlertRuleTriggerDataCondition",
"AlertRuleWorkflow",
"DataCondition",
"DataConditionGroup",
"DataConditionGroupAction",
Expand All @@ -14,6 +17,9 @@
]

from .action import Action
from .alertrule_detector import AlertRuleDetector
from .alertrule_workflow import AlertRuleWorkflow
from .alertruletrigger_data_condition import AlertRuleTriggerDataCondition
from .data_condition import DataCondition
from .data_condition_group import DataConditionGroup
from .data_condition_group_action import DataConditionGroupAction
Expand Down
32 changes: 32 additions & 0 deletions src/sentry/workflow_engine/models/alertrule_detector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.db.models import CheckConstraint, Q

from sentry.backup.scopes import RelocationScope
from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model


@region_silo_model
class AlertRuleDetector(DefaultFieldsModel):
"""
A lookup model for rules and detectors.
"""

__relocation_scope__ = RelocationScope.Excluded
ceorourke marked this conversation as resolved.
Show resolved Hide resolved

alert_rule = FlexibleForeignKey("sentry.AlertRule", null=True)
rule = FlexibleForeignKey("sentry.Rule", null=True)
detector = FlexibleForeignKey("workflow_engine.Detector")

class Meta:
db_table = "workflow_engine_alertruledetector"
app_label = "workflow_engine"
unique_together = (
("detector", "rule"),
("detector", "alert_rule"),
)
constraints = [
CheckConstraint(
condition=Q(rule__isnull=False, alert_rule__isnull=True)
| Q(rule__isnull=True, alert_rule__isnull=False),
name="rule_or_alert_rule_detector",
),
]
Loading
Loading