diff --git a/grafana b/grafana index ff47eccf4b1..d5471c153ab 160000 --- a/grafana +++ b/grafana @@ -1 +1 @@ -Subproject commit ff47eccf4b119b348a7838a96b558cb2d6a36b0f +Subproject commit d5471c153ab8ab0d4be57154300d28a75af1d363 diff --git a/pkg/api/api.go b/pkg/api/api.go index 09bb4bfb6ad..6ee9fff1b75 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -45,7 +45,6 @@ func Register(r *macaron.Macaron) { r.Put("/", bind(m.UpdateUserCommand{}), UpdateUser) r.Post("/using/:id", SetUsingAccount) r.Get("/accounts", GetUserAccounts) - r.Get("/stars/", GetUserStars) r.Post("/stars/dashboard/:id", StarDashboard) r.Delete("/stars/dashboard/:id", UnstarDashboard) }) diff --git a/pkg/api/search.go b/pkg/api/search.go index 9d1765c3002..58fdfa3c2fd 100644 --- a/pkg/api/search.go +++ b/pkg/api/search.go @@ -10,6 +10,26 @@ import ( "github.com/torkelo/grafana-pro/pkg/setting" ) +// 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 +} + func Search(c *middleware.Context) { queryText := c.Query("q") starred := c.Query("starred") @@ -20,6 +40,7 @@ func Search(c *middleware.Context) { } if strings.HasPrefix(queryText, "tags!:") { + query := m.GetDashboardTagsQuery{AccountId: c.AccountId} err := bus.Dispatch(&query) if err != nil { @@ -28,7 +49,9 @@ func Search(c *middleware.Context) { } result.Tags = query.Result result.TagsOnly = true + } else { + searchQueryRegEx, _ := regexp.Compile(`(tags:(\w*)\sAND\s)?(?:title:)?(.*)?`) matches := searchQueryRegEx.FindStringSubmatch(queryText) query := m.SearchDashboardsQuery{ @@ -38,12 +61,18 @@ func Search(c *middleware.Context) { IsStarred: starred == "1", AccountId: c.AccountId, } + 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 + } + result.Dashboards = query.Result for _, dash := range result.Dashboards { dash.Url = setting.AbsUrlTo("dashboard/db/" + dash.Slug) diff --git a/pkg/api/stars.go b/pkg/api/stars.go index 05b712c8520..20436302ccd 100644 --- a/pkg/api/stars.go +++ b/pkg/api/stars.go @@ -1,9 +1,6 @@ package api import ( - "strconv" - - "github.com/torkelo/grafana-pro/pkg/api/dtos" "github.com/torkelo/grafana-pro/pkg/bus" "github.com/torkelo/grafana-pro/pkg/middleware" m "github.com/torkelo/grafana-pro/pkg/models" @@ -46,20 +43,3 @@ func UnstarDashboard(c *middleware.Context) { c.JsonOK("Dashboard unstarred") } - -func GetUserStars(c *middleware.Context) { - query := m.GetUserStarsQuery{UserId: c.UserId} - - if err := bus.Dispatch(&query); err != nil { - c.JsonApiErr(500, "Failed to get user stars", err) - return - } - - var result dtos.UserStars - result.DashboardIds = make(map[string]bool) - for _, star := range query.Result { - result.DashboardIds[strconv.FormatInt(star.DashboardId, 10)] = true - } - - c.JSON(200, &result) -} diff --git a/pkg/models/search.go b/pkg/models/search.go index 9fe6ab7a5ae..e23b5dbd5aa 100644 --- a/pkg/models/search.go +++ b/pkg/models/search.go @@ -7,11 +7,12 @@ type SearchResult struct { } type DashboardSearchHit struct { - Id int64 `json:"id"` - Title string `json:"title"` - Slug string `json:"slug"` - Tags []string `json:"tags"` - Url string `json:"url"` + Id int64 `json:"id"` + Title string `json:"title"` + Slug string `json:"slug"` + Tags []string `json:"tags"` + Url string `json:"url"` + IsStarred bool `json:"isStarred"` } type DashboardTagCloudItem struct { diff --git a/pkg/models/star.go b/pkg/models/star.go index 74d46c1b844..5d1a503cb1c 100644 --- a/pkg/models/star.go +++ b/pkg/models/star.go @@ -29,7 +29,7 @@ type UnstarDashboardCommand struct { type GetUserStarsQuery struct { UserId int64 - Result []Star + Result map[int64]bool // dashboard ids } type IsStarredByUserQuery struct { diff --git a/pkg/services/sqlstore/star.go b/pkg/services/sqlstore/star.go index 07ae1da5e7c..35543439d07 100644 --- a/pkg/services/sqlstore/star.go +++ b/pkg/services/sqlstore/star.go @@ -61,7 +61,13 @@ func UnstarDashboard(cmd *m.UnstarDashboardCommand) error { } func GetUserStars(query *m.GetUserStarsQuery) error { - query.Result = make([]m.Star, 0) - err := x.Where("user_id=?", query.UserId).Find(&query.Result) + var stars = make([]m.Star, 0) + err := x.Where("user_id=?", query.UserId).Find(&stars) + + query.Result = make(map[int64]bool) + for _, star := range stars { + query.Result[star.DashboardId] = true + } + return err }