playlist_play.go 2.1 KB

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