-
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): run filter based insights via hogql (undo revert) (#17645)
- Loading branch information
1 parent
0c37f8b
commit 03f0412
Showing
12 changed files
with
171 additions
and
18 deletions.
There are no files selected for viewing
Binary file modified
BIN
-259 Bytes
(100%)
frontend/__snapshots__/scenes-other-billing-v2--billing-v-2-with-discount.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-224 Bytes
(100%)
frontend/__snapshots__/scenes-other-billing-v2--billing-v-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
25 changes: 25 additions & 0 deletions
25
posthog/hogql_queries/legacy_compatibility/feature_flag.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,25 @@ | ||
import posthoganalytics | ||
from django.conf import settings | ||
from posthog.cloud_utils import is_cloud | ||
from posthog.models.user import User | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
|
||
def hogql_insights_enabled(user: User | AnonymousUser) -> bool: | ||
if settings.HOGQL_INSIGHTS_OVERRIDE is not None: | ||
return settings.HOGQL_INSIGHTS_OVERRIDE | ||
|
||
# on PostHog Cloud, use the feature flag | ||
if is_cloud(): | ||
if not hasattr(user, "distinct_id"): # exclude api endpoints that don't have auth from the flag | ||
return False | ||
|
||
return posthoganalytics.feature_enabled( | ||
"hogql-insights", | ||
user.distinct_id, | ||
person_properties={"email": user.email}, | ||
only_evaluate_locally=True, | ||
send_feature_flag_events=False, | ||
) | ||
else: | ||
return False |
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
51 changes: 51 additions & 0 deletions
51
posthog/hogql_queries/legacy_compatibility/process_insight.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,51 @@ | ||
from posthog.caching.fetch_from_cache import InsightResult | ||
from posthog.hogql_queries.legacy_compatibility.filter_to_query import filter_to_query | ||
from posthog.hogql_queries.insights.lifecycle_query_runner import LifecycleQueryRunner | ||
from posthog.hogql_queries.query_runner import CachedQueryResponse | ||
from posthog.models.filters.filter import Filter as LegacyFilter | ||
from posthog.models.filters.path_filter import PathFilter as LegacyPathFilter | ||
from posthog.models.filters.retention_filter import RetentionFilter as LegacyRetentionFilter | ||
from posthog.models.filters.stickiness_filter import StickinessFilter as LegacyStickinessFilter | ||
from posthog.models.insight import Insight | ||
from posthog.models.team.team import Team | ||
from posthog.types import InsightQueryNode | ||
|
||
|
||
# sync with frontend/src/queries/utils.ts | ||
def is_insight_with_hogql_support(insight: Insight): | ||
if insight.filters.get("insight") == "LIFECYCLE": | ||
return True | ||
else: | ||
return False | ||
|
||
|
||
def _insight_to_query(insight: Insight, team: Team) -> InsightQueryNode: | ||
if insight.filters.get("insight") == "RETENTION": | ||
filter = LegacyRetentionFilter(data=insight.filters, team=team) | ||
elif insight.filters.get("insight") == "PATHS": | ||
filter = LegacyPathFilter(data=insight.filters, team=team) | ||
elif insight.filters.get("insight") == "STICKINESS": | ||
filter = LegacyStickinessFilter(data=insight.filters, team=team) | ||
else: | ||
filter = LegacyFilter(data=insight.filters, team=team) | ||
return filter_to_query(filter.to_dict()) | ||
|
||
|
||
def _cached_response_to_insight_result(response: CachedQueryResponse) -> InsightResult: | ||
response_dict = response.model_dump() | ||
result_keys = InsightResult.__annotations__.keys() | ||
|
||
# replace 'result' with 'results' for schema compatibility | ||
response_keys = ["results" if key == "result" else key for key in result_keys] | ||
|
||
# use only the keys of the response that are also present in the result | ||
result = InsightResult( | ||
**{result_key: response_dict[response_key] for result_key, response_key in zip(result_keys, response_keys)} | ||
) | ||
return result | ||
|
||
|
||
def process_insight(insight: Insight, team: Team) -> InsightResult: | ||
query = _insight_to_query(insight, team) | ||
response = LifecycleQueryRunner(query=query, team=team).run(refresh_requested=False) | ||
return _cached_response_to_insight_result(response) |
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