Просмотр исходного кода

dashboards as cfg: make dashboard none editable by default

bergquist 8 лет назад
Родитель
Сommit
93e1d8a19c

+ 2 - 0
pkg/services/provisioning/dashboard/dashboard_test.go

@@ -29,6 +29,7 @@ func TestDashboardsAsConfig(t *testing.T) {
 			So(ds.Type, ShouldEqual, "file")
 			So(ds.OrgId, ShouldEqual, 2)
 			So(ds.Folder, ShouldEqual, "developers")
+			So(ds.Editable, ShouldBeTrue)
 
 			So(len(ds.Options), ShouldEqual, 1)
 			So(ds.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards")
@@ -39,6 +40,7 @@ func TestDashboardsAsConfig(t *testing.T) {
 			So(ds2.Type, ShouldEqual, "file")
 			So(ds2.OrgId, ShouldEqual, 1)
 			So(ds2.Folder, ShouldEqual, "")
+			So(ds2.Editable, ShouldBeFalse)
 
 			So(len(ds2.Options), ShouldEqual, 1)
 			So(ds2.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards")

+ 8 - 37
pkg/services/provisioning/dashboard/file_reader.go

@@ -7,7 +7,6 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
-	"sync"
 	"time"
 
 	"github.com/grafana/grafana/pkg/bus"
@@ -24,31 +23,6 @@ type fileReader struct {
 	dashboardCache *dashboardCache
 }
 
-type dashboardCache struct {
-	mutex      *sync.Mutex
-	dashboards map[string]*DashboardJson
-}
-
-func newDashboardCache() *dashboardCache {
-	return &dashboardCache{
-		dashboards: map[string]*DashboardJson{},
-		mutex:      &sync.Mutex{},
-	}
-}
-
-func (dc *dashboardCache) addCache(json *DashboardJson) {
-	dc.mutex.Lock()
-	defer dc.mutex.Unlock()
-	dc.dashboards[json.Path] = json
-}
-
-func (dc *dashboardCache) getCache(path string) (*DashboardJson, bool) {
-	dc.mutex.Lock()
-	defer dc.mutex.Unlock()
-	v, exist := dc.dashboards[path]
-	return v, exist
-}
-
 func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
 	path, ok := cfg.Options["folder"].(string)
 	if !ok {
@@ -152,20 +126,17 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error)
 		return nil, err
 	}
 
-	stat, _ := os.Stat(path)
-	dash := &DashboardJson{}
-	dash.Dashboard = models.NewDashboardFromJson(data)
-	dash.TitleLower = strings.ToLower(dash.Dashboard.Title)
-	dash.Path = path
-	dash.ModTime = stat.ModTime()
-	dash.OrgId = fr.Cfg.OrgId
-	dash.Folder = fr.Cfg.Folder
+	stat, err := os.Stat(path)
+	if err != nil {
+		return nil, err
+	}
 
-	if dash.Dashboard.Title == "" {
-		return nil, models.ErrDashboardTitleEmpty
+	dash, err := createDashboardJson(data, stat.ModTime(), fr.Cfg)
+	if err != nil {
+		return nil, err
 	}
 
-	fr.dashboardCache.addCache(dash)
+	fr.dashboardCache.addCache(path, dash)
 
 	return dash, nil
 }

+ 1 - 0
pkg/services/provisioning/dashboard/test-configs/dashboards-from-disk/dev-dashboards.yaml

@@ -1,6 +1,7 @@
 - name: 'general dashboards'
   org_id: 2
   folder: 'developers'
+  editable: true
   type: file
   options:
     folder: /var/lib/grafana/dashboards

+ 46 - 11
pkg/services/provisioning/dashboard/types.go

@@ -1,6 +1,8 @@
 package dashboard
 
 import (
+	"github.com/grafana/grafana/pkg/components/simplejson"
+	"strings"
 	"sync"
 	"time"
 
@@ -8,27 +10,60 @@ import (
 )
 
 type DashboardsAsConfig struct {
-	Name    string                 `json:"name" yaml:"name"`
-	Type    string                 `json:"type" yaml:"type"`
-	OrgId   int64                  `json:"org_id" yaml:"org_id"`
-	Folder  string                 `json:"folder" yaml:"folder"`
-	Options map[string]interface{} `json:"options" yaml:"options"`
+	Name     string                 `json:"name" yaml:"name"`
+	Type     string                 `json:"type" yaml:"type"`
+	OrgId    int64                  `json:"org_id" yaml:"org_id"`
+	Folder   string                 `json:"folder" yaml:"folder"`
+	Editable bool                   `json:"editable" yaml:"editable"`
+	Options  map[string]interface{} `json:"options" yaml:"options"`
 }
 
 type DashboardJson struct {
 	TitleLower string
-	Path       string
 	OrgId      int64
 	Folder     string
 	ModTime    time.Time
 	Dashboard  *models.Dashboard
 }
 
-type DashboardIndex struct {
-	mutex *sync.Mutex
+type dashboardCache struct {
+	mutex      *sync.Mutex
+	dashboards map[string]*DashboardJson
+}
+
+func newDashboardCache() *dashboardCache {
+	return &dashboardCache{
+		dashboards: map[string]*DashboardJson{},
+		mutex:      &sync.Mutex{},
+	}
+}
+
+func (dc *dashboardCache) addCache(key string, json *DashboardJson) {
+	dc.mutex.Lock()
+	defer dc.mutex.Unlock()
+	dc.dashboards[key] = json
+}
 
-	PathToDashboard map[string]*DashboardJson
+func (dc *dashboardCache) getCache(key string) (*DashboardJson, bool) {
+	dc.mutex.Lock()
+	defer dc.mutex.Unlock()
+	v, exist := dc.dashboards[key]
+	return v, exist
 }
 
-type InsertDashboard func(cmd *models.Dashboard) error
-type UpdateDashboard func(cmd *models.SaveDashboardCommand) error
+func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *DashboardsAsConfig) (*DashboardJson, error) {
+
+	dash := &DashboardJson{}
+	dash.Dashboard = models.NewDashboardFromJson(data)
+	dash.TitleLower = strings.ToLower(dash.Dashboard.Title)
+	dash.ModTime = lastModified
+	dash.OrgId = cfg.OrgId
+	dash.Folder = cfg.Folder
+	dash.Dashboard.Data.Set("editable", cfg.Editable)
+
+	if dash.Dashboard.Title == "" {
+		return nil, models.ErrDashboardTitleEmpty
+	}
+
+	return dash, nil
+}