Prechádzať zdrojové kódy

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

Torkel Ödegaard 11 rokov pred
rodič
commit
076905d14e

+ 1 - 1
grafana

@@ -1 +1 @@
-Subproject commit ff47eccf4b119b348a7838a96b558cb2d6a36b0f
+Subproject commit d5471c153ab8ab0d4be57154300d28a75af1d363

+ 0 - 1
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)
 		})

+ 29 - 0
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)

+ 0 - 20
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)
-}

+ 6 - 5
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 {

+ 1 - 1
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 {

+ 8 - 2
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
 }