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.

fix(cdp): batch template lookups for hog functions without a template fk (#126511)

Paul D'Ambra4c76fa9f17ac
diff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.py
index 34b2a192203d..61adaa4f7162 100644
--- a/products/cdp/backend/api/hog_function.py
+++ b/products/cdp/backend/api/hog_function.py
@@ -1,4 +1,6 @@
import json
+from collections import defaultdict
+from collections.abc import Sequence
from copy import deepcopy
from datetime import timedelta
from typing import Any, Optional, cast
@@ -1231,6 +1233,24 @@ class HogFunctionViewSet(
span.set_attribute("hog_function.search.empty", results_len == 0)
return response
+ def get_serializer(self, *args: Any, **kwargs: Any) -> BaseSerializer:
+ if self.action == "list" and args and isinstance(args[0], list):
+ self._attach_templates_without_fk(args[0])
+ return super().get_serializer(*args, **kwargs)
+
+ @staticmethod
+ def _attach_templates_without_fk(functions: Sequence[HogFunction]) -> None:
+ # Set in memory only, so HogFunction.template skips its per-row get_template fallback.
+ by_template_id: dict[str, list[HogFunction]] = defaultdict(list)
+ for fn in functions:
+ if fn.hog_function_template_id is None and fn.template_id:
+ by_template_id[fn.template_id].append(fn)
+ if not by_template_id:
+ return
+ for template_id, template in HogFunctionTemplate.get_latest_templates(by_template_id).items():
+ for fn in by_template_id[template_id]:
+ fn.hog_function_template = template
+
def retrieve(self, request: Request, *args: Any, **kwargs: Any) -> Response:
instance = self.get_object()
response = Response(self.get_serializer(instance).data)
diff --git a/products/cdp/backend/api/test/test_hog_function.py b/products/cdp/backend/api/test/test_hog_function.py
index 7ddc17b4337c..b95a213b19a6 100644
--- a/products/cdp/backend/api/test/test_hog_function.py
+++ b/products/cdp/backend/api/test/test_hog_function.py
@@ -3794,8 +3794,8 @@ 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)
+ def _create_functions_from_template(self, count: int, with_template_fk: bool) -> None:
+ template = HogFunctionTemplate.get_template(template_slack.id) if with_template_fk else None
for i in range(count):
HogFunction.objects.create(
team=self.team,
@@ -3814,13 +3814,16 @@ class TestHogFunctionAPI(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
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
+ results = response.json()["results"]
+ assert len(results) == expected_results
+ assert {row["template"]["id"] for row in results} == {template_slack.id}
return len(ctx.captured_queries)
- def test_list_query_count_does_not_grow_with_function_count(self):
- self._create_functions_from_template(2)
+ @parameterized.expand([("with_template_fk", True), ("template_id_only", False)])
+ def test_list_query_count_does_not_grow_with_function_count(self, _name: str, with_template_fk: bool) -> None:
+ self._create_functions_from_template(2, with_template_fk)
few = self._list_query_count(2)
- self._create_functions_from_template(4)
+ self._create_functions_from_template(4, with_template_fk)
many = self._list_query_count(6)
assert few == many, f"listing 6 functions cost {many} queries vs {few} for 2, so something N+1s"
diff --git a/products/cdp/backend/legacy_destination_migration.py b/products/cdp/backend/legacy_destination_migration.py
index 76df3e071589..3e7bab2e7bfc 100644
--- a/products/cdp/backend/legacy_destination_migration.py
+++ b/products/cdp/backend/legacy_destination_migration.py
@@ -119,6 +119,7 @@ def _build_hog_function(plugin_config: Mapping[str, Any], drop_unmapped_inputs:
team_id=team_id,
type=HogFunctionType.LEGACY_DESTINATION,
template_id=template.template_id,
+ hog_function_template=template,
name=plugin_config["plugin__name"],
description=template.description or "",
icon_url=template.icon_url,
diff --git a/products/cdp/backend/models/hog_function_template.py b/products/cdp/backend/models/hog_function_template.py
index 897fff413aae..9273a06914c4 100644
--- a/products/cdp/backend/models/hog_function_template.py
+++ b/products/cdp/backend/models/hog_function_template.py
@@ -1,5 +1,6 @@
import json
import hashlib
+from collections.abc import Iterable
from typing import Optional
from django.db import models
@@ -127,6 +128,16 @@ class HogFunctionTemplate(UUIDTModel):
)
return None
+ @classmethod
+ def get_latest_templates(cls, template_ids: Iterable[str]) -> dict[str, "HogFunctionTemplate"]:
+ """Resolves the latest version of each template id, as get_template does, in one query."""
+ latest = (
+ cls.objects.filter(template_id__in=set(template_ids))
+ .order_by("template_id", "-created_at")
+ .distinct("template_id")
+ )
+ return {template.template_id: template for template in latest}
+
def compile_bytecode(self):
"""
Compiles the Hog code_language code to bytecode and stores it in the bytecode field.
diff --git a/products/cdp/backend/test/test_legacy_destination_migration.py b/products/cdp/backend/test/test_legacy_destination_migration.py
index 687a5fc05f92..a468d3e6f287 100644
--- a/products/cdp/backend/test/test_legacy_destination_migration.py
+++ b/products/cdp/backend/test/test_legacy_destination_migration.py
@@ -66,6 +66,7 @@ class TestLegacyDestinationMigration(BaseTest):
assert result.created == [plugin_config.id]
hog_function = self._hog_functions().get()
assert hog_function.template_id == "plugin-customerio-plugin"
+ assert hog_function.hog_function_template_id == self.template.id
assert hog_function.enabled is True
assert hog_function.inputs["customerioSiteId"] == {"value": "site-1"}