:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(activity-log): load the acting user with activity list queries (#126410)
Paul D'Ambrac582adb47df9
diff --git a/posthog/api/advanced_activity_logs/viewset.py b/posthog/api/advanced_activity_logs/viewset.pyindex 780ec654980f..457099c5d84a 100644--- a/posthog/api/advanced_activity_logs/viewset.py+++ b/posthog/api/advanced_activity_logs/viewset.py@@ -303,7 +303,10 @@ class ActivityLogViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet, mixinsparams = self.request.GET.dict()queryset = apply_organization_scoped_filter(- queryset, bool(self.team.receive_org_level_activity_logs), self.team_id, self.organization.id+ queryset.select_related("user"),+ bool(self.team.receive_org_level_activity_logs),+ self.team_id,+ self.organization.id,)if params.get("user"):diff --git a/posthog/api/my_notifications.py b/posthog/api/my_notifications.pyindex 21a943e3d2af..b81fa855c2b6 100644--- a/posthog/api/my_notifications.py+++ b/posthog/api/my_notifications.py@@ -207,6 +207,7 @@ class MyNotificationsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):if interesting is Noneelse (self.queryset.exclude(user=user)+ .select_related("user").filter(team_id=self.team.id, created_at__gte=history_cutoff).filter(interesting)# notebooks log several activities per logical edit, so only the deduplicated ones count@@ -219,7 +220,7 @@ class MyNotificationsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):# truncate created_at to millisecond precision to match last_read precision# nosemgrep: python.django.security.audit.query-set-extra.avoid-query-set-extra (parameterized via params)other_peoples_changes = other_peoples_changes.extra(- where=["date_trunc('millisecond', created_at) > %s"], params=[last_read_date]+ where=["date_trunc('millisecond', posthog_activitylog.created_at) > %s"], params=[last_read_date])with timer("query_for_data"):diff --git a/posthog/api/test/test_activity_log.py b/posthog/api/test/test_activity_log.pyindex 279b6f7931bf..4daa8a753ff1 100644--- a/posthog/api/test/test_activity_log.py+++ b/posthog/api/test/test_activity_log.py@@ -6,7 +6,9 @@ import time_machinefrom posthog.test.base import APIBaseTest, QueryMatchingTestfrom unittest.mock import patch+from django.db import connectionfrom django.test import SimpleTestCase+from django.test.utils import CaptureQueriesContextfrom django.utils import timezonefrom parameterized import parameterized@@ -206,6 +208,20 @@ class TestActivityLog(APIBaseTest, QueryMatchingTest):assert res.status_code == status.HTTP_200_OKassert len(res.json()["results"]) == 39+ def test_list_query_count_does_not_grow_with_page_size(self) -> None:+ def query_count(page_size: int) -> int:+ with CaptureQueriesContext(connection) as ctx:+ res = self.client.get(f"/api/projects/{self.team.id}/activity_log?page_size={page_size}")+ assert res.status_code == status.HTTP_200_OK+ assert len(res.json()["results"]) == page_size+ return len(ctx)++ query_count(1)+ few = query_count(2)+ many = query_count(8)++ assert few == many, f"listing 8 entries cost {many} queries vs {few} for 2, so something N+1s"+def test_can_list_all_activity_filtered_by_scope(self) -> None:res = self.client.get(f"/api/projects/{self.team.id}/activity_log?scope=FeatureFlag")assert res.status_code == status.HTTP_200_OKdiff --git a/posthog/api/test/test_my_notifications.py b/posthog/api/test/test_my_notifications.pyindex 6048c542a6a3..cb9a83bd3e28 100644--- a/posthog/api/test/test_my_notifications.py+++ b/posthog/api/test/test_my_notifications.py@@ -349,7 +349,7 @@ class TestMyNotifications(APIBaseTest, QueryMatchingTest):user=user, defaults={"last_viewed_activity_date": f"2023-0{i}-17T04:36:50Z"})- with self.assertNumQueries(FuzzyInt(28, 28)):+ with self.assertNumQueries(FuzzyInt(18, 18)):self.client.get(f"/api/projects/{self.team.id}/my_notifications")def test_microsecond_precision_mismatch(self):