From a53b0ef5a85e877d4952007dc0ffcac5d0ffe836 Mon Sep 17 00:00:00 2001 From: Josh Callender <1569818+saponifi3d@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:17:49 -0700 Subject: [PATCH] Updates to the models based on discussions throughout the week. - Changed workflow_action to be action, and more generic. will use this to emit events on detectors or send notifications in workflows - Created lookup tables; detector_workflow and workflow_data_condition_group - Updated detector and workflow models to represent new design --- .../models/{workflow_action.py => action.py} | 8 ++++---- src/sentry/workflow_engine/models/detector.py | 9 +++++++-- src/sentry/workflow_engine/models/detector_workflow.py | 10 ++++++++++ src/sentry/workflow_engine/models/workflow.py | 5 +++++ .../models/workflow_data_condition_group.py | 10 ++++++++++ 5 files changed, 36 insertions(+), 6 deletions(-) rename src/sentry/workflow_engine/models/{workflow_action.py => action.py} (73%) create mode 100644 src/sentry/workflow_engine/models/detector_workflow.py create mode 100644 src/sentry/workflow_engine/models/workflow_data_condition_group.py diff --git a/src/sentry/workflow_engine/models/workflow_action.py b/src/sentry/workflow_engine/models/action.py similarity index 73% rename from src/sentry/workflow_engine/models/workflow_action.py rename to src/sentry/workflow_engine/models/action.py index cb810dce3840e8..fee4015345556e 100644 --- a/src/sentry/workflow_engine/models/workflow_action.py +++ b/src/sentry/workflow_engine/models/action.py @@ -1,11 +1,11 @@ from django.db import models from sentry.backup.scopes import RelocationScope -from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model, sane_repr +from sentry.db.models import DefaultFieldsModel, region_silo_model, sane_repr @region_silo_model -class WorkflowAction(DefaultFieldsModel): +class Action(DefaultFieldsModel): """ A workflow action is an action to be taken as part of a workflow. These will be executed in order as part of a workflow. @@ -14,11 +14,11 @@ class WorkflowAction(DefaultFieldsModel): __relocation_scope__ = RelocationScope.Organization __repr__ = sane_repr("workflow_id", "type") + # TODO (@saponifi3d): Don't hardcode these values class Type(models.TextChoices): - NOTIFICATION = "SendNotificationAction" + Notification = "SendNotificationAction" required = models.BooleanField(default=False) - workflow = FlexibleForeignKey("workflow_engine.Workflow") type = models.TextField(choices=Type.choices) data = models.JSONField(default=dict) data_condition_group = models.ForeignKey( diff --git a/src/sentry/workflow_engine/models/detector.py b/src/sentry/workflow_engine/models/detector.py index 05c3cfe5d518e5..610ffcbb015f34 100644 --- a/src/sentry/workflow_engine/models/detector.py +++ b/src/sentry/workflow_engine/models/detector.py @@ -14,8 +14,13 @@ class Detector(DefaultFieldsModel, OwnerModel): organization = FlexibleForeignKey("sentry.Organization") name = models.CharField(max_length=200) data_sources = models.ManyToManyField("workflow_engine.DataSource", through=DataSourceDetector) - data_condition_group = models.ForeignKey( - "workflow_engine.DataConditionGroup", on_delete=models.CASCADE, blank=True, null=True + + condition_group = models.ForeignKey( + "workflow_engine.DataConditionGroup", + on_delete=models.CASCADE, + blank=True, + null=True, + unique=True, ) class Meta(OwnerModel.Meta): diff --git a/src/sentry/workflow_engine/models/detector_workflow.py b/src/sentry/workflow_engine/models/detector_workflow.py new file mode 100644 index 00000000000000..e93c39fab51d95 --- /dev/null +++ b/src/sentry/workflow_engine/models/detector_workflow.py @@ -0,0 +1,10 @@ +from sentry.backup.scopes import RelocationScope +from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model + + +@region_silo_model +class DetectorWorkflow(DefaultFieldsModel): + __relocation_scope__ = RelocationScope.Organization + + detector = FlexibleForeignKey("workflow_engine.Detector") + workflow = FlexibleForeignKey("workflow_engine.Workflow") diff --git a/src/sentry/workflow_engine/models/workflow.py b/src/sentry/workflow_engine/models/workflow.py index ca7bf3b1044ca6..c0621f86315b9a 100644 --- a/src/sentry/workflow_engine/models/workflow.py +++ b/src/sentry/workflow_engine/models/workflow.py @@ -3,6 +3,8 @@ from sentry.backup.scopes import RelocationScope from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model, sane_repr +from .data_condition_group import DataConditionGroup + @region_silo_model class Workflow(DefaultFieldsModel): @@ -15,6 +17,9 @@ class Workflow(DefaultFieldsModel): name = models.CharField(max_length=200) organization = FlexibleForeignKey("sentry.Organization") + # Required as the 'when' condition for the workflow + condition_group = FlexibleForeignKey(DataConditionGroup, on_delete=models.CASCADE) + __repr__ = sane_repr("name", "organization_id") class Meta: diff --git a/src/sentry/workflow_engine/models/workflow_data_condition_group.py b/src/sentry/workflow_engine/models/workflow_data_condition_group.py new file mode 100644 index 00000000000000..8738d3a5a7a060 --- /dev/null +++ b/src/sentry/workflow_engine/models/workflow_data_condition_group.py @@ -0,0 +1,10 @@ +from sentry.backup.scopes import RelocationScope +from sentry.db.models import DefaultFieldsModel, FlexibleForeignKey, region_silo_model + + +@region_silo_model +class WorkflowDataConditionGroup(DefaultFieldsModel): + __relocation_scope__ = RelocationScope.Organization + + workflow = FlexibleForeignKey("workflow_engine.Workflow") + condition_group = FlexibleForeignKey("workflow_engine.WorkflowDataCondition", unique=True)