grafana/pkg/plugins/queries.go

88 lines
1.9 KiB
Go
Raw Normal View History

package plugins
import (
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
2016-03-08 11:17:47 -06:00
func GetPluginSettings(orgId int64) (map[string]*m.PluginSettingInfoDTO, error) {
query := m.GetPluginSettingsQuery{OrgId: orgId}
if err := bus.Dispatch(&query); err != nil {
return nil, err
}
2016-03-08 11:17:47 -06:00
pluginMap := make(map[string]*m.PluginSettingInfoDTO)
for _, plug := range query.Result {
pluginMap[plug.PluginId] = plug
}
2016-03-08 11:17:47 -06:00
for _, pluginDef := range Plugins {
// ignore entries that exists
if _, ok := pluginMap[pluginDef.Id]; ok {
continue
}
// default to enabled true
opt := &m.PluginSettingInfoDTO{
PluginId: pluginDef.Id,
OrgId: orgId,
Enabled: true,
}
// apps are disabled by default
if pluginDef.Type == PluginTypeApp {
opt.Enabled = false
}
2016-03-08 11:17:47 -06:00
// if it's included in app check app settings
if pluginDef.IncludedInAppId != "" {
// app componets are by default disabled
opt.Enabled = false
if appSettings, ok := pluginMap[pluginDef.IncludedInAppId]; ok {
opt.Enabled = appSettings.Enabled
}
}
pluginMap[pluginDef.Id] = opt
}
return pluginMap, nil
}
func GetEnabledPlugins(orgId int64) (*EnabledPlugins, error) {
enabledPlugins := NewEnabledPlugins()
2016-03-08 11:17:47 -06:00
pluginSettingMap, err := GetPluginSettings(orgId)
if err != nil {
return nil, err
}
2016-03-08 11:17:47 -06:00
isPluginEnabled := func(pluginId string) bool {
_, ok := pluginSettingMap[pluginId]
return ok
}
for pluginId, app := range Apps {
2016-03-08 11:17:47 -06:00
if b, ok := pluginSettingMap[pluginId]; ok {
app.Pinned = b.Pinned
enabledPlugins.Apps = append(enabledPlugins.Apps, app)
2016-01-11 11:03:08 -06:00
}
}
// add all plugins that are not part of an App.
for dsId, ds := range DataSources {
2016-03-08 11:17:47 -06:00
if isPluginEnabled(ds.Id) {
enabledPlugins.DataSources[dsId] = ds
}
}
for _, panel := range Panels {
2016-03-08 11:17:47 -06:00
if isPluginEnabled(panel.Id) {
enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
}
}
return &enabledPlugins, nil
}