Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilbert09 committed Oct 12, 2023
1 parent 4b57daf commit 688e6e1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 28 deletions.
28 changes: 16 additions & 12 deletions posthog/hogql_queries/insights/trends/breakdown.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import Dict, List, Tuple
from posthog.hogql import ast
from posthog.hogql_queries.insights.trends.breakdown_values import BreakdownValues
from posthog.hogql_queries.insights.trends.utils import series_event_name
Expand All @@ -23,41 +23,42 @@ def __init__(
self.query_date_range = query_date_range

@cached_property
def enabled(self):
def enabled(self) -> bool:
return self.query.breakdown is not None and self.query.breakdown.breakdown is not None

@cached_property
def is_histogram_breakdown(self):
def is_histogram_breakdown(self) -> bool:
return self.enabled and self.query.breakdown.breakdown_histogram_bin_count is not None

def placeholders(self):
values = self._get_breakdown_buckets_ast() if self.is_histogram_breakdown else self._get_breakdown_values_ast
def placeholders(self) -> Dict[str, ast.Expr]:
values = self._breakdown_buckets_ast if self.is_histogram_breakdown else self._breakdown_values_ast

return {"cross_join_breakdown_values": ast.Alias(alias="breakdown_value", expr=values)}

def events_select(self):
def column_expr(self) -> ast.Expr:
if self.is_histogram_breakdown:
return ast.Alias(alias="breakdown_value", expr=self._get_breakdown_histogram_multi_if())

return ast.Alias(alias="breakdown_value", expr=ast.Field(chain=["properties", self.query.breakdown.breakdown]))

def events_where_filter(self):
def events_where_filter(self) -> ast.Expr:
return ast.CompareOperation(
left=ast.Field(chain=["properties", self.query.breakdown.breakdown]),
op=ast.CompareOperationOp.In,
right=self._get_breakdown_values_ast,
right=self._breakdown_values_ast,
)

def _get_breakdown_buckets_ast(self) -> ast.Array:
@cached_property
def _breakdown_buckets_ast(self) -> ast.Array:
buckets = self._get_breakdown_histogram_buckets()
values = list(map(lambda t: f"[{t[0]},{t[1]}]", buckets))
values = [f"[{t[0]},{t[1]}]" for t in buckets]
values.append('["",""]')

return ast.Array(exprs=list(map(lambda v: ast.Constant(value=v), values)))

@cached_property
def _get_breakdown_values_ast(self) -> ast.Array:
return ast.Array(exprs=list(map(lambda v: ast.Constant(value=v), self._get_breakdown_values)))
def _breakdown_values_ast(self) -> ast.Array:
return ast.Array(exprs=[ast.Constant(value=v) for v in self._get_breakdown_values])

@cached_property
def _get_breakdown_values(self) -> ast.Array:
Expand All @@ -79,6 +80,9 @@ def _get_breakdown_histogram_buckets(self) -> List[Tuple[float, float]]:

for i in range(len(values) - 1):
last_value = i == len(values) - 2

# Since we always `floor(x, 2)` the value, we add 0.01 to the last bucket
# to ensure it's always slightly greater than the maximum value
lower_bound = values[i]
upper_bound = values[i + 1] + 0.01 if last_value else values[i + 1]
buckets.append((lower_bound, upper_bound))
Expand Down
4 changes: 2 additions & 2 deletions posthog/hogql_queries/insights/trends/breakdown_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ def _where_filter(self) -> ast.Expr:
filters.extend(
[
parse_expr(
"toTimeZone(timestamp, 'UTC') >= {date_from}",
"timestamp >= {date_from}",
placeholders=self.query_date_range.to_placeholders(),
),
parse_expr(
"toTimeZone(timestamp, 'UTC') <= {date_to}",
"timestamp <= {date_to}",
placeholders=self.query_date_range.to_placeholders(),
),
]
Expand Down
6 changes: 3 additions & 3 deletions posthog/hogql_queries/insights/trends/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _get_events_subquery(self) -> ast.SelectQuery:
)

if self._breakdown.enabled:
query.select.append(self._breakdown.events_select())
query.select.append(self._breakdown.column_expr())
query.group_by.append(ast.Field(chain=["breakdown_value"]))

return query
Expand Down Expand Up @@ -168,11 +168,11 @@ def _events_filter(self) -> ast.Expr:
filters.extend(
[
parse_expr(
"(toTimeZone(timestamp, 'UTC') >= {date_from})",
"timestamp >= {date_from}",
placeholders=self.query_date_range.to_placeholders(),
),
parse_expr(
"(toTimeZone(timestamp, 'UTC') <= {date_to})",
"timestamp <= {date_to}",
placeholders=self.query_date_range.to_placeholders(),
),
]
Expand Down
14 changes: 3 additions & 11 deletions posthog/hogql_queries/insights/trends/trends_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,12 @@ def apply_formula(self, formula: str, results: List[Dict[str, Any]]) -> List[Dic
return [new_result]

def _is_breakdown_field_boolean(self):
field_type = self._event_properties.get(self.query.breakdown.breakdown)
field_type = self._event_property(self.query.breakdown.breakdown)
return field_type == "Boolean"

def _convert_boolean(self, value: any):
bool_map = {1: "true", 0: "false", "": ""}
return bool_map.get(value) or value

@cached_property
def _event_properties(self):
event_property_values = PropertyDefinition.objects.filter(
team_id=self.team.pk,
type__in=[None, PropertyDefinition.Type.EVENT],
).values_list("name", "property_type")

event_properties = {name: property_type for name, property_type in event_property_values if property_type}

return event_properties
def _event_property(self, field: str):
return PropertyDefinition.objects.get(name=field).property_type

0 comments on commit 688e6e1

Please sign in to comment.