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(workflow_engine): Add the ErrorDetectorValidator to the Error Issue Group #83083

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sentry/incidents/endpoints/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
)
from sentry.snuba.subscriptions import create_snuba_query, create_snuba_subscription
from sentry.workflow_engine.endpoints.validators.base import (
BaseDataSourceValidator,
BaseGroupTypeDetectorValidator,
NumericComparisonConditionValidator,
)
from sentry.workflow_engine.models.data_condition import Condition
from sentry.workflow_engine.types import DetectorPriorityLevel
from sentry.workflow_enigne.endpoints.validators.base_data_source import BaseDataSourceValidator


class SnubaQueryDataSourceValidator(BaseDataSourceValidator[QuerySubscription]):
Expand Down
3 changes: 2 additions & 1 deletion src/sentry/issues/grouptype.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sentry.ratelimits.sliding_windows import Quota
from sentry.types.group import PriorityLevel
from sentry.utils import metrics
from sentry.workflow_engine.endpoints.validators.error_detector import ErrorDetectorValidator

if TYPE_CHECKING:
from sentry.models.organization import Organization
Expand Down Expand Up @@ -261,7 +262,7 @@ class ErrorGroupType(GroupType):
category = GroupCategory.ERROR.value
default_priority = PriorityLevel.MEDIUM
released = True
# TODO(cathy): add detector validator
detector_validator = ErrorDetectorValidator
detector_config_schema = {"type": "object", "additionalProperties": False} # empty schema


Expand Down
38 changes: 6 additions & 32 deletions src/sentry/workflow_engine/endpoints/validators/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from collections.abc import Callable
from typing import Generic, TypeVar
from typing import TYPE_CHECKING, TypeVar

from django.db import router, transaction
from rest_framework import serializers
Expand All @@ -9,7 +8,6 @@
from sentry.api.serializers.rest_framework import CamelSnakeSerializer
from sentry.db.models import Model
from sentry.issues import grouptype
from sentry.issues.grouptype import GroupType
from sentry.utils.audit import create_audit_entry
from sentry.workflow_engine.models import (
DataConditionGroup,
Expand All @@ -18,25 +16,16 @@
Detector,
)
from sentry.workflow_engine.models.data_condition import Condition, DataCondition
from sentry.workflow_engine.registry import data_source_type_registry
from sentry.workflow_engine.types import DataSourceTypeHandler, DetectorPriorityLevel
from sentry.workflow_engine.types import DetectorPriorityLevel

T = TypeVar("T", bound=Model)


class DataSourceCreator(Generic[T]):
def __init__(self, create_fn: Callable[[], T]):
self._create_fn = create_fn
self._instance: T | None = None

def create(self) -> T:
if self._instance is None:
self._instance = self._create_fn()
return self._instance
if TYPE_CHECKING:
from sentry.issues.grouptype import GroupType
from sentry.workflow_engine.endpoints.validators.base_data_source import BaseDataSourceValidator


class BaseDataConditionValidator(CamelSnakeSerializer):

type = serializers.CharField(
required=True,
max_length=200,
Expand All @@ -57,11 +46,11 @@ def validate(self, attrs):


class NumericComparisonConditionValidator(BaseDataConditionValidator):

comparison = serializers.FloatField(
required=True,
help_text="Comparison value to be compared against value from data.",
)

result = serializers.ChoiceField(
choices=[
(DetectorPriorityLevel.HIGH, "High"),
Expand Down Expand Up @@ -98,21 +87,6 @@ def validate_result(self, value: str) -> DetectorPriorityLevel:
return result


class BaseDataSourceValidator(CamelSnakeSerializer, Generic[T]):
@property
def data_source_type_handler(self) -> type[DataSourceTypeHandler]:
raise NotImplementedError

def validate(self, attrs):
attrs = super().validate(attrs)
attrs["_creator"] = DataSourceCreator[T](lambda: self.create_source(attrs))
attrs["data_source_type"] = data_source_type_registry.get_key(self.data_source_type_handler)
return attrs

def create_source(self, validated_data) -> T:
raise NotImplementedError


class BaseGroupTypeDetectorValidator(CamelSnakeSerializer):
name = serializers.CharField(
required=True,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from collections.abc import Callable
from typing import Generic, TypeVar

from sentry.api.serializers.rest_framework import CamelSnakeSerializer
from sentry.db.models import Model
from sentry.workflow_engine.registry import data_source_type_registry
from sentry.workflow_engine.types import DataSourceTypeHandler

T = TypeVar("T", bound=Model)


class DataSourceCreator(Generic[T]):
def __init__(self, create_fn: Callable[[], T]):
self._create_fn = create_fn
self._instance: T | None = None

def create(self) -> T:
if self._instance is None:
self._instance = self._create_fn()
return self._instance


class BaseDataSourceValidator(CamelSnakeSerializer, Generic[T]):
@property
def data_source_type_handler(self) -> type[DataSourceTypeHandler]:
raise NotImplementedError

def validate(self, attrs):
attrs = super().validate(attrs)
attrs["_creator"] = DataSourceCreator[T](lambda: self.create_source(attrs))
attrs["data_source_type"] = data_source_type_registry.get_key(self.data_source_type_handler)
return attrs

def create_source(self, validated_data) -> T:
raise NotImplementedError
6 changes: 4 additions & 2 deletions tests/sentry/workflow_engine/endpoints/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
)
from sentry.testutils.cases import TestCase
from sentry.workflow_engine.endpoints.validators.base import (
BaseDataSourceValidator,
BaseGroupTypeDetectorValidator,
DataSourceCreator,
NumericComparisonConditionValidator,
)
from sentry.workflow_engine.endpoints.validators.base_data_source import (
BaseDataSourceValidator,
DataSourceCreator,
)
from sentry.workflow_engine.models import DataCondition, DataConditionGroup, DataSource
from sentry.workflow_engine.models.data_condition import Condition
from sentry.workflow_engine.models.detector import Detector
Expand Down
Loading