diff --git a/posthog/hogql_queries/legacy_compatibility/filter_to_query.py b/posthog/hogql_queries/legacy_compatibility/filter_to_query.py index 2b8f59f88a421..aea2ffe5e5227 100644 --- a/posthog/hogql_queries/legacy_compatibility/filter_to_query.py +++ b/posthog/hogql_queries/legacy_compatibility/filter_to_query.py @@ -12,6 +12,7 @@ EventsNode, FunnelExclusionActionsNode, FunnelExclusionEventsNode, + FunnelPathsFilter, FunnelsFilter, FunnelsQuery, LifecycleFilter, @@ -451,9 +452,8 @@ def _insight_filter(filter: Dict): edgeLimit=filter.get("edge_limit"), minEdgeWeight=filter.get("min_edge_weight"), maxEdgeWeight=filter.get("max_edge_weight"), - funnelPaths=filter.get("funnel_paths"), - funnelFilter=filter.get("funnel_filter"), - ) + ), + "funnelPathsFilter": filters_to_funnel_paths_query(filter), } elif _insight_type(filter) == "LIFECYCLE": insight_filter = { @@ -480,6 +480,23 @@ def _insight_filter(filter: Dict): return insight_filter +def filters_to_funnel_paths_query(filter: Dict) -> FunnelPathsFilter | None: + funnel_paths = filter.get("funnel_paths") + funnel_filter = filter.get("funnel_filter") + + if funnel_paths is None or funnel_filter is None: + return None + + funnel_query = filter_to_query(funnel_filter) + assert isinstance(funnel_query, FunnelsQuery) + + return FunnelPathsFilter( + funnelPathType=funnel_paths, + funnelSource=funnel_query, + funnelStep=funnel_filter["funnel_step"], + ) + + def _insight_type(filter: Dict) -> INSIGHT_TYPE: if filter.get("insight") == "SESSIONS": return "TRENDS" diff --git a/posthog/hogql_queries/legacy_compatibility/test/test_filter_to_query.py b/posthog/hogql_queries/legacy_compatibility/test/test_filter_to_query.py index 9421ac41be854..327eeccd94663 100644 --- a/posthog/hogql_queries/legacy_compatibility/test/test_filter_to_query.py +++ b/posthog/hogql_queries/legacy_compatibility/test/test_filter_to_query.py @@ -10,6 +10,7 @@ ChartDisplayType, CohortPropertyFilter, CountPerActorMathType, + DateRange, ElementPropertyFilter, EventPropertyFilter, EventsNode, @@ -17,26 +18,34 @@ FunnelExclusionActionsNode, FunnelExclusionEventsNode, FunnelPathType, + FunnelPathsFilter, FunnelVizType, + FunnelsQuery, GroupPropertyFilter, HogQLPropertyFilter, Key, + LifecycleQuery, LifecycleToggle, + MathGroupTypeIndex, PathCleaningFilter, PathType, + PathsQuery, PersonPropertyFilter, PropertyMathType, PropertyOperator, RetentionPeriod, + RetentionQuery, RetentionType, SessionPropertyFilter, StepOrderValue, + StickinessQuery, TrendsFilter, FunnelsFilter, RetentionFilter, PathsFilter, StickinessFilter, LifecycleFilter, + TrendsQuery, ) from posthog.test.base import BaseTest @@ -954,6 +963,7 @@ def test_date_range(self): query = filter_to_query(filter) + assert isinstance(query.dateRange, DateRange) self.assertEqual(query.dateRange.date_from, "-14d") self.assertEqual(query.dateRange.date_to, "-7d") @@ -962,6 +972,7 @@ def test_interval(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual(query.interval, "hour") def test_series_default(self): @@ -969,6 +980,7 @@ def test_series_default(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual(query.series, []) def test_series_custom(self): @@ -979,6 +991,7 @@ def test_series_custom(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.series, [ @@ -1000,6 +1013,7 @@ def test_series_order(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.series, [ @@ -1038,6 +1052,7 @@ def test_series_math(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.series, [ @@ -1057,7 +1072,7 @@ def test_series_math(self): event="$pageview", name="$pageview", math="unique_group", - math_group_type_index=0, + math_group_type_index=MathGroupTypeIndex.number_0, ), EventsNode( event="$pageview", @@ -1164,6 +1179,7 @@ def test_series_properties(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.series, [ @@ -1252,6 +1268,7 @@ def test_breakdown(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.breakdownFilter, BreakdownFilter(breakdown_type=BreakdownType.event, breakdown="$browser"), @@ -1262,6 +1279,7 @@ def test_breakdown_converts_multi(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.breakdownFilter, BreakdownFilter(breakdown_type=BreakdownType.event, breakdown="$browser"), @@ -1272,6 +1290,7 @@ def test_breakdown_type_default(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.breakdownFilter, BreakdownFilter(breakdown_type=BreakdownType.event, breakdown="some_prop"), @@ -1294,6 +1313,7 @@ def test_trends_filter(self): query = filter_to_query(filter) + assert isinstance(query, TrendsQuery) self.assertEqual( query.trendsFilter, TrendsFilter( @@ -1359,6 +1379,7 @@ def test_funnels_filter(self): query = filter_to_query(filter) + assert isinstance(query, FunnelsQuery) self.assertEqual( query.funnelsFilter, FunnelsFilter( @@ -1407,6 +1428,7 @@ def test_retention_filter(self): query = filter_to_query(filter) + assert isinstance(query, RetentionQuery) self.assertEqual( query.retentionFilter, RetentionFilter( @@ -1467,6 +1489,7 @@ def test_paths_filter(self): query = filter_to_query(filter) + assert isinstance(query, PathsQuery) self.assertEqual( query.pathsFilter, PathsFilter( @@ -1484,24 +1507,21 @@ def test_paths_filter(self): excludeEvents=["http://localhost:8000/events"], stepLimit=5, pathGroupings=["/merchant/*/payment"], - funnelPaths=FunnelPathType.funnel_path_between_steps, - funnelFilter={ - "insight": "FUNNELS", - "events": [ - { - "type": "events", - "id": "$pageview", - "order": 0, - "name": "$pageview", - "math": "total", - }, - {"type": "events", "id": None, "order": 1, "math": "total"}, + ), + ) + self.assertEqual( + query.funnelPathsFilter, + FunnelPathsFilter( + funnelPathType=FunnelPathType.funnel_path_between_steps, + funnelSource=FunnelsQuery( + series=[ + EventsNode(event="$pageview", name="$pageview"), + EventsNode(event=None, name="All events"), ], - "funnel_viz_type": "steps", - "exclusions": [], - "filter_test_accounts": True, - "funnel_step": 2, - }, + filterTestAccounts=True, + funnelsFilter=FunnelsFilter(funnelVizType=FunnelVizType.steps, exclusions=[]), + ), + funnelStep=2, ), ) @@ -1516,6 +1536,7 @@ def test_stickiness_filter(self): query = filter_to_query(filter) + assert isinstance(query, StickinessQuery) self.assertEqual( query.stickinessFilter, StickinessFilter(compare=True, showLegend=True, showValuesOnSeries=True), @@ -1531,6 +1552,7 @@ def test_lifecycle_filter(self): query = filter_to_query(filter) + assert isinstance(query, LifecycleQuery) self.assertEqual( query.lifecycleFilter, LifecycleFilter(