2016-01-10 14:37:11 -06:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetOrgAppSettings(orgId int64) (map[string]*m.AppSettings, error) {
|
|
|
|
query := m.GetAppSettingsQuery{OrgId: orgId}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
orgAppsMap := make(map[string]*m.AppSettings)
|
|
|
|
for _, orgApp := range query.Result {
|
|
|
|
orgAppsMap[orgApp.AppId] = orgApp
|
|
|
|
}
|
|
|
|
|
|
|
|
return orgAppsMap, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEnabledPlugins(orgId int64) (*EnabledPlugins, error) {
|
|
|
|
enabledPlugins := NewEnabledPlugins()
|
|
|
|
orgApps, err := GetOrgAppSettings(orgId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-19 11:18:53 -06:00
|
|
|
enabledApps := make(map[string]bool)
|
2016-01-10 14:37:11 -06:00
|
|
|
|
2016-01-11 11:03:08 -06:00
|
|
|
for appId, installedApp := range Apps {
|
2016-01-10 14:37:11 -06:00
|
|
|
var app AppPlugin
|
|
|
|
app = *installedApp
|
|
|
|
|
|
|
|
// check if the app is stored in the DB for this org and if so, use the
|
|
|
|
// state stored there.
|
2016-01-11 11:03:08 -06:00
|
|
|
if b, ok := orgApps[appId]; ok {
|
2016-01-10 14:37:11 -06:00
|
|
|
app.Enabled = b.Enabled
|
|
|
|
app.Pinned = b.Pinned
|
|
|
|
}
|
|
|
|
|
2016-01-11 11:03:08 -06:00
|
|
|
if app.Enabled {
|
2016-01-19 11:18:53 -06:00
|
|
|
enabledApps[app.Id] = true
|
2016-01-11 11:03:08 -06:00
|
|
|
enabledPlugins.Apps = append(enabledPlugins.Apps, &app)
|
|
|
|
}
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
|
2016-01-19 11:18:53 -06:00
|
|
|
isPluginEnabled := func(appId string) bool {
|
|
|
|
if appId == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := enabledApps[appId]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-01-10 14:37:11 -06:00
|
|
|
// add all plugins that are not part of an App.
|
2016-01-19 11:18:53 -06:00
|
|
|
for dsId, ds := range DataSources {
|
|
|
|
if isPluginEnabled(ds.IncludedInAppId) {
|
|
|
|
enabledPlugins.DataSources[dsId] = ds
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 11:18:53 -06:00
|
|
|
for _, panel := range Panels {
|
|
|
|
if isPluginEnabled(panel.IncludedInAppId) {
|
|
|
|
enabledPlugins.Panels = append(enabledPlugins.Panels, panel)
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 11:18:53 -06:00
|
|
|
for _, api := range ApiPlugins {
|
|
|
|
if isPluginEnabled(api.IncludedInAppId) {
|
|
|
|
enabledPlugins.ApiList = append(enabledPlugins.ApiList, api)
|
2016-01-10 14:37:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &enabledPlugins, nil
|
|
|
|
}
|