Skip to content

Commit

Permalink
fix(issues): Fix issue count discrepancy (#57915)
Browse files Browse the repository at this point in the history
Add buffer count to issue count to prevent issue count discrepancy
  • Loading branch information
jangjodi committed Oct 13, 2023
1 parent 70ef5b5 commit b79b9c3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/sentry/api/endpoints/group_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from sentry.plugins.base import plugins
from sentry.plugins.bases.issue2 import IssueTrackingPlugin2
from sentry.services.hybrid_cloud.user.service import user_service
from sentry.tasks.post_process import fetch_buffered_group_stats
from sentry.types.ratelimit import RateLimit, RateLimitCategory
from sentry.utils import metrics
from sentry.utils.safe import safe_execute
Expand Down Expand Up @@ -156,6 +157,11 @@ def __group_hourly_daily_stats(group: Group, environment_ids: Sequence[int]):

return hourly_stats, daily_stats

@staticmethod
def __get_group_global_count(group: Group) -> str:
fetch_buffered_group_stats(group)
return str(group.times_seen_with_pending)

def get(self, request: Request, group) -> Response:
"""
Retrieve an Issue
Expand Down Expand Up @@ -257,6 +263,7 @@ def get(self, request: Request, group) -> Response:
"pluginContexts": self._get_context_plugins(request, group),
"userReportCount": user_reports.count(),
"stats": {"24h": hourly_stats, "30d": daily_stats},
"count": self.__get_group_global_count(group),
}
)

Expand Down
31 changes: 30 additions & 1 deletion tests/sentry/api/endpoints/test_group_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.test import override_settings
from django.utils import timezone

from sentry import tsdb
from sentry import buffer, tsdb
from sentry.buffer.redis import RedisBuffer
from sentry.issues.grouptype import PerformanceSlowDBQueryGroupType
from sentry.models.activity import Activity
from sentry.models.apikey import ApiKey
Expand Down Expand Up @@ -264,6 +265,34 @@ def test_collapse_tags(self):
response = self.client.get(url, {"collapse": ["tags"]})
assert "tags" not in response.data

def test_count_with_buffer(self):
"""Test that group count includes the count from the buffer."""
self.login_as(user=self.user)

redis_buffer = RedisBuffer()
with mock.patch("sentry.buffer.backend.get", redis_buffer.get), mock.patch(
"sentry.buffer.backend.incr", redis_buffer.incr
):
event = self.store_event(
data={"message": "testing", "fingerprint": ["group-1"]}, project_id=self.project.id
)
group = event.group
event.group.update(times_seen=1)
buffer.backend.incr(Group, {"times_seen": 15}, filters={"pk": event.group.id})

url = f"/api/0/issues/{group.id}/"
response = self.client.get(url, format="json")
assert response.status_code == 200, response.content
assert response.data["id"] == str(group.id)
assert response.data["count"] == "16"

url = f"/api/0/organizations/{group.organization.slug}/issues/{group.id}/"
response = self.client.get(url, format="json")

assert response.status_code == 200, response.content
assert response.data["id"] == str(group.id)
assert response.data["count"] == "16"


@region_silo_test(stable=True)
class GroupUpdateTest(APITestCase):
Expand Down

0 comments on commit b79b9c3

Please sign in to comment.