dashboards.go 2.8 KB

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