Skip to content

Commit 315b438

Browse files
authored
feat(dashboards): Backfill positions when requested in starred endpoint (#93920)
If any of the dashboards that are requested have position unset, then take the order and commit them so the position is backfilled There shouldn't be a scenario where there are positioned dashboards and unpositioned dashboards, but the test case shows that it can be handled. This is meant to fill in data for a user's first visit. When we see that the data is all backfilled we can remove the code related to this check.
1 parent 99c3e1c commit 315b438

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/sentry/api/endpoints/organization_dashboards_starred.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def get(self, request: Request, organization: Organization) -> Response:
4646
organization=organization, user_id=request.user.id
4747
).select_related("dashboard")
4848

49+
if favorites.filter(position__isnull=True).exists():
50+
# Commit the order of the dashboards of this current response if there are any
51+
# that don't have a position assigned
52+
DashboardFavoriteUser.objects.reorder_favorite_dashboards(
53+
organization=organization,
54+
user_id=request.user.id,
55+
new_dashboard_positions=[favorite.dashboard.id for favorite in favorites],
56+
)
57+
4958
def data_fn(offset, limit):
5059
return [favorite.dashboard for favorite in favorites[offset : offset + limit]]
5160

tests/sentry/api/endpoints/test_organization_dashboards_starred.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,41 @@ def test_get_favorite_dashboards(self):
4747
self.dashboard_1.id,
4848
]
4949

50+
def test_get_request_assigns_positions_if_missing(self):
51+
self.create_dashboard_favorite(self.dashboard_1, self.user, self.organization, None)
52+
self.create_dashboard_favorite(self.dashboard_2, self.user, self.organization, 2)
53+
self.create_dashboard_favorite(self.dashboard_3, self.user, self.organization, None)
54+
55+
response = self.do_request("get", self.url)
56+
assert response.status_code == 200
57+
assert len(response.data) == 3
58+
assert [int(dashboard["id"]) for dashboard in response.data] == [
59+
self.dashboard_2.id,
60+
self.dashboard_1.id,
61+
self.dashboard_3.id,
62+
]
63+
64+
assert (
65+
DashboardFavoriteUser.objects.get(
66+
organization=self.organization, user_id=self.user.id, dashboard=self.dashboard_1
67+
).position
68+
== 1
69+
)
70+
assert (
71+
DashboardFavoriteUser.objects.get(
72+
organization=self.organization, user_id=self.user.id, dashboard=self.dashboard_3
73+
).position
74+
== 2
75+
)
76+
77+
# Positioned dashboards are at the top of the list in this operation
78+
assert (
79+
DashboardFavoriteUser.objects.get(
80+
organization=self.organization, user_id=self.user.id, dashboard=self.dashboard_2
81+
).position
82+
== 0
83+
)
84+
5085

5186
class OrganizationDashboardsStarredOrderTest(StarredDashboardTestCase):
5287
def setUp(self):

0 commit comments

Comments
 (0)