mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
feat(apps): plugin dashboard sync is starting to work
This commit is contained in:
parent
68a8d9bc91
commit
ebdf0564eb
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user