Skip to content

Commit fb379ae

Browse files
authored
Chore: Introduce playlist service (grafana#52252)
* Store: Introduce playlist service * Integrate playlist service * Update swagger
1 parent 332639c commit fb379ae

File tree

13 files changed

+587
-58
lines changed

13 files changed

+587
-58
lines changed

pkg/api/docs/definitions/playlists.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package definitions
22

33
import (
44
"github.com/grafana/grafana/pkg/api/dtos"
5-
"github.com/grafana/grafana/pkg/models"
5+
"github.com/grafana/grafana/pkg/services/playlist"
66
)
77

88
// swagger:route GET /playlists playlists searchPlaylists
@@ -121,7 +121,7 @@ type DeletePlaylistParams struct {
121121
type UpdatePlaylistParams struct {
122122
// in:body
123123
// required:true
124-
Body models.UpdatePlaylistCommand
124+
Body playlist.UpdatePlaylistCommand
125125
// in:path
126126
// required:true
127127
UID string `json:"uid"`
@@ -131,28 +131,28 @@ type UpdatePlaylistParams struct {
131131
type CreatePlaylistParams struct {
132132
// in:body
133133
// required:true
134-
Body models.CreatePlaylistCommand
134+
Body playlist.CreatePlaylistCommand
135135
}
136136

137137
// swagger:response searchPlaylistsResponse
138138
type SearchPlaylistsResponse struct {
139139
// The response message
140140
// in: body
141-
Body models.Playlists `json:"body"`
141+
Body playlist.Playlists `json:"body"`
142142
}
143143

144144
// swagger:response getPlaylistResponse
145145
type GetPlaylistResponse struct {
146146
// The response message
147147
// in: body
148-
Body *models.PlaylistDTO `json:"body"`
148+
Body *playlist.PlaylistDTO `json:"body"`
149149
}
150150

151151
// swagger:response getPlaylistItemsResponse
152152
type GetPlaylistItemsResponse struct {
153153
// The response message
154154
// in: body
155-
Body []models.PlaylistItemDTO `json:"body"`
155+
Body []playlist.PlaylistItemDTO `json:"body"`
156156
}
157157

158158
// swagger:response getPlaylistDashboardsResponse
@@ -166,12 +166,12 @@ type GetPlaylistDashboardsResponse struct {
166166
type UpdatePlaylistResponseResponse struct {
167167
// The response message
168168
// in: body
169-
Body *models.PlaylistDTO `json:"body"`
169+
Body *playlist.PlaylistDTO `json:"body"`
170170
}
171171

172172
// swagger:response createPlaylistResponse
173173
type CreatePlaylistResponse struct {
174174
// The response message
175175
// in: body
176-
Body *models.Playlist `json:"body"`
176+
Body *playlist.Playlist `json:"body"`
177177
}

pkg/api/http_server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ import (
5858
"github.com/grafana/grafana/pkg/services/login"
5959
"github.com/grafana/grafana/pkg/services/ngalert"
6060
"github.com/grafana/grafana/pkg/services/notifications"
61+
"github.com/grafana/grafana/pkg/services/playlist"
6162
"github.com/grafana/grafana/pkg/services/plugindashboards"
6263
pluginSettings "github.com/grafana/grafana/pkg/services/pluginsettings/service"
6364
pref "github.com/grafana/grafana/pkg/services/preference"
@@ -168,6 +169,7 @@ type HTTPServer struct {
168169
dashboardVersionService dashver.Service
169170
PublicDashboardsApi *publicdashboardsApi.Api
170171
starService star.Service
172+
playlistService playlist.Service
171173
CoremodelRegistry *registry.Generic
172174
CoremodelStaticRegistry *registry.Static
173175
kvStore kvstore.KVStore
@@ -206,7 +208,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
206208
avatarCacheServer *avatar.AvatarCacheServer, preferenceService pref.Service, entityEventsService store.EntityEventsService,
207209
teamsPermissionsService accesscontrol.TeamPermissionsService, folderPermissionsService accesscontrol.FolderPermissionsService,
208210
dashboardPermissionsService accesscontrol.DashboardPermissionsService, dashboardVersionService dashver.Service,
209-
starService star.Service, csrfService csrf.Service, coremodelRegistry *registry.Generic, coremodelStaticRegistry *registry.Static,
211+
starService star.Service, playlistService playlist.Service, csrfService csrf.Service, coremodelRegistry *registry.Generic, coremodelStaticRegistry *registry.Static,
210212
kvStore kvstore.KVStore, secretsMigrator secrets.Migrator, remoteSecretsCheck secretsKV.UseRemoteSecretsPluginCheck,
211213
publicDashboardsApi *publicdashboardsApi.Api, userService user.Service) (*HTTPServer, error) {
212214
web.Env = cfg.Env
@@ -289,6 +291,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
289291
dashboardPermissionsService: dashboardPermissionsService,
290292
dashboardVersionService: dashboardVersionService,
291293
starService: starService,
294+
playlistService: playlistService,
292295
CoremodelRegistry: coremodelRegistry,
293296
CoremodelStaticRegistry: coremodelStaticRegistry,
294297
kvStore: kvStore,

pkg/api/playlist.go

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,26 @@ import (
66

77
"github.com/grafana/grafana/pkg/api/response"
88
"github.com/grafana/grafana/pkg/models"
9+
"github.com/grafana/grafana/pkg/services/playlist"
910
"github.com/grafana/grafana/pkg/web"
1011
)
1112

1213
func (hs *HTTPServer) ValidateOrgPlaylist(c *models.ReqContext) {
1314
uid := web.Params(c.Req)[":uid"]
14-
query := models.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgId}
15-
err := hs.SQLStore.GetPlaylist(c.Req.Context(), &query)
15+
query := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgId}
16+
p, err := hs.playlistService.Get(c.Req.Context(), &query)
1617

1718
if err != nil {
1819
c.JsonApiErr(404, "Playlist not found", err)
1920
return
2021
}
2122

22-
if query.Result.OrgId == 0 {
23+
if p.OrgId == 0 {
2324
c.JsonApiErr(404, "Playlist not found", err)
2425
return
2526
}
2627

27-
if query.Result.OrgId != c.OrgId {
28+
if p.OrgId != c.OrgId {
2829
c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
2930
return
3031
}
@@ -38,53 +39,54 @@ func (hs *HTTPServer) SearchPlaylists(c *models.ReqContext) response.Response {
3839
limit = 1000
3940
}
4041

41-
searchQuery := models.GetPlaylistsQuery{
42+
searchQuery := playlist.GetPlaylistsQuery{
4243
Name: query,
4344
Limit: limit,
4445
OrgId: c.OrgId,
4546
}
4647

47-
err := hs.SQLStore.SearchPlaylists(c.Req.Context(), &searchQuery)
48+
playlists, err := hs.playlistService.Search(c.Req.Context(), &searchQuery)
4849
if err != nil {
4950
return response.Error(500, "Search failed", err)
5051
}
5152

52-
return response.JSON(http.StatusOK, searchQuery.Result)
53+
return response.JSON(http.StatusOK, playlists)
5354
}
5455

5556
func (hs *HTTPServer) GetPlaylist(c *models.ReqContext) response.Response {
5657
uid := web.Params(c.Req)[":uid"]
57-
cmd := models.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgId}
58+
cmd := playlist.GetPlaylistByUidQuery{UID: uid, OrgId: c.OrgId}
5859

59-
if err := hs.SQLStore.GetPlaylist(c.Req.Context(), &cmd); err != nil {
60+
p, err := hs.playlistService.Get(c.Req.Context(), &cmd)
61+
if err != nil {
6062
return response.Error(500, "Playlist not found", err)
6163
}
6264

6365
playlistDTOs, _ := hs.LoadPlaylistItemDTOs(c.Req.Context(), uid, c.OrgId)
6466

65-
dto := &models.PlaylistDTO{
66-
Id: cmd.Result.Id,
67-
UID: cmd.Result.UID,
68-
Name: cmd.Result.Name,
69-
Interval: cmd.Result.Interval,
70-
OrgId: cmd.Result.OrgId,
67+
dto := &playlist.PlaylistDTO{
68+
Id: p.Id,
69+
UID: p.UID,
70+
Name: p.Name,
71+
Interval: p.Interval,
72+
OrgId: p.OrgId,
7173
Items: playlistDTOs,
7274
}
7375

7476
return response.JSON(http.StatusOK, dto)
7577
}
7678

77-
func (hs *HTTPServer) LoadPlaylistItemDTOs(ctx context.Context, uid string, orgId int64) ([]models.PlaylistItemDTO, error) {
79+
func (hs *HTTPServer) LoadPlaylistItemDTOs(ctx context.Context, uid string, orgId int64) ([]playlist.PlaylistItemDTO, error) {
7880
playlistitems, err := hs.LoadPlaylistItems(ctx, uid, orgId)
7981

8082
if err != nil {
8183
return nil, err
8284
}
8385

84-
playlistDTOs := make([]models.PlaylistItemDTO, 0)
86+
playlistDTOs := make([]playlist.PlaylistItemDTO, 0)
8587

8688
for _, item := range playlistitems {
87-
playlistDTOs = append(playlistDTOs, models.PlaylistItemDTO{
89+
playlistDTOs = append(playlistDTOs, playlist.PlaylistItemDTO{
8890
Id: item.Id,
8991
PlaylistId: item.PlaylistId,
9092
Type: item.Type,
@@ -97,13 +99,14 @@ func (hs *HTTPServer) LoadPlaylistItemDTOs(ctx context.Context, uid string, orgI
9799
return playlistDTOs, nil
98100
}
99101

100-
func (hs *HTTPServer) LoadPlaylistItems(ctx context.Context, uid string, orgId int64) ([]models.PlaylistItem, error) {
101-
itemQuery := models.GetPlaylistItemsByUidQuery{PlaylistUID: uid, OrgId: orgId}
102-
if err := hs.SQLStore.GetPlaylistItem(ctx, &itemQuery); err != nil {
102+
func (hs *HTTPServer) LoadPlaylistItems(ctx context.Context, uid string, orgId int64) ([]playlist.PlaylistItem, error) {
103+
itemQuery := playlist.GetPlaylistItemsByUidQuery{PlaylistUID: uid, OrgId: orgId}
104+
items, err := hs.playlistService.GetItems(ctx, &itemQuery)
105+
if err != nil {
103106
return nil, err
104107
}
105108

106-
return *itemQuery.Result, nil
109+
return items, nil
107110
}
108111

109112
func (hs *HTTPServer) GetPlaylistItems(c *models.ReqContext) response.Response {
@@ -132,37 +135,39 @@ func (hs *HTTPServer) GetPlaylistDashboards(c *models.ReqContext) response.Respo
132135
func (hs *HTTPServer) DeletePlaylist(c *models.ReqContext) response.Response {
133136
uid := web.Params(c.Req)[":uid"]
134137

135-
cmd := models.DeletePlaylistCommand{UID: uid, OrgId: c.OrgId}
136-
if err := hs.SQLStore.DeletePlaylist(c.Req.Context(), &cmd); err != nil {
138+
cmd := playlist.DeletePlaylistCommand{UID: uid, OrgId: c.OrgId}
139+
if err := hs.playlistService.Delete(c.Req.Context(), &cmd); err != nil {
137140
return response.Error(500, "Failed to delete playlist", err)
138141
}
139142

140143
return response.JSON(http.StatusOK, "")
141144
}
142145

143146
func (hs *HTTPServer) CreatePlaylist(c *models.ReqContext) response.Response {
144-
cmd := models.CreatePlaylistCommand{}
147+
cmd := playlist.CreatePlaylistCommand{}
145148
if err := web.Bind(c.Req, &cmd); err != nil {
146149
return response.Error(http.StatusBadRequest, "bad request data", err)
147150
}
148151
cmd.OrgId = c.OrgId
149152

150-
if err := hs.SQLStore.CreatePlaylist(c.Req.Context(), &cmd); err != nil {
153+
p, err := hs.playlistService.Create(c.Req.Context(), &cmd)
154+
if err != nil {
151155
return response.Error(500, "Failed to create playlist", err)
152156
}
153157

154-
return response.JSON(http.StatusOK, cmd.Result)
158+
return response.JSON(http.StatusOK, p)
155159
}
156160

157161
func (hs *HTTPServer) UpdatePlaylist(c *models.ReqContext) response.Response {
158-
cmd := models.UpdatePlaylistCommand{}
162+
cmd := playlist.UpdatePlaylistCommand{}
159163
if err := web.Bind(c.Req, &cmd); err != nil {
160164
return response.Error(http.StatusBadRequest, "bad request data", err)
161165
}
162166
cmd.OrgId = c.OrgId
163167
cmd.UID = web.Params(c.Req)[":uid"]
164168

165-
if err := hs.SQLStore.UpdatePlaylist(c.Req.Context(), &cmd); err != nil {
169+
p, err := hs.playlistService.Update(c.Req.Context(), &cmd)
170+
if err != nil {
166171
return response.Error(500, "Failed to save playlist", err)
167172
}
168173

@@ -171,6 +176,6 @@ func (hs *HTTPServer) UpdatePlaylist(c *models.ReqContext) response.Response {
171176
return response.Error(500, "Failed to save playlist", err)
172177
}
173178

174-
cmd.Result.Items = playlistDTOs
175-
return response.JSON(http.StatusOK, cmd.Result)
179+
p.Items = playlistDTOs
180+
return response.JSON(http.StatusOK, p)
176181
}

pkg/server/wire.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package server
66
import (
77
"github.com/google/wire"
88
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
9+
"github.com/grafana/grafana/pkg/services/playlist/playlistimpl"
910
"github.com/grafana/grafana/pkg/services/store/sanitizer"
1011

1112
"github.com/grafana/grafana/pkg/api"
@@ -286,6 +287,7 @@ var wireBasicSet = wire.NewSet(
286287
ossaccesscontrol.ProvideDashboardPermissions,
287288
wire.Bind(new(accesscontrol.DashboardPermissionsService), new(*ossaccesscontrol.DashboardPermissionsService)),
288289
starimpl.ProvideService,
290+
playlistimpl.ProvideService,
289291
dashverimpl.ProvideService,
290292
publicdashboardsService.ProvideService,
291293
wire.Bind(new(publicdashboards.Service), new(*publicdashboardsService.PublicDashboardServiceImpl)),

pkg/services/playlist/model.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package playlist
2+
3+
import (
4+
"errors"
5+
)
6+
7+
// Typed errors
8+
var (
9+
ErrPlaylistNotFound = errors.New("Playlist not found")
10+
ErrPlaylistFailedGenerateUniqueUid = errors.New("failed to generate unique playlist UID")
11+
ErrCommandValidationFailed = errors.New("command missing required fields")
12+
)
13+
14+
// Playlist model
15+
type Playlist struct {
16+
Id int64 `json:"id"`
17+
UID string `json:"uid" xorm:"uid"`
18+
Name string `json:"name"`
19+
Interval string `json:"interval"`
20+
OrgId int64 `json:"-"`
21+
}
22+
23+
type PlaylistDTO struct {
24+
Id int64 `json:"id"`
25+
UID string `json:"uid"`
26+
Name string `json:"name"`
27+
Interval string `json:"interval"`
28+
OrgId int64 `json:"-"`
29+
Items []PlaylistItemDTO `json:"items"`
30+
}
31+
32+
type PlaylistItemDTO struct {
33+
Id int64 `json:"id"`
34+
PlaylistId int64 `json:"playlistid"`
35+
Type string `json:"type"`
36+
Title string `json:"title"`
37+
Value string `json:"value"`
38+
Order int `json:"order"`
39+
}
40+
41+
type PlaylistItem struct {
42+
Id int64
43+
PlaylistId int64
44+
Type string
45+
Value string
46+
Order int
47+
Title string
48+
}
49+
50+
type Playlists []*Playlist
51+
52+
//
53+
// COMMANDS
54+
//
55+
56+
type UpdatePlaylistCommand struct {
57+
OrgId int64 `json:"-"`
58+
UID string `json:"uid"`
59+
Name string `json:"name" binding:"Required"`
60+
Interval string `json:"interval"`
61+
Items []PlaylistItemDTO `json:"items"`
62+
}
63+
64+
type CreatePlaylistCommand struct {
65+
Name string `json:"name" binding:"Required"`
66+
Interval string `json:"interval"`
67+
Items []PlaylistItemDTO `json:"items"`
68+
69+
OrgId int64 `json:"-"`
70+
}
71+
72+
type DeletePlaylistCommand struct {
73+
UID string
74+
OrgId int64
75+
}
76+
77+
//
78+
// QUERIES
79+
//
80+
81+
type GetPlaylistsQuery struct {
82+
Name string
83+
Limit int
84+
OrgId int64
85+
}
86+
87+
type GetPlaylistByUidQuery struct {
88+
UID string
89+
OrgId int64
90+
}
91+
92+
type GetPlaylistItemsByUidQuery struct {
93+
PlaylistUID string
94+
OrgId int64
95+
}

0 commit comments

Comments
 (0)