Browse Source

dashboard: refactor logic for retrieving url for folder/dashboard

Marcus Efraimsson 8 years ago
parent
commit
90933b0621
3 changed files with 18 additions and 20 deletions
  1. 3 13
      pkg/api/dashboard.go
  2. 14 0
      pkg/models/dashboards.go
  3. 1 7
      pkg/services/sqlstore/dashboard.go

+ 3 - 13
pkg/api/dashboard.go

@@ -90,6 +90,7 @@ func GetDashboard(c *middleware.Context) Response {
 		IsFolder:    dash.IsFolder,
 		FolderId:    dash.FolderId,
 		FolderTitle: "Root",
+		Url:         dash.GetUrl(),
 	}
 
 	// lookup folder title
@@ -101,12 +102,6 @@ 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)
 
@@ -238,12 +233,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
 		return ApiError(500, "Invalid alert data. Cannot save dashboard", err)
 	}
 
-	var url string
-	if dash.IsFolder {
-		url = m.GetFolderUrl(dashboard.Uid, dashboard.Slug)
-	} else {
-		url = m.GetDashboardUrl(dashboard.Uid, dashboard.Slug)
-	}
+	dashboard.IsFolder = dash.IsFolder
 
 	c.TimeRequest(metrics.M_Api_Dashboard_Save)
 	return Json(200, util.DynMap{
@@ -252,7 +242,7 @@ func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) Response {
 		"version": dashboard.Version,
 		"id":      dashboard.Id,
 		"uid":     dashboard.Uid,
-		"url":     url,
+		"url":     dashboard.GetUrl(),
 	})
 }
 

+ 14 - 0
pkg/models/dashboards.go

@@ -158,6 +158,20 @@ func SlugifyTitle(title string) string {
 	return slug.Make(strings.ToLower(title))
 }
 
+// GetUrl return the html url for a folder if it's folder, otherwise for a dashboard
+func (dash *Dashboard) GetUrl() string {
+	return GetDashboardFolderUrl(dash.IsFolder, dash.Uid, dash.Slug)
+}
+
+// GetDashboardFolderUrl return the html url for a folder if it's folder, otherwise for a dashboard
+func GetDashboardFolderUrl(isFolder bool, uid string, slug string) string {
+	if isFolder {
+		return GetFolderUrl(uid, slug)
+	}
+
+	return GetDashboardUrl(uid, slug)
+}
+
 // 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)

+ 1 - 7
pkg/services/sqlstore/dashboard.go

@@ -258,17 +258,11 @@ 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,
+				Url:         m.GetDashboardFolderUrl(item.IsFolder, item.Uid, item.Slug),
 				Slug:        item.Slug,
 				Type:        getHitType(item),
 				FolderId:    item.FolderId,