Skip to content
Merged
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
19 changes: 13 additions & 6 deletions src/sentry/dashboards/endpoints/organization_dashboards.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class PrebuiltDashboard(TypedDict):
def sync_prebuilt_dashboards(organization: Organization) -> None:
"""
Queries the database to check if prebuilt dashboards have a Dashboard record and
creates them if they don't, or deletes them if they should no longer exist.
creates them if they don't, updates titles if they've changed, or deletes them
if they should no longer exist.
"""

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

saved_prebuilt_dashboard_ids = set(
saved_prebuilt_dashboards.values_list("prebuilt_id", flat=True)
)
saved_prebuilt_dashboard_map = {d.prebuilt_id: d for d in saved_prebuilt_dashboards}

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

if prebuilt_id not in saved_prebuilt_dashboard_ids:
if prebuilt_id not in saved_prebuilt_dashboard_map:
# Create new dashboard
Dashboard.objects.create(
organization=organization,
title=prebuilt_dashboard["title"],
created_by_id=None,
prebuilt_id=prebuilt_id,
)
elif saved_prebuilt_dashboard_map[prebuilt_id].title != prebuilt_dashboard["title"]:
# Update title if changed
saved_prebuilt_dashboard_map[prebuilt_id].title = prebuilt_dashboard["title"]
dashboards_to_update.append(saved_prebuilt_dashboard_map[prebuilt_id])

if dashboards_to_update:
Dashboard.objects.bulk_update(dashboards_to_update, ["title"])

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