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.

refactor(insights): resolve insight series customizations from the query (#120257)

Thomas Obermüller12880ed1ec95
diff --git a/products/product_analytics/backend/insight_series.py b/products/product_analytics/backend/insight_series.py
index e188489f686e..bdd0ca1a78ab 100644
--- a/products/product_analytics/backend/insight_series.py
+++ b/products/product_analytics/backend/insight_series.py
@@ -1,4 +1,5 @@
import json
+from copy import deepcopy
from datetime import date
from typing import Any
@@ -6,7 +7,15 @@ from pydantic import BaseModel
from posthog.hogql_queries.apply_dashboard_filters import resolve_effective_dashboard_filters
from posthog.hogql_queries.query_runner import ExecutionMode
+from posthog.hogql_queries.utils.result_series import (
+ get_action_names,
+ get_result_series,
+ get_result_series_index,
+ get_series_custom_name,
+ get_series_key,
+)
from posthog.models import Team, User
+from posthog.schema_migrations.upgrade import upgrade
from posthog.week_start_day import WeekStartDay
from products.product_analytics.backend.facade.contracts import InsightDashboardContext, InsightSeries
@@ -57,10 +66,10 @@ def _run(
return response.model_dump() if isinstance(response, BaseModel) else response
-def _value_key(result: dict[str, Any], position: int) -> str:
- order = (result.get("action") or {}).get("order")
+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] = {
- "series": order if isinstance(order, int) else f"formula{position + 1}" if position else "formula"
+ "series": index if index is not None else f"formula{position + 1}" if position else "formula"
}
if "breakdown_value" in result:
breakdown = result["breakdown_value"]
@@ -68,18 +77,20 @@ def _value_key(result: dict[str, Any], position: int) -> str:
return json.dumps(payload, separators=(",", ":"), ensure_ascii=False)
-def _visible(trends: dict[str, Any], results: list[dict[str, Any]]) -> list[dict[str, Any]]:
+def _visible(query: dict[str, Any], results: list[dict[str, Any]]) -> list[dict[str, Any]]:
+ trends = query.get("trendsFilter") or {}
customizations = trends.get("resultCustomizations") or {}
if not customizations:
return results
by_position = trends.get("resultCustomizationBy") == "position"
- ranked = sorted(range(len(results)), key=lambda i: (results[i].get("action") or {}).get("order") or 0)
+ ranked = sorted(range(len(results)), key=lambda i: get_result_series_index(query, results[i]) or 0)
positions = {index: rank for rank, index in enumerate(ranked)}
return [
result
for index, result in enumerate(results)
if not (
- customizations.get(str(positions[index]) if by_position else _value_key(result, positions[index])) or {}
+ customizations.get(str(positions[index]) if by_position else _value_key(query, result, positions[index]))
+ or {}
).get("hidden")
]
@@ -232,18 +243,23 @@ def insight_series(
response = _run(team, user, bounded, filters, variables, blocking)
formula = bool(trends.get("formula") or trends.get("formulas") or trends.get("formulaNodes"))
several = len(source.get("series") or []) > 1
+ # `_run` executes the upgraded query, which stamps `kind` on series saved without one.
+ series_source = upgrade(deepcopy(source))
+ action_names = get_action_names(team, series_source)
series: dict[str, list[tuple[date, float]]] = {}
averaged: list[str] = []
- for result in _visible(trends, response.get("results") or []):
+ for result in _visible(series_source, response.get("results") or []):
if not result.get("days"):
continue
- action = result.get("action") or {}
+ node = get_result_series(series_source, result)
shown = str(result.get("label") or "Series")
- label = str(action.get("custom_name") or action.get("name") or shown)
+ series_label = (get_series_key(node, action_names) or "All events") if node is not None else None
+ custom_name = get_series_custom_name(node, action_names) if node is not None else None
+ label = str(custom_name or series_label or shown)
breakdown = result.get("breakdown_value")
if breakdown not in (None, ""):
- if action:
- value = shown.removeprefix(f"{action.get('name')} - ") if several else shown
+ if series_label is not None:
+ value = shown.removeprefix(f"{series_label} - ") if several else shown
else:
value = ", ".join(str(b) for b in breakdown) if isinstance(breakdown, list) else str(breakdown)
label = f"{label} · {value}" if several else value
@@ -252,7 +268,7 @@ def insight_series(
series[label] = [
(date.fromisoformat(str(d)[:10]), float(v or 0)) for d, v in zip(result["days"], result["data"])
]
- if formula or (action.get("math") or "total") not in ADDITIVE_MATH:
+ if formula or ((node or {}).get("math") or "total") not in ADDITIVE_MATH:
averaged.append(label)
if not series:
return None
diff --git a/products/product_analytics/backend/tests/test_insight_series.py b/products/product_analytics/backend/tests/test_insight_series.py
index 8b9e5c504e54..db977e17c275 100644
--- a/products/product_analytics/backend/tests/test_insight_series.py
+++ b/products/product_analytics/backend/tests/test_insight_series.py
@@ -4,7 +4,7 @@ from typing import Any, cast
from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, _create_person, flush_persons_and_events
from unittest.mock import patch
-from django.test import SimpleTestCase
+from django.test import SimpleTestCase, override_settings
from django.utils import timezone
from parameterized import parameterized
@@ -15,6 +15,7 @@ from posthog.models import Team, User
from posthog.week_start_day import WeekStartDay
from products.access_control.backend.facade.testing import create_access_control
+from products.actions.backend.models.action import Action
from products.product_analytics.backend import insight_series
from products.product_analytics.backend.facade.api import get_insight_series
from products.product_analytics.backend.facade.contracts import InsightDashboardContext
@@ -29,38 +30,58 @@ class TestVisibleResults(SimpleTestCase):
"by value",
None,
{'{"series":1,"breakdown_value":["Chrome"]}': {"hidden": True}},
+ None,
["0 Chrome", "1 Safari"],
),
- ("by position", "position", {"2": {"hidden": True}}, ["0 Chrome", "1 Chrome"]),
+ ("by position", "position", {"2": {"hidden": True}}, None, ["0 Chrome", "1 Chrome"]),
(
"a visible customization",
None,
{'{"series":0,"breakdown_value":["Chrome"]}': {"color": "preset-2"}},
+ None,
+ ["0 Chrome", "1 Chrome", "1 Safari"],
+ ),
+ (
+ "a formula row is keyed by its formula, not the series at its order",
+ None,
+ {'{"series":1,"breakdown_value":["Chrome"]}': {"hidden": True}},
+ [{"formula": "A+B"}, {"formula": "A-B"}],
["0 Chrome", "1 Chrome", "1 Safari"],
),
]
)
def test_hidden_trend_results_are_left_out(
- self, _name: str, by: str | None, customizations: dict[str, Any], expected: list[str]
+ self,
+ _name: str,
+ by: str | None,
+ customizations: dict[str, Any],
+ formula_nodes: list[dict[str, Any]] | None,
+ expected: list[str],
) -> None:
results = [
- {"action": {"order": order}, "breakdown_value": browser}
+ {"order": order, "breakdown_value": browser}
for order, browser in ((1, "Chrome"), (1, "Safari"), (0, "Chrome"))
]
+ query = {
+ "kind": "TrendsQuery",
+ "series": [{"kind": "EventsNode", "event": "signed up"}, {"kind": "EventsNode", "event": "logged in"}],
+ "trendsFilter": {
+ "resultCustomizationBy": by,
+ "resultCustomizations": customizations,
+ "formulaNodes": formula_nodes,
+ },
+ }
- visible = _visible({"resultCustomizationBy": by, "resultCustomizations": customizations}, results)
+ visible = _visible(query, results)
- self.assertEqual(sorted(f"{r['action']['order']} {r['breakdown_value']}" for r in visible), expected)
+ self.assertEqual(sorted(f"{r['order']} {r['breakdown_value']}" for r in visible), expected)
-def _result(label: str, breakdown: Any = None, **action: Any) -> dict[str, Any]:
- return {
- "action": {"name": "signed up", "order": 0, **action},
- "label": label,
- "breakdown_value": breakdown,
- "days": ["2026-10-12"],
- "data": [1],
- }
+SIGNED_UP = {"kind": "EventsNode", "event": "signed up", "name": "signed up"}
+
+
+def _result(label: str, order: int = 0, breakdown: Any = None) -> dict[str, Any]:
+ return {"label": label, "order": order, "breakdown_value": breakdown, "days": ["2026-10-12"], "data": [1]}
class TestSeriesNames(SimpleTestCase):
@@ -68,33 +89,36 @@ class TestSeriesNames(SimpleTestCase):
[
(
"a cohort breakdown shows the cohort name",
- 1,
- [_result("Enterprise", 12), _result("Free", 13)],
+ [SIGNED_UP],
+ [_result("Enterprise", breakdown=12), _result("Free", breakdown=13)],
{"Enterprise": False, "Free": False},
),
(
"several series name each series once",
- 2,
- [
- _result("signed up - Chrome", "Chrome"),
- _result("logged in - Chrome", "Chrome", name="logged in", custom_name="Logins"),
- ],
+ [SIGNED_UP, {"kind": "EventsNode", "event": "logged in", "name": "logged in", "custom_name": "Logins"}],
+ [_result("signed up - Chrome", 0, "Chrome"), _result("logged in - Chrome", 1, "Chrome")],
{"signed up · Chrome": False, "Logins · Chrome": False},
),
+ (
+ "a series saved without a kind takes its event",
+ [{"event": "signed up"}],
+ [_result("signed up")],
+ {"signed up": False},
+ ),
(
"first group occurrences add up",
- 3,
[
- _result("signed up", math="first_time_for_group"),
- _result("signed up", math="first_matching_event_for_group", custom_name="Matches"),
- _result("signed up", math="dau", custom_name="Daily people"),
+ {**SIGNED_UP, "math": "first_time_for_group"},
+ {**SIGNED_UP, "math": "first_matching_event_for_group", "custom_name": "Matches"},
+ {**SIGNED_UP, "math": "dau", "custom_name": "Daily people"},
],
+ [_result("signed up", 0), _result("signed up", 1), _result("signed up", 2)],
{"signed up": False, "Matches": False, "Daily people": True},
),
]
)
def test_series_names_and_averaging(
- self, _name: str, series_count: int, results: list[dict[str, Any]], expected: dict[str, bool]
+ self, _name: str, series: list[dict[str, Any]], results: list[dict[str, Any]], expected: dict[str, bool]
) -> None:
with patch.object(insight_series, "_run", return_value={"results": results}):
found = insight_series.insight_series(
@@ -102,7 +126,7 @@ class TestSeriesNames(SimpleTestCase):
cast(User, None),
short_id="abc",
name="Sign-ups",
- query={"kind": "TrendsQuery", "series": [{}] * series_count},
+ query={"kind": "TrendsQuery", "series": series},
dashboard=None,
)
@@ -346,12 +370,15 @@ class TestGetInsightSeries(ClickhouseTestMixin, BaseTest):
query={"kind": "InsightVizNode", "source": {"kind": "TrendsQuery", **query}},
)
+ @override_settings(IN_UNIT_TESTING=True)
def test_a_daily_trend_returns_each_series_with_its_averaging_and_display_format(self) -> None:
self._signups((2, "Chrome"), (2, "Chrome"), (9, "Chrome"))
+ action = Action.objects.create(team=self.team, name="Signed up action", steps_json=[{"event": "signed up"}])
insight = self._insight(
series=[
{"kind": "EventsNode", "event": "signed up", "custom_name": "Sign-ups"},
{"kind": "EventsNode", "event": "signed up", "math": "dau", "custom_name": "Daily people"},
+ {"kind": "ActionsNode", "id": action.pk},
],
trendsFilter={"aggregationAxisFormat": "percentage", "display": "Metric", "decimalPlaces": 1_000_000},
)
@@ -367,7 +394,13 @@ class TestGetInsightSeries(ClickhouseTestMixin, BaseTest):
found.decimal_places,
{name: (len(points), sum(v for _, v in points)) for name, points in found.series.items()},
),
- ("day", ["Daily people"], "percentage", 9, {"Sign-ups": (56, 3.0), "Daily people": (56, 2.0)}),
+ (
+ "day",
+ ["Daily people"],
+ "percentage",
+ 9,
+ {"Sign-ups": (56, 3.0), "Daily people": (56, 2.0), "Signed up action": (56, 3.0)},
+ ),
)
def test_a_charted_sql_insight_returns_its_daily_series(self) -> None: