Commit 93e1d8a1 by bergquist

dashboards as cfg: make dashboard none editable by default

parent 7f3a7ea1
...@@ -29,6 +29,7 @@ func TestDashboardsAsConfig(t *testing.T) { ...@@ -29,6 +29,7 @@ func TestDashboardsAsConfig(t *testing.T) {
So(ds.Type, ShouldEqual, "file") So(ds.Type, ShouldEqual, "file")
So(ds.OrgId, ShouldEqual, 2) So(ds.OrgId, ShouldEqual, 2)
So(ds.Folder, ShouldEqual, "developers") So(ds.Folder, ShouldEqual, "developers")
So(ds.Editable, ShouldBeTrue)
So(len(ds.Options), ShouldEqual, 1) So(len(ds.Options), ShouldEqual, 1)
So(ds.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards") So(ds.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards")
...@@ -39,6 +40,7 @@ func TestDashboardsAsConfig(t *testing.T) { ...@@ -39,6 +40,7 @@ func TestDashboardsAsConfig(t *testing.T) {
So(ds2.Type, ShouldEqual, "file") So(ds2.Type, ShouldEqual, "file")
So(ds2.OrgId, ShouldEqual, 1) So(ds2.OrgId, ShouldEqual, 1)
So(ds2.Folder, ShouldEqual, "") So(ds2.Folder, ShouldEqual, "")
So(ds2.Editable, ShouldBeFalse)
So(len(ds2.Options), ShouldEqual, 1) So(len(ds2.Options), ShouldEqual, 1)
So(ds2.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards") So(ds2.Options["folder"], ShouldEqual, "/var/lib/grafana/dashboards")
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"time" "time"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
...@@ -24,31 +23,6 @@ type fileReader struct { ...@@ -24,31 +23,6 @@ type fileReader struct {
dashboardCache *dashboardCache 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) { func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
path, ok := cfg.Options["folder"].(string) path, ok := cfg.Options["folder"].(string)
if !ok { if !ok {
...@@ -152,20 +126,17 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error) ...@@ -152,20 +126,17 @@ func (fr *fileReader) readDashboardFromFile(path string) (*DashboardJson, error)
return nil, err return nil, err
} }
stat, _ := os.Stat(path) stat, err := os.Stat(path)
dash := &DashboardJson{} if err != nil {
dash.Dashboard = models.NewDashboardFromJson(data) return nil, err
dash.TitleLower = strings.ToLower(dash.Dashboard.Title) }
dash.Path = path
dash.ModTime = stat.ModTime()
dash.OrgId = fr.Cfg.OrgId
dash.Folder = fr.Cfg.Folder
if dash.Dashboard.Title == "" { dash, err := createDashboardJson(data, stat.ModTime(), fr.Cfg)
return nil, models.ErrDashboardTitleEmpty if err != nil {
return nil, err
} }
fr.dashboardCache.addCache(dash) fr.dashboardCache.addCache(path, dash)
return dash, nil return dash, nil
} }
......
- name: 'general dashboards' - name: 'general dashboards'
org_id: 2 org_id: 2
folder: 'developers' folder: 'developers'
editable: true
type: file type: file
options: options:
folder: /var/lib/grafana/dashboards folder: /var/lib/grafana/dashboards
......
package dashboard package dashboard
import ( import (
"github.com/grafana/grafana/pkg/components/simplejson"
"strings"
"sync" "sync"
"time" "time"
...@@ -12,23 +14,56 @@ type DashboardsAsConfig struct { ...@@ -12,23 +14,56 @@ type DashboardsAsConfig struct {
Type string `json:"type" yaml:"type"` Type string `json:"type" yaml:"type"`
OrgId int64 `json:"org_id" yaml:"org_id"` OrgId int64 `json:"org_id" yaml:"org_id"`
Folder string `json:"folder" yaml:"folder"` Folder string `json:"folder" yaml:"folder"`
Editable bool `json:"editable" yaml:"editable"`
Options map[string]interface{} `json:"options" yaml:"options"` Options map[string]interface{} `json:"options" yaml:"options"`
} }
type DashboardJson struct { type DashboardJson struct {
TitleLower string TitleLower string
Path string
OrgId int64 OrgId int64
Folder string Folder string
ModTime time.Time ModTime time.Time
Dashboard *models.Dashboard Dashboard *models.Dashboard
} }
type DashboardIndex struct { type dashboardCache struct {
mutex *sync.Mutex 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 func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *DashboardsAsConfig) (*DashboardJson, error) {
type UpdateDashboard func(cmd *models.SaveDashboardCommand) 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment