Skip to content

Commit

Permalink
Updating behavior or dashboard to not be so intrusive.
Browse files Browse the repository at this point in the history
Fixes #296

Previous behavior:
  1. Delete all monitored dashboards
  2. Upload all dashboards from backup.

New Behavior:
  1. Upload all dashboards from backup overwriting existing Dashboards.
  2. Delete any dashboards that exists in grafana but are missing from backup.
  • Loading branch information
safaci2000 committed Jan 16, 2025
1 parent 0bb1b07 commit 037ce4c
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions internal/service/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,7 @@ func (s *DashNGoImpl) UploadDashboards(filterReq filters.Filter) error {

}

// Delete all dashboards that match prior to import
s.DeleteAllDashboards(filterReq)
currentDashboards := s.ListDashboards(filterReq)

folderUidMap := s.getFolderNameUIDMap(s.ListFolders(NewFolderFilter()))

Expand Down Expand Up @@ -641,9 +640,32 @@ func (s *DashNGoImpl) UploadDashboards(filterReq filters.Filter) error {
}

}

for _, item := range currentDashboards {
if ok, _ := alreadyProcessed[item.UID]; !ok {

Check failure on line 645 in internal/service/dashboards.go

View workflow job for this annotation

GitHub Actions / code_scanning (lint)

S1005: unnecessary assignment to the blank identifier (gosimple)
slog.Info("Deleting Dashboard not found in backup", "folder", item.FolderTitle, "dashboard", item.Title)
err := s.deleteDashboard(item)
if err != nil {
slog.Error("Unable to delete dashboard", "folder", item.FolderTitle, "dashboard", item.Title)
}
}
}
return nil
}

// deleteDashboard removes a dashboard from grafana. If the dashboard doesn't exist,
// an error is returned.
//
// Parameters:
// item - dashboard to be deleted
//
// Returns:
// error - error returned from the grafana API
func (s *DashNGoImpl) deleteDashboard(item *models.Hit) error {
_, err := s.GetClient().Dashboards.DeleteDashboardByUID(item.UID)
return err
}

// DeleteAllDashboards clears all current dashboards being monitored. Any folder not white listed
// will not be affected
func (s *DashNGoImpl) DeleteAllDashboards(filter filters.Filter) []string {
Expand All @@ -652,7 +674,7 @@ func (s *DashNGoImpl) DeleteAllDashboards(filter filters.Filter) []string {
items := s.ListDashboards(filter)
for _, item := range items {
if filter.ValidateAll(map[filters.FilterType]string{filters.FolderFilter: item.FolderTitle, filters.DashFilter: item.Slug}) {
_, err := s.GetClient().Dashboards.DeleteDashboardByUID(item.UID)
err := s.deleteDashboard(item)
if err == nil {
dashboardListing = append(dashboardListing, item.Title)
} else {
Expand Down

0 comments on commit 037ce4c

Please sign in to comment.