Refactoring search to support more than just db dashboards
This commit is contained in:
@@ -102,6 +102,7 @@ func Register(r *macaron.Macaron) {
|
||||
r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
|
||||
r.Get("/file/:file", GetDashboardFromJsonFile)
|
||||
r.Get("/home", GetHomeDashboard)
|
||||
r.Get("/tags", GetDashboardTags)
|
||||
})
|
||||
|
||||
// Search
|
||||
|
||||
@@ -140,3 +140,14 @@ func GetDashboardFromJsonFile(c *middleware.Context) {
|
||||
|
||||
c.JSON(200, &dash)
|
||||
}
|
||||
|
||||
func GetDashboardTags(c *middleware.Context) {
|
||||
query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&query)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Failed to get tags from database", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, query.Result)
|
||||
}
|
||||
|
||||
+12
-35
@@ -3,14 +3,12 @@ package api
|
||||
import (
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
"github.com/grafana/grafana/pkg/middleware"
|
||||
m "github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/search"
|
||||
)
|
||||
|
||||
func Search(c *middleware.Context) {
|
||||
query := c.Query("query")
|
||||
tag := c.Query("tag")
|
||||
tagcloud := c.Query("tagcloud")
|
||||
starred := c.Query("starred")
|
||||
limit := c.QueryInt("limit")
|
||||
|
||||
@@ -18,41 +16,20 @@ func Search(c *middleware.Context) {
|
||||
limit = 200
|
||||
}
|
||||
|
||||
result := m.SearchResult{
|
||||
Dashboards: []*m.DashboardSearchHit{},
|
||||
Tags: []*m.DashboardTagCloudItem{},
|
||||
searchQuery := search.Query{
|
||||
Title: query,
|
||||
Tag: tag,
|
||||
UserId: c.UserId,
|
||||
Limit: limit,
|
||||
IsStarred: starred == "true",
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
if tagcloud == "true" {
|
||||
|
||||
query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
|
||||
err := bus.Dispatch(&query)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Failed to get tags from database", err)
|
||||
return
|
||||
}
|
||||
result.Tags = query.Result
|
||||
result.TagsOnly = true
|
||||
|
||||
} else {
|
||||
|
||||
query := search.Query{
|
||||
Title: query,
|
||||
Tag: tag,
|
||||
UserId: c.UserId,
|
||||
Limit: limit,
|
||||
IsStarred: starred == "true",
|
||||
OrgId: c.OrgId,
|
||||
}
|
||||
|
||||
err := bus.Dispatch(&query)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Search failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
result.Dashboards = query.Result
|
||||
err := bus.Dispatch(&searchQuery)
|
||||
if err != nil {
|
||||
c.JsonApiErr(500, "Search failed", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result)
|
||||
c.JSON(200, searchQuery.Result)
|
||||
}
|
||||
|
||||
@@ -24,11 +24,10 @@ type JsonDashIndexItem struct {
|
||||
}
|
||||
|
||||
func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex {
|
||||
log.Info("Creating json dashboard index for path: ", path)
|
||||
|
||||
index := JsonDashIndex{}
|
||||
index.path = path
|
||||
// if orgIds != "" || orgIds != "*" {
|
||||
// }
|
||||
|
||||
index.updateIndex()
|
||||
return &index
|
||||
}
|
||||
@@ -37,8 +36,21 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error
|
||||
results := make([]*m.DashboardSearchHit, 0)
|
||||
|
||||
for _, item := range index.items {
|
||||
if len(results) > query.Limit {
|
||||
break
|
||||
}
|
||||
|
||||
// filter out results with tag filter
|
||||
if query.Tag != "" {
|
||||
if !strings.Contains(item.TagsCsv, query.Tag) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// add results with matchig title filter
|
||||
if strings.Contains(item.TitleLower, query.Title) {
|
||||
results = append(results, &m.DashboardSearchHit{
|
||||
Type: m.DashTypeJson,
|
||||
Title: item.Dashboard.Title,
|
||||
Tags: item.Dashboard.GetTags(),
|
||||
Uri: "file/" + item.Path,
|
||||
@@ -60,7 +72,6 @@ func (index *JsonDashIndex) GetDashboard(path string) *m.Dashboard {
|
||||
}
|
||||
|
||||
func (index *JsonDashIndex) updateIndex() error {
|
||||
log.Info("Updating JSON dashboard index, path: %v", index.path)
|
||||
|
||||
index.items = make([]*JsonDashIndexItem, 0)
|
||||
|
||||
|
||||
@@ -176,6 +176,7 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error {
|
||||
Id: item.Id,
|
||||
Title: item.Title,
|
||||
Uri: "db/" + item.Slug,
|
||||
Type: m.DashTypeDB,
|
||||
Tags: []string{},
|
||||
}
|
||||
query.Result = append(query.Result, hit)
|
||||
|
||||
Reference in New Issue
Block a user