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.

feat(today): draw charted sql insights as pulse tiles (#119148)

Shy Altere018950bfd6f
diff --git a/products/product_analytics/backend/insight_series.py b/products/product_analytics/backend/insight_series.py
index d1e40c4d6bbf..e8063bbf190e 100644
--- a/products/product_analytics/backend/insight_series.py
+++ b/products/product_analytics/backend/insight_series.py
@@ -7,6 +7,7 @@ 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.models import Team, User
+from posthog.week_start_day import WeekStartDay
from products.product_analytics.backend.facade.contracts import InsightDashboardContext, InsightSeries
@@ -27,6 +28,8 @@ UNCHARTED_DISPLAYS = {
"ActionsLineGraphCumulative",
"SlopeGraph",
}
+CHARTED_SQL_DISPLAYS = {"ActionsLineGraph", "ActionsBar", "ActionsStackedBar", "ActionsAreaGraph"}
+NO_BREAKDOWN_VALUE = "[No value]"
LOOKBACK = {"day": "-56d", "week": "-16w", "month": "-24m"}
DATE_FILTERS = ("date_from", "date_to", "explicitDate", "interval", "compareFilter")
EXECUTION_MODE = ExecutionMode.RECENT_CACHE_CALCULATE_ASYNC_IF_STALE
@@ -79,6 +82,104 @@ def _visible(trends: dict[str, Any], results: list[dict[str, Any]]) -> list[dict
]
+def _decimal_places(places: Any) -> int | None:
+ return None if places is None else min(max(int(places), 0), 9)
+
+
+def _axis_settings(axis: dict[str, Any], key: str) -> dict[str, Any]:
+ return (axis.get("settings") or {}).get(key) or {}
+
+
+def _sql_period(days: list[date], week_start_day: int | None) -> str | None:
+ gaps = {(later - earlier).days for earlier, later in zip(days, days[1:])}
+ if not gaps:
+ return None
+ if min(gaps) == 1:
+ return "day"
+ first_weekday = 0 if week_start_day == WeekStartDay.MONDAY else 6
+ if gaps == {7} and all(day.weekday() == first_weekday for day in days):
+ return "week"
+ months = [day.year * 12 + day.month for day in days]
+ if all(day.day == 1 for day in days) and all(later - earlier == 1 for earlier, later in zip(months, months[1:])):
+ return "month"
+ return None
+
+
+def _sql_series(
+ team: Team,
+ user: User,
+ *,
+ short_id: str,
+ name: str | None,
+ query: dict[str, Any],
+ filters: dict[str, Any],
+ variables: dict[str, Any] | None,
+) -> InsightSeries | None:
+ settings = query.get("chartSettings") or {}
+ x_column = (settings.get("xAxis") or {}).get("column")
+ y_axes = [axis for axis in settings.get("yAxis") or [] if axis.get("column")]
+ if query.get("display") not in CHARTED_SQL_DISPLAYS or not x_column or not y_axes:
+ return None
+ response = _run(team, user, query["source"], filters, variables)
+ if response.get("hasMore"):
+ return None
+ position = {column: index for index, column in enumerate(response.get("columns") or [])}
+ breakdown_column = settings.get("seriesBreakdownColumn")
+ zero_fill = bool(settings.get("showNullsAsZero"))
+ series: dict[tuple[int, tuple[bool, str] | None], dict[date, float]] = {}
+ with_gaps: set[tuple[int, tuple[bool, str] | None]] = set()
+ try:
+ for row in response.get("results") or []:
+ stamp = str(row[position[x_column]])
+ if stamp[11:19] not in ("", "00:00:00"):
+ return None
+ day = date.fromisoformat(stamp[:10])
+ breakdown = None
+ if breakdown_column:
+ raw = row[position[breakdown_column]]
+ breakdown = (raw is None, NO_BREAKDOWN_VALUE if raw is None else str(raw))
+ for index, axis in enumerate(y_axes):
+ key = (index, breakdown)
+ points = series.setdefault(key, {})
+ if day in points:
+ return None
+ value = row[position[axis["column"]]]
+ if value is None and not zero_fill:
+ with_gaps.add(key)
+ points[day] = float(value or 0)
+ except (KeyError, TypeError, ValueError):
+ return None
+ series = {key: points for key, points in series.items() if key not in with_gaps}
+ period = _sql_period(sorted({day for points in series.values() for day in points}), team.week_start_day)
+ if period is None:
+ return None
+ named: dict[str, list[tuple[date, float]]] = {}
+ copies: dict[str, int] = {}
+ for (index, breakdown), points in series.items():
+ base = str(_axis_settings(y_axes[index], "display").get("label") or y_axes[index]["column"])
+ if breakdown is not None:
+ base = f"{base} · {breakdown[1]}" if len(y_axes) > 1 else breakdown[1]
+ label, copy = base, copies.get(base, 1)
+ while label in named:
+ copy += 1
+ label = f"{base} ({copy})"
+ copies[base] = copy
+ named[label] = sorted(points.items())
+ formatting = _axis_settings(y_axes[0], "formatting") if len(y_axes) == 1 else {}
+ return InsightSeries(
+ short_id=short_id,
+ name=name,
+ series=named,
+ period=period,
+ averaged_series=list(named),
+ event_name="",
+ value_format="percentage_scaled" if formatting.get("style") == "percent" else None,
+ value_prefix=formatting.get("prefix") or None,
+ value_postfix=formatting.get("suffix") or None,
+ decimal_places=_decimal_places(formatting.get("decimalPlaces")),
+ )
+
+
def insight_series(
team: Team,
user: User,
@@ -97,6 +198,8 @@ def insight_series(
variables = dashboard.variables
filters = {key: value for key, value in dashboard_filters.items() if key not in DATE_FILTERS}
source = query.get("source", query)
+ if query.get("kind") == "DataVisualizationNode" and source.get("kind") == "HogQLQuery":
+ return _sql_series(team, user, short_id=short_id, name=name, query=query, filters=filters, variables=variables)
trends = source.get("trendsFilter") or {}
if source.get("kind") != "TrendsQuery" or trends.get("display") in UNCHARTED_DISPLAYS:
return None
@@ -142,7 +245,6 @@ def insight_series(
return None
nodes = source.get("series") or []
node = nodes[0] if len(nodes) == 1 else {}
- places = trends.get("decimalPlaces")
return InsightSeries(
short_id=short_id,
name=name,
@@ -153,5 +255,5 @@ def insight_series(
value_format=trends.get("aggregationAxisFormat"),
value_prefix=trends.get("aggregationAxisPrefix"),
value_postfix=trends.get("aggregationAxisPostfix"),
- decimal_places=None if places is None else min(max(int(places), 0), 9),
+ decimal_places=_decimal_places(trends.get("decimalPlaces")),
)
diff --git a/products/product_analytics/backend/tests/test_insight_series.py b/products/product_analytics/backend/tests/test_insight_series.py
index 518fc10139b9..8b9e5c504e54 100644
--- a/products/product_analytics/backend/tests/test_insight_series.py
+++ b/products/product_analytics/backend/tests/test_insight_series.py
@@ -12,6 +12,7 @@ from parameterized import parameterized
from posthog.constants import AvailableFeature
from posthog.hogql_queries.query_runner import ExecutionMode
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.product_analytics.backend import insight_series
@@ -109,6 +110,209 @@ class TestSeriesNames(SimpleTestCase):
self.assertEqual({name: name in found.averaged_series for name in found.series}, expected)
+def _sql_query(display: str = "ActionsLineGraph", **chart_settings: Any) -> dict[str, Any]:
+ return {
+ "kind": "DataVisualizationNode",
+ "source": {"kind": "HogQLQuery", "query": "SELECT 1"},
+ "display": display,
+ "chartSettings": {"xAxis": {"column": "week"}, "yAxis": [{"column": "p50"}], **chart_settings},
+ }
+
+
+def _weekly(*rows: list[Any]) -> dict[str, Any]:
+ return {"columns": ["week", "p50"], "results": list(rows)}
+
+
+class TestSqlSeries(SimpleTestCase):
+ @parameterized.expand(
+ [
+ (
+ "a weekly breakdown names each series by its column and value",
+ _sql_query(yAxis=[{"column": "p50"}, {"column": "p90"}], seriesBreakdownColumn="kind"),
+ {
+ "columns": ["week", "kind", "p50", "p90"],
+ "results": [["2026-10-04", "html", 1, 2], ["2026-10-11", "html", 3, 4]],
+ },
+ WeekStartDay.SUNDAY,
+ ("week", {"p50 · html": [1.0, 3.0], "p90 · html": [2.0, 4.0]}, None, None),
+ ),
+ (
+ "a null breakdown value stays apart from the text None",
+ _sql_query(seriesBreakdownColumn="kind"),
+ {
+ "columns": ["week", "kind", "p50"],
+ "results": [
+ ["2026-10-04", None, 1],
+ ["2026-10-04", "None", 2],
+ ["2026-10-11", None, 3],
+ ["2026-10-11", "None", 4],
+ ],
+ },
+ WeekStartDay.SUNDAY,
+ ("week", {"[No value]": [1.0, 3.0], "None": [2.0, 4.0]}, None, None),
+ ),
+ (
+ "a series with a null leaves the tile and keeps the others",
+ _sql_query(yAxis=[{"column": "p50"}, {"column": "p90"}]),
+ {
+ "columns": ["week", "p50", "p90"],
+ "results": [["2026-10-04", 1, 2], ["2026-10-11", 3, None]],
+ },
+ WeekStartDay.SUNDAY,
+ ("week", {"p50": [1.0, 3.0]}, None, None),
+ ),
+ (
+ "a chart that shows nulls as zero keeps the zero",
+ _sql_query(showNullsAsZero=True),
+ _weekly(["2026-10-04", 1], ["2026-10-11", None]),
+ WeekStartDay.SUNDAY,
+ ("week", {"p50": [1.0, 0.0]}, None, None),
+ ),
+ (
+ "midnight timestamps are days",
+ _sql_query(),
+ _weekly(["2026-10-04T00:00:00-07:00", 1], ["2026-10-11T00:00:00-07:00", 2]),
+ WeekStartDay.SUNDAY,
+ ("week", {"p50": [1.0, 2.0]}, None, None),
+ ),
+ (
+ "hourly timestamps are not days",
+ _sql_query(),
+ _weekly(["2026-10-01T10:00:00", 1], ["2026-10-02T13:00:00", 2]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "two columns that share a label keep both series",
+ _sql_query(
+ yAxis=[
+ {"column": "a", "settings": {"display": {"label": "Users"}}},
+ {"column": "b", "settings": {"display": {"label": "Users"}}},
+ {"column": "c", "settings": {"display": {"label": "Users"}}},
+ ]
+ ),
+ {"columns": ["week", "a", "b", "c"], "results": [["2026-10-04", 1, 5, 7], ["2026-10-11", 2, 6, 8]]},
+ WeekStartDay.SUNDAY,
+ ("week", {"Users": [1.0, 2.0], "Users (2)": [5.0, 6.0], "Users (3)": [7.0, 8.0]}, None, None),
+ ),
+ (
+ "weeks that start on Monday in a Monday project",
+ _sql_query(),
+ _weekly(["2026-09-28", 1], ["2026-10-05", 2]),
+ WeekStartDay.MONDAY,
+ ("week", {"p50": [1.0, 2.0]}, None, None),
+ ),
+ (
+ "weeks that start on Monday in a Sunday project",
+ _sql_query(),
+ _weekly(["2026-09-28", 1], ["2026-10-05", 2]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "a monthly series keeps its label and currency prefix",
+ _sql_query(
+ xAxis={"column": "month"},
+ yAxis=[
+ {
+ "column": "revenue",
+ "settings": {"display": {"label": "Revenue"}, "formatting": {"prefix": "$"}},
+ }
+ ],
+ ),
+ {"columns": ["month", "revenue"], "results": [["2026-08-01", 10], ["2026-09-01", 12]]},
+ WeekStartDay.SUNDAY,
+ ("month", {"Revenue": [10.0, 12.0]}, "$", None),
+ ),
+ (
+ "a percent column is scaled like the chart",
+ _sql_query(yAxis=[{"column": "rate", "settings": {"formatting": {"style": "percent"}}}]),
+ {"columns": ["week", "rate"], "results": [["2026-10-04", 0.2], ["2026-10-11", 0.25]]},
+ WeekStartDay.SUNDAY,
+ ("week", {"rate": [0.2, 0.25]}, None, "percentage_scaled"),
+ ),
+ (
+ "a table",
+ _sql_query(display="ActionsTable"),
+ _weekly(),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "a truncated result",
+ _sql_query(),
+ {**_weekly(["2026-10-04", 1], ["2026-10-11", 2]), "hasMore": True},
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "rows that repeat a date",
+ _sql_query(),
+ _weekly(["2026-10-04", 1], ["2026-10-04", 2]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "an x axis that is not a date",
+ _sql_query(),
+ _weekly(["html", 1], ["react", 2]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "dates without a regular interval",
+ _sql_query(),
+ _weekly(["2026-10-04", 1], ["2026-10-07", 2]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "a missing week",
+ _sql_query(),
+ _weekly(["2026-09-20", 1], ["2026-09-27", 2], ["2026-10-11", 3]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ (
+ "a missing month",
+ _sql_query(),
+ _weekly(["2026-07-01", 1], ["2026-09-01", 2]),
+ WeekStartDay.SUNDAY,
+ None,
+ ),
+ ]
+ )
+ def test_sql_insight_series(
+ self,
+ _name: str,
+ query: dict[str, Any],
+ response: dict[str, Any],
+ week_start_day: WeekStartDay,
+ expected: tuple[str, dict[str, list[float]], str | None, str | None] | None,
+ ) -> None:
+ with patch.object(insight_series, "_run", return_value=response):
+ found = insight_series.insight_series(
+ Team(week_start_day=week_start_day),
+ cast(User, None),
+ short_id="abc",
+ name="Build time",
+ query=query,
+ dashboard=None,
+ )
+
+ summary = (
+ None
+ if found is None
+ else (
+ found.period,
+ {name: [v for _, v in points] for name, points in found.series.items()},
+ found.value_prefix,
+ found.value_format,
+ )
+ )
+ self.assertEqual(summary, expected)
+
+
class TestGetInsightSeries(ClickhouseTestMixin, BaseTest):
def setUp(self) -> None:
super().setUp()
@@ -166,6 +370,32 @@ class TestGetInsightSeries(ClickhouseTestMixin, BaseTest):
("day", ["Daily people"], "percentage", 9, {"Sign-ups": (56, 3.0), "Daily people": (56, 2.0)}),
)
+ def test_a_charted_sql_insight_returns_its_daily_series(self) -> None:
+ self._signups((2, "Chrome"), (2, "Chrome"), (3, "Chrome"))
+ insight = Insight.objects.create(
+ team=self.team,
+ name="Sign-ups",
+ saved=True,
+ query={
+ "kind": "DataVisualizationNode",
+ "source": {
+ "kind": "HogQLQuery",
+ "query": "SELECT toStartOfDay(timestamp) AS day, count() AS signups FROM events "
+ "WHERE event = 'signed up' GROUP BY day ORDER BY day",
+ },
+ "display": "ActionsLineGraph",
+ "chartSettings": {"xAxis": {"column": "day"}, "yAxis": [{"column": "signups"}]},
+ },
+ )
+
+ found = get_insight_series(team=self.team, user=self.user, short_id=insight.short_id)
+
+ assert found is not None
+ self.assertEqual(
+ (found.period, found.averaged_series, found.series),
+ ("day", ["signups"], {"signups": [(self._ago(3).date(), 1.0), (self._ago(2).date(), 2.0)]}),
+ )
+
def test_a_smoothed_trend_returns_its_raw_daily_counts(self) -> None:
self._signups((9, "Chrome"), (10, "Chrome"), (11, "Chrome"))
insight = self._insight(