-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hogql): add remaining funnel actors (#20496)
- Loading branch information
1 parent
4a12660
commit da1d9fb
Showing
20 changed files
with
1,178 additions
and
99 deletions.
There are no files selected for viewing
Binary file modified
BIN
-1 Byte
(100%)
...insights-error-empty-states--estimated-query-execution-time-too-long--light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
posthog/hogql_queries/insights/funnels/funnel_strict_persons.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import List | ||
|
||
from posthog.hogql import ast | ||
from posthog.hogql_queries.insights.funnels.funnel_strict import FunnelStrict | ||
|
||
|
||
class FunnelStrictActors(FunnelStrict): | ||
def actor_query( | ||
self, | ||
# extra_fields: Optional[List[str]] = None, | ||
) -> ast.SelectQuery: | ||
select: List[ast.Expr] = [ | ||
ast.Alias(alias="actor_id", expr=ast.Field(chain=["aggregation_target"])), | ||
*self._get_funnel_person_step_events(), | ||
*self._get_timestamp_outer_select(), | ||
# {extra_fields} | ||
] | ||
select_from = ast.JoinExpr(table=self.get_step_counts_query()) | ||
where = self._get_funnel_person_step_condition() | ||
order_by = [ast.OrderExpr(expr=ast.Field(chain=["aggregation_target"]))] | ||
|
||
return ast.SelectQuery( | ||
select=select, | ||
select_from=select_from, | ||
order_by=order_by, | ||
where=where, | ||
# SETTINGS max_ast_elements=1000000, max_expanded_ast_elements=1000000 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
posthog/hogql_queries/insights/funnels/funnel_trends_persons.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
from datetime import datetime | ||
from typing import List | ||
|
||
from rest_framework.exceptions import ValidationError | ||
|
||
from posthog.hogql import ast | ||
from posthog.hogql.parser import parse_expr | ||
from posthog.hogql_queries.insights.funnels.funnel_query_context import FunnelQueryContext | ||
from posthog.hogql_queries.insights.funnels.funnel_trends import FunnelTrends | ||
from posthog.utils import relative_date_parse | ||
|
||
|
||
class FunnelTrendsActors(FunnelTrends): | ||
entrancePeriodStart: datetime | ||
dropOff: bool | ||
|
||
def __init__(self, context: FunnelQueryContext, just_summarize=False): | ||
super().__init__(context, just_summarize) | ||
|
||
team, actorsQuery = self.context.team, self.context.actorsQuery | ||
|
||
if actorsQuery is None: | ||
raise ValidationError("No actors query present.") | ||
|
||
if actorsQuery.funnelTrendsDropOff is None: | ||
raise ValidationError(f"Actors parameter `funnelTrendsDropOff` must be provided for funnel trends persons!") | ||
|
||
if actorsQuery.funnelTrendsEntrancePeriodStart is None: | ||
raise ValidationError( | ||
f"Actors parameter `funnelTrendsEntrancePeriodStart` must be provided funnel trends persons!" | ||
) | ||
|
||
entrancePeriodStart = relative_date_parse(actorsQuery.funnelTrendsEntrancePeriodStart, team.timezone_info) | ||
if entrancePeriodStart is None: | ||
raise ValidationError( | ||
f"Actors parameter `funnelTrendsEntrancePeriodStart` must be a valid relative date string!" | ||
) | ||
|
||
self.dropOff = actorsQuery.funnelTrendsDropOff | ||
self.entrancePeriodStart = entrancePeriodStart | ||
|
||
def _get_funnel_person_step_events(self) -> List[ast.Expr]: | ||
# if self._filter.include_recordings: | ||
# # Get the event that should be used to match the recording | ||
# funnel_to_step = self._filter.funnel_to_step | ||
# is_drop_off = self._filter.drop_off | ||
|
||
# if funnel_to_step is None or is_drop_off: | ||
# # If there is no funnel_to_step or if we are looking for drop off, we need to get the users final event | ||
# return ", final_matching_events as matching_events" | ||
# else: | ||
# # Otherwise, we return the event of the funnel_to_step | ||
# self.params.update({"matching_events_step_num": funnel_to_step}) | ||
# return ", step_%(matching_events_step_num)s_matching_events as matching_events" | ||
return [] | ||
|
||
def actor_query(self) -> ast.SelectQuery: | ||
step_counts_query = self.get_step_counts_without_aggregation_query( | ||
specific_entrance_period_start=self.entrancePeriodStart | ||
) | ||
|
||
# Expects multiple rows for same person, first event time, steps taken. | ||
( | ||
_, | ||
reached_to_step_count_condition, | ||
did_not_reach_to_step_count_condition, | ||
) = self.get_steps_reached_conditions() | ||
|
||
select: List[ast.Expr] = [ | ||
ast.Alias(alias="actor_id", expr=ast.Field(chain=["aggregation_target"])), | ||
*self._get_funnel_person_step_events(), | ||
] | ||
select_from = ast.JoinExpr(table=step_counts_query) | ||
where = ( | ||
parse_expr(did_not_reach_to_step_count_condition) | ||
if self.dropOff | ||
else parse_expr(reached_to_step_count_condition) | ||
) | ||
order_by = [ast.OrderExpr(expr=ast.Field(chain=["aggregation_target"]))] | ||
|
||
return ast.SelectQuery( | ||
select=select, | ||
select_from=select_from, | ||
order_by=order_by, | ||
where=where, | ||
# SETTINGS max_ast_elements=1000000, max_expanded_ast_elements=1000000 | ||
) |
37 changes: 37 additions & 0 deletions
37
posthog/hogql_queries/insights/funnels/funnel_unordered_persons.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import List | ||
|
||
from posthog.hogql import ast | ||
from posthog.hogql.parser import parse_expr | ||
from posthog.hogql_queries.insights.funnels.funnel_unordered import FunnelUnordered | ||
|
||
|
||
class FunnelUnorderedActors(FunnelUnordered): | ||
def _get_funnel_person_step_events(self) -> List[ast.Expr]: | ||
# Unordered funnels does not support matching events (and thereby recordings), | ||
# but it simplifies the logic if we return an empty array for matching events | ||
# if self._filter.include_recordings: | ||
if False: | ||
return [parse_expr("array() as matching_events")] # type: ignore | ||
return [] | ||
|
||
def actor_query( | ||
self, | ||
# extra_fields: Optional[List[str]] = None, | ||
) -> ast.SelectQuery: | ||
select: List[ast.Expr] = [ | ||
ast.Alias(alias="actor_id", expr=ast.Field(chain=["aggregation_target"])), | ||
*self._get_funnel_person_step_events(), | ||
*self._get_timestamp_outer_select(), | ||
# {extra_fields} | ||
] | ||
select_from = ast.JoinExpr(table=self.get_step_counts_query()) | ||
where = self._get_funnel_person_step_condition() | ||
order_by = [ast.OrderExpr(expr=ast.Field(chain=["aggregation_target"]))] | ||
|
||
return ast.SelectQuery( | ||
select=select, | ||
select_from=select_from, | ||
order_by=order_by, | ||
where=where, | ||
# SETTINGS max_ast_elements=1000000, max_expanded_ast_elements=1000000 | ||
) |
Oops, something went wrong.