queries.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. enabledApps := make(map[string]bool)
  24. for appId, installedApp := range Apps {
  25. var app AppPlugin
  26. app = *installedApp
  27. // check if the app is stored in the DB for this org and if so, use the
  28. // state stored there.
  29. if b, ok := orgApps[appId]; ok {
  30. app.Enabled = b.Enabled
  31. app.Pinned = b.Pinned
  32. }
  33. if app.Enabled {
  34. enabledApps[app.Id] = true
  35. enabledPlugins.Apps = append(enabledPlugins.Apps, &app)
  36. }
  37. }
  38. isPluginEnabled := func(appId string) bool {
  39. if appId == "" {
  40. return true
  41. }
  42. _, ok := enabledApps[appId]
  43. return ok
  44. }
  45. // add all plugins that are not part of an App.
  46. for dsId, ds := range DataSources {
  47. if isPluginEnabled(ds.IncludedInAppId) {
  48. enabledPlugins.DataSources[dsId] = ds
  49. }
  50. }
  51. for _, panel := range Panels {
  52. if isPluginEnabled(panel.IncludedInAppId) {
  53. enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
  54. }
  55. }
  56. for _, api := range ApiPlugins {
  57. if isPluginEnabled(api.IncludedInAppId) {
  58. enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
  59. }
  60. }
  61. return &enabledPlugins, nil
  62. }