feat(plugins): worked on plugin dashboard import handling

This commit is contained in:
Torkel Ödegaard
2016-03-10 19:57:48 +01:00
parent 78b9c3004a
commit 60adcedebe
14 changed files with 189 additions and 84 deletions
+77
View File
@@ -0,0 +1,77 @@
package plugins
import (
"encoding/json"
"os"
"path/filepath"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
)
type PluginDashboardInfoDTO struct {
Title string
InstalledURI string
InstalledRevision string
Revision string
Description string
}
func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDTO, error) {
plugin, exists := Plugins[pluginId]
if !exists {
return nil, &PluginNotFoundError{pluginId}
}
result := make([]*PluginDashboardInfoDTO, 0)
for _, include := range plugin.Includes {
if include.Type == PluginTypeDashboard {
if dashInfo, err := getDashboardImportStatus(orgId, plugin, include); err != nil {
return nil, err
} else {
result = append(result, dashInfo)
}
}
}
return result, nil
}
func getDashboardImportStatus(orgId int64, plugin *PluginBase, dashInclude *PluginInclude) (*PluginDashboardInfoDTO, error) {
res := &PluginDashboardInfoDTO{}
dashboardFilePath := filepath.Join(plugin.PluginDir, dashInclude.Path)
reader, err := os.Open(dashboardFilePath)
if err != nil {
return nil, err
}
defer reader.Close()
jsonParser := json.NewDecoder(reader)
var data map[string]interface{}
if err := jsonParser.Decode(&data); err != nil {
return nil, err
}
dashboard := m.NewDashboardFromJson(data)
res.Title = dashboard.Title
res.Revision = dashboard.GetString("revision", "1.0")
query := m.GetDashboardQuery{OrgId: orgId, Slug: dashboard.Slug}
if err := bus.Dispatch(&query); err != nil {
if err != m.ErrDashboardNotFound {
return nil, err
}
} else {
res.InstalledURI = "db/" + query.Result.Slug
res.InstalledRevision = query.Result.GetString("revision", "1.0")
}
return res, nil
}
+53
View File
@@ -0,0 +1,53 @@
package plugins
import (
"testing"
"github.com/grafana/grafana/pkg/bus"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/ini.v1"
)
func TestPluginDashboards(t *testing.T) {
Convey("When asking plugin dashboard info", t, func() {
setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.nginx-app")
sec.NewKey("path", "../../examples/nginx-app")
err := Init()
So(err, ShouldBeNil)
bus.AddHandler("test", func(query *m.GetDashboardQuery) error {
if query.Slug == "nginx-connections" {
dash := m.NewDashboard("Nginx Connections")
dash.Data["revision"] = "1.1"
query.Result = dash
return nil
}
return m.ErrDashboardNotFound
})
dashboards, err := GetPluginDashboards(1, "nginx-app")
So(err, ShouldBeNil)
Convey("should return 2 dashboarrd", func() {
So(len(dashboards), ShouldEqual, 2)
})
Convey("should include installed version info", func() {
So(dashboards[0].Title, ShouldEqual, "Nginx Connections")
So(dashboards[0].Revision, ShouldEqual, "1.5")
So(dashboards[0].InstalledRevision, ShouldEqual, "1.1")
So(dashboards[0].InstalledURI, ShouldEqual, "db/nginx-connections")
So(dashboards[1].Revision, ShouldEqual, "2.0")
So(dashboards[1].InstalledRevision, ShouldEqual, "")
})
})
}
+16
View File
@@ -3,12 +3,28 @@ package plugins
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/setting"
)
var (
PluginTypeApp = "app"
PluginTypeDatasource = "datasource"
PluginTypePanel = "panel"
PluginTypeDashboard = "dashboard"
)
type PluginNotFoundError struct {
PluginId string
}
func (e *PluginNotFoundError) Error() string {
return fmt.Sprintf("Plugin with id %s not found", e.PluginId)
}
type PluginLoader interface {
Load(decoder *json.Decoder, pluginDir string) error
}
+5 -4
View File
@@ -27,14 +27,15 @@ func TestPluginScans(t *testing.T) {
Convey("When reading app plugin definition", t, func() {
setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.app-test")
sec.NewKey("path", "../../tests/app-plugin-json")
sec, _ := setting.Cfg.NewSection("plugin.nginx-app")
sec.NewKey("path", "../../examples/nginx-app")
err := Init()
So(err, ShouldBeNil)
So(len(Apps), ShouldBeGreaterThan, 0)
So(Apps["app-example"].Info.Logos.Large, ShouldEqual, "public/plugins/app-example/img/logo_large.png")
So(Apps["app-example"].Info.Screenshots[1].Path, ShouldEqual, "public/plugins/app-example/img/screenshot2.png")
So(Apps["nginx-app"].Info.Logos.Large, ShouldEqual, "public/plugins/nginx-app/img/logo_large.png")
So(Apps["nginx-app"].Info.Screenshots[1].Path, ShouldEqual, "public/plugins/nginx-app/img/screenshot2.png")
})
}