Prechádzať zdrojové kódy

Updated changelog with completed story, JSON dashboards added to search, Closes #960

Torkel Ödegaard 10 rokov pred
rodič
commit
1a401780ba

+ 1 - 0
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

+ 1 - 0
pkg/search/handlers.go

@@ -24,6 +24,7 @@ func Init() {
 		}
 
 		jsonDashIndex = NewJsonDashIndex(jsonFilesPath)
+		go jsonDashIndex.updateLoop()
 	}
 }
 

+ 21 - 14
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
 }
 

+ 1 - 1
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()