Skip to content

Commit

Permalink
feat(dashboards): enable 'pinning' favorites in dashboards endpoint (#…
Browse files Browse the repository at this point in the history
…81811)

Add new parameter, `pin` to the `dashboards` endpoint.

This will enable pinning favourited dashboards (i.e. always on top) in
the dashboards grid and table views.
  • Loading branch information
harshithadurai authored Dec 6, 2024
1 parent c29f456 commit da89770
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/sentry/api/endpoints/organization_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,21 @@ def get(self, request: Request, organization) -> Response:
else:
order_by = ["title"]

dashboards = dashboards.order_by(*order_by)
if features.has("organizations:dashboards-favourite", organization, actor=request.user):
pin_by = request.query_params.get("pin")
if pin_by == "favorites":
order_by_favorites = [
Case(
When(dashboardfavoriteuser__user_id=request.user.id, then=-1),
default=1,
output_field=IntegerField(),
)
]
dashboards = dashboards.order_by(*order_by_favorites, *order_by)
else:
dashboards = dashboards.order_by(*order_by)
else:
dashboards = dashboards.order_by(*order_by)

list_serializer = DashboardListSerializer()

Expand All @@ -203,17 +217,14 @@ def handle_results(results):
serialized.extend(serialize(dashboards, request.user, serializer=list_serializer))
return serialized

render_pre_built_dashboard = True
if features.has("organizations:dashboards-favourite", organization, actor=request.user):
if filter_by and filter_by == "onlyFavorites" or pin_by and pin_by == "favorites":
render_pre_built_dashboard = False

return self.paginate(
request=request,
sources=(
[dashboards]
if features.has(
"organizations:dashboards-favourite", organization, actor=request.user
)
and filter_by
and filter_by == "onlyFavorites"
else [prebuilt, dashboards]
),
sources=([prebuilt, dashboards] if render_pre_built_dashboard else [dashboards]),
paginator_cls=ChainPaginator,
on_results=handle_results,
)
Expand Down
102 changes: 102 additions & 0 deletions tests/sentry/api/endpoints/test_organization_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,108 @@ def test_get_exclude_favorites_with_sort(self):
values = [row["title"] for row in response.data]
assert values == ["General", "Dashboard 1", "Dashboard 2", "Dashboard 7", "Dashboard 6"]

def test_pin_favorites_with_my_dashboards_sort(self):
user_1 = self.create_user(username="user_1")
self.create_member(organization=self.organization, user=user_1)

Dashboard.objects.create(
title="Dashboard A",
created_by_id=self.user.id,
organization=self.organization,
)
dashboard_B = Dashboard.objects.create(
title="Dashboard B",
created_by_id=user_1.id,
organization=self.organization,
)
dashboard_C = Dashboard.objects.create(
title="Dashboard C",
created_by_id=user_1.id,
organization=self.organization,
)
dashboard_D = Dashboard.objects.create(
title="Dashboard D",
created_by_id=self.user.id,
organization=self.organization,
)
dashboard_E = Dashboard.objects.create(
title="Dashboard E",
created_by_id=user_1.id,
organization=self.organization,
)

dashboard_B.favorited_by = [self.user.id]
dashboard_D.favorited_by = [self.user.id]
dashboard_E.favorited_by = [self.user.id]
dashboard_C.favorited_by = [user_1.id]

with self.feature("organizations:dashboards-favourite"):
response = self.client.get(self.url, data={"sort": "mydashboards", "pin": "favorites"})
assert response.status_code == 200, response.content
values = [row["title"] for row in response.data]
assert values == [
# favorites
"Dashboard D", # self.user's favorite
"Dashboard E", # user_1's dashboard
"Dashboard B", # user_1's dashboard
# other dashboards
"Dashboard A", # self.user's dashboard
"Dashboard 2", # self.user's dashboard
"Dashboard 1", # self.user's dashboard
"Dashboard C", # user_1's dashbaord
]

def test_pin_favorites_with_my_date_created_sort(self):
user_1 = self.create_user(username="user_1")
self.create_member(organization=self.organization, user=user_1)

Dashboard.objects.create(
title="Dashboard A",
created_by_id=self.user.id,
organization=self.organization,
)
dashboard_B = Dashboard.objects.create(
title="Dashboard B",
created_by_id=user_1.id,
organization=self.organization,
)
dashboard_C = Dashboard.objects.create(
title="Dashboard C",
created_by_id=user_1.id,
organization=self.organization,
)
dashboard_D = Dashboard.objects.create(
title="Dashboard D",
created_by_id=self.user.id,
organization=self.organization,
)
dashboard_E = Dashboard.objects.create(
title="Dashboard E",
created_by_id=user_1.id,
organization=self.organization,
)

dashboard_B.favorited_by = [self.user.id]
dashboard_D.favorited_by = [self.user.id]
dashboard_E.favorited_by = [self.user.id]
dashboard_C.favorited_by = [user_1.id]

with self.feature("organizations:dashboards-favourite"):
response = self.client.get(self.url, data={"sort": "dateCreated", "pin": "favorites"})
assert response.status_code == 200, response.content
values = [row["title"] for row in response.data]
assert values == [
# favorites
"Dashboard B",
"Dashboard D",
"Dashboard E",
# other dashboards
"Dashboard 1",
"Dashboard 2",
"Dashboard A",
"Dashboard C",
]

def test_post(self):
response = self.do_request("post", self.url, data={"title": "Dashboard from Post"})
assert response.status_code == 201
Expand Down

0 comments on commit da89770

Please sign in to comment.