From 1fa2e0d05509e11a7ea5ebb9c66d20a1ddd9455e Mon Sep 17 00:00:00 2001 From: Tom Owers Date: Thu, 26 Oct 2023 16:39:00 +0100 Subject: [PATCH] chore: removed custom trends session code (#18211) Removed custom trends session code --- .../insights/trends/breakdown.py | 8 --- .../insights/trends/breakdown_session.py | 52 ------------------- .../insights/trends/breakdown_values.py | 11 ---- .../insights/trends/query_builder.py | 17 ------ .../hogql_queries/insights/trends/utils.py | 3 ++ 5 files changed, 3 insertions(+), 88 deletions(-) delete mode 100644 posthog/hogql_queries/insights/trends/breakdown_session.py diff --git a/posthog/hogql_queries/insights/trends/breakdown.py b/posthog/hogql_queries/insights/trends/breakdown.py index a713cb09dcee1..7524dd5eb5ffe 100644 --- a/posthog/hogql_queries/insights/trends/breakdown.py +++ b/posthog/hogql_queries/insights/trends/breakdown.py @@ -2,7 +2,6 @@ from posthog.hogql import ast from posthog.hogql.parser import parse_expr from posthog.hogql.timings import HogQLTimings -from posthog.hogql_queries.insights.trends.breakdown_session import BreakdownSession from posthog.hogql_queries.insights.trends.breakdown_values import BreakdownValues from posthog.hogql_queries.insights.trends.utils import ( get_properties_chain, @@ -173,15 +172,8 @@ def _get_breakdown_histogram_multi_if(self) -> ast.Expr: @cached_property def _properties_chain(self): - if self.is_session_type: - return self._breakdown_session.session_duration_property_chain() - return get_properties_chain( breakdown_type=self.query.breakdown.breakdown_type, breakdown_field=self.query.breakdown.breakdown, group_type_index=self.query.breakdown.breakdown_group_type_index, ) - - @cached_property - def _breakdown_session(self): - return BreakdownSession(self.query_date_range) diff --git a/posthog/hogql_queries/insights/trends/breakdown_session.py b/posthog/hogql_queries/insights/trends/breakdown_session.py deleted file mode 100644 index 1ab518cbec34e..0000000000000 --- a/posthog/hogql_queries/insights/trends/breakdown_session.py +++ /dev/null @@ -1,52 +0,0 @@ -from typing import List -from posthog.hogql import ast -from posthog.hogql.parser import parse_select -from posthog.hogql_queries.utils.query_date_range import QueryDateRange - - -class BreakdownSession: - query_date_range: QueryDateRange - - def __init__(self, query_date_range: QueryDateRange): - self.query_date_range = query_date_range - - def session_inner_join(self) -> ast.JoinExpr: - join = ast.JoinExpr( - table=ast.Field(chain=["events"]), - alias="e", - next_join=ast.JoinExpr( - join_type="INNER JOIN", - alias="sessions", - table=self._session_select_query(), - constraint=ast.JoinConstraint( - expr=ast.CompareOperation( - left=ast.Field(chain=["sessions", "$session_id"]), - op=ast.CompareOperationOp.Eq, - right=ast.Field(chain=["e", "$session_id"]), - ) - ), - ), - ) - - return join - - def session_duration_property_chain(self) -> List[str]: - return ["sessions", "session_duration"] - - def session_duration_field(self) -> ast.Field: - return ast.Field(chain=self.session_duration_property_chain()) - - def _session_select_query(self) -> ast.SelectQuery: - return parse_select( - """ - SELECT - "$session_id", dateDiff('second', min(timestamp), max(timestamp)) as session_duration - FROM events - WHERE - "$session_id" != '' AND - timestamp >= {date_from} - INTERVAL 24 HOUR AND - timestamp <= {date_to} + INTERVAL 24 HOUR - GROUP BY "$session_id" - """, - placeholders=self.query_date_range.to_placeholders(), - ) diff --git a/posthog/hogql_queries/insights/trends/breakdown_values.py b/posthog/hogql_queries/insights/trends/breakdown_values.py index 37d9f7168e121..3754d24bab70b 100644 --- a/posthog/hogql_queries/insights/trends/breakdown_values.py +++ b/posthog/hogql_queries/insights/trends/breakdown_values.py @@ -2,10 +2,8 @@ from posthog.hogql import ast from posthog.hogql.parser import parse_expr, parse_select from posthog.hogql.query import execute_hogql_query -from posthog.hogql_queries.insights.trends.breakdown_session import BreakdownSession from posthog.hogql_queries.insights.trends.utils import get_properties_chain from posthog.hogql_queries.utils.query_date_range import QueryDateRange -from posthog.models.filters.mixins.utils import cached_property from posthog.models.team.team import Team @@ -45,8 +43,6 @@ def get_breakdown_values(self) -> List[str]: alias="value", expr=parse_expr(self.breakdown_field), ) - elif self.breakdown_type == "session": - select_field = ast.Alias(alias="value", expr=self._breakdown_session.session_duration_field()) else: select_field = ast.Alias( alias="value", @@ -92,9 +88,6 @@ def get_breakdown_values(self) -> List[str]: if self.histogram_bin_count is not None: query.select = [self._to_bucketing_expression()] - if self.breakdown_type == "session": - inner_events_query.select_from = self._breakdown_session.session_inner_join() - response = execute_hogql_query( query_type="TrendsQueryBreakdownValues", query=query, @@ -145,7 +138,3 @@ def _to_bucketing_expression(self) -> ast.Expr: qunatile_expression = f"quantiles({','.join([f'{quantile:.2f}' for quantile in quantiles])})(value)" return parse_expr(f"arrayCompact(arrayMap(x -> floor(x, 2), {qunatile_expression}))") - - @cached_property - def _breakdown_session(self): - return BreakdownSession(self.query_date_range) diff --git a/posthog/hogql_queries/insights/trends/query_builder.py b/posthog/hogql_queries/insights/trends/query_builder.py index 0a90cae985dba..6c4ba3aa849ba 100644 --- a/posthog/hogql_queries/insights/trends/query_builder.py +++ b/posthog/hogql_queries/insights/trends/query_builder.py @@ -7,7 +7,6 @@ AggregationOperations, ) from posthog.hogql_queries.insights.trends.breakdown import Breakdown -from posthog.hogql_queries.insights.trends.breakdown_session import BreakdownSession from posthog.hogql_queries.insights.trends.utils import series_event_name from posthog.hogql_queries.utils.query_date_range import QueryDateRange from posthog.models.filters.mixins.utils import cached_property @@ -140,8 +139,6 @@ def _get_events_subquery(self) -> ast.SelectQuery: orchestrator.events_query_builder.append_select(self._breakdown.column_expr()) orchestrator.events_query_builder.append_group_by(ast.Field(chain=["breakdown_value"])) - if self._breakdown.is_session_type: - orchestrator.events_query_builder.replace_select_from(self._breakdown_session.session_inner_join()) orchestrator.inner_select_query_builder.append_select(ast.Field(chain=["breakdown_value"])) orchestrator.inner_select_query_builder.append_group_by(ast.Field(chain=["breakdown_value"])) @@ -154,8 +151,6 @@ def _get_events_subquery(self) -> ast.SelectQuery: default_query.select.append(self._breakdown.column_expr()) default_query.group_by.append(ast.Field(chain=["breakdown_value"])) - if self._breakdown.is_session_type: - default_query.select_from = self._breakdown_session.session_inner_join() # Just complex series aggregation elif self._aggregation_operation.requires_query_orchestration(): return self._aggregation_operation.get_query_orchestrator( @@ -250,14 +245,6 @@ def _events_filter(self) -> ast.Expr: # Breakdown if self._breakdown.enabled and not self._breakdown.is_histogram_breakdown: filters.append(self._breakdown.events_where_filter()) - if self._breakdown.is_session_type: - filters.append( - ast.CompareOperation( - left=self._breakdown_session.session_duration_field(), - op=ast.CompareOperationOp.NotEq, - right=ast.Constant(value=None), - ) - ) if len(filters) == 0: return ast.Constant(value=True) @@ -282,10 +269,6 @@ def _breakdown(self): timings=self.timings, ) - @cached_property - def _breakdown_session(self): - return BreakdownSession(self.query_date_range) - @cached_property def _aggregation_operation(self): return AggregationOperations(self.series, self.query_date_range) diff --git a/posthog/hogql_queries/insights/trends/utils.py b/posthog/hogql_queries/insights/trends/utils.py index cc1e906291c5a..9a7566969c61b 100644 --- a/posthog/hogql_queries/insights/trends/utils.py +++ b/posthog/hogql_queries/insights/trends/utils.py @@ -14,6 +14,9 @@ def get_properties_chain( if breakdown_type == "person": return ["person", "properties", breakdown_field] + if breakdown_type == "session": + return ["session", "session_duration"] + if breakdown_type == "group" and group_type_index is not None: group_type_index_int = int(group_type_index) return [f"group_{group_type_index_int}", "properties", breakdown_field]