Skip to content

: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.

perf(activity-log): remove per-item bookmark query in my_notifications (#68600)

Frank Hamand0a39edbf4434
diff --git a/posthog/api/my_notifications.py b/posthog/api/my_notifications.py
index a9e0e9d17951..23669d568e6f 100644
--- a/posthog/api/my_notifications.py
+++ b/posthog/api/my_notifications.py
@@ -35,15 +35,12 @@ class MyNotificationsSerializer(serializers.ModelSerializer):
if "user" not in self.context:
return False
- user_bookmark: Optional[NotificationViewed] = NotificationViewed.objects.filter(
- user=self.context["user"]
- ).first()
+ bookmark_date: Optional[datetime] = self.context.get("last_read_date")
- if user_bookmark is None:
+ if bookmark_date is None:
return True
else:
# API call from browser only includes milliseconds but python datetime in created_at includes microseconds
- bookmark_date = user_bookmark.last_viewed_activity_date
return bookmark_date < obj.created_at.replace(microsecond=obj.created_at.microsecond // 1000 * 1000)
@@ -252,7 +249,9 @@ class MyNotificationsViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
page_of_data = other_peoples_changes[:10]
with timer("serialize"):
- serialized_data = MyNotificationsSerializer(instance=page_of_data, many=True, context={"user": user}).data
+ serialized_data = MyNotificationsSerializer(
+ instance=page_of_data, many=True, context={"user": user, "last_read_date": last_read_date}
+ ).data
response = Response(
status=status.HTTP_200_OK,
diff --git a/posthog/api/test/test_my_notifications.py b/posthog/api/test/test_my_notifications.py
index d2c5d0e7c796..c3cab402c87b 100644
--- a/posthog/api/test/test_my_notifications.py
+++ b/posthog/api/test/test_my_notifications.py
@@ -300,7 +300,7 @@ class TestMyNotifications(APIBaseTest, QueryMatchingTest):
user=user, defaults={"last_viewed_activity_date": f"2023-0{i}-17T04:36:50Z"}
)
- with self.assertNumQueries(FuzzyInt(43, 43)):
+ with self.assertNumQueries(FuzzyInt(33, 33)):
self.client.get(f"/api/projects/{self.team.id}/my_notifications")
def test_microsecond_precision_mismatch(self):