2015-12-22 04:07:15 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-01-08 10:32:55 -06:00
|
|
|
"errors"
|
2015-12-22 04:07:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-01-08 10:32:55 -06:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2015-12-22 04:07:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2016-01-08 10:32:55 -06:00
|
|
|
"strconv"
|
2015-12-22 04:07:15 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func ValidateOrgPlaylist(c *middleware.Context) {
|
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
query := m.GetPlaylistByIdQuery{Id: id}
|
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(404, "Playlist not found", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if query.Result.OrgId != c.OrgId {
|
|
|
|
c.JsonApiErr(403, "You are not allowed to edit/view playlist", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func SearchPlaylists(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
query := c.Query("query")
|
|
|
|
limit := c.QueryInt("limit")
|
|
|
|
|
|
|
|
if limit == 0 {
|
|
|
|
limit = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
searchQuery := m.PlaylistQuery{
|
|
|
|
Title: query,
|
|
|
|
Limit: limit,
|
|
|
|
OrgId: c.OrgId,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := bus.Dispatch(&searchQuery)
|
|
|
|
if err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Search failed", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, searchQuery.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func GetPlaylist(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
cmd := m.GetPlaylistByIdQuery{Id: id}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Playlist not found", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
playlistDTOs, _ := LoadPlaylistItemDTOs(id)
|
|
|
|
|
|
|
|
dto := &m.PlaylistDTO{
|
|
|
|
Id: cmd.Result.Id,
|
|
|
|
Title: cmd.Result.Title,
|
2016-01-12 06:56:47 -06:00
|
|
|
Interval: cmd.Result.Interval,
|
2016-01-12 01:36:25 -06:00
|
|
|
OrgId: cmd.Result.OrgId,
|
|
|
|
Items: playlistDTOs,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, dto)
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadPlaylistItemDTOs(id int64) ([]m.PlaylistItemDTO, error) {
|
|
|
|
playlistitems, err := LoadPlaylistItems(id)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-08 10:32:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
playlistDTOs := make([]m.PlaylistItemDTO, 0)
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
for _, item := range playlistitems {
|
2016-01-08 10:32:55 -06:00
|
|
|
playlistDTOs = append(playlistDTOs, m.PlaylistItemDTO{
|
|
|
|
Id: item.Id,
|
|
|
|
PlaylistId: item.PlaylistId,
|
|
|
|
Type: item.Type,
|
|
|
|
Value: item.Value,
|
|
|
|
Order: item.Order,
|
2016-01-12 01:36:25 -06:00
|
|
|
Title: item.Title,
|
2016-01-08 10:32:55 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
return playlistDTOs, nil
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 10:32:55 -06:00
|
|
|
func LoadPlaylistItems(id int64) ([]m.PlaylistItem, error) {
|
|
|
|
itemQuery := m.GetPlaylistItemsByIdQuery{PlaylistId: id}
|
|
|
|
if err := bus.Dispatch(&itemQuery); err != nil {
|
|
|
|
log.Warn("itemQuery failed: %v", err)
|
|
|
|
return nil, errors.New("Playlist not found")
|
|
|
|
}
|
2015-12-22 04:07:15 -06:00
|
|
|
|
2016-01-08 10:32:55 -06:00
|
|
|
return *itemQuery.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadPlaylistDashboards(id int64) ([]m.PlaylistDashboardDto, error) {
|
|
|
|
playlistItems, _ := LoadPlaylistItems(id)
|
|
|
|
|
|
|
|
dashboardIds := make([]int64, 0)
|
|
|
|
|
|
|
|
for _, i := range playlistItems {
|
|
|
|
dashboardId, _ := strconv.ParseInt(i.Value, 10, 64)
|
|
|
|
dashboardIds = append(dashboardIds, dashboardId)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dashboardIds) == 0 {
|
|
|
|
return make([]m.PlaylistDashboardDto, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
dashboardQuery := m.GetPlaylistDashboardsQuery{DashboardIds: dashboardIds}
|
|
|
|
if err := bus.Dispatch(&dashboardQuery); err != nil {
|
|
|
|
log.Warn("dashboardquery failed: %v", err)
|
|
|
|
return nil, errors.New("Playlist not found")
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 09:13:49 -06:00
|
|
|
dtos := make([]m.PlaylistDashboardDto, 0)
|
2016-01-08 10:32:55 -06:00
|
|
|
for _, item := range *dashboardQuery.Result {
|
2016-01-08 09:13:49 -06:00
|
|
|
dtos = append(dtos, m.PlaylistDashboardDto{
|
|
|
|
Id: item.Id,
|
|
|
|
Slug: item.Slug,
|
|
|
|
Title: item.Title,
|
|
|
|
Uri: "db/" + item.Slug,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-08 10:32:55 -06:00
|
|
|
return dtos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPlaylistItems(c *middleware.Context) Response {
|
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
playlistDTOs, err := LoadPlaylistItemDTOs(id)
|
2016-01-08 10:32:55 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ApiError(500, "Could not load playlist items", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, playlistDTOs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPlaylistDashboards(c *middleware.Context) Response {
|
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
playlists, err := LoadPlaylistDashboards(id)
|
|
|
|
if err != nil {
|
|
|
|
return ApiError(500, "Could not load dashboards", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, playlists)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func DeletePlaylist(c *middleware.Context) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
id := c.ParamsInt64(":id")
|
|
|
|
|
|
|
|
cmd := m.DeletePlaylistQuery{Id: id}
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Failed to delete playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, "")
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func CreatePlaylist(c *middleware.Context, query m.CreatePlaylistQuery) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
query.OrgId = c.OrgId
|
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
if err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Failed to create playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, query.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
func UpdatePlaylist(c *middleware.Context, query m.UpdatePlaylistQuery) Response {
|
2015-12-22 04:07:15 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
if err != nil {
|
2016-01-08 07:21:30 -06:00
|
|
|
return ApiError(500, "Failed to save playlist", err)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|
|
|
|
|
2016-01-12 01:36:25 -06:00
|
|
|
playlistDTOs, err := LoadPlaylistItemDTOs(query.Id)
|
2016-01-08 10:32:55 -06:00
|
|
|
if err != nil {
|
|
|
|
return ApiError(500, "Failed to save playlist", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
query.Result.Items = playlistDTOs
|
|
|
|
|
2016-01-08 07:21:30 -06:00
|
|
|
return Json(200, query.Result)
|
2015-12-22 04:07:15 -06:00
|
|
|
}
|