2015-05-11 13:03:57 -05:00
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
2015-05-12 05:20:03 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
2015-05-11 13:03:57 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-05-12 05:20:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-05-11 13:03:57 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Query struct {
|
|
|
|
Title string
|
|
|
|
Tag string
|
|
|
|
OrgId int64
|
|
|
|
UserId int64
|
|
|
|
Limit int
|
|
|
|
IsStarred bool
|
|
|
|
|
|
|
|
Result []*m.DashboardSearchHit
|
|
|
|
}
|
|
|
|
|
2015-05-12 05:20:03 -05:00
|
|
|
var jsonDashIndex *JsonDashIndex
|
|
|
|
|
2015-05-11 13:03:57 -05:00
|
|
|
func Init() {
|
|
|
|
bus.AddHandler("search", searchHandler)
|
2015-05-12 05:20:03 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
orgIds := jsonIndexCfg.Key("org_ids").String()
|
|
|
|
jsonDashIndex = NewJsonDashIndex(jsonFilesPath, orgIds)
|
|
|
|
}
|
2015-05-11 13:03:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func searchHandler(query *Query) error {
|
2015-05-12 05:20:03 -05:00
|
|
|
hits := make([]*m.DashboardSearchHit, 0)
|
|
|
|
|
2015-05-11 13:03:57 -05:00
|
|
|
dashQuery := m.SearchDashboardsQuery{
|
|
|
|
Title: query.Title,
|
|
|
|
Tag: query.Tag,
|
|
|
|
UserId: query.UserId,
|
|
|
|
Limit: query.Limit,
|
|
|
|
IsStarred: query.IsStarred,
|
|
|
|
OrgId: query.OrgId,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&dashQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-12 05:20:03 -05:00
|
|
|
hits = append(hits, dashQuery.Result...)
|
|
|
|
|
|
|
|
if jsonDashIndex != nil {
|
|
|
|
jsonHits, err := jsonDashIndex.Search(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hits = append(hits, jsonHits...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := setIsStarredFlagOnSearchResults(query.UserId, hits); err != nil {
|
2015-05-11 13:03:57 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-12 05:20:03 -05:00
|
|
|
query.Result = hits
|
2015-05-11 13:03:57 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setIsStarredFlagOnSearchResults(userId int64, hits []*m.DashboardSearchHit) 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
|
|
|
|
}
|
2015-05-12 07:11:30 -05:00
|
|
|
|
|
|
|
func GetDashboardFromJsonIndex(filename string) *m.Dashboard {
|
|
|
|
if jsonDashIndex == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return jsonDashIndex.GetDashboard(filename)
|
|
|
|
}
|