dashboards.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package plugins
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. m "github.com/grafana/grafana/pkg/models"
  8. )
  9. type PluginDashboardInfoDTO struct {
  10. PluginId string `json:"pluginId"`
  11. Title string `json:"title"`
  12. Imported bool `json:"imported"`
  13. ImportedUri string `json:"importedUri"`
  14. ImportedUrl string `json:"importedUrl"`
  15. Slug string `json:"slug"`
  16. DashboardId int64 `json:"dashboardId"`
  17. FolderId int64 `json:"folderId"`
  18. ImportedRevision int64 `json:"importedRevision"`
  19. Revision int64 `json:"revision"`
  20. Description string `json:"description"`
  21. Path string `json:"path"`
  22. Removed bool `json:"removed"`
  23. }
  24. func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDTO, error) {
  25. plugin, exists := Plugins[pluginId]
  26. if !exists {
  27. return nil, PluginNotFoundError{pluginId}
  28. }
  29. result := make([]*PluginDashboardInfoDTO, 0)
  30. // load current dashboards
  31. query := m.GetDashboardsByPluginIdQuery{OrgId: orgId, PluginId: pluginId}
  32. if err := bus.Dispatch(&query); err != nil {
  33. return nil, err
  34. }
  35. existingMatches := make(map[int64]bool)
  36. for _, include := range plugin.Includes {
  37. if include.Type != PluginTypeDashboard {
  38. continue
  39. }
  40. res := &PluginDashboardInfoDTO{}
  41. var dashboard *m.Dashboard
  42. var err error
  43. if dashboard, err = loadPluginDashboard(plugin.Id, include.Path); err != nil {
  44. return nil, err
  45. }
  46. res.Path = include.Path
  47. res.PluginId = plugin.Id
  48. res.Title = dashboard.Title
  49. res.Revision = dashboard.Data.Get("revision").MustInt64(1)
  50. // find existing dashboard
  51. for _, existingDash := range query.Result {
  52. if existingDash.Slug == dashboard.Slug {
  53. res.DashboardId = existingDash.Id
  54. res.Imported = true
  55. res.ImportedUri = "db/" + existingDash.Slug
  56. res.ImportedUrl = existingDash.GetUrl()
  57. res.ImportedRevision = existingDash.Data.Get("revision").MustInt64(1)
  58. existingMatches[existingDash.Id] = true
  59. }
  60. }
  61. result = append(result, res)
  62. }
  63. // find deleted dashboards
  64. for _, dash := range query.Result {
  65. if _, exists := existingMatches[dash.Id]; !exists {
  66. result = append(result, &PluginDashboardInfoDTO{
  67. Slug: dash.Slug,
  68. DashboardId: dash.Id,
  69. Removed: true,
  70. })
  71. }
  72. }
  73. return result, nil
  74. }
  75. func loadPluginDashboard(pluginId, path string) (*m.Dashboard, error) {
  76. plugin, exists := Plugins[pluginId]
  77. if !exists {
  78. return nil, PluginNotFoundError{pluginId}
  79. }
  80. dashboardFilePath := filepath.Join(plugin.PluginDir, path)
  81. reader, err := os.Open(dashboardFilePath)
  82. if err != nil {
  83. return nil, err
  84. }
  85. defer reader.Close()
  86. data, err := simplejson.NewFromReader(reader)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return m.NewDashboardFromJson(data), nil
  91. }