|
| 1 | +from datetime import UTC, datetime, timedelta |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from sentry.models.group import DEFAULT_TYPE_ID |
| 5 | +from sentry.tasks.seer.backfill_supergroups_lightweight import ( |
| 6 | + BATCH_SIZE, |
| 7 | + backfill_supergroups_lightweight_for_org, |
| 8 | +) |
| 9 | +from sentry.testutils.cases import TestCase |
| 10 | +from sentry.testutils.helpers.features import with_feature |
| 11 | +from sentry.types.group import GroupSubStatus |
| 12 | + |
| 13 | + |
| 14 | +class BackfillSupergroupsLightweightForOrgTest(TestCase): |
| 15 | + def setUp(self): |
| 16 | + super().setUp() |
| 17 | + self.event = self.store_event( |
| 18 | + data={"message": "test error", "level": "error"}, |
| 19 | + project_id=self.project.id, |
| 20 | + ) |
| 21 | + self.group = self.event.group |
| 22 | + self.group.substatus = GroupSubStatus.NEW |
| 23 | + self.group.save(update_fields=["substatus"]) |
| 24 | + |
| 25 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 26 | + @patch( |
| 27 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 28 | + ) |
| 29 | + def test_processes_groups_and_sends_to_seer(self, mock_request): |
| 30 | + mock_request.return_value = MagicMock(status=200) |
| 31 | + |
| 32 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 33 | + |
| 34 | + mock_request.assert_called_once() |
| 35 | + body = mock_request.call_args.args[0] |
| 36 | + assert body["group_id"] == self.group.id |
| 37 | + assert body["project_id"] == self.project.id |
| 38 | + assert body["organization_id"] == self.organization.id |
| 39 | + assert body["issue"]["id"] == self.group.id |
| 40 | + assert len(body["issue"]["events"]) == 1 |
| 41 | + |
| 42 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 43 | + @patch( |
| 44 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 45 | + ) |
| 46 | + def test_processes_groups_across_projects(self, mock_request): |
| 47 | + mock_request.return_value = MagicMock(status=200) |
| 48 | + |
| 49 | + project2 = self.create_project(organization=self.organization) |
| 50 | + event2 = self.store_event( |
| 51 | + data={"message": "error in project2", "level": "error"}, |
| 52 | + project_id=project2.id, |
| 53 | + ) |
| 54 | + event2.group.substatus = GroupSubStatus.NEW |
| 55 | + event2.group.save(update_fields=["substatus"]) |
| 56 | + |
| 57 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 58 | + |
| 59 | + assert mock_request.call_count == 2 |
| 60 | + project_ids = {call.args[0]["project_id"] for call in mock_request.call_args_list} |
| 61 | + assert project_ids == {self.project.id, project2.id} |
| 62 | + |
| 63 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 64 | + @patch( |
| 65 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 66 | + ) |
| 67 | + def test_self_chains_when_more_groups_exist(self, mock_request): |
| 68 | + mock_request.return_value = MagicMock(status=200) |
| 69 | + |
| 70 | + # Create enough groups to fill a batch |
| 71 | + for i in range(BATCH_SIZE): |
| 72 | + evt = self.store_event( |
| 73 | + data={ |
| 74 | + "message": f"error {i}", |
| 75 | + "level": "error", |
| 76 | + "fingerprint": [f"group-{i}"], |
| 77 | + }, |
| 78 | + project_id=self.project.id, |
| 79 | + ) |
| 80 | + evt.group.substatus = GroupSubStatus.NEW |
| 81 | + evt.group.save(update_fields=["substatus"]) |
| 82 | + |
| 83 | + with patch( |
| 84 | + "sentry.tasks.seer.backfill_supergroups_lightweight.backfill_supergroups_lightweight_for_org.apply_async" |
| 85 | + ) as mock_chain: |
| 86 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 87 | + |
| 88 | + mock_chain.assert_called_once() |
| 89 | + call_kwargs = mock_chain.call_args.kwargs["kwargs"] |
| 90 | + assert call_kwargs["last_project_id"] == self.project.id |
| 91 | + assert call_kwargs["last_group_id"] > 0 |
| 92 | + |
| 93 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 94 | + @patch( |
| 95 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 96 | + ) |
| 97 | + def test_does_not_chain_when_batch_incomplete(self, mock_request): |
| 98 | + mock_request.return_value = MagicMock(status=200) |
| 99 | + |
| 100 | + with patch( |
| 101 | + "sentry.tasks.seer.backfill_supergroups_lightweight.backfill_supergroups_lightweight_for_org.apply_async" |
| 102 | + ) as mock_chain: |
| 103 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 104 | + |
| 105 | + mock_chain.assert_not_called() |
| 106 | + |
| 107 | + @patch( |
| 108 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 109 | + ) |
| 110 | + def test_respects_killswitch(self, mock_request): |
| 111 | + with self.options({"seer.supergroups_backfill_lightweight.killswitch": True}): |
| 112 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 113 | + |
| 114 | + mock_request.assert_not_called() |
| 115 | + |
| 116 | + @patch( |
| 117 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 118 | + ) |
| 119 | + def test_skips_without_feature_flag(self, mock_request): |
| 120 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 121 | + |
| 122 | + mock_request.assert_not_called() |
| 123 | + |
| 124 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 125 | + @patch( |
| 126 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 127 | + ) |
| 128 | + def test_continues_on_individual_group_failure(self, mock_request): |
| 129 | + event2 = self.store_event( |
| 130 | + data={"message": "second error", "level": "error", "fingerprint": ["group2"]}, |
| 131 | + project_id=self.project.id, |
| 132 | + ) |
| 133 | + event2.group.substatus = GroupSubStatus.NEW |
| 134 | + event2.group.save(update_fields=["substatus"]) |
| 135 | + |
| 136 | + mock_request.side_effect = [ |
| 137 | + MagicMock(status=500), |
| 138 | + MagicMock(status=200), |
| 139 | + ] |
| 140 | + |
| 141 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 142 | + |
| 143 | + assert mock_request.call_count == 2 |
| 144 | + |
| 145 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 146 | + @patch( |
| 147 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 148 | + ) |
| 149 | + def test_filters_old_groups(self, mock_request): |
| 150 | + self.group.last_seen = datetime.now(UTC) - timedelta(days=91) |
| 151 | + self.group.save(update_fields=["last_seen"]) |
| 152 | + |
| 153 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 154 | + |
| 155 | + mock_request.assert_not_called() |
| 156 | + |
| 157 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 158 | + @patch( |
| 159 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 160 | + ) |
| 161 | + def test_skips_non_error_groups(self, mock_request): |
| 162 | + self.group.type = DEFAULT_TYPE_ID + 1 |
| 163 | + self.group.save(update_fields=["type"]) |
| 164 | + |
| 165 | + backfill_supergroups_lightweight_for_org(self.organization.id) |
| 166 | + |
| 167 | + mock_request.assert_not_called() |
| 168 | + |
| 169 | + @with_feature("organizations:supergroups-lightweight-rca-clustering") |
| 170 | + @patch( |
| 171 | + "sentry.tasks.seer.backfill_supergroups_lightweight.make_lightweight_rca_cluster_request" |
| 172 | + ) |
| 173 | + def test_resumes_from_cursor(self, mock_request): |
| 174 | + backfill_supergroups_lightweight_for_org( |
| 175 | + self.organization.id, |
| 176 | + last_project_id=self.project.id, |
| 177 | + last_group_id=self.group.id, |
| 178 | + ) |
| 179 | + |
| 180 | + mock_request.assert_not_called() |
0 commit comments