Commit 076905d1 by Torkel Ödegaard

Added isStarred to search result hit, very inefficient loading right now but can be cached later on

parent 69e7279c
Subproject commit ff47eccf4b119b348a7838a96b558cb2d6a36b0f
Subproject commit d5471c153ab8ab0d4be57154300d28a75af1d363
......@@ -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)
})
......
......@@ -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)
......
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)
}
......@@ -12,6 +12,7 @@ type DashboardSearchHit struct {
Slug string `json:"slug"`
Tags []string `json:"tags"`
Url string `json:"url"`
IsStarred bool `json:"isStarred"`
}
type DashboardTagCloudItem struct {
......
......@@ -29,7 +29,7 @@ type UnstarDashboardCommand struct {
type GetUserStarsQuery struct {
UserId int64
Result []Star
Result map[int64]bool // dashboard ids
}
type IsStarredByUserQuery struct {
......
......@@ -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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment