diff --git a/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py b/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py index 30987e9f1861e7..f59d89aebdda86 100644 --- a/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py +++ b/src/sentry/workflow_engine/endpoints/serializers/workflow_group_history_serializer.py @@ -10,7 +10,7 @@ from sentry.api.serializers.models.group import BaseGroupSerializerResponse from sentry.models.group import Group from sentry.utils.cursors import Cursor, CursorResult -from sentry.workflow_engine.models import Detector, Workflow, WorkflowFireHistory +from sentry.workflow_engine.models import Detector, DetectorGroup, Workflow, WorkflowFireHistory @dataclass(frozen=True) @@ -118,13 +118,22 @@ def fetch_workflow_groups_paginated( # subquery that retrieves row with the largest date in a group group_max_dates = filtered_history.filter(group=OuterRef("group")).order_by("-date_added")[:1] + + # Subquery to get the detector_id from DetectorGroup. + # The detector does not currently need to be connected to the workflow. + detector_subquery = DetectorGroup.objects.filter( + group=OuterRef("group"), + ).values( + "detector_id" + )[:1] + qs = ( - filtered_history.select_related("group", "detector") + filtered_history.select_related("group") .values("group") .annotate(count=Count("group")) .annotate(event_id=Subquery(group_max_dates.values("event_id"))) .annotate(last_triggered=Max("date_added")) - .annotate(detector_id=Subquery(group_max_dates.values("detector_id"))) + .annotate(detector_id=Subquery(detector_subquery)) ) # Count distinct groups for pagination diff --git a/tests/sentry/workflow_engine/endpoints/serializers/test_workflow_group_history_serializer.py b/tests/sentry/workflow_engine/endpoints/serializers/test_workflow_group_history_serializer.py index 47cca155551bf5..e7f7d1235cf874 100644 --- a/tests/sentry/workflow_engine/endpoints/serializers/test_workflow_group_history_serializer.py +++ b/tests/sentry/workflow_engine/endpoints/serializers/test_workflow_group_history_serializer.py @@ -11,7 +11,7 @@ WorkflowGroupHistory, fetch_workflow_groups_paginated, ) -from sentry.workflow_engine.models import Workflow, WorkflowFireHistory +from sentry.workflow_engine.models import DetectorGroup, Workflow, WorkflowFireHistory pytestmark = [requires_snuba] @@ -32,6 +32,10 @@ def setUp(self) -> None: project_id=self.project.id, type=MetricIssue.slug, ) + DetectorGroup.objects.create( + detector=self.detector_1, + group=self.group, + ) for i in range(3): self.history.append( WorkflowFireHistory( @@ -46,6 +50,10 @@ def setUp(self) -> None: project_id=self.project.id, type=MetricIssue.slug, ) + DetectorGroup.objects.create( + detector=self.detector_2, + group=self.group_2, + ) self.history.append( WorkflowFireHistory( detector=self.detector_2, @@ -59,6 +67,10 @@ def setUp(self) -> None: project_id=self.project.id, type=MetricIssue.slug, ) + DetectorGroup.objects.create( + detector=self.detector_3, + group=self.group_3, + ) for i in range(2): self.history.append( WorkflowFireHistory( diff --git a/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py b/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py index 87c1a61e869852..46a937c282a8e4 100644 --- a/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py +++ b/tests/sentry/workflow_engine/endpoints/test_organization_workflow_group_history.py @@ -1,6 +1,8 @@ from uuid import uuid4 from sentry.api.serializers import serialize +from sentry.grouping.grouptype import ErrorGroupType +from sentry.incidents.grouptype import MetricIssue from sentry.testutils.cases import APITestCase from sentry.testutils.helpers.datetime import before_now, freeze_time from sentry.testutils.skips import requires_snuba @@ -8,6 +10,7 @@ WorkflowGroupHistory, WorkflowGroupHistorySerializer, ) +from sentry.workflow_engine.models import DetectorGroup from sentry.workflow_engine.models.workflow_fire_history import WorkflowFireHistory pytestmark = [requires_snuba] @@ -26,6 +29,14 @@ def setUp(self) -> None: self.history: list[WorkflowFireHistory] = [] self.workflow = self.create_workflow(organization=self.organization) + self.detector = self.create_detector( + project=self.project, + type=ErrorGroupType.slug, + ) + DetectorGroup.objects.create( + detector=self.detector, + group=self.group, + ) for i in range(3): self.history.append( WorkflowFireHistory( @@ -35,6 +46,14 @@ def setUp(self) -> None: ) ) self.group_2 = self.create_group() + self.detector_2 = self.create_detector( + project=self.project, + type=MetricIssue.slug, + ) + DetectorGroup.objects.create( + detector=self.detector_2, + group=self.group_2, + ) self.history.append( WorkflowFireHistory( workflow=self.workflow, @@ -63,14 +82,18 @@ def test_simple(self) -> None: assert resp.data == serialize( [ WorkflowGroupHistory( - self.group, 3, self.base_triggered_date, self.history[0].event_id, detector=None + self.group, + 3, + self.base_triggered_date, + self.history[0].event_id, + detector=self.detector, ), WorkflowGroupHistory( self.group_2, 1, self.base_triggered_date, self.history[-1].event_id, - detector=None, + detector=self.detector_2, ), ], self.user, @@ -88,7 +111,11 @@ def test_pagination(self) -> None: assert resp.data == serialize( [ WorkflowGroupHistory( - self.group, 3, self.base_triggered_date, self.history[0].event_id, detector=None + self.group, + 3, + self.base_triggered_date, + self.history[0].event_id, + detector=self.detector, ) ], self.user, @@ -111,7 +138,7 @@ def test_pagination(self) -> None: 1, self.base_triggered_date, self.history[-1].event_id, - detector=None, + detector=self.detector_2, ) ], self.user,