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": true,
"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": "Organization",
"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": "Organization",
"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": "Organization",
"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
9 changes: 9 additions & 0 deletions src/sentry/backup/comparators.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,15 @@ def get_default_comparators() -> dict[str, list[JSONScrubbingComparator]]:
"workflow_engine.workflowdataconditiongroup": [
DateUpdatedComparator("date_updated", "date_added")
],
"workflow_engine.alertruledetector": [
DateUpdatedComparator("date_updated", "date_added")
],
"workflow_engine.alertruleworkflow": [
DateUpdatedComparator("date_updated", "date_added")
],
"workflow_engine.alertruletriggerdatacondition": [
DateUpdatedComparator("date_updated", "date_added")
],
},
)

Expand Down
16 changes: 14 additions & 2 deletions src/sentry/testutils/helpers/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@
from sentry.users.models.userip import UserIP
from sentry.users.models.userrole import UserRole, UserRoleUser
from sentry.utils import json
from sentry.workflow_engine.models import Action, DataConditionGroup
from sentry.workflow_engine.models import (
Action,
AlertRuleDetector,
AlertRuleTriggerDataCondition,
AlertRuleWorkflow,
DataConditionGroup,
)

__all__ = [
"export_to_file",
Expand Down Expand Up @@ -668,7 +674,7 @@ def create_exhaustive_organization(
)

# TODO @saponifi3d: Update comparison to be DetectorState.Critical
self.create_data_condition(
data_condition = self.create_data_condition(
condition="eq",
comparison="critical",
type="WorkflowCondition",
Expand Down Expand Up @@ -702,6 +708,12 @@ def create_exhaustive_organization(
)
detector.workflow_condition_group = detector_conditions

AlertRuleDetector.objects.create(detector=detector, alert_rule=alert)
AlertRuleWorkflow.objects.create(workflow=workflow, alert_rule=alert)
AlertRuleTriggerDataCondition.objects.create(
alert_rule_trigger=trigger, data_condition=data_condition
)

return org

@assume_test_silo_mode(SiloMode.CONTROL)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Generated by Django 5.1.1 on 2024-12-05 18:15

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

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=Q(rule__isnull=False, alert_rule__isnull=True)
| Q(rule__isnull=True, alert_rule__isnull=False),
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=Q(rule__isnull=False, alert_rule__isnull=True)
| Q(rule__isnull=True, alert_rule__isnull=False),
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
Loading
Loading