2016-01-28 16:56:56 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-10-28 04:29:07 -05:00
|
|
|
"context"
|
2016-09-22 03:00:09 -05:00
|
|
|
"sort"
|
2016-01-28 16:56:56 -06:00
|
|
|
"strconv"
|
|
|
|
|
2016-09-22 03:00:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2019-05-13 01:45:54 -05:00
|
|
|
_ "github.com/grafana/grafana/pkg/infra/log"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-01-28 16:56:56 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/search"
|
|
|
|
)
|
|
|
|
|
2022-01-27 03:33:02 -06:00
|
|
|
func (hs *HTTPServer) populateDashboardsByID(ctx context.Context, dashboardByIDs []int64, dashboardIDOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) {
|
2016-09-22 03:00:09 -05:00
|
|
|
result := make(dtos.PlaylistDashboardsSlice, 0)
|
2016-01-28 16:56:56 -06:00
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
if len(dashboardByIDs) > 0 {
|
2020-03-04 05:57:20 -06:00
|
|
|
dashboardQuery := models.GetDashboardsQuery{DashboardIds: dashboardByIDs}
|
2022-05-19 09:59:12 -05:00
|
|
|
if err := hs.dashboardService.GetDashboards(ctx, &dashboardQuery); err != nil {
|
2016-02-15 03:09:15 -06:00
|
|
|
return result, err
|
2016-01-28 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
2016-07-08 02:35:06 -05:00
|
|
|
for _, item := range dashboardQuery.Result {
|
2016-09-22 03:00:09 -05:00
|
|
|
result = append(result, dtos.PlaylistDashboard{
|
2016-01-28 16:56:56 -06:00
|
|
|
Id: item.Id,
|
|
|
|
Slug: item.Slug,
|
|
|
|
Title: item.Title,
|
|
|
|
Uri: "db/" + item.Slug,
|
2020-03-04 05:57:20 -06:00
|
|
|
Url: models.GetDashboardUrl(item.Uid, item.Slug),
|
2018-03-22 16:13:46 -05:00
|
|
|
Order: dashboardIDOrder[item.Id],
|
2016-01-28 16:56:56 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-01-27 03:33:02 -06:00
|
|
|
func (hs *HTTPServer) populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice {
|
2016-09-22 03:00:09 -05:00
|
|
|
result := make(dtos.PlaylistDashboardsSlice, 0)
|
2016-01-28 16:56:56 -06:00
|
|
|
|
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,
|
|
|
|
}
|
2016-01-28 16:56:56 -06:00
|
|
|
|
2022-01-27 03:33:02 -06:00
|
|
|
if err := hs.SearchService.SearchHandler(ctx, &searchQuery); err == nil {
|
2018-03-22 06:37:35 -05:00
|
|
|
for _, item := range searchQuery.Result {
|
|
|
|
result = append(result, dtos.PlaylistDashboard{
|
2021-02-11 01:49:16 -06:00
|
|
|
Id: item.ID,
|
2019-02-28 03:35:53 -06:00
|
|
|
Slug: item.Slug,
|
2018-03-22 06:37:35 -05:00
|
|
|
Title: item.Title,
|
2021-02-11 01:49:16 -06:00
|
|
|
Uri: item.URI,
|
|
|
|
Url: item.URL,
|
2018-03-22 06:37:35 -05:00
|
|
|
Order: dashboardTagOrder[tag],
|
|
|
|
})
|
2016-01-28 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-06-14 14:32:52 -05:00
|
|
|
func (hs *HTTPServer) LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, playlistUID string) (dtos.PlaylistDashboardsSlice, error) {
|
|
|
|
playlistItems, _ := hs.LoadPlaylistItems(ctx, playlistUID, orgID)
|
2016-01-28 16:56:56 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
dashboardByIDs := make([]int64, 0)
|
2016-01-28 16:56:56 -06:00
|
|
|
dashboardByTag := make([]string, 0)
|
2018-03-22 06:37:35 -05:00
|
|
|
dashboardIDOrder := make(map[int64]int)
|
2016-09-22 03:00:09 -05:00
|
|
|
dashboardTagOrder := make(map[string]int)
|
2016-01-28 16:56:56 -06:00
|
|
|
|
|
|
|
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
|
2016-01-28 16:56:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if i.Type == "dashboard_by_tag" {
|
|
|
|
dashboardByTag = append(dashboardByTag, i.Value)
|
2016-09-22 03:00:09 -05:00
|
|
|
dashboardTagOrder[i.Value] = i.Order
|
2016-01-28 16:56:56 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-22 03:00:09 -05:00
|
|
|
result := make(dtos.PlaylistDashboardsSlice, 0)
|
2016-01-28 16:56:56 -06:00
|
|
|
|
2022-01-27 03:33:02 -06:00
|
|
|
var k, _ = hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder)
|
2016-01-28 16:56:56 -06:00
|
|
|
result = append(result, k...)
|
2022-01-27 03:33:02 -06:00
|
|
|
result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)
|
2016-01-28 16:56:56 -06:00
|
|
|
|
2017-05-24 06:21:43 -05:00
|
|
|
sort.Sort(result)
|
2016-01-28 16:56:56 -06:00
|
|
|
return result, nil
|
|
|
|
}
|