Commit 09917846 by bergquist

dashboards as cfg: wire up dashboard repo

parent 288cc355
......@@ -33,9 +33,9 @@ type SaveDashboardItem struct {
Dashboard *models.Dashboard
}
type dashboardRepository struct{}
type DashboardRepository struct{}
func (dr *dashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.Dashboard, error) {
func (dr *DashboardRepository) SaveDashboard(json *SaveDashboardItem) (*models.Dashboard, error) {
dashboard := json.Dashboard
if dashboard.Title == "" {
......
......@@ -41,7 +41,7 @@ func NewDashboardFilereader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
Path: path,
log: log,
dashboardRepo: dashboards.GetRepository(),
cache: gocache.New(5*time.Minute, 10*time.Minute),
cache: gocache.New(5*time.Minute, 30*time.Minute),
}, nil
}
......@@ -87,23 +87,23 @@ func (fr *fileReader) walkFolder() error {
}
}
return filepath.Walk(fr.Path, func(path string, f os.FileInfo, err error) error {
return filepath.Walk(fr.Path, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
if strings.HasPrefix(f.Name(), ".") {
if fileInfo.IsDir() {
if strings.HasPrefix(fileInfo.Name(), ".") {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(f.Name(), ".json") {
if !strings.HasSuffix(fileInfo.Name(), ".json") {
return nil
}
cachedDashboard, exist := fr.getCache(path)
if exist && cachedDashboard.UpdatedAt == f.ModTime() {
if exist && cachedDashboard.UpdatedAt == fileInfo.ModTime() {
return nil
}
......@@ -116,6 +116,7 @@ func (fr *fileReader) walkFolder() error {
cmd := &models.GetDashboardQuery{Slug: dash.Dashboard.Slug}
err = bus.Dispatch(cmd)
// if we dont have the dashboard in the db, save it!
if err == models.ErrDashboardNotFound {
fr.log.Debug("saving new dashboard", "file", path)
_, err = fr.dashboardRepo.SaveDashboard(dash)
......@@ -127,11 +128,12 @@ func (fr *fileReader) walkFolder() error {
return nil
}
if cmd.Result.Updated.Unix() >= f.ModTime().Unix() {
// break if db version is newer then fil version
if cmd.Result.Updated.Unix() >= fileInfo.ModTime().Unix() {
return nil
}
fr.log.Debug("no dashboard in cache. loading dashboard from disk into database.", "file", path)
fr.log.Debug("loading dashboard from disk into database.", "file", path)
_, err = fr.dashboardRepo.SaveDashboard(dash)
return err
})
......
......@@ -25,6 +25,7 @@ func createDashboardJson(data *simplejson.Json, lastModified time.Time, cfg *Das
dash.Dashboard = models.NewDashboardFromJson(data)
dash.TitleLower = strings.ToLower(dash.Dashboard.Title)
dash.UpdatedAt = lastModified
dash.Overwrite = true
dash.OrgId = cfg.OrgId
dash.Folder = cfg.Folder
dash.Dashboard.Data.Set("editable", cfg.Editable)
......
......@@ -12,6 +12,7 @@ import (
"github.com/grafana/grafana/pkg/log"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/annotations"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/setting"
......@@ -100,6 +101,7 @@ func SetEngine(engine *xorm.Engine) (err error) {
// Init repo instances
annotations.SetRepository(&SqlAnnotationRepo{})
dashboards.SetRepository(&dashboards.DashboardRepository{})
return 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