Playlist: update service so it supports both read+write (#55959)
This commit is contained in:
+13
-58
@@ -1,12 +1,10 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
cmplaylist "github.com/grafana/grafana/pkg/coremodel/playlist"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
@@ -15,7 +13,7 @@ import (
|
||||
func (hs *HTTPServer) ValidateOrgPlaylist(c *models.ReqContext) {
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
query := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgID}
|
||||
p, err := hs.playlistService.Get(c.Req.Context(), &query)
|
||||
p, err := hs.playlistService.GetWithoutItems(c.Req.Context(), &query)
|
||||
|
||||
if err != nil {
|
||||
c.JsonApiErr(404, "Playlist not found", err)
|
||||
@@ -76,58 +74,14 @@ func (hs *HTTPServer) GetPlaylist(c *models.ReqContext) response.Response {
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
cmd := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgID}
|
||||
|
||||
p, err := hs.playlistService.Get(c.Req.Context(), &cmd)
|
||||
dto, err := hs.playlistService.Get(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
return response.Error(500, "Playlist not found", err)
|
||||
}
|
||||
|
||||
playlistDTOs, _ := hs.LoadPlaylistItemDTOs(c.Req.Context(), uid, c.OrgID)
|
||||
|
||||
dto := &playlist.PlaylistDTO{
|
||||
OrgId: p.OrgId,
|
||||
}
|
||||
dto.Id = p.Id
|
||||
dto.Uid = p.UID
|
||||
dto.Name = p.Name
|
||||
dto.Interval = p.Interval
|
||||
dto.Items = &playlistDTOs
|
||||
|
||||
return response.JSON(http.StatusOK, dto)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) LoadPlaylistItemDTOs(ctx context.Context, uid string, orgId int64) ([]playlist.PlaylistItemDTO, error) {
|
||||
playlistitems, err := hs.LoadPlaylistItems(ctx, uid, orgId)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
playlistDTOs := make([]playlist.PlaylistItemDTO, 0)
|
||||
|
||||
for _, item := range playlistitems {
|
||||
playlistDTOs = append(playlistDTOs, playlist.PlaylistItemDTO{
|
||||
Id: item.Id,
|
||||
Playlistid: item.PlaylistId,
|
||||
Type: cmplaylist.PlaylistItemType(item.Type),
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
return playlistDTOs, nil
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) LoadPlaylistItems(ctx context.Context, uid string, orgId int64) ([]playlist.PlaylistItem, error) {
|
||||
itemQuery := playlist.GetPlaylistItemsByUidQuery{PlaylistUID: uid, OrgId: orgId}
|
||||
items, err := hs.playlistService.GetItems(ctx, &itemQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
// swagger:route GET /playlists/{uid}/items playlists getPlaylistItems
|
||||
//
|
||||
// Get playlist items.
|
||||
@@ -140,14 +94,14 @@ func (hs *HTTPServer) LoadPlaylistItems(ctx context.Context, uid string, orgId i
|
||||
// 500: internalServerError
|
||||
func (hs *HTTPServer) GetPlaylistItems(c *models.ReqContext) response.Response {
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
cmd := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgID}
|
||||
|
||||
playlistDTOs, err := hs.LoadPlaylistItemDTOs(c.Req.Context(), uid, c.OrgID)
|
||||
|
||||
dto, err := hs.playlistService.Get(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
return response.Error(500, "Could not load playlist items", err)
|
||||
return response.Error(500, "Playlist not found", err)
|
||||
}
|
||||
|
||||
return response.JSON(http.StatusOK, playlistDTOs)
|
||||
return response.JSON(http.StatusOK, dto.Items)
|
||||
}
|
||||
|
||||
// swagger:route GET /playlists/{uid}/dashboards playlists getPlaylistDashboards
|
||||
@@ -235,18 +189,19 @@ func (hs *HTTPServer) UpdatePlaylist(c *models.ReqContext) response.Response {
|
||||
cmd.OrgId = c.OrgID
|
||||
cmd.UID = web.Params(c.Req)[":uid"]
|
||||
|
||||
p, err := hs.playlistService.Update(c.Req.Context(), &cmd)
|
||||
_, err := hs.playlistService.Update(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to save playlist", err)
|
||||
}
|
||||
|
||||
playlistDTOs, err := hs.LoadPlaylistItemDTOs(c.Req.Context(), cmd.UID, c.OrgID)
|
||||
dto, err := hs.playlistService.Get(c.Req.Context(), &playlist.GetPlaylistByUidQuery{
|
||||
UID: cmd.UID,
|
||||
OrgId: c.OrgID,
|
||||
})
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to save playlist", err)
|
||||
return response.Error(500, "Failed to load playlist", err)
|
||||
}
|
||||
|
||||
p.Items = &playlistDTOs
|
||||
return response.JSON(http.StatusOK, p)
|
||||
return response.JSON(http.StatusOK, dto)
|
||||
}
|
||||
|
||||
// swagger:parameters searchPlaylists
|
||||
|
||||
+21
-10
@@ -2,12 +2,14 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos"
|
||||
_ "github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/services/search"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
)
|
||||
@@ -68,28 +70,37 @@ func (hs *HTTPServer) populateDashboardsByTag(ctx context.Context, orgID int64,
|
||||
|
||||
// Deprecated -- the frontend can do this better
|
||||
func (hs *HTTPServer) LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, playlistUID string) (dtos.PlaylistDashboardsSlice, error) {
|
||||
playlistItems, _ := hs.LoadPlaylistItems(ctx, playlistUID, orgID)
|
||||
result := make(dtos.PlaylistDashboardsSlice, 0)
|
||||
dto, err := hs.playlistService.Get(ctx,
|
||||
&playlist.GetPlaylistByUidQuery{UID: playlistUID, OrgId: orgID})
|
||||
if err != nil || dto == nil || dto.Items == nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
playlistItems := *dto.Items
|
||||
|
||||
dashboardByIDs := make([]int64, 0)
|
||||
dashboardByTag := make([]string, 0)
|
||||
dashboardIDOrder := make(map[int64]int)
|
||||
dashboardTagOrder := make(map[string]int)
|
||||
|
||||
for _, i := range playlistItems {
|
||||
if i.Type == "dashboard_by_id" {
|
||||
dashboardID, _ := strconv.ParseInt(i.Value, 10, 64)
|
||||
for i, item := range playlistItems {
|
||||
if item.Type == "dashboard_by_id" {
|
||||
dashboardID, _ := strconv.ParseInt(item.Value, 10, 64)
|
||||
dashboardByIDs = append(dashboardByIDs, dashboardID)
|
||||
dashboardIDOrder[dashboardID] = i.Order
|
||||
dashboardIDOrder[dashboardID] = i
|
||||
}
|
||||
|
||||
if i.Type == "dashboard_by_tag" {
|
||||
dashboardByTag = append(dashboardByTag, i.Value)
|
||||
dashboardTagOrder[i.Value] = i.Order
|
||||
if item.Type == "dashboard_by_tag" {
|
||||
dashboardByTag = append(dashboardByTag, item.Value)
|
||||
dashboardTagOrder[item.Value] = i
|
||||
}
|
||||
|
||||
if item.Type == "dashboard_by_uid" {
|
||||
return nil, fmt.Errorf("dashboard_by_uid not supported by this deprecated API")
|
||||
}
|
||||
}
|
||||
|
||||
result := make(dtos.PlaylistDashboardsSlice, 0)
|
||||
|
||||
var k, _ = hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder)
|
||||
result = append(result, k...)
|
||||
result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)
|
||||
|
||||
Reference in New Issue
Block a user