grafana/pkg/api/playlist_play.go

98 lines
2.7 KiB
Go
Raw Normal View History

package api
import (
"sort"
"strconv"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
_ "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
)
2018-03-22 16:13:46 -05:00
func populateDashboardsByID(dashboardByIDs []int64, dashboardIDOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) {
result := make(dtos.PlaylistDashboardsSlice, 0)
2018-03-22 16:13:46 -05:00
if len(dashboardByIDs) > 0 {
dashboardQuery := models.GetDashboardsQuery{DashboardIds: dashboardByIDs}
if err := bus.Dispatch(&dashboardQuery); err != nil {
2016-02-15 03:09:15 -06:00
return result, err
}
for _, item := range dashboardQuery.Result {
result = append(result, dtos.PlaylistDashboard{
Id: item.Id,
Slug: item.Slug,
Title: item.Title,
Uri: "db/" + item.Slug,
Url: models.GetDashboardUrl(item.Uid, item.Slug),
2018-03-22 16:13:46 -05:00
Order: dashboardIDOrder[item.Id],
})
}
}
return result, nil
}
func populateDashboardsByTag(orgID int64, signedInUser *models.SignedInUser, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice {
result := make(dtos.PlaylistDashboardsSlice, 0)
2018-03-22 06:37:35 -05:00
for _, tag := range dashboardByTag {
searchQuery := search.Query{
Title: "",
Tags: []string{tag},
SignedInUser: signedInUser,
Limit: 100,
IsStarred: false,
OrgId: orgID,
}
2018-03-22 06:37:35 -05:00
if err := bus.Dispatch(&searchQuery); err == nil {
for _, item := range searchQuery.Result {
result = append(result, dtos.PlaylistDashboard{
Id: item.Id,
Slug: item.Slug,
2018-03-22 06:37:35 -05:00
Title: item.Title,
Uri: item.Uri,
Url: item.Url,
2018-03-22 06:37:35 -05:00
Order: dashboardTagOrder[tag],
})
}
}
}
return result
}
func LoadPlaylistDashboards(orgID int64, signedInUser *models.SignedInUser, playlistID int64) (dtos.PlaylistDashboardsSlice, error) {
2018-03-22 06:37:35 -05:00
playlistItems, _ := LoadPlaylistItems(playlistID)
2018-03-22 06:37:35 -05:00
dashboardByIDs := make([]int64, 0)
dashboardByTag := make([]string, 0)
2018-03-22 06:37:35 -05:00
dashboardIDOrder := make(map[int64]int)
dashboardTagOrder := make(map[string]int)
for _, i := range playlistItems {
if i.Type == "dashboard_by_id" {
2018-03-22 06:37:35 -05:00
dashboardID, _ := strconv.ParseInt(i.Value, 10, 64)
dashboardByIDs = append(dashboardByIDs, dashboardID)
dashboardIDOrder[dashboardID] = i.Order
}
if i.Type == "dashboard_by_tag" {
dashboardByTag = append(dashboardByTag, i.Value)
dashboardTagOrder[i.Value] = i.Order
}
}
result := make(dtos.PlaylistDashboardsSlice, 0)
2018-03-22 16:13:46 -05:00
var k, _ = populateDashboardsByID(dashboardByIDs, dashboardIDOrder)
result = append(result, k...)
2018-03-22 06:37:35 -05:00
result = append(result, populateDashboardsByTag(orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)
sort.Sort(result)
return result, nil
}