mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
More work on email and notification infra #1456
This commit is contained in:
127
pkg/services/search/handlers.go
Normal file
127
pkg/services/search/handlers.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
var jsonDashIndex *JsonDashIndex
|
||||
|
||||
func Init() {
|
||||
bus.AddHandler("search", searchHandler)
|
||||
|
||||
jsonIndexCfg, _ := setting.Cfg.GetSection("dashboards.json")
|
||||
jsonIndexEnabled := jsonIndexCfg.Key("enabled").MustBool(false)
|
||||
|
||||
if jsonIndexEnabled {
|
||||
jsonFilesPath := jsonIndexCfg.Key("path").String()
|
||||
if !filepath.IsAbs(jsonFilesPath) {
|
||||
jsonFilesPath = filepath.Join(setting.HomePath, jsonFilesPath)
|
||||
}
|
||||
|
||||
jsonDashIndex = NewJsonDashIndex(jsonFilesPath)
|
||||
go jsonDashIndex.updateLoop()
|
||||
}
|
||||
}
|
||||
|
||||
func searchHandler(query *Query) error {
|
||||
hits := make(HitList, 0)
|
||||
|
||||
dashQuery := FindPersistedDashboardsQuery{
|
||||
Title: query.Title,
|
||||
UserId: query.UserId,
|
||||
IsStarred: query.IsStarred,
|
||||
OrgId: query.OrgId,
|
||||
}
|
||||
|
||||
if err := bus.Dispatch(&dashQuery); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hits = append(hits, dashQuery.Result...)
|
||||
|
||||
if jsonDashIndex != nil {
|
||||
jsonHits, err := jsonDashIndex.Search(query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hits = append(hits, jsonHits...)
|
||||
}
|
||||
|
||||
// filter out results with tag filter
|
||||
if len(query.Tags) > 0 {
|
||||
filtered := HitList{}
|
||||
for _, hit := range hits {
|
||||
if hasRequiredTags(query.Tags, hit.Tags) {
|
||||
filtered = append(filtered, hit)
|
||||
}
|
||||
}
|
||||
hits = filtered
|
||||
}
|
||||
|
||||
// sort main result array
|
||||
sort.Sort(hits)
|
||||
|
||||
if len(hits) > query.Limit {
|
||||
hits = hits[0:query.Limit]
|
||||
}
|
||||
|
||||
// sort tags
|
||||
for _, hit := range hits {
|
||||
sort.Strings(hit.Tags)
|
||||
}
|
||||
|
||||
// add isStarred info
|
||||
if err := setIsStarredFlagOnSearchResults(query.UserId, hits); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
query.Result = hits
|
||||
return nil
|
||||
}
|
||||
|
||||
func stringInSlice(a string, list []string) bool {
|
||||
for _, b := range list {
|
||||
if b == a {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func hasRequiredTags(queryTags, hitTags []string) bool {
|
||||
for _, queryTag := range queryTags {
|
||||
if !stringInSlice(queryTag, hitTags) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func setIsStarredFlagOnSearchResults(userId int64, hits []*Hit) error {
|
||||
query := m.GetUserStarsQuery{UserId: userId}
|
||||
if err := bus.Dispatch(&query); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, dash := range hits {
|
||||
if _, exists := query.Result[dash.Id]; exists {
|
||||
dash.IsStarred = true
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDashboardFromJsonIndex(filename string) *m.Dashboard {
|
||||
if jsonDashIndex == nil {
|
||||
return nil
|
||||
}
|
||||
return jsonDashIndex.GetDashboard(filename)
|
||||
}
|
||||
61
pkg/services/search/handlers_test.go
Normal file
61
pkg/services/search/handlers_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
|
||||
Convey("Given search query", t, func() {
|
||||
jsonDashIndex = NewJsonDashIndex("../../public/dashboards/")
|
||||
query := Query{Limit: 2000}
|
||||
|
||||
bus.AddHandler("test", func(query *FindPersistedDashboardsQuery) error {
|
||||
query.Result = HitList{
|
||||
&Hit{Id: 16, Title: "CCAA", Tags: []string{"BB", "AA"}},
|
||||
&Hit{Id: 10, Title: "AABB", Tags: []string{"CC", "AA"}},
|
||||
&Hit{Id: 15, Title: "BBAA", Tags: []string{"EE", "AA", "BB"}},
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddHandler("test", func(query *m.GetUserStarsQuery) error {
|
||||
query.Result = map[int64]bool{10: true, 12: true}
|
||||
return nil
|
||||
})
|
||||
|
||||
Convey("That is empty", func() {
|
||||
err := searchHandler(&query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("should return sorted results", func() {
|
||||
So(query.Result[0].Title, ShouldEqual, "AABB")
|
||||
So(query.Result[1].Title, ShouldEqual, "BBAA")
|
||||
So(query.Result[2].Title, ShouldEqual, "CCAA")
|
||||
})
|
||||
|
||||
Convey("should return sorted tags", func() {
|
||||
So(query.Result[1].Tags[0], ShouldEqual, "AA")
|
||||
So(query.Result[1].Tags[1], ShouldEqual, "BB")
|
||||
So(query.Result[1].Tags[2], ShouldEqual, "EE")
|
||||
})
|
||||
})
|
||||
|
||||
Convey("That filters by tag", func() {
|
||||
query.Tags = []string{"BB", "AA"}
|
||||
err := searchHandler(&query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
Convey("should return correct results", func() {
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
So(query.Result[0].Title, ShouldEqual, "BBAA")
|
||||
So(query.Result[1].Title, ShouldEqual, "CCAA")
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
}
|
||||
137
pkg/services/search/json_index.go
Normal file
137
pkg/services/search/json_index.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
type JsonDashIndex struct {
|
||||
path string
|
||||
items []*JsonDashIndexItem
|
||||
}
|
||||
|
||||
type JsonDashIndexItem struct {
|
||||
TitleLower string
|
||||
TagsCsv string
|
||||
Path string
|
||||
Dashboard *m.Dashboard
|
||||
}
|
||||
|
||||
func NewJsonDashIndex(path string) *JsonDashIndex {
|
||||
log.Info("Creating json dashboard index for path: %v", path)
|
||||
|
||||
index := JsonDashIndex{}
|
||||
index.path = path
|
||||
index.updateIndex()
|
||||
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)
|
||||
|
||||
if query.IsStarred {
|
||||
return results, nil
|
||||
}
|
||||
|
||||
for _, item := range index.items {
|
||||
if len(results) > query.Limit {
|
||||
break
|
||||
}
|
||||
|
||||
// add results with matchig title filter
|
||||
if strings.Contains(item.TitleLower, query.Title) {
|
||||
results = append(results, &Hit{
|
||||
Type: DashHitJson,
|
||||
Title: item.Dashboard.Title,
|
||||
Tags: item.Dashboard.GetTags(),
|
||||
Uri: "file/" + item.Path,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (index *JsonDashIndex) GetDashboard(path string) *m.Dashboard {
|
||||
for _, item := range index.items {
|
||||
if item.Path == path {
|
||||
return item.Dashboard
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (index *JsonDashIndex) updateIndex() error {
|
||||
var items = make([]*JsonDashIndexItem, 0)
|
||||
|
||||
visitor := func(path string, f os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if f.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if strings.HasSuffix(f.Name(), ".json") {
|
||||
dash, err := loadDashboardFromFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
items = append(items, dash)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := filepath.Walk(index.path, visitor); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
index.items = items
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadDashboardFromFile(filename string) (*JsonDashIndexItem, error) {
|
||||
reader, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer reader.Close()
|
||||
|
||||
jsonParser := json.NewDecoder(reader)
|
||||
var data map[string]interface{}
|
||||
|
||||
if err := jsonParser.Decode(&data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stat, _ := os.Stat(filename)
|
||||
|
||||
item := &JsonDashIndexItem{}
|
||||
item.Dashboard = m.NewDashboardFromJson(data)
|
||||
item.TitleLower = strings.ToLower(item.Dashboard.Title)
|
||||
item.TagsCsv = strings.Join(item.Dashboard.GetTags(), ",")
|
||||
item.Path = stat.Name()
|
||||
|
||||
return item, nil
|
||||
}
|
||||
42
pkg/services/search/json_index_test.go
Normal file
42
pkg/services/search/json_index_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestJsonDashIndex(t *testing.T) {
|
||||
|
||||
Convey("Given the json dash index", t, func() {
|
||||
index := NewJsonDashIndex("../../public/dashboards/")
|
||||
|
||||
Convey("Should be able to update index", func() {
|
||||
err := index.updateIndex()
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Should be able to search index", func() {
|
||||
res, err := index.Search(&Query{Title: "", Limit: 20})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(res), ShouldEqual, 3)
|
||||
})
|
||||
|
||||
Convey("Should be able to search index by title", func() {
|
||||
res, err := index.Search(&Query{Title: "home", Limit: 20})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(res), ShouldEqual, 1)
|
||||
So(res[0].Title, ShouldEqual, "Home")
|
||||
})
|
||||
|
||||
Convey("Should not return when starred is filtered", func() {
|
||||
res, err := index.Search(&Query{Title: "", IsStarred: true})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(res), ShouldEqual, 0)
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
45
pkg/services/search/models.go
Normal file
45
pkg/services/search/models.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package search
|
||||
|
||||
type HitType string
|
||||
|
||||
const (
|
||||
DashHitDB HitType = "dash-db"
|
||||
DashHitHome HitType = "dash-home"
|
||||
DashHitJson HitType = "dash-json"
|
||||
DashHitScripted HitType = "dash-scripted"
|
||||
)
|
||||
|
||||
type Hit struct {
|
||||
Id int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Uri string `json:"uri"`
|
||||
Type HitType `json:"type"`
|
||||
Tags []string `json:"tags"`
|
||||
IsStarred bool `json:"isStarred"`
|
||||
}
|
||||
|
||||
type HitList []*Hit
|
||||
|
||||
func (s HitList) Len() int { return len(s) }
|
||||
func (s HitList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s HitList) Less(i, j int) bool { return s[i].Title < s[j].Title }
|
||||
|
||||
type Query struct {
|
||||
Title string
|
||||
Tags []string
|
||||
OrgId int64
|
||||
UserId int64
|
||||
Limit int
|
||||
IsStarred bool
|
||||
|
||||
Result HitList
|
||||
}
|
||||
|
||||
type FindPersistedDashboardsQuery struct {
|
||||
Title string
|
||||
OrgId int64
|
||||
UserId int64
|
||||
IsStarred bool
|
||||
|
||||
Result HitList
|
||||
}
|
||||
Reference in New Issue
Block a user