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(slack): serve unfurl chart renders from recent insight cache (#122115)

Vojta Bartošc88691dd7447
diff --git a/products/exports/backend/api/exports.py b/products/exports/backend/api/exports.py
index 37c9cea99d6a..a21c8b9bffb0 100644
--- a/products/exports/backend/api/exports.py
+++ b/products/exports/backend/api/exports.py
@@ -184,8 +184,9 @@ class ExportedAssetSerializer(UserAccessControlSerializerMixin, serializers.Mode
raise ValidationError({"insight": ["This insight does not belong to your team."]})
export_context = data.get("export_context") or {}
- if "limit_context" in export_context:
- raise ValidationError({"export_context": ["limit_context is not supported for exports."]})
+ for internal_key in ("limit_context", "execution_mode"):
+ if internal_key in export_context:
+ raise ValidationError({"export_context": [f"{internal_key} is not supported for exports."]})
if export_context.get("path") and (
str(export_context.get("method", "GET")).upper() != "GET" or export_context.get("body") is not None
):
diff --git a/products/exports/backend/api/test/test_exports.py b/products/exports/backend/api/test/test_exports.py
index ff2f82d6b555..5534dc6509ce 100644
--- a/products/exports/backend/api/test/test_exports.py
+++ b/products/exports/backend/api/test/test_exports.py
@@ -502,21 +502,22 @@ class TestExports(APIBaseTest):
},
)
- def test_errors_if_the_request_picks_its_own_limit_context(self) -> None:
+ @parameterized.expand([("limit_context", "posthog_ai"), ("execution_mode", "blocking")])
+ def test_errors_if_the_request_picks_an_internal_export_setting(self, key: str, value: str) -> None:
response = self.client.post(
f"/api/projects/{self.team.id}/exports",
{
"export_format": "image/png",
"export_context": {
"source": {"kind": "HogQLQuery", "query": "SELECT 1"},
- "limit_context": "posthog_ai",
+ key: value,
},
},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json()["attr"], "export_context")
- self.assertEqual(response.json()["detail"], "limit_context is not supported for exports.")
+ self.assertEqual(response.json()["detail"], f"{key} is not supported for exports.")
@parameterized.expand(["not/allowed", ExportedAsset.ExportFormat.JSONL])
def test_errors_if_bad_format(self, export_format: str) -> None:
diff --git a/products/exports/backend/facade/api.py b/products/exports/backend/facade/api.py
index 322177475d07..b9c6244c824b 100644
--- a/products/exports/backend/facade/api.py
+++ b/products/exports/backend/facade/api.py
@@ -15,6 +15,7 @@ from temporalio.common import WorkflowIDReusePolicy
from posthog.hogql.constants import LimitContext
+from posthog.hogql_queries.query_runner import ExecutionMode
from posthog.models import Team, User
from posthog.storage import object_storage
from posthog.temporal.common.client import async_connect
@@ -133,6 +134,20 @@ def export_limit_context(export_context: dict | None) -> LimitContext:
return LimitContext.QUERY
+# The execution modes an insight export can warm its cache with. Async modes return no result on a
+# cache miss, so the render page would recompute inside its load timeout.
+_EXPORT_EXECUTION_MODES = frozenset(
+ {ExecutionMode.CALCULATE_BLOCKING_ALWAYS, ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE}
+)
+
+
+def export_execution_mode(export_context: dict | None) -> ExecutionMode:
+ requested = (export_context or {}).get("execution_mode")
+ if requested in _EXPORT_EXECUTION_MODES:
+ return ExecutionMode(requested)
+ return ExecutionMode.CALCULATE_BLOCKING_ALWAYS
+
+
def _validate_adhoc_export_context(export_context: dict) -> None:
"""The ad-hoc render pipeline (viewport sizing, the exporter page's Query dispatch) draws a
chart for an InsightVizNode-wrapped source, or for a DataVisualizationNode over HogQL. Anything
@@ -228,7 +243,10 @@ def _create_png_export_asset(
dashboard_id: int | None = None,
is_system: bool = False,
expires_after: datetime | None = None,
+ execution_mode: ExecutionMode = ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
) -> ExportedAsset:
+ if execution_mode not in _EXPORT_EXECUTION_MODES:
+ raise ValueError(f"Unsupported execution mode for an export: {execution_mode}")
if created_by is None:
# Access control below resolves against created_by; a principal-less render would
# silently skip it, so service callers must attribute the render to a real user.
@@ -256,6 +274,8 @@ def _create_png_export_asset(
dashboard, "viewer"
):
raise ValueError("Dashboard not found")
+ if execution_mode != ExecutionMode.CALCULATE_BLOCKING_ALWAYS:
+ export_context = {**(export_context or {}), "execution_mode": execution_mode.value}
asset = ExportedAsset.objects.create(
team=team,
@@ -279,6 +299,7 @@ def create_png_export(
query: dict | None = None,
is_system: bool = False,
expires_after: datetime | None = None,
+ execution_mode: ExecutionMode = ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
) -> int:
"""Check access and create the PNG asset that ``ExportAssetWorkflow`` renders, without rendering it.
@@ -288,6 +309,13 @@ def create_png_export(
Pass exactly one of a saved ``insight_id`` or an ad hoc ``query``. A query must be an
InsightVizNode or a DataVisualizationNode over HogQL. A request that breaks a rule raises ``ValueError``.
+
+ ``execution_mode`` sets how a saved insight warms its cache before the render. The default,
+ ``CALCULATE_BLOCKING_ALWAYS``, recomputes the insight on every render.
+ ``RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE`` uses a recent cached result, and computes and waits
+ only when the cache is stale. Use it where a slightly older chart is acceptable and query load
+ matters, such as link previews. Other modes raise ``ValueError``. An ad hoc query always renders
+ with the recent cache.
"""
return _create_png_export_asset(
team=team,
@@ -296,6 +324,7 @@ def create_png_export(
export_context={"source": query} if query is not None else None,
is_system=is_system,
expires_after=expires_after,
+ execution_mode=execution_mode,
).id
diff --git a/products/exports/backend/tasks/image_exporter.py b/products/exports/backend/tasks/image_exporter.py
index 9b131aefe5f2..cf03e7d990c7 100644
--- a/products/exports/backend/tasks/image_exporter.py
+++ b/products/exports/backend/tasks/image_exporter.py
@@ -35,7 +35,7 @@ from posthog.utils import absolute_uri
from products.access_control.backend.facade.user_access_control import UserAccessControlError
from products.dashboards.backend.models.dashboard_tile import DashboardTile
-from products.exports.backend.facade.api import export_limit_context
+from products.exports.backend.facade.api import export_execution_mode, export_limit_context
from products.exports.backend.models.exported_asset import ExportedAsset, get_render_access_token, save_content
from products.exports.backend.tasks.exporter_utils import log_error_if_site_url_not_reachable
from products.exports.backend.tasks.failure_handler import (
@@ -563,6 +563,7 @@ def export_image(
# Falls back to the saved insight query for subscriptions and other server-initiated exports.
export_context = exported_asset.export_context or {}
query_override = export_context.get("source")
+ insight_execution_mode = export_execution_mode(export_context)
dashboard_variables = None
tile_filters_override = None
@@ -588,7 +589,7 @@ def export_image(
exported_asset.insight,
team=exported_asset.team,
dashboard=exported_asset.dashboard,
- execution_mode=ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
+ execution_mode=insight_execution_mode,
# Background render (no request user); attribute the read to the export owner.
user=exported_asset.created_by,
variables_override=None,
@@ -612,7 +613,7 @@ def export_image(
exported_asset.insight,
team=exported_asset.team,
dashboard=exported_asset.dashboard,
- execution_mode=ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
+ execution_mode=insight_execution_mode,
# Background render (no request user); attribute the read to the export owner.
user=exported_asset.created_by,
variables_override=dashboard_variables,
diff --git a/products/exports/backend/tasks/test/test_image_exporter.py b/products/exports/backend/tasks/test/test_image_exporter.py
index 416c940a7e5e..a09c8f991d87 100644
--- a/products/exports/backend/tasks/test/test_image_exporter.py
+++ b/products/exports/backend/tasks/test/test_image_exporter.py
@@ -456,12 +456,14 @@ class TestImageExporter(APIBaseTest):
{"source": {"kind": "HogQLQuery", "query": "SELECT 1"}},
{"kind": "HogQLQuery", "query": "SELECT 1"},
False,
+ ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
),
(
"with_source_and_dashboard_variables",
{"source": {"kind": "HogQLQuery", "query": "SELECT 1"}},
{"kind": "HogQLQuery", "query": "SELECT 1"},
True,
+ ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
),
(
"source_vars_not_overridden_by_dashboard_vars",
@@ -478,12 +480,28 @@ class TestImageExporter(APIBaseTest):
"variables": {"var_1": {"code_name": "eventName", "value": "$pageview"}},
},
True,
+ ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
),
(
"without_export_context",
None,
None,
False,
+ ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
+ ),
+ (
+ "recent_cache_execution_mode",
+ {"execution_mode": ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE.value},
+ None,
+ False,
+ ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE,
+ ),
+ (
+ "async_execution_mode_falls_back",
+ {"execution_mode": ExecutionMode.RECENT_CACHE_CALCULATE_ASYNC_IF_STALE.value},
+ None,
+ False,
+ ExecutionMode.CALCULATE_BLOCKING_ALWAYS,
),
]
)
@@ -494,6 +512,7 @@ class TestImageExporter(APIBaseTest):
export_context: dict | None,
expected_query_override: dict | None,
with_dashboard: bool,
+ expected_execution_mode: ExecutionMode,
mock_calculate: Any,
*args: Any,
) -> None:
@@ -530,6 +549,7 @@ class TestImageExporter(APIBaseTest):
image_exporter.export_image(exported_asset)
call_kwargs = mock_calculate.call_args[1]
+ assert call_kwargs["execution_mode"] == expected_execution_mode
if expected_query_override is not None:
assert call_kwargs["query_override"] == expected_query_override
else:
diff --git a/products/slack_app/backend/temporal/activities/unfurl_charts.py b/products/slack_app/backend/temporal/activities/unfurl_charts.py
index 94ea31947a9b..98159686269f 100644
--- a/products/slack_app/backend/temporal/activities/unfurl_charts.py
+++ b/products/slack_app/backend/temporal/activities/unfurl_charts.py
@@ -2,6 +2,7 @@ from datetime import UTC, datetime
from temporalio import activity
+from posthog.hogql_queries.query_runner import ExecutionMode
from posthog.temporal.common.utils import close_db_connections
from products.exports.backend.facade.api import create_png_export, get_delivery_image_url, png_export_has_content
@@ -34,6 +35,8 @@ def prepare_slack_unfurl_charts_activity(inputs: SlackAppUnfurlChartsWorkflowInp
query=chart.query,
is_system=True,
expires_after=expires_after,
+ # A recent cached result is fresh enough for a preview and saves a query per unfurl.
+ execution_mode=ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE,
)
except ValueError:
continue
diff --git a/products/slack_app/backend/temporal/tests/test_unfurl_charts_activities.py b/products/slack_app/backend/temporal/tests/test_unfurl_charts_activities.py
index 0c8a61058145..2d7fcc9a3dd5 100644
--- a/products/slack_app/backend/temporal/tests/test_unfurl_charts_activities.py
+++ b/products/slack_app/backend/temporal/tests/test_unfurl_charts_activities.py
@@ -3,11 +3,16 @@ from unittest.mock import MagicMock, patch
from parameterized import parameterized
+from posthog.hogql_queries.query_runner import ExecutionMode
from posthog.models.integration import Integration
+from products.exports.backend.facade.api import export_execution_mode
from products.exports.backend.models.exported_asset import ExportedAsset
from products.product_analytics.backend.facade.models import Insight
-from products.slack_app.backend.temporal.activities.unfurl_charts import post_slack_unfurl_charts_activity
+from products.slack_app.backend.temporal.activities.unfurl_charts import (
+ post_slack_unfurl_charts_activity,
+ prepare_slack_unfurl_charts_activity,
+)
from products.slack_app.backend.temporal.types import (
SlackAppUnfurlChartsPostInput,
SlackAppUnfurlChartsWorkflowInputs,
@@ -18,6 +23,31 @@ from products.slack_app.backend.temporal.types import (
_URL = "https://us.posthog.com/project/1/insights/abc123"
+class TestPrepareSlackUnfurlChartsActivity(BaseTest):
+ def test_saved_insight_render_prefers_recent_cache(self) -> None:
+ insight = Insight.objects.create(team=self.team, short_id="abc123", name="Weekly active users", saved=True)
+ request = SlackAppUnfurlChartsWorkflowInputs(
+ team_id=self.team.pk,
+ integration_id=1,
+ user_id=self.user.pk,
+ slack_user_id="U1",
+ channel="C1",
+ message_ts="123.456",
+ unfurl_id=None,
+ source=None,
+ unfurls={},
+ charts=[SlackUnfurlChartRequest(url=_URL, insight_id=insight.id, title="Weekly active users")],
+ export_task_queue="unused",
+ )
+
+ prepared = prepare_slack_unfurl_charts_activity(request)
+
+ assert len(prepared.assets) == 1
+ asset = ExportedAsset.objects.get(id=prepared.assets[0].asset_id)
+ assert asset.insight_id == insight.id
+ assert export_execution_mode(asset.export_context) == ExecutionMode.RECENT_CACHE_CALCULATE_BLOCKING_IF_STALE
+
+
class TestPostSlackUnfurlChartsActivity(BaseTest):
@parameterized.expand([("rendered", None, True), ("render_failed", "Export failed", False)])
@patch("products.slack_app.backend.logic.link_unfurl_charts.SlackIntegration")