Lots of work on search and dashboard loading, trying to generalize concepts and code, #960

This commit is contained in:
Torkel Ödegaard
2015-05-12 14:11:30 +02:00
parent a40299b4dc
commit b3be51f17f
15 changed files with 120 additions and 176 deletions
+1
View File
@@ -100,6 +100,7 @@ func Register(r *macaron.Macaron) {
r.Group("/dashboards", func() {
r.Combo("/db/:slug").Get(GetDashboard).Delete(DeleteDashboard)
r.Post("/db", reqEditorRole, bind(m.SaveDashboardCommand{}), PostDashboard)
r.Get("/file/:file", GetDashboardFromJsonFile)
r.Get("/home", GetHomeDashboard)
})
+18 -1
View File
@@ -10,6 +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/setting"
"github.com/grafana/grafana/pkg/util"
)
@@ -48,7 +49,7 @@ func GetDashboard(c *middleware.Context) {
dash := query.Result
dto := dtos.DashboardFullWithMeta{
Dashboard: dash.Data,
Meta: dtos.DashboardMeta{IsStarred: isStarred, Slug: slug},
Meta: dtos.DashboardMeta{IsStarred: isStarred, Slug: slug, Type: m.DashTypeDB},
}
c.JSON(200, dto)
@@ -118,3 +119,19 @@ func GetHomeDashboard(c *middleware.Context) {
c.JSON(200, &dash)
}
func GetDashboardFromJsonFile(c *middleware.Context) {
file := c.Params(":file")
dashboard := search.GetDashboardFromJsonIndex(file)
if dashboard == nil {
c.JsonApiErr(404, "Dashboard not found", nil)
return
}
dash := dtos.DashboardFullWithMeta{Dashboard: dashboard.Data}
dash.Meta.Type = m.DashTypeJson
dash.Meta.CanSave = false
c.JSON(200, &dash)
}
+2
View File
@@ -32,6 +32,8 @@ type DashboardMeta struct {
IsStarred bool `json:"isStarred,omitempty"`
IsHome bool `json:"isHome,omitempty"`
IsSnapshot bool `json:"isSnapshot,omitempty"`
Type string `json:"type,omitempty"`
CanSave bool `json:"canSave"`
Slug string `json:"slug"`
Expires time.Time `json:"expires"`
Created time.Time `json:"created"`
+6
View File
@@ -15,6 +15,12 @@ var (
ErrDashboardVersionMismatch = errors.New("The dashboard has been changed by someone else")
)
var (
DashTypeJson = "file"
DashTypeDB = "db"
DashTypeScript = "script"
)
// Dashboard model
type Dashboard struct {
Id int64
+2 -1
View File
@@ -9,7 +9,8 @@ type SearchResult struct {
type DashboardSearchHit struct {
Id int64 `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Uri string `json:"uri"`
Type string `json:"type"`
Tags []string `json:"tags"`
IsStarred bool `json:"isStarred"`
}
+20 -11
View File
@@ -17,11 +17,10 @@ type JsonDashIndex struct {
}
type JsonDashIndexItem struct {
Title string
TitleLower string
Tags []string
TagsCsv string
FilePath string
Path string
Dashboard *m.Dashboard
}
func NewJsonDashIndex(path string, orgIds string) *JsonDashIndex {
@@ -40,9 +39,9 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error
for _, item := range index.items {
if strings.Contains(item.TitleLower, query.Title) {
results = append(results, &m.DashboardSearchHit{
Title: item.Title,
Tags: item.Tags,
Slug: item.FilePath,
Title: item.Dashboard.Title,
Tags: item.Dashboard.GetTags(),
Uri: "file/" + item.Path,
})
}
}
@@ -50,6 +49,16 @@ func (index *JsonDashIndex) Search(query *Query) ([]*m.DashboardSearchHit, error
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 {
log.Info("Updating JSON dashboard index, path: %v", index.path)
@@ -102,13 +111,13 @@ func loadDashboardFromFile(filename string) (*JsonDashIndexItem, error) {
return nil, err
}
dash := m.NewDashboardFromJson(data)
stat, _ := os.Stat(filename)
item := &JsonDashIndexItem{}
item.Title = dash.Title
item.TitleLower = strings.ToLower(item.Title)
item.Tags = dash.GetTags()
item.TagsCsv = strings.Join(item.Tags, ",")
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
}
+7
View File
@@ -87,3 +87,10 @@ func setIsStarredFlagOnSearchResults(userId int64, hits []*m.DashboardSearchHit)
return nil
}
func GetDashboardFromJsonIndex(filename string) *m.Dashboard {
if jsonDashIndex == nil {
return nil
}
return jsonDashIndex.GetDashboard(filename)
}
+1 -1
View File
@@ -175,7 +175,7 @@ func SearchDashboards(query *m.SearchDashboardsQuery) error {
hit = &m.DashboardSearchHit{
Id: item.Id,
Title: item.Title,
Slug: item.Slug,
Uri: "db/" + item.Slug,
Tags: []string{},
}
query.Result = append(query.Result, hit)