Skip to content

Commit

Permalink
fix(insights): correctly filter hogql insights in list (#26096)
Browse files Browse the repository at this point in the history
  • Loading branch information
thmsobrmlr authored Nov 9, 2024
1 parent b51bcb1 commit d844b47
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
19 changes: 17 additions & 2 deletions posthog/api/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
32 changes: 32 additions & 0 deletions posthog/api/test/test_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
HogQLFilters,
HogQLQuery,
InsightNodeKind,
InsightVizNode,
NodeKind,
PropertyGroupFilter,
PropertyGroupFilterValue,
Expand Down Expand Up @@ -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"}]}}
Expand Down

0 comments on commit d844b47

Please sign in to comment.