: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
fix(dashboards): stop tile display saves from baking in dashboard filters (#122501)
Sam Pennington232afcb364e3
diff --git a/frontend/src/scenes/insights/insightDataLogic.test.ts b/frontend/src/scenes/insights/insightDataLogic.test.tsindex 405c737a885c..3595dfd97186 100644--- a/frontend/src/scenes/insights/insightDataLogic.test.ts+++ b/frontend/src/scenes/insights/insightDataLogic.test.ts@@ -37,7 +37,7 @@ import {} from '~/queries/schema/schema-general'import { setLatestVersionsOnQuery } from '~/queries/utils'import { initKeaTests } from '~/test/init'-import { ChartDisplayType, FunnelVizType, InsightShortId, InsightType } from '~/types'+import { ChartDisplayType, FilterLogicalOperator, FunnelVizType, InsightShortId, InsightType } from '~/types'import { insightDataLogic } from './insightDataLogic'@@ -665,6 +665,91 @@ describe('insightDataLogic', () => {expect(patchSpy).toHaveBeenCalledTimes(1)})+ describe('on a filtered dashboard tile', () => {+ const storedProperties = {+ type: FilterLogicalOperator.And,+ values: [{ type: FilterLogicalOperator.And, values: [{ key: 'id', type: 'cohort', value: 1 }] }],+ }+ const storedQuery: InsightVizNode = {+ kind: NodeKind.InsightVizNode,+ source: {+ kind: NodeKind.TrendsQuery,+ series: [],+ properties: storedProperties,+ dateRange: { date_from: '-30d' },+ } as TrendsQuery,+ }+ const dashboardFilteredQuery: InsightVizNode = {+ ...storedQuery,+ source: {+ ...storedQuery.source,+ properties: {+ type: FilterLogicalOperator.And,+ values: [+ storedProperties,+ {+ type: FilterLogicalOperator.And,+ values: [{ key: 'platform', type: 'event', value: ['ios'], operator: 'exact' }],+ },+ ],+ },+ dateRange: { date_from: '-7d' },+ interval: 'week',+ } as TrendsQuery,+ }+ const worldMapBreakdown = { breakdown: '$geoip_country_code', breakdown_type: 'event' } as const+ const tileEdit: InsightVizNode = {+ ...dashboardFilteredQuery,+ source: {+ ...dashboardFilteredQuery.source,+ trendsFilter: { display: ChartDisplayType.WorldMap },+ breakdownFilter: worldMapBreakdown,+ } as TrendsQuery,+ }++ let tileLogic: ReturnType<typeof insightDataLogic.build>+ let patchedBody: Record<string, any>++ beforeEach(async () => {+ useMocks({+ get: {+ '/api/environments/:team_id/insights/': () => [+ 200,+ { results: [{ id: insightId, short_id: Insight42, query: storedQuery }] },+ ],+ },+ })+ patchSpy.mockImplementationOnce(async ({ request }: { request: Request }) => {+ patchedBody = (await request.json()) as Record<string, any>+ return [200, { id: insightId, short_id: Insight42, ...patchedBody }]+ })+ const props = {+ dashboardItemId: Insight42,+ dashboardId: 7,+ cachedInsight: { id: insightId, short_id: Insight42, query: dashboardFilteredQuery } as any,+ }+ insightLogic(props).mount()+ tileLogic = insightDataLogic(props)+ tileLogic.mount()++ await expectLogic(tileLogic, () => {+ tileLogic.actions.persistDisplayOptions(tileEdit)+ }).toFinishAllListeners()+ })++ it('saves the stored filters with the changes the tile made', () => {+ expect(patchedBody.query.source).toEqual({+ ...storedQuery.source,+ trendsFilter: { display: ChartDisplayType.WorldMap },+ breakdownFilter: worldMapBreakdown,+ })+ })++ it('keeps the dashboard-filtered query on the tile after saving', () => {+ expect(tileLogic.values.savedInsight.query).toEqual(tileEdit)+ })+ })+it('skips the PATCH when the query is unchanged from the saved insight', async () => {await expectLogic(logic, () => {logic.actions.persistDisplayOptions(baseQuery)diff --git a/frontend/src/scenes/insights/insightDataLogic.tsx b/frontend/src/scenes/insights/insightDataLogic.tsxindex 1f426129f50b..e504d2fb2876 100644--- a/frontend/src/scenes/insights/insightDataLogic.tsx+++ b/frontend/src/scenes/insights/insightDataLogic.tsx@@ -129,6 +129,50 @@ const isMatchingSqlQuery = (query.source.query === reference.source.query &&objectsEqual(query.source.variables ?? {}, reference.source.variables ?? {})+// A tile's query carries the dashboard's filters in these fields (resolve_effective_dashboard_filters and+// QueryRunner.apply_dashboard_filters). Saving them back bakes the filters into the insight, nesting+// properties one group deeper per save.+const DASHBOARD_FILTERED_SOURCE_KEYS = [+ 'series',+ 'properties',+ 'dateRange',+ 'breakdownFilter',+ 'interval',+ 'filterTestAccounts',+ 'compareFilter',+] as const++// Restores a field from the stored query only when the tile left it as loaded, so a change the tile+// made on purpose (switching to World map adds a country breakdown) is still saved.+const withStoredDashboardFilteredFields = (+ query: Node,+ tileBaseQuery: Node | null | undefined,+ storedQuery: Node | null | undefined+): Node => {+ if (+ !isInsightVizNode(query) ||+ !isInsightVizNode(tileBaseQuery) ||+ !isInsightVizNode(storedQuery) ||+ query.source.kind !== storedQuery.source.kind+ ) {+ return query+ }+ const source: Record<string, any> = { ...query.source }+ const baseSource: Record<string, any> = tileBaseQuery.source+ const storedSource: Record<string, any> = storedQuery.source+ for (const key of DASHBOARD_FILTERED_SOURCE_KEYS) {+ if (!objectsEqual(source[key], baseSource[key])) {+ continue+ }+ if (key in storedSource) {+ source[key] = storedSource[key]+ } else {+ delete source[key]+ }+ }+ return { ...query, source } as InsightVizNode+}+export type SqlVisualizationUpdate =| { type: 'chart-type'; display: ChartDisplayType }| { type: 'display-options'; query: VisualizationNode }@@ -869,8 +913,19 @@ export const insightDataLogic = kea<insightDataLogicType>([// Breakpoints cannot cancel PATCHes that have already started, so serialize them per insight.await cache.displayOptionsSavebreakpoint()+ const shortId = values.insight.short_id+ const tileBaseQuery = values.savedInsight.queryconst save = withTimeout(- (signal) => insightsApi.update(insightId, { query }, { signal }),+ async (signal) => {+ const storedInsight =+ props.dashboardId != null && shortId ? await insightsApi.getByShortId(shortId, true) : null+ const persistedQuery = withStoredDashboardFilteredFields(+ query,+ tileBaseQuery,+ storedInsight?.query+ )+ return insightsApi.update(insightId, { query: persistedQuery }, { signal })+ },15_000,'Saving display options timed out')@@ -880,9 +935,11 @@ export const insightDataLogic = kea<insightDataLogicType>([if (saveId !== cache.displayOptionsSaveId) {return}- actions.renameInsightSuccess(updatedItem)+ // The response holds the stored query; the tile keeps its dashboard-filtered one.+ const tileItem = props.dashboardId != null ? { ...updatedItem, query } : updatedItem+ actions.renameInsightSuccess(tileItem)if (refreshResults) {- props.refreshAfterDisplayOptionsChange?.(updatedItem)+ props.refreshAfterDisplayOptionsChange?.(tileItem)}actions.persistDisplayOptionsSettled()lemonToast.success('Insight updated')diff --git a/posthog/renderers.py b/posthog/renderers.pyindex aee62c681799..b89aa387ddc1 100644--- a/posthog/renderers.py+++ b/posthog/renderers.py@@ -1,10 +1,25 @@+import math+import orjsonfrom rest_framework.renderers import BaseRenderer, JSONRendererfrom rest_framework.utils.encoders import JSONEncoder+from posthog.exceptions_capture import capture_exception+CleaningMarker = bool | dict[int, "CleaningMarker"]+def _null_non_finite(value):+ # Matches orjson, which writes NaN and infinity as null; the stdlib encoder would emit invalid JSON.+ if isinstance(value, float) and not math.isfinite(value):+ return None+ if isinstance(value, dict):+ return {key: _null_non_finite(item) for key, item in value.items()}+ if isinstance(value, list | tuple):+ return [_null_non_finite(item) for item in value]+ return value++class SafeJSONRenderer(JSONRenderer):def render(self, data, accepted_media_type=None, renderer_context=None) -> bytes:if data is None:@@ -15,7 +30,15 @@ class SafeJSONRenderer(JSONRenderer):if renderer_context and renderer_context.get("indent"):option |= orjson.OPT_INDENT_2- return orjson.dumps(data, default=JSONEncoder().default, option=option)+ try:+ return orjson.dumps(data, default=JSONEncoder().default, option=option)+ except orjson.JSONEncodeError as e:+ # orjson caps nesting at 255 levels, while stdlib json is bounded only by the+ # interpreter's recursion limit, so a deep but finite payload can still be served.+ if str(e) != "Recursion limit reached":+ raise+ capture_exception(e)+ return super().render(_null_non_finite(data), accepted_media_type, renderer_context)class ServerSentEventRenderer(BaseRenderer):diff --git a/posthog/test/test_renderers.py b/posthog/test/test_renderers.pyindex 5a616bde265a..872d14d345ff 100644--- a/posthog/test/test_renderers.py+++ b/posthog/test/test_renderers.py@@ -1,5 +1,7 @@import json+from unittest.mock import patch+from django.test import SimpleTestCasefrom posthog.renderers import SafeJSONRenderer@@ -93,3 +95,25 @@ class TestCleanDataForJSON(SimpleTestCase):],},)++ def test_renders_payload_nested_deeper_than_orjson_allows(self):+ def nested(leaf: dict) -> dict:+ root: dict = {}+ node = root+ for _ in range(300):+ node["values"] = [{}]+ node = node["values"][0]+ node.update(leaf)+ return root++ with patch("posthog.renderers.capture_exception") as mock_capture:+ data = SafeJSONRenderer().render(nested({"key": "platform", "value": float("nan")}))++ self.assertEqual(json.loads(data), nested({"key": "platform", "value": None}))+ mock_capture.assert_called_once()++ def test_unserializable_payload_raises_without_the_fallback(self):+ with patch("posthog.renderers.capture_exception") as mock_capture, self.assertRaises(TypeError):+ SafeJSONRenderer().render({"value": object()})++ mock_capture.assert_not_called()