grafana/pkg/api/search.go

82 lines
1.6 KiB
Go
Raw Normal View History

2015-01-08 02:00:00 -06:00
package api
import (
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m "github.com/grafana/grafana/pkg/models"
2015-01-08 02:00:00 -06:00
)
// TODO: this needs to be cached or improved somehow
func setIsStarredFlagOnSearchResults(c *middleware.Context, hits []*m.DashboardSearchHit) error {
if !c.IsSignedIn {
return nil
}
query := m.GetUserStarsQuery{UserId: c.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-01-08 02:00:00 -06:00
func Search(c *middleware.Context) {
2015-02-07 09:12:29 -06:00
query := c.Query("query")
tag := c.Query("tag")
tagcloud := c.Query("tagcloud")
starred := c.Query("starred")
limit := c.QueryInt("limit")
if limit == 0 {
limit = 200
}
2015-01-08 02:00:00 -06:00
result := m.SearchResult{
Dashboards: []*m.DashboardSearchHit{},
Tags: []*m.DashboardTagCloudItem{},
}
2015-02-07 09:12:29 -06:00
if tagcloud == "true" {
query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
2015-01-08 02:00:00 -06:00
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
2015-01-08 02:00:00 -06:00
} else {
query := m.SearchDashboardsQuery{
2015-02-07 09:12:29 -06:00
Title: query,
Tag: tag,
UserId: c.UserId,
Limit: limit,
2015-02-07 09:12:29 -06:00
IsStarred: starred == "true",
OrgId: c.OrgId,
2015-01-08 02:00:00 -06:00
}
2015-01-08 02:00:00 -06:00
err := bus.Dispatch(&query)
if err != nil {
c.JsonApiErr(500, "Search failed", err)
return
}
if err := setIsStarredFlagOnSearchResults(c, query.Result); err != nil {
c.JsonApiErr(500, "Failed to get user stars", err)
return
}
2015-01-08 02:00:00 -06:00
result.Dashboards = query.Result
}
c.JSON(200, result)
}