grafana/pkg/api/playlist_play.go
owensmallwood 0b4af38bfa
Public Dashboards: Query Caching (#51403)
* passes id and uid to PublicDashboardDatasource

* betterer results

* If for a public dashboard, return the PublicDashboardDataSource first or else getDatasourceSrv.get() will fail bc of no authed user.

Added some unit tests for resolving the uid from the many possible datasource types.

* updates betterer

* Exports DashboardService. Adds method to DashboardService to build anonymous user for use with public dashboards where there is no authed user. Adds method on dashboard_queries to get all dashboard uids from a dashboard.

* refactors to get unique datasource uids

* Adds tests for getting all unique datasource uids off a dashboard

* adds test for building anonymous user with read and query actions that are scoped to each datasource uid in the dashboard

* updates casing of DashboardService

* updates test case to have additional panel with a different datasource

* gives default interval to public dashboard data source
2022-07-06 12:42:39 -06:00

98 lines
2.8 KiB
Go

package api
import (
"context"
"sort"
"strconv"
"github.com/grafana/grafana/pkg/api/dtos"
_ "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/search"
)
func (hs *HTTPServer) populateDashboardsByID(ctx context.Context, dashboardByIDs []int64, dashboardIDOrder map[int64]int) (dtos.PlaylistDashboardsSlice, error) {
result := make(dtos.PlaylistDashboardsSlice, 0)
if len(dashboardByIDs) > 0 {
dashboardQuery := models.GetDashboardsQuery{DashboardIds: dashboardByIDs}
if err := hs.DashboardService.GetDashboards(ctx, &dashboardQuery); err != nil {
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),
Order: dashboardIDOrder[item.Id],
})
}
}
return result, nil
}
func (hs *HTTPServer) populateDashboardsByTag(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, dashboardByTag []string, dashboardTagOrder map[string]int) dtos.PlaylistDashboardsSlice {
result := make(dtos.PlaylistDashboardsSlice, 0)
for _, tag := range dashboardByTag {
searchQuery := search.Query{
Title: "",
Tags: []string{tag},
SignedInUser: signedInUser,
Limit: 100,
IsStarred: false,
OrgId: orgID,
}
if err := hs.SearchService.SearchHandler(ctx, &searchQuery); err == nil {
for _, item := range searchQuery.Result {
result = append(result, dtos.PlaylistDashboard{
Id: item.ID,
Slug: item.Slug,
Title: item.Title,
Uri: item.URI,
Url: item.URL,
Order: dashboardTagOrder[tag],
})
}
}
}
return result
}
func (hs *HTTPServer) LoadPlaylistDashboards(ctx context.Context, orgID int64, signedInUser *models.SignedInUser, playlistUID string) (dtos.PlaylistDashboardsSlice, error) {
playlistItems, _ := hs.LoadPlaylistItems(ctx, playlistUID, orgID)
dashboardByIDs := make([]int64, 0)
dashboardByTag := make([]string, 0)
dashboardIDOrder := make(map[int64]int)
dashboardTagOrder := make(map[string]int)
for _, i := range playlistItems {
if i.Type == "dashboard_by_id" {
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)
var k, _ = hs.populateDashboardsByID(ctx, dashboardByIDs, dashboardIDOrder)
result = append(result, k...)
result = append(result, hs.populateDashboardsByTag(ctx, orgID, signedInUser, dashboardByTag, dashboardTagOrder)...)
sort.Sort(result)
return result, nil
}