コード例 #1
0
ファイル: test_update_cache.py プロジェクト: GalDayan/posthog
    def _test_refresh_dashboard_cache_types(
        self,
        filter: FilterType,
        cache_type: CacheType,
        patch_update_cache_item: MagicMock,
    ) -> None:
        self._create_dashboard(filter)

        update_cached_items()

        expected_args = [
            generate_cache_key("{}_{}".format(filter.toJSON(), self.team.pk)),
            cache_type,
            {
                "filter": filter.toJSON(),
                "team_id": self.team.pk,
            },
        ]

        patch_update_cache_item.assert_any_call(*expected_args)

        update_cache_item(*expected_args)  # type: ignore

        item_key = generate_cache_key("{}_{}".format(filter.toJSON(),
                                                     self.team.pk))
        self.assertIsNotNone(get_safe_cache(item_key))
コード例 #2
0
    def test_update_cache_item_calls_right_class(
            self, patch_import_from: MagicMock) -> None:
        filter = Filter(data={
            "insight": "TRENDS",
            "events": [{
                "id": "$pageview"
            }]
        })
        dashboard_item = self._create_dashboard(filter)

        with self.settings(EE_AVAILABLE=False):
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(),
                                                  self.team.pk)),
                CacheType.TRENDS,
                {
                    "filter": filter.toJSON(),
                    "team_id": self.team.pk,
                },
            )

        patch_import_from.assert_called_once_with("posthog.queries.trends",
                                                  "Trends")

        updated_dashboard_item = DashboardItem.objects.get(
            pk=dashboard_item.pk)
        self.assertEqual(updated_dashboard_item.refreshing, False)
        self.assertEqual(updated_dashboard_item.last_refresh, now())
コード例 #3
0
ファイル: test_update_cache.py プロジェクト: copyit/posthog
    def test_update_cache_item_calls_right_funnel_class(self, funnel_mock: MagicMock) -> None:
        #  basic funnel
        filter = Filter(
            data={
                "insight": "FUNNELS",
                "events": [
                    {"id": "$pageview", "order": 0, "type": "events"},
                    {"id": "$pageview", "order": 1, "type": "events"},
                ],
            }
        )
        dashboard_item = self._create_dashboard(filter)

        funnel_mock.return_value.run.return_value = {}
        with self.settings(EE_AVAILABLE=False):
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(), self.team.pk)),
                CacheType.FUNNEL,
                {"filter": filter.toJSON(), "team_id": self.team.pk,},
            )

        updated_dashboard_item = DashboardItem.objects.get(pk=dashboard_item.pk)
        self.assertEqual(updated_dashboard_item.refreshing, False)
        self.assertEqual(updated_dashboard_item.last_refresh, now())
        funnel_mock.assert_called_once()
コード例 #4
0
    def _test_refresh_dashboard_cache_types(
        self,
        filter: FilterType,
        patch_update_cache_item: MagicMock,
        patch_apply_async: MagicMock,
    ) -> None:

        dashboard_to_cache = Dashboard.objects.create(team=self.team,
                                                      is_shared=True,
                                                      last_accessed_at=now())

        DashboardItem.objects.create(
            dashboard=dashboard_to_cache,
            filters=filter.to_dict(),
            team=self.team,
            last_refresh=now() - timedelta(days=30),
        )
        update_cached_items()

        for call_item in patch_update_cache_item.call_args_list:
            update_cache_item(*call_item[0])

        item_key = generate_cache_key("{}_{}".format(filter.toJSON(),
                                                     self.team.pk))
        self.assertIsNotNone(cache.get(item_key))
コード例 #5
0
    def test_stickiness_regression(self, patch_update_cache_item: MagicMock, patch_apply_async: MagicMock) -> None:
        # We moved Stickiness from being a "shown_as" item to its own insight
        # This move caused issues hence a regression test
        filter_stickiness = StickinessFilter(
            data={
                "events": [{"id": "$pageview"}],
                "properties": [{"key": "$browser", "value": "Mac OS X"}],
                "date_from": "2012-01-10",
                "date_to": "2012-01-15",
                "insight": INSIGHT_STICKINESS,
                "shown_as": "Stickiness",
            },
            team=self.team,
            get_earliest_timestamp=Event.objects.earliest_timestamp,
        )
        filter = Filter(
            data={
                "events": [{"id": "$pageview"}],
                "properties": [{"key": "$browser", "value": "Mac OS X"}],
                "date_from": "2012-01-10",
                "date_to": "2012-01-15",
            }
        )
        shared_dashboard = Dashboard.objects.create(team=self.team, is_shared=True)

        DashboardItem.objects.create(dashboard=shared_dashboard, filters=filter_stickiness.to_dict(), team=self.team)
        DashboardItem.objects.create(dashboard=shared_dashboard, filters=filter.to_dict(), team=self.team)

        item_stickiness_key = generate_cache_key(filter_stickiness.toJSON() + "_" + str(self.team.pk))
        item_key = generate_cache_key(filter.toJSON() + "_" + str(self.team.pk))

        update_cached_items()

        for call_item in patch_update_cache_item.call_args_list:
            update_cache_item(*call_item[0])

        self.assertEqual(
            get_safe_cache(item_stickiness_key)["result"][0]["labels"],
            ["1 day", "2 days", "3 days", "4 days", "5 days", "6 days"],
        )
        self.assertEqual(
            get_safe_cache(item_key)["result"][0]["labels"],
            [
                "Tue. 10 January",
                "Wed. 11 January",
                "Thu. 12 January",
                "Fri. 13 January",
                "Sat. 14 January",
                "Sun. 15 January",
            ],
        )
コード例 #6
0
    def test_refresh_dashboard_cache(self, patch_update_cache_item: MagicMock, patch_apply_async: MagicMock) -> None:
        # There's two things we want to refresh
        # Any shared dashboard, as we only use cached items to show those
        # Any dashboard accessed in the last 7 days
        filter_dict = {
            "events": [{"id": "$pageview"}],
            "properties": [{"key": "$browser", "value": "Mac OS X"}],
        }
        filter = Filter(data=filter_dict)
        shared_dashboard = Dashboard.objects.create(team=self.team, is_shared=True)
        funnel_filter = Filter(data={"events": [{"id": "user signed up", "type": "events", "order": 0},],})

        item = DashboardItem.objects.create(dashboard=shared_dashboard, filters=filter.to_dict(), team=self.team)
        funnel_item = DashboardItem.objects.create(
            dashboard=shared_dashboard, filters=funnel_filter.to_dict(), team=self.team
        )

        dashboard_to_cache = Dashboard.objects.create(team=self.team, is_shared=True, last_accessed_at=now())
        item_to_cache = DashboardItem.objects.create(
            dashboard=dashboard_to_cache,
            filters=Filter(data={"events": [{"id": "cache this"}]}).to_dict(),
            team=self.team,
        )

        dashboard_do_not_cache = Dashboard.objects.create(
            team=self.team, is_shared=True, last_accessed_at="2020-01-01T12:00:00Z"
        )
        item_do_not_cache = DashboardItem.objects.create(
            dashboard=dashboard_do_not_cache,
            filters=Filter(data={"events": [{"id": "do not cache this"}]}).to_dict(),
            team=self.team,
        )

        item_key = generate_cache_key(filter.toJSON() + "_" + str(self.team.pk))
        funnel_key = generate_cache_key(filter.toJSON() + "_" + str(self.team.pk))
        update_cached_items()

        # pass the caught calls straight to the function
        # we do this to skip Redis
        for call_item in patch_update_cache_item.call_args_list:
            update_cache_item(*call_item[0])

        self.assertIsNotNone(DashboardItem.objects.get(pk=item.pk).last_refresh)
        self.assertIsNotNone(DashboardItem.objects.get(pk=item_to_cache.pk).last_refresh)
        self.assertIsNotNone(DashboardItem.objects.get(pk=item_do_not_cache.pk).last_refresh)
        self.assertEqual(get_safe_cache(item_key)["result"][0]["count"], 0)
        self.assertEqual(get_safe_cache(funnel_key)["result"][0]["count"], 0)
コード例 #7
0
    def test_update_cache_item_calls_right_class(self) -> None:
        filter = Filter(data={
            "insight": "TRENDS",
            "events": [{
                "id": "$pageview"
            }]
        })
        dashboard_item = self._create_dashboard(filter)

        update_cache_item(
            generate_cache_key("{}_{}".format(filter.toJSON(), self.team.pk)),
            CacheType.TRENDS,
            {
                "filter": filter.toJSON(),
                "team_id": self.team.pk,
            },
        )

        updated_dashboard_item = Insight.objects.get(pk=dashboard_item.pk)
        self.assertEqual(updated_dashboard_item.refreshing, False)
        self.assertEqual(updated_dashboard_item.last_refresh, now())
コード例 #8
0
ファイル: celery.py プロジェクト: coherentdigital/posthog
def update_cache_item_task(key: str, cache_type, payload: dict) -> None:
    from posthog.tasks.update_cache import update_cache_item

    update_cache_item(key, cache_type, payload)
コード例 #9
0
ファイル: test_update_cache.py プロジェクト: GalDayan/posthog
    def test_update_cache_item_calls_right_funnel_class_clickhouse(
        self,
        funnel_mock: MagicMock,
        funnel_trends_mock: MagicMock,
        funnel_time_to_convert_mock: MagicMock,
        funnel_strict_mock: MagicMock,
        funnel_unordered_mock: MagicMock,
    ) -> None:
        #  basic funnel
        base_filter = Filter(
            data={
                "insight":
                "FUNNELS",
                "events": [
                    {
                        "id": "$pageview",
                        "order": 0,
                        "type": "events"
                    },
                    {
                        "id": "$pageview",
                        "order": 1,
                        "type": "events"
                    },
                ],
            })

        with self.settings(EE_AVAILABLE=True, PRIMARY_DB="clickhouse"):
            filter = base_filter
            funnel_mock.return_value.run.return_value = {}
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(),
                                                  self.team.pk)),
                CacheType.FUNNEL,
                {
                    "filter": filter.toJSON(),
                    "team_id": self.team.pk,
                },
            )
            funnel_mock.assert_called_once()

            # trends funnel
            filter = base_filter.with_data({"funnel_viz_type": "trends"})
            funnel_trends_mock.return_value.run.return_value = {}
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(),
                                                  self.team.pk)),
                CacheType.FUNNEL,
                {
                    "filter": filter.toJSON(),
                    "team_id": self.team.pk,
                },
            )

            funnel_trends_mock.assert_called_once()
            self.assertEqual(
                funnel_trends_mock.call_args[1]["funnel_order_class"],
                funnel_mock)
            funnel_trends_mock.reset_mock()

            # trends unordered funnel
            filter = base_filter.with_data({
                "funnel_viz_type": "trends",
                "funnel_order_type": "unordered"
            })
            funnel_trends_mock.return_value.run.return_value = {}
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(),
                                                  self.team.pk)),
                CacheType.FUNNEL,
                {
                    "filter": filter.toJSON(),
                    "team_id": self.team.pk,
                },
            )

            funnel_trends_mock.assert_called_once()
            self.assertEqual(
                funnel_trends_mock.call_args[1]["funnel_order_class"],
                funnel_unordered_mock)
            funnel_trends_mock.reset_mock()

            # time to convert strict funnel
            filter = base_filter.with_data({
                "funnel_viz_type": "time_to_convert",
                "funnel_order_type": "strict"
            })
            funnel_time_to_convert_mock.return_value.run.return_value = {}
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(),
                                                  self.team.pk)),
                CacheType.FUNNEL,
                {
                    "filter": filter.toJSON(),
                    "team_id": self.team.pk,
                },
            )

            funnel_time_to_convert_mock.assert_called_once()
            self.assertEqual(
                funnel_time_to_convert_mock.call_args[1]["funnel_order_class"],
                funnel_strict_mock)
            funnel_time_to_convert_mock.reset_mock()

            # strict funnel
            filter = base_filter.with_data({"funnel_order_type": "strict"})
            funnel_strict_mock.return_value.run.return_value = {}
            update_cache_item(
                generate_cache_key("{}_{}".format(filter.toJSON(),
                                                  self.team.pk)),
                CacheType.FUNNEL,
                {
                    "filter": filter.toJSON(),
                    "team_id": self.team.pk,
                },
            )

            funnel_strict_mock.assert_called_once()