: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
feat(alerts): show threshold lines in chart exports (#123535)
Matt Puaefcf63331a82
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 2bc8efad02fc..20c84e712b85 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -3968,6 +3968,10 @@ snapshots:hash: v1.k794b7964.77db5fd39abf5b41db372ff16af2044f3c03b303aabe0d592f6c25de1898339a.3VJBqA15CtN76iDMdubH40FmnriyLRFCW_mGRXdYrsMinsights-funnellinechart--with-legend--light:hash: v1.k794b7964.d3d505f78dc4785055c98a602b0edd9c39549001dab1a3f1c3bf753f1cff60be.Lu6z04Zq2foZ5axwQK8Hg4ys33BYX2OIMjQkpYBycKM+ insights-funnelstepsbarchart--alert-thresholds--dark:+ hash: v1.k794b7964.f07a01625198eb67f2bf3709a2b562abf1e5c9fb6e0ad50d32bc3c0455810e4c.kcHPaKTNWm1MsjfK7ZXC-YOSHARImNlxhU63hmrI9zk+ insights-funnelstepsbarchart--alert-thresholds--light:+ hash: v1.k794b7964.1615420c253f38e627b2d08757e17d1bd748369b520353077fb7cb69b60ce73d.fckv-4nelywj6ArxmnSQa8Xt5h6ZySRMvXTpCuoTF-Ainsights-funnelstepsbarchart--bar-labels--dark:hash: v1.k794b7964.5de2ee4fdd7ba021e1af1363b1e7977b10a245ce3ae6486008b83552cc41296d.tMGnyCUxbncHrGz42VrPrOAdXqsv6oqpZP2N6hjMW48insights-funnelstepsbarchart--bar-labels--light:diff --git a/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsx b/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsxindex 769f58f5d362..ef0a67e5bb51 100644--- a/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsx+++ b/packages/quill/packages/charts/src/charts/BarList/StackedBarList.tsx@@ -41,6 +41,7 @@ export interface StackedBarListProps<Meta = unknown> {onPointClick?: (data: PointClickData<Meta>) => voiddataAttr?: stringonError?: (error: Error, info: React.ErrorInfo) => void+ children?: React.ReactNode}const DEFAULT_ROW_HEIGHT = { start: 28, top: 40 }@@ -71,6 +72,7 @@ function StackedBarListInner<Meta = unknown>({onPointClick,dataAttr,onError,+ children,}: StackedBarListProps<Meta>): React.ReactElement {const labelPosition = config?.labelPosition ?? 'start'const {@@ -135,7 +137,9 @@ function StackedBarListInner<Meta = unknown>({onPointClick={onPointClick}dataAttr={dataAttr}onError={onError}- />+ >+ {children}+ </BarChart>) : nullif (labelPosition === 'top') {diff --git a/packages/quill/packages/charts/src/docs/overlays.md b/packages/quill/packages/charts/src/docs/overlays.mdindex 54f7ce294cd1..0e80a7adcbe5 100644--- a/packages/quill/packages/charts/src/docs/overlays.md+++ b/packages/quill/packages/charts/src/docs/overlays.md@@ -10,6 +10,8 @@ They compose as children of a chart:</TimeSeriesLineChart>```+`StackedBarList` also accepts overlay children. It places them in the chart area, which excludes the label and value cells.+Trend lines, moving averages, and confidence bands are not overlays.They are derived `Series` entries (`overlay: true` for the first two), so the legend, the tooltip, and the axis already know about them.diff --git a/posthog/api/sharing.py b/posthog/api/sharing.pyindex f57f9a239cf4..aad37d93db16 100644--- a/posthog/api/sharing.py+++ b/posthog/api/sharing.py@@ -74,7 +74,7 @@ from products.dashboards.backend.access import dashboard_access_method, record_dfrom products.dashboards.backend.api.dashboard import DashboardSerializerfrom products.dashboards.backend.models.dashboard import Dashboardfrom products.exports.backend.api.exports import ExportedAssetCreateSerializer-from products.exports.backend.facade.api import export_limit_context+from products.exports.backend.facade.api import export_limit_context, query_with_reference_linesfrom products.exports.backend.models.exported_asset import (EXPORTED_ASSET_PURPOSE_RENDER,EXPORTED_ASSET_PURPOSE_SUBSCRIPTION_DELIVERY,@@ -1168,6 +1168,15 @@ class SharingViewerPageViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSe# Add hideExtraDetails to context so that PII related information is not returned to the clientinsight_context = {**context, "hide_extra_details": state.get("hideExtraDetails", False)}insight_data = InsightSerializer(resource.insight, many=False, context=insight_context).data+ if (+ is_export_render+ and isinstance(resource, ExportedAsset)+ and resource.export_context+ and resource.export_context.get("reference_lines")+ ):+ insight_data["query"] = query_with_reference_lines(+ insight_data.get("query") or {}, resource.export_context["reference_lines"]+ )exported_data.update({"insight": insight_data})exported_data.update({"themes": get_themes_for_team(resource.team)})exported_data.update({"cohorts": _collect_cohorts_for_sharing([resource.insight], resource.team)})diff --git a/posthog/api/test/test_sharing.py b/posthog/api/test/test_sharing.pyindex 0c32f2beab0b..b9976f88a795 100644--- a/posthog/api/test/test_sharing.py+++ b/posthog/api/test/test_sharing.py@@ -1614,6 +1614,24 @@ class TestExportCacheKeyFlow(APIBaseTest):timezone="UTC",)+ @parameterized.expand([("render", True), ("shared", False)])+ @patch("posthog.caching.calculate_results.calculate_for_query_based_insight")+ @mock_exporter_template+ def test_threshold_snapshot_is_only_used_by_the_export_renderer(+ self, _name: str, is_renderer: bool, mock_calculate: MagicMock+ ) -> None:+ self.insight.query = {"kind": "BIVisualizationNode"}+ self.insight.save(update_fields=["query"])+ self.exported_asset.export_context = {"reference_lines": [{"label": "Threshold snapshot", "value": 250}]}+ self.exported_asset.save(update_fields=["export_context"])+ self._calculated_result(mock_calculate)+ url = self._render_url("") if is_renderer else f"/shared/{self.sharing_config.access_token}"+ response = self.client.get(url)+ assert response.status_code == 200+ assert (b"Threshold snapshot" in response.content) is is_renderer+ self.insight.refresh_from_db()+ assert "chartSettings" not in self.insight.query+@patch("posthog.caching.calculate_results.calculate_for_query_based_insight")@patch("products.product_analytics.backend.presentation.insight.QueryCache")@mock_exporter_templatediff --git a/posthog/tasks/alerts/utils.py b/posthog/tasks/alerts/utils.pyindex f9ee2576966c..799577da00a6 100644--- a/posthog/tasks/alerts/utils.py+++ b/posthog/tasks/alerts/utils.py@@ -1,6 +1,7 @@from collections.abc import Collectionfrom contextlib import ExitStackfrom datetime import UTC, datetime, timedelta+from typing import Anyfrom zoneinfo import ZoneInfofrom django.db.models import Q@@ -448,10 +449,32 @@ def render_alert_insight_chart()if not any(destination.destination_type == "slack" for destination in destinations):return None+ alert_threshold_lines: list[dict[str, Any]] = []+ for insight_alert in (+ AlertConfiguration.objects.filter(team_id=alert.team_id, insight_id=alert.insight_id, enabled=True)+ .select_related("threshold")+ .order_by("created_at", "id")+ ):+ if insight_alert.threshold is None or insight_alert.condition.get("type") != "absolute_value":+ continue+ threshold_config = insight_alert.threshold.configuration+ if threshold_config.get("type") != "absolute":+ continue+ for side, value in (threshold_config.get("bounds") or {}).items():+ if side in {"upper", "lower"} and value is not None:+ alert_threshold_lines.append(+ {+ "label": f"{insight_alert.name} {side} threshold",+ "value": value,+ "borderColor": "#d14343",+ "displayLabel": False,+ }+ )asset, content = exports.render_png_export(team=alert.team,created_by=render_user,insight_id=alert.insight_id,+ reference_lines=alert_threshold_lines,# System render: keep it out of the user's export listings and quota.is_system=True,expires_after=datetime.now(UTC) + _INSIGHT_CHART_ASSET_TTL,diff --git a/products/exports/backend/facade/api.py b/products/exports/backend/facade/api.pyindex b9c6244c824b..940fc8119922 100644--- a/products/exports/backend/facade/api.py+++ b/products/exports/backend/facade/api.py@@ -1,7 +1,9 @@"""Public Python interface for creating and retrieving one-off exports."""import io+from copy import deepcopyfrom datetime import datetime, timedelta+from typing import Anyfrom django.conf import settingsfrom django.db.models import Q@@ -244,6 +246,7 @@ def _create_png_export_asset(is_system: bool = False,expires_after: datetime | None = None,execution_mode: ExecutionMode = ExecutionMode.CALCULATE_BLOCKING_ALWAYS,+ reference_lines: list[dict[str, Any]] | None = None,) -> ExportedAsset:if execution_mode not in _EXPORT_EXECUTION_MODES:raise ValueError(f"Unsupported execution mode for an export: {execution_mode}")@@ -277,6 +280,9 @@ def _create_png_export_asset(if execution_mode != ExecutionMode.CALCULATE_BLOCKING_ALWAYS:export_context = {**(export_context or {}), "execution_mode": execution_mode.value}+ if reference_lines:+ export_context = {**(export_context or {}), "reference_lines": reference_lines}+asset = ExportedAsset.objects.create(team=team,created_by=created_by,@@ -328,6 +334,28 @@ def create_png_export().id+def query_with_reference_lines(query: dict[str, Any], reference_lines: list[dict[str, Any]]) -> dict[str, Any]:+ query = deepcopy(query)+ target = query+ if query.get("kind") == "InsightVizNode":+ target = query.get("source") or {}+ settings_key = {+ "TrendsQuery": "trendsFilter",+ "FunnelsQuery": "funnelsFilter",+ "RetentionQuery": "retentionFilter",+ "DataVisualizationNode": "chartSettings",+ "BIVisualizationNode": "chartSettings",+ "MetricsQuery": "display",+ }.get(target.get("kind", ""))+ if settings_key is None:+ return query+ settings = target.get(settings_key) or {}+ target[settings_key] = settings+ existing = settings.get("goalLines") or []+ settings["goalLines"] = existing + [line for line in reference_lines if line not in existing]+ return query++def png_export_has_content(*, team_id: int, asset_id: int) -> bool:return (ExportedAsset.objects.filter(team_id=team_id, id=asset_id, export_format=ExportedAsset.ExportFormat.PNG)@@ -347,6 +375,7 @@ def render_png_export(dashboard_id: int | None = None,is_system: bool = False,expires_after: datetime | None = None,+ reference_lines: list[dict[str, Any]] | None = None,) -> tuple[ExportedAsset, bytes | None]:"""Render a PNG export synchronously and return the asset together with its content bytes.@@ -367,6 +396,7 @@ def render_png_export(dashboard_id=dashboard_id,is_system=is_system,expires_after=expires_after,+ reference_lines=reference_lines,)async def _run() -> None:diff --git a/products/exports/backend/tests/test_facade.py b/products/exports/backend/tests/test_facade.pyindex e4ad34438458..4a6cb7b8b8bf 100644--- a/products/exports/backend/tests/test_facade.py+++ b/products/exports/backend/tests/test_facade.py@@ -1,3 +1,4 @@+from copy import deepcopyfrom datetime import timedeltafrom posthog.test.base import BaseTest@@ -14,6 +15,7 @@ from products.dashboards.backend.models.dashboard import Dashboardfrom products.exports.backend.facade.api import (_validate_adhoc_export_context,get_delivery_image_url,+ query_with_reference_lines,render_png_export,)from products.exports.backend.models.exported_asset import ExportedAsset@@ -55,6 +57,72 @@ class TestValidateAdhocExportContext(SimpleTestCase):with self.assertRaises(ValueError):_validate_adhoc_export_context(export_context)+ @parameterized.expand(+ [+ (+ "trends",+ {+ "kind": "InsightVizNode",+ "source": {+ "kind": "TrendsQuery",+ "trendsFilter": {"goalLines": [{"label": "Target", "value": 100}]},+ },+ },+ "trendsFilter",+ ),+ (+ "unwrapped_trends",+ {"kind": "TrendsQuery", "trendsFilter": {"goalLines": [{"label": "Target", "value": 100}]}},+ "trendsFilter",+ ),+ (+ "funnel",+ {+ "kind": "InsightVizNode",+ "source": {+ "kind": "FunnelsQuery",+ "funnelsFilter": {"goalLines": [{"label": "Target", "value": 100}]},+ },+ },+ "funnelsFilter",+ ),+ (+ "retention",+ {+ "kind": "InsightVizNode",+ "source": {+ "kind": "RetentionQuery",+ "retentionFilter": {"goalLines": [{"label": "Target", "value": 100}]},+ },+ },+ "retentionFilter",+ ),+ (+ "bi",+ {"kind": "BIVisualizationNode", "chartSettings": {"goalLines": [{"label": "Target", "value": 100}]}},+ "chartSettings",+ ),+ (+ "metrics",+ {"kind": "MetricsQuery", "display": {"goalLines": [{"label": "Target", "value": 100}]}},+ "display",+ ),+ (+ "sql",+ {"kind": "DataVisualizationNode", "chartSettings": {"goalLines": [{"label": "Target", "value": 100}]}},+ "chartSettings",+ ),+ ]+ )+ def test_export_bounds_preserve_saved_goals(self, _name: str, query: dict, settings_key: str) -> None:+ bounds = [{"label": "Lower threshold", "value": 50}, {"label": "Upper threshold", "value": 250}]+ original = deepcopy(query)+ result = query_with_reference_lines(query, bounds)+ settings = result.get("source", result)[settings_key]+ assert [line["value"] for line in settings["goalLines"]] == [100, 50, 250]+ assert query == original+ assert query_with_reference_lines(result, bounds) == result+class TestRenderPngExportInsightLookup(BaseTest):def _render(self, **kwargs):diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.test.tsx b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.test.tsxindex 28f9e9b11a17..82c7d8c9044b 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.test.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/FunnelBarHorizontalChart.test.tsx@@ -3,7 +3,7 @@ import '@testing-library/jest-dom'import { cleanup, render, screen, waitFor } from '@testing-library/react'import { BindLogic } from 'kea'-import { clickAtIndex, ensureJsdom, hoverUntilTooltip } from '@posthog/quill-charts/testing'+import { clickAtIndex, ensureJsdom, getChart, hoverUntilTooltip } from '@posthog/quill-charts/testing'import { insightLogic } from 'scenes/insights/insightLogic'@@ -16,6 +16,8 @@ import { NodeKind } from '~/queries/schema/schema-general'import { initKeaTests } from '~/test/init'import { InsightLogicProps, InsightShortId } from '~/types'+import { FunnelBarListChart } from '../FunnelBarListChart/FunnelBarListChart'+import { FunnelStepsBarChart } from '../FunnelStepsBarChart/FunnelStepsBarChart'import { FunnelBarHorizontalChart } from './FunnelBarHorizontalChart'jest.mock('scenes/trends/persons-modal/PersonsModal', () => ({ openPersonsModal: jest.fn() }))@@ -27,11 +29,26 @@ ensureJsdom()let uniqueNode = 0/** Mounts the chart and returns the event wrapper of the first step's bar. */-async function renderFirstStepBar(inCardView: boolean): Promise<HTMLElement> {+async function renderFirstStepBar(+ inCardView: boolean,+ Chart = FunnelBarHorizontalChart,+ withThresholds = false+): Promise<HTMLElement> {const dashboardItemId = `FunnelBarHorizontalChartTest.${uniqueNode++}` as InsightShortIdconst fixture = funnelTopToBottomFixture as any- const source = fixture.query.source- const cachedInsight = { ...fixture, short_id: dashboardItemId }+ const source = {+ ...fixture.query.source,+ funnelsFilter: {+ ...fixture.query.source.funnelsFilter,+ goalLines: withThresholds+ ? [+ { label: 'Lower threshold', value: 20 },+ { label: 'Upper threshold', value: 80 },+ ]+ : undefined,+ },+ }+ const cachedInsight = { ...fixture, query: { ...fixture.query, source }, short_id: dashboardItemId }const insightProps: InsightLogicProps = { dashboardItemId, doNotLoad: true, cachedInsight }const dataNodeLogicProps: DataNodeLogicProps = {query: source,@@ -43,7 +60,7 @@ async function renderFirstStepBar(inCardView: boolean): Promise<HTMLElement> {render(<BindLogic logic={insightLogic} props={insightProps}><BindLogic logic={dataNodeLogic} props={dataNodeLogicProps}>- <FunnelBarHorizontalChart inCardView={inCardView} />+ <Chart inCardView={inCardView} /></BindLogic></BindLogic>)@@ -63,6 +80,22 @@ describe('FunnelBarHorizontalChart', () => {cleanup()})+ it.each([+ ['vertical steps', FunnelStepsBarChart],+ ['horizontal steps', FunnelBarHorizontalChart],+ ['bar list', FunnelBarListChart],+ ])('shows both alert limits for %s', async (_name, Chart) => {+ const bar = await renderFirstStepBar(true, Chart, true)++ await waitFor(() => {+ expect(+ getChart(bar)+ .referenceLines()+ .map((line) => line.label)+ ).toEqual(['Lower threshold', 'Upper threshold'])+ })+ })+// A card-view bar used to render the hint while its click handler was withheld, so the tooltip// promised a drill-in that never happened on dashboards, notebooks, and PostHog AI answers.describe.each([diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/SingleStepBar.tsx b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/SingleStepBar.tsxindex fa722558ad75..f72b4c774488 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/SingleStepBar.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarHorizontalChart/SingleStepBar.tsx@@ -8,6 +8,7 @@ import {type TooltipContext,} from '@posthog/quill-charts'+import { FunnelGoalLines } from '../FunnelGoalLines'import {FUNNEL_BAR_HORIZONTAL_VALUE_DOMAIN,type FunnelBarHorizontalSegmentMeta,@@ -78,7 +79,9 @@ export function SingleStepBar({tooltip={renderTooltip}onPointClick={onPointClick}onError={onError}- />+ >+ <FunnelGoalLines />+ </BarChart></div>)}diff --git a/products/product_analytics/frontend/insights/funnels/FunnelBarListChart/FunnelBarListChart.tsx b/products/product_analytics/frontend/insights/funnels/FunnelBarListChart/FunnelBarListChart.tsxindex 3e8f457c5a5f..b39d697c81b5 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelBarListChart/FunnelBarListChart.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelBarListChart/FunnelBarListChart.tsx@@ -24,6 +24,7 @@ import {import { GlyphColumn } from '../FunnelBarHorizontalChart/GlyphColumn'import { openFunnelSegmentPersons } from '../FunnelBarHorizontalChart/openFunnelSegmentPersons'import { funnelDataLogic } from '../funnelDataLogic'+import { FunnelGoalLines } from '../FunnelGoalLines'import { funnelPersonsModalLogic } from '../funnelPersonsModalLogic'import { FunnelBarListDropOff } from './FunnelBarListDropOff'import { FunnelBarListLabel } from './FunnelBarListLabel'@@ -211,7 +212,9 @@ export function FunnelBarListChart({ showPersonsModal: showPersonsModalProp = trtooltip={renderTooltip}onPointClick={onPointClick}onError={handleChartError}- />+ >+ <FunnelGoalLines />+ </StackedBarList></div></div>)diff --git a/products/product_analytics/frontend/insights/funnels/FunnelGoalLines.tsx b/products/product_analytics/frontend/insights/funnels/FunnelGoalLines.tsxnew file mode 100644index 000000000000..0a8fdbf8af8e--- /dev/null+++ b/products/product_analytics/frontend/insights/funnels/FunnelGoalLines.tsx@@ -0,0 +1,16 @@+import { useValues } from 'kea'++import { ReferenceLines, useChartLayout } from '@posthog/quill-charts'++import { insightLogic } from 'scenes/insights/insightLogic'++import { goalLinesToReferenceLines } from '../trends/shared/goalLinesAdapter'+import { funnelDataLogic } from './funnelDataLogic'++export function FunnelGoalLines(): JSX.Element {+ const { series } = useChartLayout()+ const { insightProps } = useValues(insightLogic)+ const { goalLines } = useValues(funnelDataLogic(insightProps))++ return <ReferenceLines lines={goalLinesToReferenceLines(goalLines, series)} />+}diff --git a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.stories.tsx b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.stories.tsxindex 70d315d23112..a9e575bf92da 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.stories.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.stories.tsx@@ -164,3 +164,26 @@ EditSceneViewports.parameters = {},}EditSceneViewports.play = waitForFunnelToStabilize++export const AlertThresholds: Story = {+ render: () => (+ <StoryRender+ insightFixture={{+ ...funnelTopToBottomFixture,+ query: {+ ...funnelTopToBottomFixture.query,+ source: {+ ...funnelTopToBottomFixture.query.source,+ funnelsFilter: {+ ...funnelTopToBottomFixture.query.source.funnelsFilter,+ goalLines: [+ { label: 'Upper threshold', value: 80, borderColor: '#d14343' },+ { label: 'Lower threshold', value: 20, borderColor: '#d14343' },+ ],+ },+ },+ },+ }}+ />+ ),+}diff --git a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsx b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsxindex 4bb1cafa35cc..ea8f9c15585d 100644--- a/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsx+++ b/products/product_analytics/frontend/insights/funnels/FunnelStepsBarChart/FunnelStepsBarChart.tsx@@ -16,6 +16,7 @@ import { groupsModel } from '~/models/groupsModel'import { ChartParams, FunnelStepReference } from '~/types'import { funnelDataLogic } from '../funnelDataLogic'+import { FunnelGoalLines } from '../FunnelGoalLines'import { funnelPersonsModalLogic } from '../funnelPersonsModalLogic'import { FunnelStepsBarTooltip } from './FunnelStepsBarTooltip'import {@@ -193,6 +194,7 @@ export function FunnelStepsBarChart({dataAttr="funnel-steps-bar-chart-canvas"onError={handleChartError}>+ <FunnelGoalLines />{hasBarLabels && <ValueLabels valueFormatter={formatFunnelStepBarLabel} offset={4} />}{conversionChipStepIndexes.length > 0 && (<StepConversionChips steps={steps} stepIndexes={conversionChipStepIndexes} />