queries.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package plugins
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. m "github.com/grafana/grafana/pkg/models"
  5. )
  6. func GetOrgAppSettings(orgId int64) (map[string]*m.AppSettings, error) {
  7. query := m.GetAppSettingsQuery{OrgId: orgId}
  8. if err := bus.Dispatch(&query); err != nil {
  9. return nil, err
  10. }
  11. orgAppsMap := make(map[string]*m.AppSettings)
  12. for _, orgApp := range query.Result {
  13. orgAppsMap[orgApp.AppId] = orgApp
  14. }
  15. return orgAppsMap, nil
  16. }
  17. func GetEnabledPlugins(orgId int64) (*EnabledPlugins, error) {
  18. enabledPlugins := NewEnabledPlugins()
  19. orgApps, err := GetOrgAppSettings(orgId)
  20. if err != nil {
  21. return nil, err
  22. }
  23. seenPanels := make(map[string]bool)
  24. seenApi := make(map[string]bool)
  25. for appId, installedApp := range Apps {
  26. var app AppPlugin
  27. app = *installedApp
  28. // check if the app is stored in the DB for this org and if so, use the
  29. // state stored there.
  30. if b, ok := orgApps[appId]; ok {
  31. app.Enabled = b.Enabled
  32. app.Pinned = b.Pinned
  33. }
  34. if app.Enabled {
  35. enabledPlugins.Apps = append(enabledPlugins.Apps, &app)
  36. }
  37. }
  38. // add all plugins that are not part of an App.
  39. for d, installedDs := range DataSources {
  40. if installedDs.App == "" {
  41. enabledPlugins.DataSources[d] = installedDs
  42. }
  43. }
  44. for p, panel := range Panels {
  45. if panel.App == "" {
  46. if _, ok := seenPanels[p]; !ok {
  47. seenPanels[p] = true
  48. enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
  49. }
  50. }
  51. }
  52. for a, api := range ApiPlugins {
  53. if api.App == "" {
  54. if _, ok := seenApi[a]; !ok {
  55. seenApi[a] = true
  56. enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
  57. }
  58. }
  59. }
  60. return &enabledPlugins, nil
  61. }