: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): cache the activity overview between polls (#124669)
Paul D'Ambraf190419d2584
diff --git a/products/mcp_analytics/backend/facade/api.py b/products/mcp_analytics/backend/facade/api.pyindex c86a1583221b..2f94b0b03f38 100644--- a/products/mcp_analytics/backend/facade/api.py+++ b/products/mcp_analytics/backend/facade/api.py@@ -181,8 +181,10 @@ def get_activity_overview() -> contracts.ActivityOverview:"""Compute the activity view's aggregates and recent-call feed in one pass.- Bounded to the last 30 days; always computed fresh (the view polls to watch- data arrive). ``properties`` and ``filter_test_accounts`` are the tabs' shared filters.+ Bounded to the last 30 days. Cached by the filters and the caller's property restrictions:+ for 5 minutes without restrictions, for less than one poll interval with them.++ ``properties`` and ``filter_test_accounts`` are the tabs' shared filters."""return logic.get_activity_overview(team, properties=properties, filter_test_accounts=filter_test_accounts, user=userdiff --git a/products/mcp_analytics/backend/logic.py b/products/mcp_analytics/backend/logic.pyindex 7fffd660b9c7..53b6715cb92c 100644--- a/products/mcp_analytics/backend/logic.py+++ b/products/mcp_analytics/backend/logic.py@@ -15,6 +15,7 @@ from posthog.hogql.context import HogQLContextfrom posthog.hogql.database.database import Databasefrom posthog.hogql.modifiers import create_default_modifiers_for_teamfrom posthog.hogql.parser import parse_select+from posthog.hogql.property_access_types import RestrictedPropertyfrom posthog.hogql.query import execute_hogql_queryfrom posthog.clickhouse.query_tagging import Feature, Product, tags_context@@ -28,6 +29,8 @@ from posthog.models.user import Userfrom posthog.personhog_client.caller_tag import personhog_caller_tagfrom posthog.utils import generate_cache_key+from products.access_control.backend.facade.api import get_restricted_properties_with_group_type_index_for_team+from products.access_control.backend.facade.property_access import restriction_fingerprintfrom products.mcp_analytics.backend import intent_generation, mcp_harnessfrom products.mcp_analytics.backend.constants import (MAX_SNAPSHOT_CLUSTERS,@@ -115,6 +118,12 @@ SESSION_OVERLAP_BUFFER = timedelta(days=7)# short enough that "Reload" still feels live.SESSIONS_CACHE_TTL_SECONDS = 30+# The activity view polls every 60 s. Restricted members each get their own entry, so it+# expires before their next poll. Unrestricted members share one entry, so it can live longer;+# empty overviews skip the cache, so a new team still sees its first call on the next poll.+ACTIVITY_OVERVIEW_CACHE_TTL_SECONDS = 20+UNRESTRICTED_ACTIVITY_OVERVIEW_CACHE_TTL_SECONDS = 5 * 60+# One row per $session_id, aggregated straight from events. The column shape# (min/max/count/groupUniqArray/argMax) maps 1:1 onto a future AggregatingMergeTree# if per-team volume ever warrants materialising it. __SEARCH__ / __ORDER__ are@@ -683,16 +692,73 @@ def _shared_activity_database(team: Team, user: User | None) -> Database | None:)+_MEMBER_INDEPENDENT_FILTER_TYPES = frozenset({"event", "person"})+++def _filters_read_the_same_for_every_member(+ team: Team, properties: list[AnyPropertyFilterDiscriminated] | None, filter_test_accounts: bool+) -> bool:+ # Other filter types (session, HogQL, cohort, warehouse) and $virt_* keys can read tables or fields+ # that the query database adds per member, so property restrictions alone don't decide+ # what the member sees.+ filters = [prop.model_dump(mode="json") for prop in (properties or [])]+ if filter_test_accounts:+ filters += team.test_account_filters or []+ return all(+ isinstance(f, dict)+ and f.get("type") in _MEMBER_INDEPENDENT_FILTER_TYPES+ and not str(f.get("key", "")).startswith("$virt")+ for f in filters+ )+++def _activity_overview_cache_key(+ team: Team,+ properties: list[AnyPropertyFilterDiscriminated] | None,+ filter_test_accounts: bool,+ restrictions: set[RestrictedProperty],+ user: User | None,+) -> str:+ filters = _filters_cache_fragment(team, properties, filter_test_accounts)+ shared = _filters_read_the_same_for_every_member(team, properties, filter_test_accounts)+ member = None if shared or user is None else user.pk+ return generate_cache_key(+ team.pk, f"mcp_activity_overview_{filters}_{restriction_fingerprint(restrictions)}_{member}"+ )++def get_activity_overview(team: Team,properties: list[AnyPropertyFilterDiscriminated] | None = None,filter_test_accounts: bool = False,user: User | None = None,) -> contracts.ActivityOverview:- """Compute everything the activity view renders, bounded to ``ACTIVITY_WINDOW``.+ """Return everything the activity view renders, bounded to ``ACTIVITY_WINDOW``.++ Members without property restrictions share one cache entry, so it lives for minutes.+ Restricted members each have their own entry, which lives for less than one poll interval.+ """+ restrictions = get_restricted_properties_with_group_type_index_for_team(user=user, team_id=team.pk)+ cache_key = _activity_overview_cache_key(team, properties, filter_test_accounts, restrictions, user)+ cached = cache.get(cache_key)+ if cached is not None:+ return cached++ overview = _query_activity_overview(team, properties, filter_test_accounts, user)+ # Don't cache an empty overview: a newly set-up team waits on this view for its first call.+ if overview.stats.total_calls:+ ttl = ACTIVITY_OVERVIEW_CACHE_TTL_SECONDS if restrictions else UNRESTRICTED_ACTIVITY_OVERVIEW_CACHE_TTL_SECONDS+ cache.set(cache_key, overview, ttl)+ return overview- Always computed fresh: the view's whole point is watching data arrive, so callers- poll this endpoint rather than a stale cache.++def _query_activity_overview(+ team: Team,+ properties: list[AnyPropertyFilterDiscriminated] | None,+ filter_test_accounts: bool,+ user: User | None,+) -> contracts.ActivityOverview:+ """Compute everything the activity view renders, bounded to ``ACTIVITY_WINDOW``.The four queries are independent, so they run concurrently. Served serially theirlatency summed, which left the summary sentence landing long after the live feeddiff --git a/products/mcp_analytics/backend/presentation/views.py b/products/mcp_analytics/backend/presentation/views.pyindex f64cec33b3a2..31701247c483 100644--- a/products/mcp_analytics/backend/presentation/views.py+++ b/products/mcp_analytics/backend/presentation/views.py@@ -292,8 +292,8 @@ class MCPSessionViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):operation_id="mcp_analytics_sessions_activity_overview",description=("Aggregate counters, top tools, agent clients, and the most recent tool calls for the last 30 days, "- "computed in one request. Powers the dashboard's activity view; always computed fresh so polling "- "callers watch data arrive."+ "computed in one request. Powers the dashboard's activity view. Results are cached for up to 5 minutes, "+ "or 20 seconds when the caller has property restrictions. An empty result is never cached."),)@action(detail=False, methods=["get"], url_path="activity_overview", pagination_class=None)diff --git a/products/mcp_analytics/backend/tests/test_api.py b/products/mcp_analytics/backend/tests/test_api.pyindex 4ee4c19d691c..d295d1aeb823 100644--- a/products/mcp_analytics/backend/tests/test_api.py+++ b/products/mcp_analytics/backend/tests/test_api.py@@ -1,7 +1,8 @@import hashlibfrom datetime import UTC, datetime, timedelta-from typing import Any+from typing import Any, Literal+import time_machinefrom posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_eventsfrom unittest.mock import patch@@ -14,11 +15,15 @@ from posthog.schema import AnyPropertyFilterDiscriminated, EventPropertyFilter,from posthog.hogql.database.database import Database+from posthog.constants import AvailableFeature+from posthog.models import PropertyDefinitionfrom posthog.models.utils import uuid7from posthog.test.persons import create_personfrom posthog.utils import generate_cache_key-from products.mcp_analytics.backend import intent_generation, mcp_harness+from products.access_control.backend.models.property_access_control import PropertyAccessControl+from products.access_control.backend.property_access_control import PropertyAccessLevel+from products.mcp_analytics.backend import intent_generation, logic, mcp_harnessfrom products.mcp_analytics.backend.facade import api, contracts, enumsfrom products.mcp_analytics.backend.models import MCPAnalyticsSubmission, MCPSessionfrom products.mcp_analytics.backend.tests import _MCPAnalyticsTeamScopedTestMixin@@ -779,6 +784,10 @@ class TestLLMConsentGate(APIBaseTest):class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixin, APIBaseTest):+ def setUp(self) -> None:+ super().setUp()+ cache.clear()+def test_aggregates_window_and_extracts_error_messages(self) -> None:session_id = str(uuid7())_create_event(@@ -857,6 +866,7 @@ class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixintimestamp=datetime.now(tz=UTC) - timedelta(minutes=5),properties={"$session_id": str(uuid7()), "$mcp_tool_name": "query_run", **properties},)+ flush_persons_and_events()@parameterized.expand([@@ -886,6 +896,81 @@ class TestActivityOverview(_MCPAnalyticsTeamScopedTestMixin, ClickhouseTestMixinassert sum(row.calls for row in overview.clients) == expected_callsassert len(overview.recent_calls) == expected_calls+ def _restrict_a_property(self) -> None:+ self.organization.available_product_features = [+ {"name": AvailableFeature.PROPERTY_ACCESS_CONTROL, "key": AvailableFeature.PROPERTY_ACCESS_CONTROL}+ ]+ self.organization.save()+ email = PropertyDefinition.objects.create(+ team=self.team, name="email", property_type="String", type=PropertyDefinition.Type.PERSON+ )+ PropertyAccessControl.objects.create(+ team=self.team, property_definition=email, access_level=PropertyAccessLevel.NONE.value+ )++ @parameterized.expand(+ [+ ("same_request_is_served_from_cache", 1, False, "never", 0, 1),+ ("empty_result_is_not_cached", 0, False, "never", 0, 2),+ ("other_filters_query_again", 1, True, "never", 0, 2),+ ("other_property_restrictions_query_again", 1, False, "between_requests", 0, 2),+ ("unrestricted_entry_outlives_a_poll_interval", 1, False, "never", 61, 1),+ ("restricted_entry_expires_before_the_next_poll", 1, False, "before_requests", 21, 2),+ ]+ )+ def test_caches_overview_per_filters_and_property_restrictions(+ self,+ _name: str,+ calls_before: int,+ change_filters: bool,+ restrict_a_property: Literal["never", "before_requests", "between_requests"],+ seconds_later: int,+ expected_calls: int,+ ) -> None:+ self.team.test_account_filters = TEST_ACCOUNT_FILTERS+ self.team.save()+ if restrict_a_property == "before_requests":+ self._restrict_a_property()+ self._emit_call({"$mcp_client_name": "claude-code"}, count=calls_before)+ api.get_activity_overview(self.team, user=self.user)++ self._emit_call({"$mcp_client_name": "claude-code"}, count=2 - calls_before)+ if restrict_a_property == "between_requests":+ self._restrict_a_property()+ with time_machine.travel(datetime.now(tz=UTC) + timedelta(seconds=seconds_later), tick=False):+ overview = api.get_activity_overview(self.team, filter_test_accounts=change_filters, user=self.user)++ assert overview.stats.total_calls == expected_calls++ @parameterized.expand(+ [+ ("plain_property_is_shared", "$mcp_tool_name", TEST_ACCOUNT_FILTERS, True),+ ("virtual_property_is_per_member", "$virt_tier", TEST_ACCOUNT_FILTERS, False),+ (+ "hogql_test_account_filter_is_per_member",+ "$mcp_tool_name",+ [{"key": "1 = 1", "type": "hogql"}],+ False,+ ),+ ]+ )+ def test_overview_cache_key_is_per_member_unless_filters_read_the_same_for_everyone(+ self, _name: str, key: str, test_account_filters: list[dict[str, Any]], shared: bool+ ) -> None:+ self.team.test_account_filters = test_account_filters+ self.team.save()+ other_user = self._create_user("[email protected]")+ properties: list[AnyPropertyFilterDiscriminated] = [+ EventPropertyFilter(key=key, value="x", operator=PropertyOperator.EXACT)+ ]++ keys = {+ logic._activity_overview_cache_key(self.team, properties, True, set(), user)+ for user in (self.user, other_user)+ }++ assert (len(keys) == 1) == shared+def test_tool_filter_does_not_hide_missing_capability_reports(self) -> None:self._emit_call({"$mcp_tool_name": "query_run"})_create_event(diff --git a/products/mcp_analytics/frontend/generated/api.ts b/products/mcp_analytics/frontend/generated/api.tsindex 93eacb875f7f..d294efc045ab 100644--- a/products/mcp_analytics/frontend/generated/api.ts+++ b/products/mcp_analytics/frontend/generated/api.ts@@ -304,7 +304,7 @@ export const getMcpAnalyticsSessionsActivityOverviewUrl = (}/**- * Aggregate counters, top tools, agent clients, and the most recent tool calls for the last 30 days, computed in one request. Powers the dashboard's activity view; always computed fresh so polling callers watch data arrive.+ * Aggregate counters, top tools, agent clients, and the most recent tool calls for the last 30 days, computed in one request. Powers the dashboard's activity view. Results are cached for up to 5 minutes, or 20 seconds when the caller has property restrictions. An empty result is never cached.*/export const mcpAnalyticsSessionsActivityOverview = async (projectId: string,