def test_filter_properties(self): filter = StickinessFilter( data={ "interval": "month", "date_from": "2020-01-01T20:00:00Z", "date_to": "2020-02-01T20:00:00Z", "events": [{"id": "$pageview", "custom_name": "Custom event"}], "compare": True, }, team=self.team, get_earliest_timestamp=get_earliest_timestamp, ) self.assertEqual( filter.to_dict(), { "compare": True, "date_from": "2020-01-01T20:00:00Z", "date_to": "2020-02-01T20:00:00Z", "events": [ { "id": "$pageview", "type": "events", "order": None, "name": "$pageview", "custom_name": "Custom event", "math": None, "math_property": None, "math_group_type_index": None, "properties": {}, } ], "insight": "TRENDS", "interval": "month", }, )
def test_filter_properties(self): earliest_timestamp_func = lambda team_id: Event.objects.earliest_timestamp(team_id) filter = StickinessFilter( data={ "interval": "month", "date_from": "2020-01-01T20:00:00Z", "date_to": "2020-02-01T20:00:00Z", "events": [{"id": "$pageview"}], "compare": True, }, team=self.team, get_earliest_timestamp=earliest_timestamp_func, ) self.assertEqual( filter.to_dict(), { "compare": True, "date_from": "2020-01-01T20:00:00Z", "date_to": "2020-02-01T20:00:00Z", "events": [ { "id": "$pageview", "type": "events", "order": None, "name": "$pageview", "math": None, "math_property": None, "properties": [], } ], "insight": "TRENDS", "interval": "month", }, )
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", ], )
def _handle_stickiness_people(self, target_entity: Entity, cohort: Cohort, filter: StickinessFilter) -> None: insert_cohort_from_query.delay(cohort.pk, INSIGHT_STICKINESS, filter.to_dict(), entity_data=target_entity.to_dict())