playlist_play.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package api
  2. import (
  3. "errors"
  4. "strconv"
  5. "github.com/grafana/grafana/pkg/bus"
  6. _ "github.com/grafana/grafana/pkg/log"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/search"
  9. )
  10. func populateDashboardsById(dashboardByIds []int64) ([]m.PlaylistDashboardDto, error) {
  11. result := make([]m.PlaylistDashboardDto, 0)
  12. if len(dashboardByIds) > 0 {
  13. dashboardQuery := m.GetDashboardsQuery{DashboardIds: dashboardByIds}
  14. if err := bus.Dispatch(&dashboardQuery); err != nil {
  15. return result, errors.New("Playlist not found") //TODO: dont swallow error
  16. }
  17. for _, item := range *dashboardQuery.Result {
  18. result = append(result, m.PlaylistDashboardDto{
  19. Id: item.Id,
  20. Slug: item.Slug,
  21. Title: item.Title,
  22. Uri: "db/" + item.Slug,
  23. })
  24. }
  25. }
  26. return result, nil
  27. }
  28. func populateDashboardsByTag(orgId, userId int64, dashboardByTag []string) []m.PlaylistDashboardDto {
  29. result := make([]m.PlaylistDashboardDto, 0)
  30. if len(dashboardByTag) > 0 {
  31. for _, tag := range dashboardByTag {
  32. searchQuery := search.Query{
  33. Title: "",
  34. Tags: []string{tag},
  35. UserId: userId,
  36. Limit: 100,
  37. IsStarred: false,
  38. OrgId: orgId,
  39. }
  40. if err := bus.Dispatch(&searchQuery); err == nil {
  41. for _, item := range searchQuery.Result {
  42. result = append(result, m.PlaylistDashboardDto{
  43. Id: item.Id,
  44. Title: item.Title,
  45. Uri: item.Uri,
  46. })
  47. }
  48. }
  49. }
  50. }
  51. return result
  52. }
  53. func LoadPlaylistDashboards(orgId, userId, playlistId int64) ([]m.PlaylistDashboardDto, error) {
  54. playlistItems, _ := LoadPlaylistItems(playlistId)
  55. dashboardByIds := make([]int64, 0)
  56. dashboardByTag := make([]string, 0)
  57. for _, i := range playlistItems {
  58. if i.Type == "dashboard_by_id" {
  59. dashboardId, _ := strconv.ParseInt(i.Value, 10, 64)
  60. dashboardByIds = append(dashboardByIds, dashboardId)
  61. }
  62. if i.Type == "dashboard_by_tag" {
  63. dashboardByTag = append(dashboardByTag, i.Value)
  64. }
  65. }
  66. result := make([]m.PlaylistDashboardDto, 0)
  67. var k, _ = populateDashboardsById(dashboardByIds)
  68. result = append(result, k...)
  69. result = append(result, populateDashboardsByTag(orgId, userId, dashboardByTag)...)
  70. return result, nil
  71. }