Commit 9fb7b887 by Marcus Efraimsson

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

#7883
parent 7ee691dc
...@@ -101,6 +101,12 @@ func GetDashboard(c *middleware.Context) Response { ...@@ -101,6 +101,12 @@ func GetDashboard(c *middleware.Context) Response {
meta.FolderTitle = query.Result.Title 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 // make sure db version is in sync with json model version
dash.Data.Set("version", dash.Version) dash.Data.Set("version", dash.Version)
......
...@@ -16,6 +16,7 @@ type DashboardMeta struct { ...@@ -16,6 +16,7 @@ type DashboardMeta struct {
CanAdmin bool `json:"canAdmin"` CanAdmin bool `json:"canAdmin"`
CanStar bool `json:"canStar"` CanStar bool `json:"canStar"`
Slug string `json:"slug"` Slug string `json:"slug"`
Url string `json:"url"`
Expires time.Time `json:"expires"` Expires time.Time `json:"expires"`
Created time.Time `json:"created"` Created time.Time `json:"created"`
Updated time.Time `json:"updated"` Updated time.Time `json:"updated"`
......
...@@ -2,11 +2,13 @@ package models ...@@ -2,11 +2,13 @@ package models
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"time" "time"
"github.com/gosimple/slug" "github.com/gosimple/slug"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
...@@ -156,6 +158,16 @@ func SlugifyTitle(title string) string { ...@@ -156,6 +158,16 @@ func SlugifyTitle(title string) string {
return slug.Make(strings.ToLower(title)) 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 // COMMANDS
// //
......
...@@ -15,6 +15,7 @@ type Hit struct { ...@@ -15,6 +15,7 @@ type Hit struct {
Id int64 `json:"id"` Id int64 `json:"id"`
Title string `json:"title"` Title string `json:"title"`
Uri string `json:"uri"` Uri string `json:"uri"`
Url string `json:"url"`
Slug string `json:"slug"` Slug string `json:"slug"`
Type HitType `json:"type"` Type HitType `json:"type"`
Tags []string `json:"tags"` Tags []string `json:"tags"`
......
...@@ -182,6 +182,7 @@ func GetDashboard(query *m.GetDashboardQuery) error { ...@@ -182,6 +182,7 @@ func GetDashboard(query *m.GetDashboardQuery) error {
type DashboardSearchProjection struct { type DashboardSearchProjection struct {
Id int64 Id int64
Uid string
Title string Title string
Slug string Slug string
Term string Term string
...@@ -257,10 +258,17 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard ...@@ -257,10 +258,17 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard
for _, item := range res { for _, item := range res {
hit, exists := hits[item.Id] hit, exists := hits[item.Id]
if !exists { 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{ hit = &search.Hit{
Id: item.Id, Id: item.Id,
Title: item.Title, Title: item.Title,
Uri: "db/" + item.Slug, Uri: "db/" + item.Slug,
Url: url,
Slug: item.Slug, Slug: item.Slug,
Type: getHitType(item), Type: getHitType(item),
FolderId: item.FolderId, FolderId: item.FolderId,
...@@ -268,6 +276,7 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard ...@@ -268,6 +276,7 @@ func makeQueryResult(query *search.FindPersistedDashboardsQuery, res []Dashboard
FolderSlug: item.FolderSlug, FolderSlug: item.FolderSlug,
Tags: []string{}, Tags: []string{},
} }
query.Result = append(query.Result, hit) query.Result = append(query.Result, hit)
hits[item.Id] = hit hits[item.Id] = hit
} }
......
package sqlstore package sqlstore
import ( import (
"fmt"
"testing" "testing"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
...@@ -145,6 +146,7 @@ func TestDashboardDataAccess(t *testing.T) { ...@@ -145,6 +146,7 @@ func TestDashboardDataAccess(t *testing.T) {
So(len(query.Result), ShouldEqual, 1) So(len(query.Result), ShouldEqual, 1)
hit := query.Result[0] hit := query.Result[0]
So(hit.Type, ShouldEqual, search.DashHitFolder) 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() { Convey("Should be able to search for a dashboard folder's children", func() {
...@@ -160,6 +162,7 @@ func TestDashboardDataAccess(t *testing.T) { ...@@ -160,6 +162,7 @@ func TestDashboardDataAccess(t *testing.T) {
So(len(query.Result), ShouldEqual, 2) So(len(query.Result), ShouldEqual, 2)
hit := query.Result[0] hit := query.Result[0]
So(hit.Id, ShouldEqual, savedDash.Id) 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() { Convey("Should be able to search for dashboard by dashboard ids", func() {
......
...@@ -101,6 +101,7 @@ func (sb *SearchBuilder) buildSelect() { ...@@ -101,6 +101,7 @@ func (sb *SearchBuilder) buildSelect() {
sb.sql.WriteString( sb.sql.WriteString(
`SELECT `SELECT
dashboard.id, dashboard.id,
dashboard.uid,
dashboard.title, dashboard.title,
dashboard.slug, dashboard.slug,
dashboard_tag.term, dashboard_tag.term,
......
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