dashboards_updater.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 * 5)
  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. dashboards, err := GetPluginDashboards(orgId, pluginDef.Id)
  53. if err != nil {
  54. plog.Error("Failed to load app dashboards", "error", err)
  55. return
  56. }
  57. // Update dashboards with updated revisions
  58. for _, dash := range dashboards {
  59. // remove removed ones
  60. if dash.Removed {
  61. plog.Info("Deleting plugin dashboard", "pluginId", pluginDef.Id, "dashboard", dash.Slug)
  62. deleteCmd := m.DeleteDashboardCommand{OrgId: orgId, Id: dash.DashboardId}
  63. if err := bus.Dispatch(&deleteCmd); err != nil {
  64. plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
  65. return
  66. }
  67. continue
  68. }
  69. // update updated ones
  70. if dash.ImportedRevision != dash.Revision {
  71. if err := autoUpdateAppDashboard(dash, orgId); err != nil {
  72. plog.Error("Failed to auto update app dashboard", "pluginId", pluginDef.Id, "error", err)
  73. return
  74. }
  75. }
  76. }
  77. // update version in plugin_setting table to mark that we have processed the update
  78. query := m.GetPluginSettingByIdQuery{PluginId: pluginDef.Id, OrgId: orgId}
  79. if err := bus.Dispatch(&query); err != nil {
  80. plog.Error("Failed to read plugin setting by id", "error", err)
  81. return
  82. }
  83. appSetting := query.Result
  84. cmd := m.UpdatePluginSettingVersionCmd{
  85. OrgId: appSetting.OrgId,
  86. PluginId: appSetting.PluginId,
  87. PluginVersion: pluginDef.Info.Version,
  88. }
  89. if err := bus.Dispatch(&cmd); err != nil {
  90. plog.Error("Failed to update plugin setting version", "error", err)
  91. }
  92. }
  93. func handlePluginStateChanged(event *m.PluginStateChangedEvent) error {
  94. plog.Info("Plugin state changed", "pluginId", event.PluginId, "enabled", event.Enabled)
  95. if event.Enabled {
  96. syncPluginDashboards(Plugins[event.PluginId], event.OrgId)
  97. } else {
  98. query := m.GetDashboardsByPluginIdQuery{PluginId: event.PluginId, OrgId: event.OrgId}
  99. if err := bus.Dispatch(&query); err != nil {
  100. return err
  101. } else {
  102. for _, dash := range query.Result {
  103. deleteCmd := m.DeleteDashboardCommand{OrgId: dash.OrgId, Id: dash.Id}
  104. plog.Info("Deleting plugin dashboard", "pluginId", event.PluginId, "dashboard", dash.Slug)
  105. if err := bus.Dispatch(&deleteCmd); err != nil {
  106. return err
  107. }
  108. }
  109. }
  110. }
  111. return nil
  112. }