-
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.
- Loading branch information
Showing
6 changed files
with
95 additions
and
85 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
74 changes: 74 additions & 0 deletions
74
posthog/hogql_queries/web_analytics/web_analytics_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 |
---|---|---|
@@ -1,12 +1,86 @@ | ||
from abc import ABC | ||
from typing import Optional, List, Union, Type | ||
|
||
from django.utils.timezone import datetime | ||
from posthog.caching.insights_api import BASE_MINIMUM_INSIGHT_REFRESH_INTERVAL | ||
from posthog.hogql.parser import parse_expr | ||
from posthog.hogql.property import property_to_expr | ||
from posthog.hogql_queries.query_runner import QueryRunner | ||
from posthog.hogql_queries.utils.query_date_range import QueryDateRange | ||
from posthog.models.filters.mixins.utils import cached_property | ||
from posthog.schema import EventPropertyFilter, WebTopSourcesQuery, WebTopClicksQuery, WebTopPagesQuery | ||
|
||
WebQueryNode = Union[ | ||
WebTopSourcesQuery, | ||
WebTopClicksQuery, | ||
WebTopPagesQuery, | ||
] | ||
|
||
|
||
class WebAnalyticsQueryRunner(QueryRunner, ABC): | ||
query: WebQueryNode | ||
query_type: Type[WebQueryNode] | ||
|
||
def _is_stale(self, cached_result_package): | ||
return True | ||
|
||
def _refresh_frequency(self): | ||
return BASE_MINIMUM_INSIGHT_REFRESH_INTERVAL | ||
|
||
@cached_property | ||
def query_date_range(self): | ||
return QueryDateRange(date_range=self.query.dateRange, team=self.team, interval=None, now=datetime.now()) | ||
|
||
@cached_property | ||
def pathname_property_filter(self) -> Optional[EventPropertyFilter]: | ||
return next((p for p in self.query.properties if p.key == "$pathname"), None) | ||
|
||
@cached_property | ||
def property_filters_without_pathname(self) -> List[EventPropertyFilter]: | ||
return [p for p in self.query.properties if p.key != "$pathname"] | ||
|
||
def session_where(self): | ||
properties = [ | ||
parse_expr( | ||
"events.timestamp < {date_to} AND events.timestamp >= minus({date_from}, toIntervalHour(1))", | ||
placeholders={ | ||
"date_from": self.query_date_range.date_from_as_hogql(), | ||
"date_to": self.query_date_range.date_to_as_hogql(), | ||
}, | ||
) | ||
] + self.property_filters_without_pathname | ||
return property_to_expr( | ||
properties, | ||
self.team, | ||
) | ||
|
||
def session_having(self): | ||
properties = [ | ||
parse_expr( | ||
"min_timestamp >= {date_from}", | ||
placeholders={"date_from": self.query_date_range.date_from_as_hogql()}, | ||
) | ||
] | ||
pathname = self.pathname_property_filter | ||
if pathname: | ||
properties.append( | ||
EventPropertyFilter( | ||
key="earliest_pathname", label=pathname.label, operator=pathname.operator, value=pathname.value | ||
) | ||
) | ||
return property_to_expr( | ||
properties, | ||
self.team, | ||
) | ||
|
||
def events_where(self): | ||
properties = [ | ||
parse_expr( | ||
"events.timestamp >= {date_from}", | ||
placeholders={"date_from": self.query_date_range.date_from_as_hogql()}, | ||
) | ||
] + self.query.properties | ||
return property_to_expr( | ||
properties, | ||
self.team, | ||
) |