From e2008371d02ee6db1cb9a4024ff63855a01f05e0 Mon Sep 17 00:00:00 2001 From: Robbie Coomber Date: Wed, 25 Oct 2023 15:33:33 +0100 Subject: [PATCH] Re-enable caching --- .../web_analytics_query_runner.py | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py b/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py index a9d0092565f59..4f27414e7f64b 100644 --- a/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py +++ b/posthog/hogql_queries/web_analytics/web_analytics_query_runner.py @@ -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 @@ -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()) @@ -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