Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(hogql): Add trends sorting for breakdowns #20976

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ def _get_events_subquery(
if not is_actors_query:
default_query.select.append(breakdown.column_expr())
default_query.group_by.append(ast.Field(chain=["breakdown_value"]))
default_query.order_by = [ast.OrderExpr(expr=ast.Field(chain=["breakdown_value"]), order="ASC")]
# Just session duration math property
elif self._aggregation_operation.aggregating_on_session_duration():
default_query.select = [
Expand Down
21 changes: 21 additions & 0 deletions posthog/hogql_queries/insights/trends/trends_query_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import re
from typing import Union
from copy import deepcopy
from datetime import timedelta
Expand Down Expand Up @@ -351,6 +353,20 @@

return TrendsQueryResponse(results=res, timings=timings, hogql=response_hogql)

def breakdown_sort_function(self, value, field="count"):
if False: # TODO if self.filter.using_histogram:
breakdown_value = value.get("breakdown_value")
breakdown_value = re.sub(r"\bnan\b", "NaN", breakdown_value) # fix NaN values for JSON loading
return json.loads(breakdown_value)[0]
if value.get("breakdown_value") == "all":
return (-1, "")
if self.query.breakdownFilter.breakdown_type == "session":

Check failure on line 363 in posthog/hogql_queries/insights/trends/trends_query_runner.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Item "None" of "BreakdownFilter | None" has no attribute "breakdown_type"
# if session duration breakdown, we want ordering based on the time buckets, not the value
return (-1, "")

count_or_aggregated_value = value.get(field) or 0
return count_or_aggregated_value * -1, value.get("label") # reverse it

def build_series_response(self, response: HogQLQueryResponse, series: SeriesWithExtras, series_count: int):
if response.results is None:
return []
Expand Down Expand Up @@ -512,6 +528,11 @@
series_object["label"] = "Other"

res.append(series_object)

if self.query.breakdownFilter is not None and self.query.breakdownFilter.breakdown is not None:
field = "count" if not series.aggregate_values else "aggregated_value"
res = sorted(res, key=lambda value: self.breakdown_sort_function(value, field=field))

return res

@cached_property
Expand Down Expand Up @@ -543,7 +564,7 @@
if isinstance(series, DataWarehouseNode):
return series.table_name

return None

Check failure on line 567 in posthog/hogql_queries/insights/trends/trends_query_runner.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

Statement is unreachable

def update_hogql_modifiers(self) -> None:
if (
Expand Down
Loading