Skip to content

Commit

Permalink
Re-enable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c committed Oct 25, 2023
1 parent 46c0750 commit e200837
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions posthog/hogql_queries/web_analytics/web_analytics_query_runner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from abc import ABC
from datetime import timedelta
from math import ceil
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.caching.insights_api import BASE_MINIMUM_INSIGHT_REFRESH_INTERVAL, REDUCED_MINIMUM_INSIGHT_REFRESH_INTERVAL
from posthog.caching.utils import is_stale
from posthog.hogql.parser import parse_expr
from posthog.hogql.property import property_to_expr
from posthog.hogql_queries.query_runner import QueryRunner
Expand All @@ -27,12 +30,6 @@ 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())
Expand Down Expand Up @@ -102,3 +99,25 @@ def events_where(self):
properties,
self.team,
)

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

0 comments on commit e200837

Please sign in to comment.