dashboards_updater.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package plugins
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. m "github.com/grafana/grafana/pkg/models"
  5. )
  6. func init() {
  7. bus.AddEventListener(handlePluginStateChanged)
  8. }
  9. func (pm *PluginManager) updateAppDashboards() {
  10. pm.log.Debug("Looking for App Dashboard Updates")
  11. query := m.GetPluginSettingsQuery{OrgId: 0}
  12. if err := bus.Dispatch(&query); err != nil {
  13. plog.Error("Failed to get all plugin settings", "error", err)
  14. return
  15. }
  16. for _, pluginSetting := range query.Result {
  17. // ignore disabled plugins
  18. if !pluginSetting.Enabled {
  19. continue
  20. }
  21. if pluginDef, exist := Plugins[pluginSetting.PluginId]; exist {
  22. if pluginDef.Info.Version != pluginSetting.PluginVersion {
  23. syncPluginDashboards(pluginDef, pluginSetting.OrgId)
  24. }
  25. }
  26. }
  27. }
  28. func autoUpdateAppDashboard(pluginDashInfo *PluginDashboardInfoDTO, orgId int64) error {
  29. dash, err := loadPluginDashboard(pluginDashInfo.PluginId, pluginDashInfo.Path)
  30. if err != nil {
  31. return err
  32. }
  33. plog.Info("Auto updating App dashboard", "dashboard", dash.Title, "newRev", pluginDashInfo.Revision, "oldRev", pluginDashInfo.ImportedRevision)
  34. updateCmd := ImportDashboardCommand{
  35. OrgId: orgId,
  36. PluginId: pluginDashInfo.PluginId,
  37. Overwrite: true,
  38. Dashboard: dash.Data,
  39. User: &m.SignedInUser{UserId: 0, OrgRole: m.ROLE_ADMIN},
  40. Path: pluginDashInfo.Path,
  41. }
  42. if err := bus.Dispatch(&updateCmd); err != nil {
  43. return err
  44. }
  45. return nil
  46. }
  47. func syncPluginDashboards(pluginDef *PluginBase, orgId int64) {
  48. plog.Info("Syncing plugin dashboards to DB", "pluginId", pluginDef.Id)
  49. // Get plugin dashboards
  50. dashboards, err := GetPluginDashboards(orgId, pluginDef.Id)
  51. if err != nil {
  52. plog.Error("Failed to load app dashboards", "error", err)
  53. return
  54. }
  55. // Update dashboards with updated revisions
  56. for _, dash := range dashboards {
  57. // remove removed ones
  58. if dash.Removed {
  59. plog.Info("Deleting plugin dashboard", "pluginId", pluginDef.Id, "dashboard", dash.Slug)
  60. deleteCmd := m.DeleteDashboardCommand{OrgId: orgId, Id: dash.DashboardId}
  61. if err := bus.Dispatch(&deleteCmd); err != nil {
  62. plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
  63. return
  64. }
  65. continue
  66. }
  67. // update updated ones
  68. if dash.ImportedRevision != dash.Revision {
  69. if err := autoUpdateAppDashboard(dash, orgId); err != nil {
  70. plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
  71. return
  72. }
  73. }
  74. }
  75. // update version in plugin_setting table to mark that we have processed the update
  76. query := m.GetPluginSettingByIdQuery{PluginId: pluginDef.Id, OrgId: orgId}
  77. if err := bus.Dispatch(&query); err != nil {
  78. plog.Error("Failed to read plugin setting by id", "error", err)
  79. return
  80. }
  81. appSetting := query.Result
  82. cmd := m.UpdatePluginSettingVersionCmd{
  83. OrgId: appSetting.OrgId,
  84. PluginId: appSetting.PluginId,
  85. PluginVersion: pluginDef.Info.Version,
  86. }
  87. if err := bus.Dispatch(&cmd); err != nil {
  88. plog.Error("Failed to update plugin setting version", "error", err)
  89. }
  90. }
  91. func handlePluginStateChanged(event *m.PluginStateChangedEvent) error {
  92. plog.Info("Plugin state changed", "pluginId", event.PluginId, "enabled", event.Enabled)
  93. if event.Enabled {
  94. syncPluginDashboards(Plugins[event.PluginId], event.OrgId)
  95. } else {
  96. query := m.GetDashboardsByPluginIdQuery{PluginId: event.PluginId, OrgId: event.OrgId}
  97. if err := bus.Dispatch(&query); err != nil {
  98. return err
  99. }
  100. for _, dash := range query.Result {
  101. deleteCmd := m.DeleteDashboardCommand{OrgId: dash.OrgId, Id: dash.Id}
  102. plog.Info("Deleting plugin dashboard", "pluginId", event.PluginId, "dashboard", dash.Slug)
  103. if err := bus.Dispatch(&deleteCmd); err != nil {
  104. return err
  105. }
  106. }
  107. }
  108. return nil
  109. }