Browse Source

fix(suspect-resolutions): filter down the activity we query and map it back to the enum name (#38018)

* filter the ActivityType we query for an issue, and map it back to its name

* bump algo_version

* fix test

Co-authored-by: Komal Saini <52506101+komal-saini@users.noreply.github.com>
Gilbert Szeto 2 years ago
parent
commit
4fc7ad9b64

+ 1 - 1
src/sentry/utils/suspect_resolutions/__init__.py

@@ -2,4 +2,4 @@ from .analytics import *  # NOQA
 
 # make sure to increment this when making changes to anything within the 'suspect_resolutions' directory
 # keeps track of changes to how we process suspect commits, so we can filter out analytics events by the algo version
-ALGO_VERSION = "0.0.4"
+ALGO_VERSION = "0.0.5"

+ 19 - 4
src/sentry/utils/suspect_resolutions/get_suspect_resolutions.py

@@ -7,6 +7,7 @@ from sentry import features
 from sentry.models import Activity, Group, GroupStatus
 from sentry.signals import issue_resolved
 from sentry.tasks.base import instrumented_task
+from sentry.types.activity import ActivityType
 from sentry.utils.suspect_resolutions import ALGO_VERSION, analytics
 from sentry.utils.suspect_resolutions.commit_correlation import is_issue_commit_correlated
 from sentry.utils.suspect_resolutions.metric_correlation import is_issue_error_rate_correlated
@@ -35,11 +36,25 @@ def record_suspect_resolutions(
 @instrumented_task(name="sentry.tasks.get_suspect_resolutions", queue="get_suspect_resolutions")
 def get_suspect_resolutions(resolved_issue_id: int) -> Sequence[int]:
     resolved_issue = Group.objects.get(id=resolved_issue_id)
-    resolution_type = (
-        Activity.objects.filter(group=resolved_issue).values_list("type", flat=True).first()
+    latest_resolved_activity = (
+        Activity.objects.filter(
+            group=resolved_issue,
+            type__in=(
+                ActivityType.SET_RESOLVED.value,
+                ActivityType.SET_RESOLVED_IN_COMMIT.value,
+                ActivityType.SET_RESOLVED_IN_PULL_REQUEST.value,
+                ActivityType.SET_RESOLVED_IN_RELEASE.value,
+            ),
+        )
+        .order_by("-datetime")
+        .values_list("type", flat=True)
+        .first()
+    )
+    latest_resolved_activity_type = (
+        ActivityType(latest_resolved_activity).name if latest_resolved_activity else None
     )
 
-    if resolved_issue.status != GroupStatus.RESOLVED or resolution_type is None:
+    if resolved_issue.status != GroupStatus.RESOLVED or latest_resolved_activity is None:
         return []
 
     suspect_issue_candidates = list(
@@ -72,7 +87,7 @@ def get_suspect_resolutions(resolved_issue_id: int) -> Sequence[int]:
             algo_version=ALGO_VERSION,
             resolved_group_id=resolved_issue.id,
             candidate_group_id=metric_correlation_result.candidate_suspect_resolution_id,
-            resolved_group_resolution_type=resolution_type,
+            resolved_group_resolution_type=latest_resolved_activity_type,
             pearson_r_coefficient=metric_correlation_result.coefficient,
             pearson_r_start_time=result.correlation_start_time,
             pearson_r_end_time=result.correlation_end_time,

+ 1 - 1
tests/sentry/utils/suspect_resolutions/test_get_suspect_resolutions.py

@@ -185,7 +185,7 @@ class GetSuspectResolutionsTest(TestCase):
                 algo_version=ALGO_VERSION,
                 resolved_group_id=resolved_issue.id,
                 candidate_group_id=0,
-                resolved_group_resolution_type=resolution_type.type,
+                resolved_group_resolution_type=ActivityType(resolution_type.type).name,
                 pearson_r_coefficient=0.5,
                 pearson_r_start_time=datetime(2022, 1, 2),
                 pearson_r_end_time=datetime(2022, 1, 1),