-
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: added session duration as a breakdown option (#18056)
* Added session duration as a breakdown option * Update query snapshots * Fixed tests --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f476f67
commit edbd4e0
Showing
8 changed files
with
140 additions
and
22 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
posthog/hogql_queries/insights/trends/breakdown_session.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,52 @@ | ||
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(), | ||
) |
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