Chore: Introduce playlist service (#52252)
* Store: Introduce playlist service * Integrate playlist service * Update swagger
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package playlist
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Typed errors
|
||||
var (
|
||||
ErrPlaylistNotFound = errors.New("Playlist not found")
|
||||
ErrPlaylistFailedGenerateUniqueUid = errors.New("failed to generate unique playlist UID")
|
||||
ErrCommandValidationFailed = errors.New("command missing required fields")
|
||||
)
|
||||
|
||||
// Playlist model
|
||||
type Playlist struct {
|
||||
Id int64 `json:"id"`
|
||||
UID string `json:"uid" xorm:"uid"`
|
||||
Name string `json:"name"`
|
||||
Interval string `json:"interval"`
|
||||
OrgId int64 `json:"-"`
|
||||
}
|
||||
|
||||
type PlaylistDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UID string `json:"uid"`
|
||||
Name string `json:"name"`
|
||||
Interval string `json:"interval"`
|
||||
OrgId int64 `json:"-"`
|
||||
Items []PlaylistItemDTO `json:"items"`
|
||||
}
|
||||
|
||||
type PlaylistItemDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
PlaylistId int64 `json:"playlistid"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Value string `json:"value"`
|
||||
Order int `json:"order"`
|
||||
}
|
||||
|
||||
type PlaylistItem struct {
|
||||
Id int64
|
||||
PlaylistId int64
|
||||
Type string
|
||||
Value string
|
||||
Order int
|
||||
Title string
|
||||
}
|
||||
|
||||
type Playlists []*Playlist
|
||||
|
||||
//
|
||||
// COMMANDS
|
||||
//
|
||||
|
||||
type UpdatePlaylistCommand struct {
|
||||
OrgId int64 `json:"-"`
|
||||
UID string `json:"uid"`
|
||||
Name string `json:"name" binding:"Required"`
|
||||
Interval string `json:"interval"`
|
||||
Items []PlaylistItemDTO `json:"items"`
|
||||
}
|
||||
|
||||
type CreatePlaylistCommand struct {
|
||||
Name string `json:"name" binding:"Required"`
|
||||
Interval string `json:"interval"`
|
||||
Items []PlaylistItemDTO `json:"items"`
|
||||
|
||||
OrgId int64 `json:"-"`
|
||||
}
|
||||
|
||||
type DeletePlaylistCommand struct {
|
||||
UID string
|
||||
OrgId int64
|
||||
}
|
||||
|
||||
//
|
||||
// QUERIES
|
||||
//
|
||||
|
||||
type GetPlaylistsQuery struct {
|
||||
Name string
|
||||
Limit int
|
||||
OrgId int64
|
||||
}
|
||||
|
||||
type GetPlaylistByUidQuery struct {
|
||||
UID string
|
||||
OrgId int64
|
||||
}
|
||||
|
||||
type GetPlaylistItemsByUidQuery struct {
|
||||
PlaylistUID string
|
||||
OrgId int64
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package playlist
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
Create(context.Context, *CreatePlaylistCommand) (*Playlist, error)
|
||||
Update(context.Context, *UpdatePlaylistCommand) (*PlaylistDTO, error)
|
||||
Get(context.Context, *GetPlaylistByUidQuery) (*Playlist, error)
|
||||
GetItems(context.Context, *GetPlaylistItemsByUidQuery) ([]PlaylistItem, error)
|
||||
Search(context.Context, *GetPlaylistsQuery) (Playlists, error)
|
||||
Delete(ctx context.Context, cmd *DeletePlaylistCommand) error
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package playlistimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
store store
|
||||
}
|
||||
|
||||
func ProvideService(db db.DB) playlist.Service {
|
||||
return &Service{
|
||||
store: &sqlStore{
|
||||
db: db,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, cmd *playlist.CreatePlaylistCommand) (*playlist.Playlist, error) {
|
||||
return s.store.Insert(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, cmd *playlist.UpdatePlaylistCommand) (*playlist.PlaylistDTO, error) {
|
||||
return s.store.Update(ctx, cmd)
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, q *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error) {
|
||||
return s.store.Get(ctx, q)
|
||||
}
|
||||
|
||||
func (s *Service) GetItems(ctx context.Context, q *playlist.GetPlaylistItemsByUidQuery) ([]playlist.PlaylistItem, error) {
|
||||
return s.store.GetItems(ctx, q)
|
||||
}
|
||||
|
||||
func (s *Service) Search(ctx context.Context, q *playlist.GetPlaylistsQuery) (playlist.Playlists, error) {
|
||||
return s.store.List(ctx, q)
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, cmd *playlist.DeletePlaylistCommand) error {
|
||||
return s.store.Delete(ctx, cmd)
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package playlistimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
type store interface {
|
||||
Insert(context.Context, *playlist.CreatePlaylistCommand) (*playlist.Playlist, error)
|
||||
Delete(context.Context, *playlist.DeletePlaylistCommand) error
|
||||
Get(context.Context, *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error)
|
||||
GetItems(context.Context, *playlist.GetPlaylistItemsByUidQuery) ([]playlist.PlaylistItem, error)
|
||||
List(context.Context, *playlist.GetPlaylistsQuery) (playlist.Playlists, error)
|
||||
Update(context.Context, *playlist.UpdatePlaylistCommand) (*playlist.PlaylistDTO, error)
|
||||
}
|
||||
|
||||
type sqlStore struct {
|
||||
db db.DB
|
||||
}
|
||||
|
||||
func (s *sqlStore) Insert(ctx context.Context, cmd *playlist.CreatePlaylistCommand) (*playlist.Playlist, error) {
|
||||
p := playlist.Playlist{}
|
||||
err := s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
uid, err := generateAndValidateNewPlaylistUid(sess, cmd.OrgId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p = playlist.Playlist{
|
||||
Name: cmd.Name,
|
||||
Interval: cmd.Interval,
|
||||
OrgId: cmd.OrgId,
|
||||
UID: uid,
|
||||
}
|
||||
|
||||
_, err = sess.Insert(&p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
playlistItems := make([]playlist.PlaylistItem, 0)
|
||||
for _, item := range cmd.Items {
|
||||
playlistItems = append(playlistItems, playlist.PlaylistItem{
|
||||
PlaylistId: p.Id,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: item.Order,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
_, err = sess.Insert(&playlistItems)
|
||||
|
||||
return err
|
||||
})
|
||||
return &p, err
|
||||
}
|
||||
|
||||
func (s *sqlStore) Update(ctx context.Context, cmd *playlist.UpdatePlaylistCommand) (*playlist.PlaylistDTO, error) {
|
||||
dto := playlist.PlaylistDTO{}
|
||||
err := s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
p := playlist.Playlist{
|
||||
UID: cmd.UID,
|
||||
OrgId: cmd.OrgId,
|
||||
Name: cmd.Name,
|
||||
Interval: cmd.Interval,
|
||||
}
|
||||
|
||||
existingPlaylist := playlist.Playlist{UID: cmd.UID, OrgId: cmd.OrgId}
|
||||
_, err := sess.Get(&existingPlaylist)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Id = existingPlaylist.Id
|
||||
|
||||
dto = playlist.PlaylistDTO{
|
||||
|
||||
Id: p.Id,
|
||||
UID: p.UID,
|
||||
OrgId: p.OrgId,
|
||||
Name: p.Name,
|
||||
Interval: p.Interval,
|
||||
}
|
||||
|
||||
_, err = sess.Where("id=?", p.Id).Cols("name", "interval").Update(&p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rawSQL := "DELETE FROM playlist_item WHERE playlist_id = ?"
|
||||
_, err = sess.Exec(rawSQL, p.Id)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
playlistItems := make([]models.PlaylistItem, 0)
|
||||
|
||||
for index, item := range cmd.Items {
|
||||
playlistItems = append(playlistItems, models.PlaylistItem{
|
||||
PlaylistId: p.Id,
|
||||
Type: item.Type,
|
||||
Value: item.Value,
|
||||
Order: index + 1,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
_, err = sess.Insert(&playlistItems)
|
||||
return err
|
||||
})
|
||||
return &dto, err
|
||||
}
|
||||
|
||||
func (s *sqlStore) Get(ctx context.Context, query *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error) {
|
||||
if query.UID == "" || query.OrgId == 0 {
|
||||
return nil, playlist.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
p := playlist.Playlist{}
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
p = playlist.Playlist{UID: query.UID, OrgId: query.OrgId}
|
||||
exists, err := sess.Get(&p)
|
||||
if !exists {
|
||||
return playlist.ErrPlaylistNotFound
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
return &p, err
|
||||
}
|
||||
|
||||
func (s *sqlStore) Delete(ctx context.Context, cmd *playlist.DeletePlaylistCommand) error {
|
||||
if cmd.UID == "" || cmd.OrgId == 0 {
|
||||
return playlist.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
playlist := playlist.Playlist{UID: cmd.UID, OrgId: cmd.OrgId}
|
||||
_, err := sess.Get(&playlist)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rawPlaylistSQL = "DELETE FROM playlist WHERE uid = ? and org_id = ?"
|
||||
_, err = sess.Exec(rawPlaylistSQL, cmd.UID, cmd.OrgId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var rawItemSQL = "DELETE FROM playlist_item WHERE playlist_id = ?"
|
||||
_, err = sess.Exec(rawItemSQL, playlist.Id)
|
||||
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *sqlStore) List(ctx context.Context, query *playlist.GetPlaylistsQuery) (playlist.Playlists, error) {
|
||||
playlists := make(playlist.Playlists, 0)
|
||||
if query.OrgId == 0 {
|
||||
return playlists, playlist.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
err := s.db.WithDbSession(ctx, func(dbSess *sqlstore.DBSession) error {
|
||||
sess := dbSess.Limit(query.Limit)
|
||||
|
||||
if query.Name != "" {
|
||||
sess.Where("name LIKE ?", "%"+query.Name+"%")
|
||||
}
|
||||
|
||||
sess.Where("org_id = ?", query.OrgId)
|
||||
err := sess.Find(&playlists)
|
||||
|
||||
return err
|
||||
})
|
||||
return playlists, err
|
||||
}
|
||||
|
||||
func (s *sqlStore) GetItems(ctx context.Context, query *playlist.GetPlaylistItemsByUidQuery) ([]playlist.PlaylistItem, error) {
|
||||
var playlistItems = make([]playlist.PlaylistItem, 0)
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
if query.PlaylistUID == "" || query.OrgId == 0 {
|
||||
return models.ErrCommandValidationFailed
|
||||
}
|
||||
|
||||
// getQuery the playlist Id
|
||||
getQuery := &playlist.GetPlaylistByUidQuery{UID: query.PlaylistUID, OrgId: query.OrgId}
|
||||
p, err := s.Get(ctx, getQuery)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = sess.Where("playlist_id=?", p.Id).Find(&playlistItems)
|
||||
|
||||
return err
|
||||
})
|
||||
return playlistItems, err
|
||||
}
|
||||
|
||||
// generateAndValidateNewPlaylistUid generates a playlistUID and verifies that
|
||||
// the uid isn't already in use. This is deliberately overly cautious, since users
|
||||
// can also specify playlist uids during provisioning.
|
||||
func generateAndValidateNewPlaylistUid(sess *sqlstore.DBSession, orgId int64) (string, error) {
|
||||
for i := 0; i < 3; i++ {
|
||||
uid := generateNewUid()
|
||||
|
||||
playlist := models.Playlist{OrgId: orgId, UID: uid}
|
||||
exists, err := sess.Get(&playlist)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return uid, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", models.ErrPlaylistFailedGenerateUniqueUid
|
||||
}
|
||||
|
||||
var generateNewUid func() string = util.GenerateShortUID
|
||||
@@ -0,0 +1,82 @@
|
||||
package playlistimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntegrationPlaylistDataAccess(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
ss := sqlstore.InitTestDB(t)
|
||||
playlistStore := sqlStore{db: ss}
|
||||
|
||||
t.Run("Can create playlist", func(t *testing.T) {
|
||||
items := []playlist.PlaylistItemDTO{
|
||||
{Title: "graphite", Value: "graphite", Type: "dashboard_by_tag"},
|
||||
{Title: "Backend response times", Value: "3", Type: "dashboard_by_id"},
|
||||
}
|
||||
cmd := playlist.CreatePlaylistCommand{Name: "NYC office", Interval: "10m", OrgId: 1, Items: items}
|
||||
p, err := playlistStore.Insert(context.Background(), &cmd)
|
||||
require.NoError(t, err)
|
||||
uid := p.UID
|
||||
|
||||
t.Run("Can get playlist items", func(t *testing.T) {
|
||||
get := &playlist.GetPlaylistItemsByUidQuery{PlaylistUID: uid, OrgId: 1}
|
||||
storedPlaylistItems, err := playlistStore.GetItems(context.Background(), get)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(storedPlaylistItems), len(items))
|
||||
})
|
||||
|
||||
t.Run("Can update playlist", func(t *testing.T) {
|
||||
items := []playlist.PlaylistItemDTO{
|
||||
{Title: "influxdb", Value: "influxdb", Type: "dashboard_by_tag"},
|
||||
{Title: "Backend response times", Value: "2", Type: "dashboard_by_id"},
|
||||
}
|
||||
query := playlist.UpdatePlaylistCommand{Name: "NYC office ", OrgId: 1, UID: uid, Interval: "10s", Items: items}
|
||||
_, err = playlistStore.Update(context.Background(), &query)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Can remove playlist", func(t *testing.T) {
|
||||
deleteQuery := playlist.DeletePlaylistCommand{UID: uid, OrgId: 1}
|
||||
err = playlistStore.Delete(context.Background(), &deleteQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
getQuery := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: 1}
|
||||
p, err := playlistStore.Get(context.Background(), &getQuery)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, uid, p.UID, "playlist should've been removed")
|
||||
require.ErrorIs(t, err, playlist.ErrPlaylistNotFound)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Delete playlist that doesn't exist", func(t *testing.T) {
|
||||
deleteQuery := playlist.DeletePlaylistCommand{UID: "654312", OrgId: 1}
|
||||
err := playlistStore.Delete(context.Background(), &deleteQuery)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Delete playlist with invalid command yields error", func(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
cmd playlist.DeletePlaylistCommand
|
||||
}{
|
||||
{desc: "none", cmd: playlist.DeletePlaylistCommand{}},
|
||||
{desc: "no OrgId", cmd: playlist.DeletePlaylistCommand{UID: "1"}},
|
||||
{desc: "no Uid", cmd: playlist.DeletePlaylistCommand{OrgId: 1}},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.desc, func(t *testing.T) {
|
||||
err := playlistStore.Delete(context.Background(), &tc.cmd)
|
||||
require.EqualError(t, err, playlist.ErrCommandValidationFailed.Error())
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package playlisttest
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/playlist"
|
||||
)
|
||||
|
||||
type FakePlaylistService struct {
|
||||
ExpectedPlaylist *playlist.Playlist
|
||||
ExpectedPlaylistDTO *playlist.PlaylistDTO
|
||||
ExpectedPlaylistItems []playlist.PlaylistItem
|
||||
ExpectedPlaylists playlist.Playlists
|
||||
ExpectedError error
|
||||
}
|
||||
|
||||
func NewPlaylistServiveFake() *FakePlaylistService {
|
||||
return &FakePlaylistService{}
|
||||
}
|
||||
|
||||
func (f *FakePlaylistService) Create(context.Context, *playlist.CreatePlaylistCommand) (*playlist.Playlist, error) {
|
||||
return f.ExpectedPlaylist, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakePlaylistService) Update(context.Context, *playlist.UpdatePlaylistCommand) (*playlist.PlaylistDTO, error) {
|
||||
return f.ExpectedPlaylistDTO, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakePlaylistService) Get(context.Context, *playlist.GetPlaylistByUidQuery) (*playlist.Playlist, error) {
|
||||
return f.ExpectedPlaylist, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakePlaylistService) GetItems(context.Context, *playlist.GetPlaylistItemsByUidQuery) ([]playlist.PlaylistItem, error) {
|
||||
return f.ExpectedPlaylistItems, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakePlaylistService) Search(context.Context, *playlist.GetPlaylistsQuery) (playlist.Playlists, error) {
|
||||
return f.ExpectedPlaylists, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakePlaylistService) Delete(ctx context.Context, cmd *playlist.DeletePlaylistCommand) error {
|
||||
return f.ExpectedError
|
||||
}
|
||||
@@ -72,11 +72,17 @@ type Store interface {
|
||||
GetGlobalQuotaByTarget(ctx context.Context, query *models.GetGlobalQuotaByTargetQuery) error
|
||||
WithTransactionalDbSession(ctx context.Context, callback DBTransactionFunc) error
|
||||
InTransaction(ctx context.Context, fn func(ctx context.Context) error) error
|
||||
// deprecated
|
||||
CreatePlaylist(ctx context.Context, cmd *models.CreatePlaylistCommand) error
|
||||
// deprecated
|
||||
UpdatePlaylist(ctx context.Context, cmd *models.UpdatePlaylistCommand) error
|
||||
// deprecated
|
||||
GetPlaylist(ctx context.Context, query *models.GetPlaylistByUidQuery) error
|
||||
// deprecated
|
||||
DeletePlaylist(ctx context.Context, cmd *models.DeletePlaylistCommand) error
|
||||
// deprecated
|
||||
SearchPlaylists(ctx context.Context, query *models.GetPlaylistsQuery) error
|
||||
// deprecated
|
||||
GetPlaylistItem(ctx context.Context, query *models.GetPlaylistItemsByUidQuery) error
|
||||
GetAlertById(ctx context.Context, query *models.GetAlertByIdQuery) error
|
||||
GetAllAlertQueryHandler(ctx context.Context, query *models.GetAllAlertsQuery) error
|
||||
|
||||
Reference in New Issue
Block a user