Major refactorings around searching, moved to seperate package, trying to move stuff out of models package, extend search support searching different types of entities and different types of dashboards, #960

This commit is contained in:
Torkel Ödegaard
2015-05-13 13:36:24 +02:00
parent c8146e759f
commit 448a8b8d1c
19 changed files with 143 additions and 128 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ import (
"github.com/grafana/grafana/pkg/metrics"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/search"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
)
+1 -1
View File
@@ -3,7 +3,7 @@ package api
import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/search"
)
func Search(c *middleware.Context) {
+10
View File
@@ -126,3 +126,13 @@ type GetDashboardQuery struct {
Result *Dashboard
}
type DashboardTagCloudItem struct {
Term string `json:"term"`
Count int `json:"count"`
}
type GetDashboardTagsQuery struct {
OrgId int64
Result []*DashboardTagCloudItem
}
+1 -28
View File
@@ -1,12 +1,6 @@
package models
type SearchResult struct {
Dashboards []*DashboardSearchHit `json:"dashboards"`
Tags []*DashboardTagCloudItem `json:"tags"`
TagsOnly bool `json:"tagsOnly"`
}
type DashboardSearchHit struct {
type SearchHit struct {
Id int64 `json:"id"`
Title string `json:"title"`
Uri string `json:"uri"`
@@ -14,24 +8,3 @@ type DashboardSearchHit struct {
Tags []string `json:"tags"`
IsStarred bool `json:"isStarred"`
}
type DashboardTagCloudItem struct {
Term string `json:"term"`
Count int `json:"count"`
}
type SearchDashboardsQuery struct {
Title string
Tag string
OrgId int64
UserId int64
Limit int
IsStarred bool
Result []*DashboardSearchHit
}
type GetDashboardTagsQuery struct {
OrgId int64
Result []*DashboardTagCloudItem
}
@@ -2,23 +2,13 @@ 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"
)
type Query struct {
Title string
Tag string
OrgId int64
UserId int64
Limit int
IsStarred bool
Result []*m.DashboardSearchHit
}
var jsonDashIndex *JsonDashIndex
func Init() {
@@ -33,15 +23,14 @@ func Init() {
jsonFilesPath = filepath.Join(setting.HomePath, jsonFilesPath)
}
orgIds := jsonIndexCfg.Key("org_ids").String()
jsonDashIndex = NewJsonDashIndex(jsonFilesPath, orgIds)
jsonDashIndex = NewJsonDashIndex(jsonFilesPath)
}
}
func searchHandler(query *Query) error {
hits := make([]*m.DashboardSearchHit, 0)
hits := make(HitList, 0)
dashQuery := m.SearchDashboardsQuery{
dashQuery := FindPersistedDashboardsQuery{
Title: query.Title,
Tag: query.Tag,
UserId: query.UserId,
@@ -65,6 +54,8 @@ func searchHandler(query *Query) error {
hits = append(hits, jsonHits...)
}
sort.Sort(hits)
if err := setIsStarredFlagOnSearchResults(query.UserId, hits); err != nil {
return err
}
@@ -73,7 +64,7 @@ func searchHandler(query *Query) error {
return nil
}
func setIsStarredFlagOnSearchResults(userId int64, hits []*m.DashboardSearchHit) error {
func setIsStarredFlagOnSearchResults(userId int64, hits []*Hit) error {
query := m.GetUserStarsQuery{UserId: userId}
if err := bus.Dispatch(&query); err != nil {
return err
@@ -11,9 +11,8 @@ import (
)
type JsonDashIndex struct {
path string
orgsIds []int64
items []*JsonDashIndexItem
path string
items []*JsonDashIndexItem
}
type JsonDashIndexItem struct {
@@ -23,7 +22,7 @@ type JsonDashIndexItem struct {
Dashboard *m.Dashboard
}
func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex {
func NewJsonDashIndex(path string) *JsonDashIndex {
log.Info("Creating json dashboard index for path: ", path)
index := JsonDashIndex{}
@@ -32,8 +31,8 @@ func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex {
return &index
}
func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error) {
results := make([]*m.DashboardSearchHit, 0)
func (index *JsonDashIndex) Search(query *Query) ([]*Hit, error) {
results := make([]*Hit, 0)
for _, item := range index.items {
if len(results) > query.Limit {
@@ -49,8 +48,8 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error
// add results with matchig title filter
if strings.Contains(item.TitleLower, query.Title) {
results = append(results, &m.DashboardSearchHit{
Type: m.DashTypeJson,
results = append(results, &Hit{
Type: DashHitJson,
Title: item.Dashboard.Title,
Tags: item.Dashboard.GetTags(),
Uri: "file/" + item.Path,
@@ -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()
+47
View File
@@ -0,0 +1,47 @@
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
Tag string
OrgId int64
UserId int64
Limit int
IsStarred bool
Result HitList
}
type FindPersistedDashboardsQuery struct {
Title string
Tag string
OrgId int64
UserId int64
Limit int
IsStarred bool
Result HitList
}
+6 -5
View File
@@ -8,6 +8,7 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/metrics"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/search"
)
func init() {
@@ -119,7 +120,7 @@ type DashboardSearchProjection struct {
Term string
}
func SearchDashboards(query *m.SearchDashboardsQuery) error {
func SearchDashboards(query *search.FindPersistedDashboardsQuery) error {
var sql bytes.Buffer
params := make([]interface{}, 0)
@@ -166,17 +167,17 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error {
return err
}
query.Result = make([]*m.DashboardSearchHit, 0)
hits := make(map[int64]*m.DashboardSearchHit)
query.Result = make([]*search.Hit, 0)
hits := make(map[int64]*search.Hit)
for _, item := range res {
hit, exists := hits[item.Id]
if !exists {
hit = &m.DashboardSearchHit{
hit = &search.Hit{
Id: item.Id,
Title: item.Title,
Uri: "db/" + item.Slug,
Type: m.DashTypeDB,
Type: search.DashHitDB,
Tags: []string{},
}
query.Result = append(query.Result, hit)
+5 -4
View File
@@ -6,6 +6,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/search"
)
func insertTestDashboard(title string, orgId int64, tags ...interface{}) *m.Dashboard {
@@ -85,7 +86,7 @@ func TestDashboardDataAccess(t *testing.T) {
})
Convey("Should be able to search for dashboard", func() {
query := m.SearchDashboardsQuery{
query := search.FindPersistedDashboardsQuery{
Title: "test",
OrgId: 1,
}
@@ -99,8 +100,8 @@ func TestDashboardDataAccess(t *testing.T) {
})
Convey("Should be able to search for dashboards using tags", func() {
query1 := m.SearchDashboardsQuery{Tag: "webapp", OrgId: 1}
query2 := m.SearchDashboardsQuery{Tag: "tagdoesnotexist", OrgId: 1}
query1 := search.FindPersistedDashboardsQuery{Tag: "webapp", OrgId: 1}
query2 := search.FindPersistedDashboardsQuery{Tag: "tagdoesnotexist", OrgId: 1}
err := SearchDashboards(&query1)
err = SearchDashboards(&query2)
@@ -146,7 +147,7 @@ func TestDashboardDataAccess(t *testing.T) {
})
Convey("Should be able to search for starred dashboards", func() {
query := m.SearchDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
query := search.FindPersistedDashboardsQuery{OrgId: 1, UserId: 10, IsStarred: true}
err := SearchDashboards(&query)
So(err, ShouldBeNil)