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(today): wait for the recalculation when an insight's cached result is stale (#130309)

Shy Alter8414a34b0ff2
diff --git a/products/product_analytics/backend/insight_series.py b/products/product_analytics/backend/insight_series.py
index 99b19cab466c..b8e1260dd7eb 100644
--- a/products/product_analytics/backend/insight_series.py
+++ b/products/product_analytics/backend/insight_series.py
@@ -106,12 +106,11 @@ def _run(
status = result.get("query_status") or {}
if raise_on_pending and (result.get("error") or status.get("error")):
raise ValueError("Insight query failed")
- if raise_on_pending and result.get("results") is None:
+ if raise_on_pending and _pending(result, status):
if not status.get("id"):
raise ValueError("Insight query returned no results or query status")
raise InsightSeriesNotReady(status["id"])
- if blocking and wait_until is not None and result.get("results") is None:
- status = result.get("query_status") or {}
+ if blocking and wait_until is not None and _pending(result, status):
while status and not status.get("complete") and not status.get("error"):
remaining = wait_until - monotonic()
if remaining <= 0:
@@ -123,6 +122,12 @@ def _run(
return result
+def _pending(result: dict[str, Any], status: dict[str, Any]) -> bool:
+ return result.get("results") is None or bool(
+ status.get("id") and not status.get("complete") and not status.get("error")
+ )
+
+
def _value_key(query: dict[str, Any], result: dict[str, Any], position: int) -> str:
index = get_result_series_index(query, result)
payload: dict[str, Any] = {
diff --git a/products/product_analytics/backend/tests/test_insight_series.py b/products/product_analytics/backend/tests/test_insight_series.py
index b886aae2de79..3018d01705ce 100644
--- a/products/product_analytics/backend/tests/test_insight_series.py
+++ b/products/product_analytics/backend/tests/test_insight_series.py
@@ -50,6 +50,12 @@ class TestBoundedInsightQuery(SimpleTestCase):
@parameterized.expand(
[
("pending", {"query_status": {"id": "q", "complete": False}}, InsightSeriesNotReady),
+ (
+ "stale_cache_recalculating",
+ {"results": [], "query_status": {"id": "q", "complete": False}},
+ InsightSeriesNotReady,
+ ),
+ ("fresh_cache", {"results": [], "query_status": {"id": "q", "complete": True}}, None),
("error", {"query_status": {"id": "q", "error": True}}, ValueError),
("empty", {"results": []}, None),
("top_level_error", {"results": None, "error": "Invalid query"}, ValueError),
@@ -117,6 +123,33 @@ class TestBoundedInsightQuery(SimpleTestCase):
self.assertLessEqual(clock[0], 0.6)
self.assertEqual(run.call_args.kwargs["execution_mode"], ExecutionMode.RECENT_CACHE_CALCULATE_ASYNC_IF_STALE)
+ def test_a_wait_replaces_a_stale_cached_result_with_the_recalculated_one(self) -> None:
+ stale = {"results": [_result("signed up")], "query_status": {"id": "query-1", "complete": False}}
+ fresh = {"results": [{**_result("signed up"), "data": [5]}]}
+
+ with (
+ patch("posthog.api.services.query.process_query_dict", return_value=stale),
+ patch.object(insight_series, "monotonic", return_value=0.0),
+ patch.object(insight_series, "sleep"),
+ patch.object(
+ insight_series,
+ "get_query_status",
+ return_value=QueryStatus(id="query-1", team_id=1, complete=True, results=fresh),
+ ),
+ ):
+ found = insight_series.insight_series(
+ cast(Team, SimpleNamespace(pk=1)),
+ cast(User, None),
+ short_id="abc",
+ name="Sign-ups",
+ query={"kind": "TrendsQuery", "series": [SIGNED_UP]},
+ dashboard=None,
+ blocking=True,
+ wait_until=0.6,
+ )
+
+ self.assertEqual([value for points in (found.series if found else {}).values() for _, value in points], [5.0])
+
class TestVisibleResults(SimpleTestCase):
@parameterized.expand(