dashboards_updater.go 3.3 KB

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