소스 검색

dashboards: add url property to dashboard meta and search api responses

#7883
Marcus Efraimsson 8 년 전
부모
커밋
9fb7b887db

+ 6 - 0
pkg/api/dashboard.go

@@ -101,6 +101,12 @@ func GetDashboard(c *middleware.Context) Response {
 		meta.FolderTitle = query.Result.Title
 	}
 
+	if dash.IsFolder {
+		meta.Url = m.GetFolderUrl(dash.Uid, dash.Slug)
+	} else {
+		meta.Url = m.GetDashboardUrl(dash.Uid, dash.Slug)
+	}
+
 	// make sure db version is in sync with json model version
 	dash.Data.Set("version", dash.Version)
 

+ 1 - 0
pkg/api/dtos/dashboard.go

@@ -16,6 +16,7 @@ type DashboardMeta struct {
 	CanAdmin    bool      `json:"canAdmin"`
 	CanStar     bool      `json:"canStar"`
 	Slug        string    `json:"slug"`
+	Url         string    `json:"url"`
 	Expires     time.Time `json:"expires"`
 	Created     time.Time `json:"created"`
 	Updated     time.Time `json:"updated"`

+ 12 - 0
pkg/models/dashboards.go

@@ -2,11 +2,13 @@ package models
 
 import (
 	"errors"
+	"fmt"
 	"strings"
 	"time"
 
 	"github.com/gosimple/slug"
 	"github.com/grafana/grafana/pkg/components/simplejson"
+	"github.com/grafana/grafana/pkg/setting"
 	"github.com/grafana/grafana/pkg/util"
 )
 
@@ -156,6 +158,16 @@ func SlugifyTitle(title string) string {
 	return slug.Make(strings.ToLower(title))
 }
 
+// GetDashboardUrl return the html url for a dashboard
+func GetDashboardUrl(uid string, slug string) string {
+	return fmt.Sprintf("%s/d/%s/%s", setting.AppSubUrl, uid, slug)
+}
+
+// GetFolderUrl return the html url for a folder
+func GetFolderUrl(folderUid string, slug string) string {
+	return fmt.Sprintf("%s/f/%v/%s", setting.AppSubUrl, folderUid, slug)
+}
+
 //
 // COMMANDS
 //

+ 1 - 0
pkg/services/search/models.go

@@ -15,6 +15,7 @@ type Hit struct {
 	Id          int64    `json:"id"`
 	Title       string   `json:"title"`
 	Uri         string   `json:"uri"`
+	Url         string   `json:"url"`
 	Slug        string   `json:"slug"`
 	Type        HitType  `json:"type"`
 	Tags        []string `json:"tags"`

+ 9 - 0
pkg/services/sqlstore/dashboard.go

@@ -182,6 +182,7 @@ func GetDashboard(query *m.GetDashboardQuery) error {
 
 type DashboardSearchProjection struct {
 	Id          int64
+	Uid         string
 	Title       string
 	Slug        string
 	Term        string
@@ -257,10 +258,17 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard
 	for _, item := range res {
 		hit, exists := hits[item.Id]
 		if !exists {
+			var url string
+			if item.IsFolder {
+				url = m.GetFolderUrl(item.Uid, item.Slug)
+			} else {
+				url = m.GetDashboardUrl(item.Uid, item.Slug)
+			}
 			hit = &search.Hit{
 				Id:          item.Id,
 				Title:       item.Title,
 				Uri:         "db/" + item.Slug,
+				Url:         url,
 				Slug:        item.Slug,
 				Type:        getHitType(item),
 				FolderId:    item.FolderId,
@@ -268,6 +276,7 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard
 				FolderSlug:  item.FolderSlug,
 				Tags:        []string{},
 			}
+
 			query.Result = append(query.Result, hit)
 			hits[item.Id] = hit
 		}

+ 3 - 0
pkg/services/sqlstore/dashboard_test.go

@@ -1,6 +1,7 @@
 package sqlstore
 
 import (
+	"fmt"
 	"testing"
 
 	"github.com/go-xorm/xorm"
@@ -145,6 +146,7 @@ func TestDashboardDataAccess(t *testing.T) {
 				So(len(query.Result), ShouldEqual, 1)
 				hit := query.Result[0]
 				So(hit.Type, ShouldEqual, search.DashHitFolder)
+				So(hit.Url, ShouldEqual, fmt.Sprintf("/f/%s/%s", savedFolder.Uid, savedFolder.Slug))
 			})
 
 			Convey("Should be able to search for a dashboard folder's children", func() {
@@ -160,6 +162,7 @@ func TestDashboardDataAccess(t *testing.T) {
 				So(len(query.Result), ShouldEqual, 2)
 				hit := query.Result[0]
 				So(hit.Id, ShouldEqual, savedDash.Id)
+				So(hit.Url, ShouldEqual, fmt.Sprintf("/d/%s/%s", savedDash.Uid, savedDash.Slug))
 			})
 
 			Convey("Should be able to search for dashboard by dashboard ids", func() {

+ 1 - 0
pkg/services/sqlstore/search_builder.go

@@ -101,6 +101,7 @@ func (sb *SearchBuilder) buildSelect() {
 	sb.sql.WriteString(
 		`SELECT
 			dashboard.id,
+			dashboard.uid,
 			dashboard.title,
 			dashboard.slug,
 			dashboard_tag.term,