From 9fb7b887db0d571b421ad34f9b021ec1c5f840bd Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Tue, 30 Jan 2018 15:24:14 +0100 Subject: [PATCH] dashboards: add url property to dashboard meta and search api responses #7883 --- pkg/api/dashboard.go | 6 ++++++ pkg/api/dtos/dashboard.go | 1 + pkg/models/dashboards.go | 12 ++++++++++++ pkg/services/search/models.go | 1 + pkg/services/sqlstore/dashboard.go | 9 +++++++++ pkg/services/sqlstore/dashboard_test.go | 3 +++ pkg/services/sqlstore/search_builder.go | 1 + 7 files changed, 33 insertions(+) diff --git a/pkg/api/dashboard.go b/pkg/api/dashboard.go index 0c30fd54ee5..efb9a790f2d 100644 --- a/pkg/api/dashboard.go +++ b/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) diff --git a/pkg/api/dtos/dashboard.go b/pkg/api/dtos/dashboard.go index 0be0537527b..604bb8d12ed 100644 --- a/pkg/api/dtos/dashboard.go +++ b/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"` diff --git a/pkg/models/dashboards.go b/pkg/models/dashboards.go index 0dfbd3b924f..616ebfeb187 100644 --- a/pkg/models/dashboards.go +++ b/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 // diff --git a/pkg/services/search/models.go b/pkg/services/search/models.go index cf510ed8462..6214a854db7 100644 --- a/pkg/services/search/models.go +++ b/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"` diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 497afa8f73f..84cfd1999c7 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/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 } diff --git a/pkg/services/sqlstore/dashboard_test.go b/pkg/services/sqlstore/dashboard_test.go index 18e283883a2..19e82614718 100644 --- a/pkg/services/sqlstore/dashboard_test.go +++ b/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() { diff --git a/pkg/services/sqlstore/search_builder.go b/pkg/services/sqlstore/search_builder.go index ddf192da5ff..480e7fa99e6 100644 --- a/pkg/services/sqlstore/search_builder.go +++ b/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,