: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
perf(mcp-analytics): build the activity overview database once per request (#124269)
Paul D'Ambrad2d237deb9c9
diff --git a/products/mcp_analytics/backend/logic.py b/products/mcp_analytics/backend/logic.pyindex 1143bf572900..7fffd660b9c7 100644--- a/products/mcp_analytics/backend/logic.py+++ b/products/mcp_analytics/backend/logic.py@@ -11,12 +11,16 @@ from django.utils import timezonefrom posthog.schema import AnyPropertyFilterDiscriminated, DateRange, EventPropertyFilterfrom posthog.hogql import ast+from posthog.hogql.context import HogQLContext+from posthog.hogql.database.database import Database+from posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.query_tagging import Feature, Product, tags_contextfrom posthog.hogql_queries.utils.caller_context import map_in_caller_contextfrom posthog.hogql_queries.utils.query_date_range import QueryDateRange+from posthog.models.instance_setting import get_instance_settingfrom posthog.models.person import Personfrom posthog.models.person.util import get_persons_mapped_by_distinct_idfrom posthog.models.team.team import Team@@ -653,14 +657,32 @@ def _extract_error_message(raw: Any) -> str | None:def _run_activity_query(- team: Team, sql: str, name: str, placeholders: dict[str, ast.Expr], user: User | None+ team: Team,+ sql: str,+ name: str,+ placeholders: dict[str, ast.Expr],+ user: User | None,+ database: Database | None,) -> list[Any]:query = parse_select(sql, placeholders={**placeholders})+ # Each thread needs its own context, because the executor writes to it while it runs.+ context = HogQLContext(team_id=team.pk, user=user, database=database)with tags_context(product=Product.MCP_ANALYTICS, feature=Feature.QUERY, team_id=team.id, name=name):- response = execute_hogql_query(query=query, team=team, user=user)+ response = execute_hogql_query(query=query, team=team, user=user, context=context)return response.results or []+def _shared_activity_database(team: Team, user: User | None) -> Database | None:+ if not get_instance_setting("HOGQL_SHARED_INSIGHT_DATABASE_ENABLED"):+ return None+ return Database.create_for(+ team=team,+ user=user,+ modifiers=create_default_modifiers_for_team(team),+ trigger="mcp_activity_overview",+ )++def get_activity_overview(team: Team,properties: list[AnyPropertyFilterDiscriminated] | None = None,@@ -698,9 +720,15 @@ def get_activity_overview(filter_test_accounts,)+ # Building the database costs more Python time than most of these queries spend in+ # ClickHouse, and the threads share the GIL, so build it once for all five.+ database = _shared_activity_database(team, user)+def run_query(spec: tuple[str, str, object]) -> list[Any]:assert isinstance(spec[2], dict)- return _run_activity_query(team, spec[0], spec[1], {**spec[2], "shared_filters": shared_filters}, user)+ return _run_activity_query(+ team, spec[0], spec[1], {**spec[2], "shared_filters": shared_filters}, user, database+ )stats_rows, missing_capability_rows, top_tools_rows, clients_rows, recent_calls_rows = map_in_caller_context(run_query,diff --git a/products/mcp_analytics/backend/tests/test_api.py b/products/mcp_analytics/backend/tests/test_api.pyindex 775f72e326a7..4ee4c19d691c 100644--- a/products/mcp_analytics/backend/tests/test_api.py+++ b/products/mcp_analytics/backend/tests/test_api.py@@ -12,6 +12,8 @@ from parameterized import parameterizedfrom posthog.schema import AnyPropertyFilterDiscriminated, EventPropertyFilter, PersonPropertyFilter, PropertyOperator+from posthog.hogql.database.database import Database+from posthog.models.utils import uuid7from posthog.test.persons import create_personfrom posthog.utils import generate_cache_key@@ -821,8 +823,10 @@ class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixinproperties={"$session_id": str(uuid7()), "$mcp_tool_name": "query_run"},)- overview = api.get_activity_overview(self.team)+ with patch.object(Database, "create_for", wraps=Database.create_for) as create_database:+ overview = api.get_activity_overview(self.team)+ assert create_database.call_count == 1assert overview.stats == contracts.ActivityStats(total_calls=2,distinct_tools=2,