|
|
@@ -2,6 +2,7 @@ package dashboards
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "errors"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
@@ -15,21 +16,21 @@ import (
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
- gocache "github.com/patrickmn/go-cache"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
checkDiskForChangesInterval time.Duration = time.Second * 3
|
|
|
+
|
|
|
+ ErrFolderNameMissing error = errors.New("Folder name missing")
|
|
|
)
|
|
|
|
|
|
type fileReader struct {
|
|
|
- Cfg *DashboardsAsConfig
|
|
|
- Path string
|
|
|
- FolderId int64
|
|
|
- log log.Logger
|
|
|
- dashboardRepo dashboards.Repository
|
|
|
- cache *gocache.Cache
|
|
|
- createWalkFunc func(fr *fileReader) filepath.WalkFunc
|
|
|
+ Cfg *DashboardsAsConfig
|
|
|
+ Path string
|
|
|
+ log log.Logger
|
|
|
+ dashboardRepo dashboards.Repository
|
|
|
+ cache *DashboardCache
|
|
|
+ createWalk func(fr *fileReader, folderId int64) filepath.WalkFunc
|
|
|
}
|
|
|
|
|
|
func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReader, error) {
|
|
|
@@ -43,19 +44,19 @@ func NewDashboardFileReader(cfg *DashboardsAsConfig, log log.Logger) (*fileReade
|
|
|
}
|
|
|
|
|
|
return &fileReader{
|
|
|
- Cfg: cfg,
|
|
|
- Path: path,
|
|
|
- log: log,
|
|
|
- dashboardRepo: dashboards.GetRepository(),
|
|
|
- cache: gocache.New(5*time.Minute, 30*time.Minute),
|
|
|
- createWalkFunc: createWalkFn,
|
|
|
+ Cfg: cfg,
|
|
|
+ Path: path,
|
|
|
+ log: log,
|
|
|
+ dashboardRepo: dashboards.GetRepository(),
|
|
|
+ cache: NewDashboardCache(),
|
|
|
+ createWalk: createWalkFn,
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
func (fr *fileReader) ReadAndListen(ctx context.Context) error {
|
|
|
ticker := time.NewTicker(checkDiskForChangesInterval)
|
|
|
|
|
|
- if err := fr.walkFolder(); err != nil {
|
|
|
+ if err := fr.startWalkingDisk(); err != nil {
|
|
|
fr.log.Error("failed to search for dashboards", "error", err)
|
|
|
}
|
|
|
|
|
|
@@ -67,7 +68,9 @@ func (fr *fileReader) ReadAndListen(ctx context.Context) error {
|
|
|
if !running { // avoid walking the filesystem in parallel. incase fs is very slow.
|
|
|
running = true
|
|
|
go func() {
|
|
|
- fr.walkFolder()
|
|
|
+ if err := fr.startWalkingDisk(); err != nil {
|
|
|
+ fr.log.Error("failed to search for dashboards", "error", err)
|
|
|
+ }
|
|
|
running = false
|
|
|
}()
|
|
|
}
|
|
|
@@ -77,17 +80,56 @@ func (fr *fileReader) ReadAndListen(ctx context.Context) error {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (fr *fileReader) walkFolder() error {
|
|
|
+func (fr *fileReader) startWalkingDisk() error {
|
|
|
if _, err := os.Stat(fr.Path); err != nil {
|
|
|
if os.IsNotExist(err) {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return filepath.Walk(fr.Path, fr.createWalkFunc(fr)) //omg this is so ugly :(
|
|
|
+ folderId, err := getOrCreateFolder(fr.Cfg, fr.dashboardRepo)
|
|
|
+ if err != nil && err != ErrFolderNameMissing {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return filepath.Walk(fr.Path, fr.createWalk(fr, folderId))
|
|
|
}
|
|
|
|
|
|
-func createWalkFn(fr *fileReader) filepath.WalkFunc {
|
|
|
+func getOrCreateFolder(cfg *DashboardsAsConfig, repo dashboards.Repository) (int64, error) {
|
|
|
+ if cfg.Folder == "" {
|
|
|
+ return 0, ErrFolderNameMissing
|
|
|
+ }
|
|
|
+
|
|
|
+ cmd := &models.GetDashboardQuery{Slug: models.SlugifyTitle(cfg.Folder), OrgId: cfg.OrgId}
|
|
|
+ err := bus.Dispatch(cmd)
|
|
|
+
|
|
|
+ if err != nil && err != models.ErrDashboardNotFound {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // dashboard folder not found. create one.
|
|
|
+ if err == models.ErrDashboardNotFound {
|
|
|
+ dash := &dashboards.SaveDashboardItem{}
|
|
|
+ dash.Dashboard = models.NewDashboard(cfg.Folder)
|
|
|
+ dash.Dashboard.IsFolder = true
|
|
|
+ dash.Overwrite = true
|
|
|
+ dash.OrgId = cfg.OrgId
|
|
|
+ dbDash, err := repo.SaveDashboard(dash)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return dbDash.Id, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if !cmd.Result.IsFolder {
|
|
|
+ return 0, fmt.Errorf("Got invalid response. Expected folder, found dashboard")
|
|
|
+ }
|
|
|
+
|
|
|
+ return cmd.Result.Id, nil
|
|
|
+}
|
|
|
+
|
|
|
+func createWalkFn(fr *fileReader, folderId int64) filepath.WalkFunc {
|
|
|
return func(path string, fileInfo os.FileInfo, err error) error {
|
|
|
if err != nil {
|
|
|
return err
|
|
|
@@ -103,12 +145,12 @@ func createWalkFn(fr *fileReader) filepath.WalkFunc {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- cachedDashboard, exist := fr.getCache(path)
|
|
|
+ cachedDashboard, exist := fr.cache.getCache(path)
|
|
|
if exist && cachedDashboard.UpdatedAt == fileInfo.ModTime() {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- dash, err := fr.readDashboardFromFile(path)
|
|
|
+ dash, err := fr.readDashboardFromFile(path, folderId)
|
|
|
if err != nil {
|
|
|
fr.log.Error("failed to load dashboard from ", "file", path, "error", err)
|
|
|
return nil
|
|
|
@@ -143,7 +185,7 @@ func createWalkFn(fr *fileReader) filepath.WalkFunc {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashboardItem, error) {
|
|
|
+func (fr *fileReader) readDashboardFromFile(path string, folderId int64) (*dashboards.SaveDashboardItem, error) {
|
|
|
reader, err := os.Open(path)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
@@ -160,30 +202,12 @@ func (fr *fileReader) readDashboardFromFile(path string) (*dashboards.SaveDashbo
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- dash, err := createDashboardJson(data, stat.ModTime(), fr.Cfg)
|
|
|
+ dash, err := createDashboardJson(data, stat.ModTime(), fr.Cfg, folderId)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
- fr.addDashboardCache(path, dash)
|
|
|
+ fr.cache.addDashboardCache(path, dash)
|
|
|
|
|
|
return dash, nil
|
|
|
}
|
|
|
-
|
|
|
-func (fr *fileReader) addDashboardCache(key string, json *dashboards.SaveDashboardItem) {
|
|
|
- fr.cache.Add(key, json, time.Minute*10)
|
|
|
-}
|
|
|
-
|
|
|
-func (fr *fileReader) getCache(key string) (*dashboards.SaveDashboardItem, bool) {
|
|
|
- obj, exist := fr.cache.Get(key)
|
|
|
- if !exist {
|
|
|
- return nil, exist
|
|
|
- }
|
|
|
-
|
|
|
- dash, ok := obj.(*dashboards.SaveDashboardItem)
|
|
|
- if !ok {
|
|
|
- return nil, ok
|
|
|
- }
|
|
|
-
|
|
|
- return dash, ok
|
|
|
-}
|