Skip to content

Commit

Permalink
Kinda sorta working
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Oct 19, 2023
1 parent 2e37b3b commit 7b48b83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
22 changes: 7 additions & 15 deletions posthog/hogql_queries/apply_dashboard_filters.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
from posthog.schema import HogQLQuery, DashboardFilter, HogQLFilters, DateRange
from posthog.models import Team
from posthog.schema import DashboardFilter


# Apply the filters from the django-style Dashboard object
def apply_dashboard_filters(query: dict, filters: dict) -> dict:
def apply_dashboard_filters(query: dict, filters: dict, team: Team) -> dict:
kind = query.get("kind", None)

if kind == "DataTableNode":
source = apply_dashboard_filters(query["source"], filters)
source = apply_dashboard_filters(query["source"], filters, team)
return {**query, "source": source}

dashboard_filter = DashboardFilter(**filters)

if kind == "HogQLQuery":
node = HogQLQuery(**query)
from posthog.hogql_queries.hogql_query_runner import HogQLQueryRunner

hogql_filters = node.filters or HogQLFilters()
date_range = hogql_filters.dateRange or DateRange()
node.filters = hogql_filters
hogql_filters.dateRange = date_range

if dashboard_filter.date_to:
date_range.date_to = dashboard_filter.date_to
if dashboard_filter.date_from:
date_range.date_from = dashboard_filter.date_from

return node.dict()
query_runner = HogQLQueryRunner(query, team)
return query_runner.apply_dashboard_filters(dashboard_filter).dict()
else:
return query
12 changes: 11 additions & 1 deletion posthog/hogql_queries/hogql_query_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from posthog.hogql.timings import HogQLTimings
from posthog.hogql_queries.query_runner import QueryRunner
from posthog.models import Team
from posthog.schema import HogQLQuery, HogQLQueryResponse
from posthog.schema import HogQLQuery, HogQLQueryResponse, DashboardFilter, HogQLFilters, DateRange


class HogQLQueryRunner(QueryRunner):
Expand Down Expand Up @@ -66,3 +66,13 @@ def _is_stale(self, cached_result_package):

def _refresh_frequency(self):
return timedelta(minutes=1)

def apply_dashboard_filters(self, dashboard_filter: DashboardFilter) -> HogQLQuery:
self.query.filters = self.query.filters or HogQLFilters()
self.query.filters.dateRange = self.query.filters.dateRange or DateRange()

if dashboard_filter.date_to or dashboard_filter.date_from:
self.query.filters.dateRange.date_to = dashboard_filter.date_to
self.query.filters.dateRange.date_from = dashboard_filter.date_from

return self.query
9 changes: 5 additions & 4 deletions posthog/models/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django_deprecate_fields import deprecate_field
from rest_framework.exceptions import ValidationError

from posthog.hogql_queries.apply_dashboard_filters import apply_dashboard_filters
from posthog.logging.timing import timed
from posthog.models.dashboard import Dashboard
from posthog.models.filters.utils import get_filter
Expand Down Expand Up @@ -158,8 +157,9 @@ def dashboard_filters(self, dashboard: Optional[Dashboard] = None):
def dashboard_query(self, dashboard: Optional[Dashboard]):
if not dashboard or not self.query:
return self.query
from posthog.hogql_queries.apply_dashboard_filters import apply_dashboard_filters

return apply_dashboard_filters(self.query, dashboard.filters)
return apply_dashboard_filters(self.query, dashboard.filters, self.team)

@property
def url(self):
Expand All @@ -181,8 +181,9 @@ class Meta:
def generate_insight_cache_key(insight: Insight, dashboard: Optional[Dashboard]) -> str:
try:
if insight.query is not None:
# TODO: dashboard filtering needs to know how to override queries and date ranges 😱
q = insight.query
from posthog.hogql_queries.apply_dashboard_filters import apply_dashboard_filters

q = apply_dashboard_filters(insight.query, dashboard.filters, insight.team)
if q.get("source"):
q = q["source"]

Expand Down

0 comments on commit 7b48b83

Please sign in to comment.