-
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): scaffold for funnels query runner (#19910)
- Loading branch information
1 parent
135c205
commit 23302f4
Showing
9 changed files
with
198 additions
and
47 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
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
100 changes: 100 additions & 0 deletions
100
posthog/hogql_queries/insights/funnels/funnels_query_runner.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,100 @@ | ||
from datetime import timedelta | ||
from math import ceil | ||
from typing import List, Optional, Any, Dict, cast | ||
|
||
from django.utils.timezone import datetime | ||
from posthog.caching.insights_api import ( | ||
BASE_MINIMUM_INSIGHT_REFRESH_INTERVAL, | ||
REDUCED_MINIMUM_INSIGHT_REFRESH_INTERVAL, | ||
) | ||
from posthog.caching.utils import is_stale | ||
|
||
from posthog.hogql import ast | ||
from posthog.hogql.constants import LimitContext | ||
from posthog.hogql.parser import parse_select | ||
from posthog.hogql.query import execute_hogql_query | ||
from posthog.hogql.timings import HogQLTimings | ||
from posthog.hogql_queries.query_runner import QueryRunner | ||
from posthog.hogql_queries.utils.query_date_range import QueryDateRange | ||
from posthog.models import Team | ||
from posthog.models.filters.mixins.utils import cached_property | ||
from posthog.schema import ( | ||
FunnelsQuery, | ||
FunnelsQueryResponse, | ||
HogQLQueryModifiers, | ||
) | ||
|
||
|
||
class FunnelsQueryRunner(QueryRunner): | ||
query: FunnelsQuery | ||
query_type = FunnelsQuery | ||
|
||
def __init__( | ||
self, | ||
query: FunnelsQuery | Dict[str, Any], | ||
team: Team, | ||
timings: Optional[HogQLTimings] = None, | ||
modifiers: Optional[HogQLQueryModifiers] = None, | ||
limit_context: Optional[LimitContext] = None, | ||
): | ||
super().__init__(query, team=team, timings=timings, modifiers=modifiers, limit_context=limit_context) | ||
|
||
def _is_stale(self, cached_result_package): | ||
date_to = self.query_date_range.date_to() | ||
interval = self.query_date_range.interval_name | ||
return is_stale(self.team, date_to, interval, cached_result_package) | ||
|
||
def _refresh_frequency(self): | ||
date_to = self.query_date_range.date_to() | ||
date_from = self.query_date_range.date_from() | ||
interval = self.query_date_range.interval_name | ||
|
||
delta_days: Optional[int] = None | ||
if date_from and date_to: | ||
delta = date_to - date_from | ||
delta_days = ceil(delta.total_seconds() / timedelta(days=1).total_seconds()) | ||
|
||
refresh_frequency = BASE_MINIMUM_INSIGHT_REFRESH_INTERVAL | ||
if interval == "hour" or (delta_days is not None and delta_days <= 7): | ||
# The interval is shorter for short-term insights | ||
refresh_frequency = REDUCED_MINIMUM_INSIGHT_REFRESH_INTERVAL | ||
|
||
return refresh_frequency | ||
|
||
def to_query(self) -> ast.SelectQuery: | ||
select_query = parse_select( | ||
""" | ||
SELECT 1 | ||
""", | ||
placeholders={}, | ||
) | ||
|
||
return cast(ast.SelectQuery, select_query) | ||
|
||
def calculate(self): | ||
query = self.to_query() | ||
|
||
res: List[Dict[str, Any]] = [] | ||
timings = [] | ||
|
||
response = execute_hogql_query( | ||
query_type="FunnelsQuery", | ||
query=query, | ||
team=self.team, | ||
timings=self.timings, | ||
modifiers=self.modifiers, | ||
) | ||
|
||
if response.timings is not None: | ||
timings.extend(response.timings) | ||
|
||
return FunnelsQueryResponse(results=res, timings=timings) | ||
|
||
@cached_property | ||
def query_date_range(self): | ||
return QueryDateRange( | ||
date_range=self.query.dateRange, | ||
team=self.team, | ||
interval=self.query.interval, | ||
now=datetime.now(), | ||
) |
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