Explorar o código

Dashboard search works better, tag cloud should be done soon

Torkel Ödegaard %!s(int64=11) %!d(string=hai) anos
pai
achega
bcdbec61d7
Modificáronse 5 ficheiros con 69 adicións e 14 borrados
  1. 10 0
      README.md
  2. 1 1
      grafana
  3. 25 6
      pkg/api/dashboard.go
  4. 21 4
      pkg/models/dashboards.go
  5. 12 3
      pkg/stores/sqlstore/dashboards.go

+ 10 - 0
README.md

@@ -24,3 +24,13 @@ npm install
 npm install -g grunt-cli
 grunt
 ```
+
+To rebuild on source change:
+```
+go get github.com/Unknwon/bra
+
+bra run
+```
+
+
+

+ 1 - 1
grafana

@@ -1 +1 @@
-Subproject commit a5c8bbfe1f04830507d981dff5a44248ffeab04c
+Subproject commit d03949a735fd6ee486e278feb3b87f252be5ce96

+ 25 - 6
pkg/api/dashboard.go

@@ -1,6 +1,8 @@
 package api
 
 import (
+	"strings"
+
 	"github.com/torkelo/grafana-pro/pkg/bus"
 	"github.com/torkelo/grafana-pro/pkg/middleware"
 	m "github.com/torkelo/grafana-pro/pkg/models"
@@ -46,15 +48,32 @@ func DeleteDashboard(c *middleware.Context) {
 
 func Search(c *middleware.Context) {
 	queryText := c.Query("q")
+	result := m.SearchResult{
+		Dashboards: []m.DashboardSearchHit{},
+		Tags:       []m.DashboardTagCloudItem{},
+	}
 
-	query := m.SearchDashboardsQuery{Query: queryText, AccountId: c.GetAccountId()}
-	err := bus.Dispatch(&query)
-	if err != nil {
-		c.JsonApiErr(500, "Search failed", err)
-		return
+	if strings.HasPrefix(queryText, "tags!:") {
+		query := m.GetDashboardTagsQuery{}
+		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
+	} else {
+		queryText := strings.TrimPrefix(queryText, "title:")
+		query := m.SearchDashboardsQuery{Query: queryText, AccountId: c.GetAccountId()}
+		err := bus.Dispatch(&query)
+		if err != nil {
+			c.JsonApiErr(500, "Search failed", err)
+			return
+		}
+		result.Dashboards = query.Result
 	}
 
-	c.JSON(200, query.Result)
+	c.JSON(200, result)
 }
 
 func PostDashboard(c *middleware.Context) {

+ 21 - 4
pkg/models/dashboards.go

@@ -27,16 +27,33 @@ type Dashboard struct {
 }
 
 type SearchResult struct {
-	Id    string `json:"id"`
-	Title string `json:"title"`
-	Slug  string `json:"slug"`
+	Dashboards []DashboardSearchHit    `json:"dashboards"`
+	Tags       []DashboardTagCloudItem `json:"tags"`
+	TagsOnly   bool                    `json:"tagsOnly"`
+}
+
+type DashboardSearchHit struct {
+	Id    string   `json:"id"`
+	Title string   `json:"title"`
+	Slug  string   `json:"slug"`
+	Tags  []string `json:"tags"`
+}
+
+type DashboardTagCloudItem struct {
+	Term  string `json:"term"`
+	Count int    `json:"count"`
 }
 
 type SearchDashboardsQuery struct {
 	Query     string
 	AccountId int64
 
-	Result []*SearchResult
+	Result []DashboardSearchHit
+}
+
+type GetDashboardTagsQuery struct {
+	AccountId int64
+	Result    []DashboardTagCloudItem
 }
 
 type SaveDashboardCommand struct {

+ 12 - 3
pkg/stores/sqlstore/dashboards.go

@@ -11,6 +11,7 @@ func init() {
 	bus.AddHandler("sql", GetDashboard)
 	bus.AddHandler("sql", DeleteDashboard)
 	bus.AddHandler("sql", SearchDashboards)
+	bus.AddHandler("sql", GetDashboardTags)
 }
 
 func SaveDashboard(cmd *m.SaveDashboardCommand) error {
@@ -55,17 +56,25 @@ func GetDashboard(query *m.GetDashboardQuery) error {
 }
 
 func SearchDashboards(query *m.SearchDashboardsQuery) error {
-	titleMatch := "%" + query.Query + "%"
+	titleQuery := "%" + query.Query + "%"
 
-	sess := x.Limit(100, 0).Where("account_id=? AND title LIKE ?", query.AccountId, titleMatch)
+	sess := x.Limit(100, 0).Where("account_id=? AND title LIKE ?", query.AccountId, titleQuery)
 	sess.Table("Dashboard")
 
-	query.Result = make([]*m.SearchResult, 0)
+	query.Result = make([]m.DashboardSearchHit, 0)
 	err := sess.Find(&query.Result)
 
 	return err
 }
 
+func GetDashboardTags(query *m.GetDashboardTagsQuery) error {
+	query.Result = []m.DashboardTagCloudItem{
+		m.DashboardTagCloudItem{Term: "test", Count: 10},
+		m.DashboardTagCloudItem{Term: "prod", Count: 20},
+	}
+	return nil
+}
+
 func DeleteDashboard(cmd *m.DeleteDashboardCommand) error {
 	sess := x.NewSession()
 	defer sess.Close()