Skip to content

Commit a65df0c

Browse files
authored
fix(dashboards): prebuilt dashboards not syncing titles (#104533)
Sync dashboard prebuilt titles if they change
1 parent 5a974c0 commit a65df0c

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/sentry/dashboards/endpoints/organization_dashboards.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class PrebuiltDashboard(TypedDict):
100100
def sync_prebuilt_dashboards(organization: Organization) -> None:
101101
"""
102102
Queries the database to check if prebuilt dashboards have a Dashboard record and
103-
creates them if they don't, or deletes them if they should no longer exist.
103+
creates them if they don't, updates titles if they've changed, or deletes them
104+
if they should no longer exist.
104105
"""
105106

106107
with transaction.atomic(router.db_for_write(Dashboard)):
@@ -109,22 +110,28 @@ def sync_prebuilt_dashboards(organization: Organization) -> None:
109110
prebuilt_id__isnull=False,
110111
)
111112

112-
saved_prebuilt_dashboard_ids = set(
113-
saved_prebuilt_dashboards.values_list("prebuilt_id", flat=True)
114-
)
113+
saved_prebuilt_dashboard_map = {d.prebuilt_id: d for d in saved_prebuilt_dashboards}
115114

116-
# Create prebuilt dashboards if they don't exist
115+
# Create prebuilt dashboards if they don't exist, or update titles if changed
116+
dashboards_to_update: list[Dashboard] = []
117117
for prebuilt_dashboard in PREBUILT_DASHBOARDS:
118118
prebuilt_id: PrebuiltDashboardId = prebuilt_dashboard["prebuilt_id"]
119119

120-
if prebuilt_id not in saved_prebuilt_dashboard_ids:
120+
if prebuilt_id not in saved_prebuilt_dashboard_map:
121121
# Create new dashboard
122122
Dashboard.objects.create(
123123
organization=organization,
124124
title=prebuilt_dashboard["title"],
125125
created_by_id=None,
126126
prebuilt_id=prebuilt_id,
127127
)
128+
elif saved_prebuilt_dashboard_map[prebuilt_id].title != prebuilt_dashboard["title"]:
129+
# Update title if changed
130+
saved_prebuilt_dashboard_map[prebuilt_id].title = prebuilt_dashboard["title"]
131+
dashboards_to_update.append(saved_prebuilt_dashboard_map[prebuilt_id])
132+
133+
if dashboards_to_update:
134+
Dashboard.objects.bulk_update(dashboards_to_update, ["title"])
128135

129136
# Delete old prebuilt dashboards if they should no longer exist
130137
prebuilt_ids = [d["prebuilt_id"] for d in PREBUILT_DASHBOARDS]

0 commit comments

Comments
 (0)