Selaa lähdekoodia

feat(apps): plugin dashboard sync is starting to work

Torkel Ödegaard 9 vuotta sitten
vanhempi
commit
ebdf0564eb
2 muutettua tiedostoa jossa 39 lisäystä ja 9 poistoa
  1. 14 0
      pkg/plugins/dashboards.go
  2. 25 9
      pkg/plugins/dashboards_updater.go

+ 14 - 0
pkg/plugins/dashboards.go

@@ -14,6 +14,7 @@ type PluginDashboardInfoDTO struct {
 	Title            string `json:"title"`
 	Imported         bool   `json:"imported"`
 	ImportedUri      string `json:"importedUri"`
+	Slug             string `json:"slug"`
 	ImportedRevision int64  `json:"importedRevision"`
 	Revision         int64  `json:"revision"`
 	Description      string `json:"description"`
@@ -36,6 +37,8 @@ func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDT
 		return nil, err
 	}
 
+	existingMatches := make(map[int64]bool)
+
 	for _, include := range plugin.Includes {
 		if include.Type != PluginTypeDashboard {
 			continue
@@ -60,12 +63,23 @@ func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDT
 				res.Imported = true
 				res.ImportedUri = "db/" + existingDash.Slug
 				res.ImportedRevision = existingDash.Data.Get("revision").MustInt64(1)
+				existingMatches[existingDash.Id] = true
 			}
 		}
 
 		result = append(result, res)
 	}
 
+	// find deleted dashboards
+	for _, dash := range query.Result {
+		if _, exists := existingMatches[dash.Id]; !exists {
+			result = append(result, &PluginDashboardInfoDTO{
+				Slug:    dash.Slug,
+				Removed: true,
+			})
+		}
+	}
+
 	return result, nil
 }
 

+ 25 - 9
pkg/plugins/dashboards_updater.go

@@ -62,17 +62,33 @@ func syncPluginDashboards(pluginDef *PluginBase, orgId int64) {
 	plog.Info("Syncing plugin dashboards to DB", "pluginId", pluginDef.Id)
 
 	// Get plugin dashboards
-	if dashboards, err := GetPluginDashboards(orgId, pluginDef.Id); err != nil {
+	dashboards, err := GetPluginDashboards(orgId, pluginDef.Id)
+
+	if err != nil {
 		plog.Error("Failed to load app dashboards", "error", err)
 		return
-	} else {
-		// Update dashboards with updated revisions
-		for _, dash := range dashboards {
-			if dash.ImportedRevision != dash.Revision {
-				if err := autoUpdateAppDashboard(dash, orgId); err != nil {
-					plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
-					return
-				}
+	}
+
+	// Update dashboards with updated revisions
+	for _, dash := range dashboards {
+		// remove removed ones
+		if dash.Removed {
+			plog.Info("Deleting plugin dashboard", "pluginId", pluginDef.Id, "dashboard", dash.Slug)
+
+			deleteCmd := m.DeleteDashboardCommand{OrgId: orgId, Slug: dash.Slug}
+			if err := bus.Dispatch(&deleteCmd); err != nil {
+				plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
+				return
+			}
+
+			continue
+		}
+
+		// update updated ones
+		if dash.ImportedRevision != dash.Revision {
+			if err := autoUpdateAppDashboard(dash, orgId); err != nil {
+				plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
+				return
 			}
 		}
 	}