Skip to content

Commit

Permalink
Clarify generate_insight_cache_key as legacy function
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed May 23, 2024
1 parent 8fa0b5f commit 0c0bb44
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions posthog/caching/calculate_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from posthog.models.filters import PathFilter
from posthog.models.filters.stickiness_filter import StickinessFilter
from posthog.models.filters.utils import get_filter
from posthog.models.insight import generate_insight_cache_key
from posthog.models.insight import generate_insight_filters_hash
from posthog.queries.funnels import ClickhouseFunnelTimeToConvert, ClickhouseFunnelTrends
from posthog.queries.funnels.utils import get_funnel_order_class
from posthog.queries.paths import Paths
Expand Down Expand Up @@ -69,7 +69,7 @@ def calculate_cache_key(target: Union[DashboardTile, Insight]) -> Optional[str]:
return query_runner.get_cache_key()

if insight.filters:
return generate_insight_cache_key(insight, dashboard)
return generate_insight_filters_hash(insight, dashboard)

return None

Expand Down Expand Up @@ -174,7 +174,7 @@ def calculate_for_filter_based_insight(
insight: Insight, dashboard: Optional[Dashboard]
) -> tuple[str, str, list | dict]:
filter = get_filter(data=insight.dashboard_filters(dashboard), team=insight.team)
cache_key = generate_insight_cache_key(insight, dashboard)
cache_key = generate_insight_filters_hash(insight, dashboard)
cache_type = get_cache_type(filter)

tag_queries(
Expand Down
4 changes: 2 additions & 2 deletions posthog/models/dashboard_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone

from posthog.models.dashboard import Dashboard
from posthog.models.insight import generate_insight_cache_key
from posthog.models.insight import generate_insight_filters_hash
from posthog.models.tagged_item import build_check


Expand Down Expand Up @@ -105,7 +105,7 @@ def save(self, *args, **kwargs) -> None:
if self.insight is not None:
has_no_filters_hash = self.filters_hash is None
if has_no_filters_hash and self.insight.filters != {}:
self.filters_hash = generate_insight_cache_key(self.insight, self.dashboard)
self.filters_hash = generate_insight_filters_hash(self.insight, self.dashboard)

if "update_fields" in kwargs:
kwargs["update_fields"].append("filters_hash")
Expand Down
2 changes: 1 addition & 1 deletion posthog/models/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Meta:


@timed("generate_insight_cache_key")
def generate_insight_cache_key(insight: Insight, dashboard: Optional[Dashboard]) -> str:
def generate_insight_filters_hash(insight: Insight, dashboard: Optional[Dashboard]) -> str:
try:
dashboard_insight_filter = get_filter(data=insight.dashboard_filters(dashboard=dashboard), team=insight.team)
candidate_filters_hash = generate_cache_key("{}_{}".format(dashboard_insight_filter.toJSON(), insight.team_id))
Expand Down
14 changes: 7 additions & 7 deletions posthog/models/test/test_insight_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.utils import IntegrityError

from posthog.models import Dashboard, Insight, Team
from posthog.models.insight import generate_insight_cache_key
from posthog.models.insight import generate_insight_filters_hash
from posthog.test.base import BaseTest


Expand Down Expand Up @@ -76,26 +76,26 @@ def test_dashboard_does_not_affect_filters_hash_with_absent_date_from(self) -> N
insight = Insight.objects.create(team=self.team, filters={"date_from": "-30d"})
dashboard = Dashboard.objects.create(team=self.team, filters={})

filters_hash_no_dashboard = generate_insight_cache_key(insight, None)
filters_hash_with_absent_date_from = generate_insight_cache_key(insight, dashboard)
filters_hash_no_dashboard = generate_insight_filters_hash(insight, None)
filters_hash_with_absent_date_from = generate_insight_filters_hash(insight, dashboard)

assert filters_hash_no_dashboard == filters_hash_with_absent_date_from

def test_dashboard_does_not_affect_filters_hash_with_null_date_from(self) -> None:
insight = Insight.objects.create(team=self.team, filters={"date_from": "-30d"})
dashboard = Dashboard.objects.create(team=self.team, filters={"date_from": None})

filters_hash_no_dashboard = generate_insight_cache_key(insight, None)
filters_hash_with_null_date_from = generate_insight_cache_key(insight, dashboard)
filters_hash_no_dashboard = generate_insight_filters_hash(insight, None)
filters_hash_with_null_date_from = generate_insight_filters_hash(insight, dashboard)

assert filters_hash_no_dashboard == filters_hash_with_null_date_from

def test_dashboard_with_date_from_changes_filters_hash(self) -> None:
insight = Insight.objects.create(team=self.team, filters={"date_from": "-30d"})
dashboard = Dashboard.objects.create(team=self.team, filters={"date_from": "-90d"})

filters_hash_one = generate_insight_cache_key(insight, None)
filters_hash_two = generate_insight_cache_key(insight, dashboard)
filters_hash_one = generate_insight_filters_hash(insight, None)
filters_hash_two = generate_insight_filters_hash(insight, dashboard)

assert filters_hash_one != filters_hash_two

Expand Down

0 comments on commit 0c0bb44

Please sign in to comment.