diff --git a/posthog/api/insight.py b/posthog/api/insight.py index d1aa643a400a0..77889e06212e6 100644 --- a/posthog/api/insight.py +++ b/posthog/api/insight.py @@ -835,15 +835,30 @@ def _filter_request(self, request: request.Request, queryset: QuerySet) -> Query ) elif key == INSIGHT: insight = request.GET[INSIGHT] + legacy_filter = Q(query__isnull=True) & Q(filters__insight=insight) + legacy_to_hogql_mapping = { + "TRENDS": schema.NodeKind.TRENDS_QUERY, + "FUNNELS": schema.NodeKind.FUNNELS_QUERY, + "RETENTION": schema.NodeKind.RETENTION_QUERY, + "PATHS": schema.NodeKind.PATHS_QUERY, + "STICKINESS": schema.NodeKind.STICKINESS_QUERY, + "LIFECYCLE": schema.NodeKind.LIFECYCLE_QUERY, + } if insight == "JSON": queryset = queryset.filter(query__isnull=False) queryset = queryset.exclude(query__kind__in=WRAPPER_NODE_KINDS, query__source__kind="HogQLQuery") elif insight == "SQL": queryset = queryset.filter(query__isnull=False) queryset = queryset.filter(query__kind__in=WRAPPER_NODE_KINDS, query__source__kind="HogQLQuery") + elif insight in legacy_to_hogql_mapping: + queryset = queryset.filter( + legacy_filter + | Q(query__isnull=False) + & Q(query__kind=schema.NodeKind.INSIGHT_VIZ_NODE) + & Q(query__source__kind=legacy_to_hogql_mapping[insight]) + ) else: - queryset = queryset.filter(query__isnull=True) - queryset = queryset.filter(filters__insight=insight) + queryset = queryset.filter(legacy_filter) elif key == "search": queryset = queryset.filter( Q(name__icontains=request.GET["search"]) diff --git a/posthog/api/test/test_insight.py b/posthog/api/test/test_insight.py index 32cd4da9df83f..e724252dcd105 100644 --- a/posthog/api/test/test_insight.py +++ b/posthog/api/test/test_insight.py @@ -46,6 +46,7 @@ HogQLFilters, HogQLQuery, InsightNodeKind, + InsightVizNode, NodeKind, PropertyGroupFilter, PropertyGroupFilterValue, @@ -515,6 +516,37 @@ def test_listing_insights_does_not_nplus1(self) -> None: f"received query counts\n\n{query_counts}", ) + def test_listing_insights_shows_legacy_and_hogql_ones(self) -> None: + self.dashboard_api.create_insight( + data={ + "short_id": f"insight", + "query": { + "kind": "DataVisualizationNode", + "source": { + "kind": "HogQLQuery", + "query": "select * from events", + }, + }, + } + ) + + self.dashboard_api.create_insight( + data={ + "short_id": f"insight", + "filters": {"insight": "TRENDS", "events": [{"id": "$pageview"}]}, + } + ) + self.dashboard_api.create_insight( + data={ + "short_id": f"insight", + "query": InsightVizNode(source=TrendsQuery(series=[EventsNode(event="$pageview")])).model_dump(), + } + ) + + response = self.client.get(f"/api/environments/{self.team.pk}/insights/?insight=TRENDS") + + self.assertEqual(len(response.json()["results"]), 2) + def test_can_list_insights_by_which_dashboards_they_are_in(self) -> None: insight_one_id, _ = self.dashboard_api.create_insight( {"name": "insight 1", "filters": {"events": [{"id": "$pageview"}]}}