: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(cdp): load hog function creators and templates with the list query (#126406)
Paul D'Ambracad58abb1598
diff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.pyindex 1e788be88e0c..34b2a192203d 100644--- a/products/cdp/backend/api/hog_function.py+++ b/products/cdp/backend/api/hog_function.py@@ -1260,6 +1260,7 @@ class HogFunctionViewSet(queryset = queryset.filter(deleted=False)if self.action == "list":+ queryset = queryset.select_related("created_by", "hog_function_template")search = self.request.GET.get("search")if search:if len(search) > MAX_SEARCH_LENGTH:diff --git a/products/cdp/backend/api/test/test_hog_function.py b/products/cdp/backend/api/test/test_hog_function.pyindex 5d3a830f61ec..7ddc17b4337c 100644--- a/products/cdp/backend/api/test/test_hog_function.py+++ b/products/cdp/backend/api/test/test_hog_function.py@@ -7,6 +7,7 @@ from posthog.test.base import APIBaseTest, ClickhouseTestMixin, QueryMatchingTesfrom unittest.mock import ANY, MagicMock, patchfrom django.db import connection+from django.test.utils import CaptureQueriesContextfrom parameterized import parameterizedfrom rest_framework import status@@ -3793,6 +3794,37 @@ class TestHogFunctionAPI(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):assert len(response.json()["results"]) == 50, "With limit=50 and offset=100, should return last 50 items"assert response.json()["count"] == 150, "Total count should still be 150"+ def _create_functions_from_template(self, count: int) -> None:+ template = HogFunctionTemplate.get_template(template_slack.id)+ for i in range(count):+ HogFunction.objects.create(+ team=self.team,+ name=f"Query count function {i}",+ hog="return event",+ type="destination",+ enabled=False,+ created_by=self.user,+ template_id=template_slack.id,+ hog_function_template=template,+ )++ def _list_query_count(self, expected_results: int) -> int:+ url = f"/api/projects/{self.team.id}/hog_functions/"+ self.client.get(url)+ with CaptureQueriesContext(connection) as ctx:+ response = self.client.get(url)+ assert response.status_code == status.HTTP_200_OK+ assert len(response.json()["results"]) == expected_results+ return len(ctx.captured_queries)++ def test_list_query_count_does_not_grow_with_function_count(self):+ self._create_functions_from_template(2)+ few = self._list_query_count(2)+ self._create_functions_from_template(4)+ many = self._list_query_count(6)++ assert few == many, f"listing 6 functions cost {many} queries vs {few} for 2, so something N+1s"+@patch("products.cdp.backend.api.hog_function.posthoganalytics.feature_enabled", return_value=False)def test_enable_backfills_blocked_without_feature_flag(self, mock_feature_enabled):"""Test that enable_backfills is blocked when the feature flag is disabled."""