diff --git a/CHANGELOG.md b/CHANGELOG.md index de405e1232b..5c5ada93d58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - [Issue #1905](https://github.com/grafana/grafana/issues/1905). Github OAuth: You can now configure a Github team membership requirement, thx @dewski - [Issue #1891](https://github.com/grafana/grafana/issues/1891). Security: New config option to disable the use of gravatar for profile images - [Issue #1921](https://github.com/grafana/grafana/issues/1921). Auth: Support for user authentication via reverse proxy header (like X-Authenticated-User, or X-WEBAUTH-USER) +- [Issue #960](https://github.com/grafana/grafana/issues/960). Search: Backend can now index a folder with json files, will be available in search (saving back to folder is not supported, this feature is meant for static generated json dashboards) **Breaking changes** - [Issue #1928](https://github.com/grafana/grafana/issues/1928). HTTP API: GET /api/dashboards/db/:slug response changed property `model` to `dashboard` to match the POST request nameing diff --git a/pkg/search/handlers.go b/pkg/search/handlers.go index bbafffb8743..874b85994ca 100644 --- a/pkg/search/handlers.go +++ b/pkg/search/handlers.go @@ -24,6 +24,7 @@ func Init() { } jsonDashIndex = NewJsonDashIndex(jsonFilesPath) + go jsonDashIndex.updateLoop() } } diff --git a/pkg/search/json_index.go b/pkg/search/json_index.go index 5a500d2f688..0b4c335e703 100644 --- a/pkg/search/json_index.go +++ b/pkg/search/json_index.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/grafana/grafana/pkg/log" m "github.com/grafana/grafana/pkg/models" @@ -23,7 +24,7 @@ type JsonDashIndexItem struct { } func NewJsonDashIndex(path string) *JsonDashIndex { - log.Info("Creating json dashboard index for path: ", path) + log.Info("Creating json dashboard index for path: %v", path) index := JsonDashIndex{} index.path = path @@ -31,6 +32,18 @@ func NewJsonDashIndex(path string) *JsonDashIndex { return &index } +func (index *JsonDashIndex) updateLoop() { + ticker := time.NewTicker(time.Minute) + for { + select { + case <-ticker.C: + if err := index.updateIndex(); err != nil { + log.Error(3, "Failed to update dashboard json index %v", err) + } + } + } +} + func (index *JsonDashIndex) Search(query *Query) ([]*Hit, error) { results := make([]*Hit, 0) @@ -71,8 +84,7 @@ func (index *JsonDashIndex) GetDashboard(path string) *m.Dashboard { } func (index *JsonDashIndex) updateIndex() error { - - index.items = make([]*JsonDashIndexItem, 0) + var items = make([]*JsonDashIndexItem, 0) visitor := func(path string, f os.FileInfo, err error) error { if err != nil { @@ -81,12 +93,16 @@ func (index *JsonDashIndex) updateIndex() error { if f.IsDir() { return nil } + if strings.HasSuffix(f.Name(), ".json") { - err = index.loadDashboardIntoCache(path) + dash, err := loadDashboardFromFile(path) if err != nil { return err } + + items = append(items, dash) } + return nil } @@ -94,16 +110,7 @@ func (index *JsonDashIndex) updateIndex() error { return err } - return nil -} - -func (index *JsonDashIndex) loadDashboardIntoCache(filename string) error { - dash, err := loadDashboardFromFile(filename) - if err != nil { - return err - } - - index.items = append(index.items, dash) + index.items = items return nil } diff --git a/pkg/search/json_index_test.go b/pkg/search/json_index_test.go index 9c4d27e6233..38fbf7207d9 100644 --- a/pkg/search/json_index_test.go +++ b/pkg/search/json_index_test.go @@ -9,7 +9,7 @@ import ( func TestJsonDashIndex(t *testing.T) { Convey("Given the json dash index", t, func() { - index := NewJsonDashIndex("../../public/dashboards/", "*") + index := NewJsonDashIndex("../../public/dashboards/") Convey("Should be able to update index", func() { err := index.updateIndex()