Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard Item date range filters #5395

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions posthog/api/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,6 @@ def get_last_refresh(self, dashboard_item: DashboardItem):
dashboard_item.save()
return None

def to_representation(self, instance):
representation = super().to_representation(instance)
representation["filters"] = instance.dashboard_filters(dashboard=self.context.get("dashboard"))
return representation


class DashboardItemsViewSet(StructuredViewSetMixin, viewsets.ModelViewSet):
legacy_team_compatibility = True # to be moved to a separate Legacy*ViewSet Class
Expand Down
5 changes: 0 additions & 5 deletions posthog/api/insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ class Meta:
def create(self, validated_data: Dict, *args: Any, **kwargs: Any) -> Any:
raise NotImplementedError()

def to_representation(self, instance):
representation = super().to_representation(instance)
representation["filters"] = instance.dashboard_filters()
return representation


class InsightSerializer(InsightBasicSerializer):
result = serializers.SerializerMethodField()
Expand Down
97 changes: 97 additions & 0 deletions posthog/api/test/test_insight.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,103 @@ def test_save_new_funnel(self):
self.assertEqual(objects[0].filters["layout"], "horizontal")
self.assertEqual(len(objects[0].short_id), 8)

def test_update_funnel_in_dashboard(self):

dashboard = Dashboard.objects.create(name="My Dashboard", team=self.team, filters={"date_from": "-30d",})
item = DashboardItem.objects.create(
team=self.team,
dashboard=dashboard,
filters={
"insight": "FUNNELS",
"events": [
{
"id": "$pageview",
"math": None,
"name": "$pageview",
"type": "events",
"order": 0,
"properties": [],
"math_property": None,
},
{
"id": "$rageclick",
"math": None,
"name": "$rageclick",
"type": "events",
"order": 2,
"properties": [],
"math_property": None,
},
],
"display": "FunnelViz",
"interval": "day",
"date_from": "-30d",
},
)

response = self.client.patch(
f"/api/insight/{item.id}",
data={
"filters": {
"insight": "FUNNELS",
"events": [
{
"id": "$pageview",
"math": None,
"name": "$pageview",
"type": "events",
"order": 0,
"properties": [],
"math_property": None,
},
{
"id": "$rageclick",
"math": None,
"name": "$rageclick",
"type": "events",
"order": 2,
"properties": [],
"math_property": None,
},
],
"display": "FunnelViz",
"interval": "day",
"date_from": "-90d", # updating dates in item
"actions": [],
"new_entity": [],
"layout": "horizontal",
},
"name": "My Funnel One",
"dashboard": dashboard.pk,
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

objects = DashboardItem.objects.all()
self.assertEqual(len(objects), 1)
self.assertEqual(objects[0].filters["events"][1]["id"], "$rageclick")
self.assertEqual(objects[0].filters["display"], "FunnelViz")
self.assertEqual(objects[0].filters["interval"], "day")
self.assertEqual(objects[0].filters["date_from"], "-90d")
self.assertEqual(objects[0].filters["layout"], "horizontal")
self.assertEqual(len(objects[0].short_id), 8)

response = self.client.get(f"/api/dashboard/{dashboard.id}")
self.assertEqual(response.status_code, status.HTTP_200_OK)

response_data = response.json()
self.assertEqual(response_data["name"], "My Dashboard")
self.assertEqual(response_data["filters"]["date_from"], "-30d")
self.assertEqual(response_data["items"][0]["filters"]["date_from"], "-90d")

response = self.client.patch(f"/api/dashboard/{dashboard.id}", {"filters": {"date_from": "-90d"}},)
self.assertEqual(response.status_code, status.HTTP_200_OK)

response_data = response.json()
self.assertEqual(response_data["name"], "My Dashboard")
self.assertEqual(response_data["filters"]["date_from"], "-90d")
self.assertEqual(response_data["items"][0]["filters"]["date_from"], "-90d")

# BASIC TESTING OF ENDPOINTS. /queries as in depth testing for each insight

def test_insight_trends_basic(self):
Expand Down
9 changes: 7 additions & 2 deletions posthog/models/dashboard_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def dashboard_saved(sender, instance: Dashboard, **kwargs):
@receiver(pre_save, sender=DashboardItem)
def dashboard_item_saved(sender, instance: DashboardItem, dashboard=None, **kwargs):
if instance.filters and instance.filters != {}:
filter = get_filter(data=instance.dashboard_filters(dashboard=dashboard), team=instance.team)

#  priority to dashboard filters when saving Dashboard
# priority to dashboard item filters otherwise
if dashboard:
data = {**instance.filters, **instance.dashboard_filters(dashboard=dashboard)}
else:
data = {**instance.dashboard_filters(dashboard=dashboard), **instance.filters}
filter = get_filter(data=data, team=instance.team)
instance.filters = filter.to_dict()
instance.filters_hash = generate_cache_key("{}_{}".format(filter.toJSON(), instance.team_id))