mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
chore(playlist): extract playlist start into new file
This commit is contained in:
parent
3ccf7c8006
commit
d66932a8a2
@ -1,11 +1,8 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
_ "github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/middleware"
|
"github.com/grafana/grafana/pkg/middleware"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
)
|
)
|
||||||
@ -101,39 +98,6 @@ func LoadPlaylistItems(id int64) ([]m.PlaylistItem, error) {
|
|||||||
return *itemQuery.Result, nil
|
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
dtos := make([]m.PlaylistDashboardDto, 0)
|
|
||||||
for _, item := range *dashboardQuery.Result {
|
|
||||||
dtos = append(dtos, m.PlaylistDashboardDto{
|
|
||||||
Id: item.Id,
|
|
||||||
Slug: item.Slug,
|
|
||||||
Title: item.Title,
|
|
||||||
Uri: "db/" + item.Slug,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return dtos, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetPlaylistItems(c *middleware.Context) Response {
|
func GetPlaylistItems(c *middleware.Context) Response {
|
||||||
id := c.ParamsInt64(":id")
|
id := c.ParamsInt64(":id")
|
||||||
|
|
||||||
@ -147,9 +111,9 @@ func GetPlaylistItems(c *middleware.Context) Response {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetPlaylistDashboards(c *middleware.Context) Response {
|
func GetPlaylistDashboards(c *middleware.Context) Response {
|
||||||
id := c.ParamsInt64(":id")
|
playlistId := c.ParamsInt64(":id")
|
||||||
|
|
||||||
playlists, err := LoadPlaylistDashboards(id)
|
playlists, err := LoadPlaylistDashboards(c.OrgId, c.UserId, playlistId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ApiError(500, "Could not load dashboards", err)
|
return ApiError(500, "Could not load dashboards", err)
|
||||||
}
|
}
|
||||||
|
88
pkg/api/playlist_play.go
Normal file
88
pkg/api/playlist_play.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
|
_ "github.com/grafana/grafana/pkg/log"
|
||||||
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/services/search"
|
||||||
|
)
|
||||||
|
|
||||||
|
func populateDashboardsById(dashboardByIds []int64) ([]m.PlaylistDashboardDto, error) {
|
||||||
|
result := make([]m.PlaylistDashboardDto, 0)
|
||||||
|
|
||||||
|
if len(dashboardByIds) > 0 {
|
||||||
|
dashboardQuery := m.GetPlaylistDashboardsQuery{DashboardIds: dashboardByIds}
|
||||||
|
if err := bus.Dispatch(&dashboardQuery); err != nil {
|
||||||
|
return result, errors.New("Playlist not found") //TODO: dont swallow error
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range *dashboardQuery.Result {
|
||||||
|
result = append(result, m.PlaylistDashboardDto{
|
||||||
|
Id: item.Id,
|
||||||
|
Slug: item.Slug,
|
||||||
|
Title: item.Title,
|
||||||
|
Uri: "db/" + item.Slug,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func populateDashboardsByTag(orgId, userId int64, dashboardByTag []string) []m.PlaylistDashboardDto {
|
||||||
|
result := make([]m.PlaylistDashboardDto, 0)
|
||||||
|
|
||||||
|
if len(dashboardByTag) > 0 {
|
||||||
|
for _, tag := range dashboardByTag {
|
||||||
|
searchQuery := search.Query{
|
||||||
|
Title: "",
|
||||||
|
Tags: []string{tag},
|
||||||
|
UserId: userId,
|
||||||
|
Limit: 100,
|
||||||
|
IsStarred: false,
|
||||||
|
OrgId: orgId,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&searchQuery); err == nil {
|
||||||
|
for _, item := range searchQuery.Result {
|
||||||
|
result = append(result, m.PlaylistDashboardDto{
|
||||||
|
Id: item.Id,
|
||||||
|
Title: item.Title,
|
||||||
|
Uri: item.Uri,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadPlaylistDashboards(orgId, userId, playlistId int64) ([]m.PlaylistDashboardDto, error) {
|
||||||
|
playlistItems, _ := LoadPlaylistItems(playlistId)
|
||||||
|
|
||||||
|
dashboardByIds := make([]int64, 0)
|
||||||
|
dashboardByTag := make([]string, 0)
|
||||||
|
|
||||||
|
for _, i := range playlistItems {
|
||||||
|
if i.Type == "dashboard_by_id" {
|
||||||
|
dashboardId, _ := strconv.ParseInt(i.Value, 10, 64)
|
||||||
|
dashboardByIds = append(dashboardByIds, dashboardId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.Type == "dashboard_by_tag" {
|
||||||
|
dashboardByTag = append(dashboardByTag, i.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make([]m.PlaylistDashboardDto, 0)
|
||||||
|
|
||||||
|
var k, _ = populateDashboardsById(dashboardByIds)
|
||||||
|
result = append(result, k...)
|
||||||
|
result = append(result, populateDashboardsByTag(orgId, userId, dashboardByTag)...)
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user