dashboards.go 2.67 KB
Newer Older
1 2 3 4 5 6 7
package plugins

import (
	"os"
	"path/filepath"

	"github.com/grafana/grafana/pkg/bus"
8
	"github.com/grafana/grafana/pkg/components/simplejson"
9 10 11 12
	m "github.com/grafana/grafana/pkg/models"
)

type PluginDashboardInfoDTO struct {
13 14 15 16
	PluginId         string `json:"pluginId"`
	Title            string `json:"title"`
	Imported         bool   `json:"imported"`
	ImportedUri      string `json:"importedUri"`
17
	Slug             string `json:"slug"`
18
	DashboardId      int64  `json:"dashboardId"`
19 20 21 22
	ImportedRevision int64  `json:"importedRevision"`
	Revision         int64  `json:"revision"`
	Description      string `json:"description"`
	Path             string `json:"path"`
23
	Removed          bool   `json:"removed"`
24 25 26 27 28 29
}

func GetPluginDashboards(orgId int64, pluginId string) ([]*PluginDashboardInfoDTO, error) {
	plugin, exists := Plugins[pluginId]

	if !exists {
30
		return nil, PluginNotFoundError{pluginId}
31 32 33 34
	}

	result := make([]*PluginDashboardInfoDTO, 0)

35 36 37 38 39 40
	// load current dashboards
	query := m.GetDashboardsByPluginIdQuery{OrgId: orgId, PluginId: pluginId}
	if err := bus.Dispatch(&query); err != nil {
		return nil, err
	}

41 42
	existingMatches := make(map[int64]bool)

43
	for _, include := range plugin.Includes {
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		if include.Type != PluginTypeDashboard {
			continue
		}

		res := &PluginDashboardInfoDTO{}
		var dashboard *m.Dashboard
		var err error

		if dashboard, err = loadPluginDashboard(plugin.Id, include.Path); err != nil {
			return nil, err
		}

		res.Path = include.Path
		res.PluginId = plugin.Id
		res.Title = dashboard.Title
		res.Revision = dashboard.Data.Get("revision").MustInt64(1)

		// find existing dashboard
		for _, existingDash := range query.Result {
			if existingDash.Slug == dashboard.Slug {
64
				res.DashboardId = existingDash.Id
65 66 67
				res.Imported = true
				res.ImportedUri = "db/" + existingDash.Slug
				res.ImportedRevision = existingDash.Data.Get("revision").MustInt64(1)
68
				existingMatches[existingDash.Id] = true
69 70
			}
		}
71 72

		result = append(result, res)
73 74
	}

75 76 77 78
	// find deleted dashboards
	for _, dash := range query.Result {
		if _, exists := existingMatches[dash.Id]; !exists {
			result = append(result, &PluginDashboardInfoDTO{
79 80 81
				Slug:        dash.Slug,
				DashboardId: dash.Id,
				Removed:     true,
82 83 84 85
			})
		}
	}

86 87 88
	return result, nil
}

89 90 91 92 93 94
func loadPluginDashboard(pluginId, path string) (*m.Dashboard, error) {
	plugin, exists := Plugins[pluginId]

	if !exists {
		return nil, PluginNotFoundError{pluginId}
	}
95

96
	dashboardFilePath := filepath.Join(plugin.PluginDir, path)
97 98 99 100 101 102 103
	reader, err := os.Open(dashboardFilePath)
	if err != nil {
		return nil, err
	}

	defer reader.Close()

104 105
	data, err := simplejson.NewFromReader(reader)
	if err != nil {
106 107 108
		return nil, err
	}

109 110
	return m.NewDashboardFromJson(data), nil
}