mirror of https://github.com/grafana/grafana
Playlist: Remove unused/deprecated api and unused wrapper (#75503)
parent
f890cb23b8
commit
bbdd1fc3b1
@ -1,110 +0,0 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"context" |
||||
"errors" |
||||
"sort" |
||||
"strconv" |
||||
|
||||
"github.com/grafana/grafana/pkg/api/dtos" |
||||
_ "github.com/grafana/grafana/pkg/infra/log" |
||||
"github.com/grafana/grafana/pkg/services/dashboards" |
||||
"github.com/grafana/grafana/pkg/services/playlist" |
||||
"github.com/grafana/grafana/pkg/services/search" |
||||
"github.com/grafana/grafana/pkg/services/user" |
||||
) |
||||
|
||||
func (hs *HTTPServer) populateDashboardsByID(ctx context.Context, dashboardByIDs []int64, dashboardIDOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) { |
||||
result := make(dtos.PlaylistDashboardsSlice, 0) |
||||
|
||||
if len(dashboardByIDs) > 0 { |
||||
dashboardQuery := dashboards.GetDashboardsQuery{DashboardIDs: dashboardByIDs} |
||||
dashboardQueryResult, err := hs.DashboardService.GetDashboards(ctx, &dashboardQuery) |
||||
if err != nil { |
||||
return result, err |
||||
} |
||||
|
||||
for _, item := range dashboardQueryResult { |
||||
result = append(result, dtos.PlaylistDashboard{ |
||||
Id: item.ID, |
||||
Slug: item.Slug, |
||||
Title: item.Title, |
||||
Uri: "db/" + item.Slug, |
||||
Url: dashboards.GetDashboardURL(item.UID, item.Slug), |
||||
Order: dashboardIDOrder[item.ID], |
||||
}) |
||||
} |
||||
} |
||||
|
||||
return result, nil |
||||
} |
||||
|
||||
func (hs *HTTPServer) populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice { |
||||
result := make(dtos.PlaylistDashboardsSlice, 0) |
||||
|
||||
for _, tag := range dashboardByTag { |
||||
searchQuery := search.Query{ |
||||
Title: "", |
||||
Tags: []string{tag}, |
||||
SignedInUser: signedInUser, |
||||
Limit: 100, |
||||
IsStarred: false, |
||||
OrgId: orgID, |
||||
} |
||||
|
||||
hits, err := hs.SearchService.SearchHandler(ctx, &searchQuery) |
||||
if err == nil { |
||||
for _, item := range hits { |
||||
result = append(result, dtos.PlaylistDashboard{ |
||||
Id: item.ID, |
||||
Slug: item.Slug, |
||||
Title: item.Title, |
||||
Uri: item.URI, |
||||
Url: item.URL, |
||||
Order: dashboardTagOrder[tag], |
||||
}) |
||||
} |
||||
} |
||||
} |
||||
|
||||
return result |
||||
} |
||||
|
||||
// Deprecated -- the frontend can do this better
|
||||
func (hs *HTTPServer) LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *user.SignedInUser, playlistUID string) (dtos.PlaylistDashboardsSlice, error) { |
||||
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, item := range playlistItems { |
||||
switch item.Type { |
||||
case "dashboard_by_id": |
||||
dashboardID, _ := strconv.ParseInt(item.Value, 10, 64) |
||||
dashboardByIDs = append(dashboardByIDs, dashboardID) |
||||
dashboardIDOrder[dashboardID] = i |
||||
case "dashboard_by_tag": |
||||
dashboardByTag = append(dashboardByTag, item.Value) |
||||
dashboardTagOrder[item.Value] = i |
||||
case "dashboard_by_uid": |
||||
return nil, errors.New("dashboard_by_uid not supported by this deprecated API") |
||||
default: |
||||
} |
||||
} |
||||
|
||||
k, _ := hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder) |
||||
result = append(result, k...) |
||||
result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...) |
||||
|
||||
sort.Sort(result) |
||||
return result, nil |
||||
} |
@ -1,188 +0,0 @@ |
||||
package playlistimpl |
||||
|
||||
import ( |
||||
"context" |
||||
"encoding/json" |
||||
"fmt" |
||||
|
||||
"github.com/grafana/grafana/pkg/infra/appcontext" |
||||
"github.com/grafana/grafana/pkg/infra/grn" |
||||
"github.com/grafana/grafana/pkg/services/playlist" |
||||
"github.com/grafana/grafana/pkg/services/sqlstore/session" |
||||
"github.com/grafana/grafana/pkg/services/store/entity" |
||||
"github.com/grafana/grafana/pkg/services/user" |
||||
) |
||||
|
||||
// This is a playlist implementation that will:
|
||||
// 1. CREATE/UPDATE/DELETE everythign with existing direct SQL store
|
||||
// 2. CREATE/UPDATE/DELETE same items to the object store
|
||||
// 3. Use the object store for all read operations
|
||||
// This givs us a safe test bed to work with the store but still roll back without any lost work
|
||||
type entityStoreImpl struct { |
||||
sess *session.SessionDB |
||||
sqlimpl *Service |
||||
store entity.EntityStoreServer |
||||
} |
||||
|
||||
var _ playlist.Service = &entityStoreImpl{} |
||||
|
||||
func (s *entityStoreImpl) sync() { |
||||
type Info struct { |
||||
OrgID int64 `db:"org_id"` |
||||
UID string `db:"uid"` |
||||
} |
||||
results := []Info{} |
||||
err := s.sess.Select(context.Background(), &results, "SELECT org_id,uid FROM playlist ORDER BY org_id asc") |
||||
if err != nil { |
||||
fmt.Printf("error loading playlists") |
||||
return |
||||
} |
||||
|
||||
// Change the org_id with each row
|
||||
rowUser := &user.SignedInUser{ |
||||
OrgID: 0, // gets filled in from each row
|
||||
UserID: 0, // Admin user
|
||||
IsGrafanaAdmin: true, |
||||
} |
||||
ctx := appcontext.WithUser(context.Background(), rowUser) |
||||
for _, info := range results { |
||||
dto, err := s.sqlimpl.Get(ctx, &playlist.GetPlaylistByUidQuery{ |
||||
OrgId: info.OrgID, |
||||
UID: info.UID, |
||||
}) |
||||
if err != nil { |
||||
fmt.Printf("error loading playlist: %v", err) |
||||
return |
||||
} |
||||
body, _ := json.Marshal(dto) |
||||
_, _ = s.store.Write(ctx, &entity.WriteEntityRequest{ |
||||
GRN: &grn.GRN{ |
||||
TenantID: info.OrgID, |
||||
ResourceIdentifier: info.UID, |
||||
ResourceKind: entity.StandardKindPlaylist, |
||||
}, |
||||
Body: body, |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func (s *entityStoreImpl) Create(ctx context.Context, cmd *playlist.CreatePlaylistCommand) (*playlist.Playlist, error) { |
||||
rsp, err := s.sqlimpl.store.Insert(ctx, cmd) |
||||
if err == nil && rsp != nil { |
||||
body, err := json.Marshal(cmd) |
||||
if err != nil { |
||||
return rsp, fmt.Errorf("unable to write playlist to store") |
||||
} |
||||
_, err = s.store.Write(ctx, &entity.WriteEntityRequest{ |
||||
GRN: &grn.GRN{ |
||||
ResourceKind: entity.StandardKindPlaylist, |
||||
ResourceIdentifier: rsp.UID, |
||||
}, |
||||
Body: body, |
||||
}) |
||||
if err != nil { |
||||
return rsp, fmt.Errorf("unable to write playlist to store") |
||||
} |
||||
} |
||||
return rsp, err |
||||
} |
||||
|
||||
func (s *entityStoreImpl) Update(ctx context.Context, cmd *playlist.UpdatePlaylistCommand) (*playlist.PlaylistDTO, error) { |
||||
rsp, err := s.sqlimpl.store.Update(ctx, cmd) |
||||
if err == nil { |
||||
body, err := json.Marshal(cmd) |
||||
if err != nil { |
||||
return rsp, fmt.Errorf("unable to write playlist to store") |
||||
} |
||||
_, err = s.store.Write(ctx, &entity.WriteEntityRequest{ |
||||
GRN: &grn.GRN{ |
||||
ResourceIdentifier: rsp.Uid, |
||||
ResourceKind: entity.StandardKindPlaylist, |
||||
}, |
||||
Body: body, |
||||
}) |
||||
if err != nil { |
||||
return rsp, fmt.Errorf("unable to write playlist to store") |
||||
} |
||||
} |
||||
return rsp, err |
||||
} |
||||
|
||||
func (s *entityStoreImpl) Delete(ctx context.Context, cmd *playlist.DeletePlaylistCommand) error { |
||||
err := s.sqlimpl.store.Delete(ctx, cmd) |
||||
if err == nil { |
||||
_, err = s.store.Delete(ctx, &entity.DeleteEntityRequest{ |
||||
GRN: &grn.GRN{ |
||||
ResourceIdentifier: cmd.UID, |
||||
ResourceKind: entity.StandardKindPlaylist, |
||||
}, |
||||
}) |
||||
if err != nil { |
||||
return fmt.Errorf("unable to delete playlist to store") |
||||
} |
||||
} |
||||
return err |
||||
} |
||||
|
||||
//------------------------------------------------------
|
||||
// Read access is managed entirely by the object store
|
||||
//------------------------------------------------------
|
||||
|
||||
func (s *entityStoreImpl) GetWithoutItems(ctx context.Context, q *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error) { |
||||
p, err := s.Get(ctx, q) // OrgID is actually picked from the user!
|
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &playlist.Playlist{ |
||||
UID: p.Uid, |
||||
OrgId: q.OrgId, |
||||
Name: p.Name, |
||||
Interval: p.Interval, |
||||
}, nil |
||||
} |
||||
|
||||
func (s *entityStoreImpl) Get(ctx context.Context, q *playlist.GetPlaylistByUidQuery) (*playlist.PlaylistDTO, error) { |
||||
rsp, err := s.store.Read(ctx, &entity.ReadEntityRequest{ |
||||
GRN: &grn.GRN{ |
||||
ResourceIdentifier: q.UID, |
||||
ResourceKind: entity.StandardKindPlaylist, |
||||
}, |
||||
WithBody: true, |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
if rsp == nil || rsp.Body == nil { |
||||
return nil, fmt.Errorf("missing object") |
||||
} |
||||
|
||||
// Get the object from payload
|
||||
found := &playlist.PlaylistDTO{} |
||||
err = json.Unmarshal(rsp.Body, found) |
||||
return found, err |
||||
} |
||||
|
||||
func (s *entityStoreImpl) Search(ctx context.Context, q *playlist.GetPlaylistsQuery) (playlist.Playlists, error) { |
||||
playlists := make(playlist.Playlists, 0) |
||||
|
||||
rsp, err := s.store.Search(ctx, &entity.EntitySearchRequest{ |
||||
Kind: []string{entity.StandardKindPlaylist}, |
||||
WithBody: true, |
||||
Limit: 1000, |
||||
}) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
for _, res := range rsp.Results { |
||||
found := &playlist.PlaylistDTO{} |
||||
if res.Body != nil { |
||||
err = json.Unmarshal(res.Body, found) |
||||
} |
||||
playlists = append(playlists, &playlist.Playlist{ |
||||
UID: res.GRN.ResourceIdentifier, |
||||
Name: res.Name, |
||||
Interval: found.Interval, |
||||
}) |
||||
} |
||||
return playlists, err |
||||
} |
Loading…
Reference in new issue