Files
grafana/pkg/services/dashboards/service/dashboard_public.go
owensmallwood 1bb2d2599c Public Dashboards: Pubdash panels get data from pubdash api (#50556)
* Public dashboard query API

* Create new API on service for building metric request

* Flesh out testing, implement BuildPublicDashboardMetricRequest

* Test for errors and missing panels

* WIP: Test for multiple datasources

* Refactor tests, add supporting code for multiple datasources

* Gets the panel data from the pubdash query api

* Adds tests to make sure we get the correct api url from retrieving panel data

* Public dashboard query API

* Create new API on service for building metric request

* Flesh out testing, implement BuildPublicDashboardMetricRequest

* Test for errors and missing panels

* WIP: Test for multiple datasources

* Refactor tests, add supporting code for multiple datasources

* Handle queries from multiple datasources

* Replace dashboard time range with pubdash time range settings

* Fix comments from review, build failure

* removes changes to DataSourceWithBackend.ts regarding getting the pubdash panel query url. Going to do this in a new class, PublicDashboardDataSource.ts

* Include pubdash Uid in dashboard meta

* Creates new PublicDashboardDataSource.ts and adds test

* Passes pubdash uid down to PanelQueryRunner.ts to a PublicDashboardDatasource can be chosen when were looking at a public dashboard

* removes comment

* checks for error when unmarshalling json

* Only replace dashboard time settings with pubdash time settings when pubdash time settings exist

* formatting and added comment

Co-authored-by: Jesse Weaver <jesse.weaver@grafana.com>
Co-authored-by: Jeff Levin <jeff@levinology.com>
2022-06-13 18:03:43 -06:00

104 lines
3.1 KiB
Go

package service
import (
"context"
"encoding/json"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
)
// Gets public dashboard via generated Uid
func (dr *DashboardServiceImpl) GetPublicDashboard(ctx context.Context, dashboardUid string) (*models.Dashboard, error) {
pdc, d, err := dr.dashboardStore.GetPublicDashboard(dashboardUid)
if err != nil {
return nil, err
}
if pdc == nil || d == nil {
return nil, models.ErrPublicDashboardNotFound
}
if !d.IsPublic {
return nil, models.ErrPublicDashboardNotFound
}
// Replace dashboard time range with pubdash time range
if pdc.TimeSettings != "" {
var pdcTimeSettings map[string]interface{}
err = json.Unmarshal([]byte(pdc.TimeSettings), &pdcTimeSettings)
if err != nil {
return nil, err
}
d.Data.Set("time", pdcTimeSettings)
}
return d, nil
}
// GetPublicDashboardConfig is a helper method to retrieve the public dashboard configuration for a given dashboard from the database
func (dr *DashboardServiceImpl) GetPublicDashboardConfig(ctx context.Context, orgId int64, dashboardUid string) (*models.PublicDashboardConfig, error) {
pdc, err := dr.dashboardStore.GetPublicDashboardConfig(orgId, dashboardUid)
if err != nil {
return nil, err
}
return pdc, nil
}
// SavePublicDashboardConfig is a helper method to persist the sharing config
// to the database. It handles validations for sharing config and persistence
func (dr *DashboardServiceImpl) SavePublicDashboardConfig(ctx context.Context, dto *dashboards.SavePublicDashboardConfigDTO) (*models.PublicDashboardConfig, error) {
cmd := models.SavePublicDashboardConfigCommand{
DashboardUid: dto.DashboardUid,
OrgId: dto.OrgId,
PublicDashboardConfig: *dto.PublicDashboardConfig,
}
// Eventually we want this to propagate to array of public dashboards
cmd.PublicDashboardConfig.PublicDashboard.OrgId = dto.OrgId
cmd.PublicDashboardConfig.PublicDashboard.DashboardUid = dto.DashboardUid
pdc, err := dr.dashboardStore.SavePublicDashboardConfig(cmd)
if err != nil {
return nil, err
}
return pdc, nil
}
func (dr *DashboardServiceImpl) BuildPublicDashboardMetricRequest(ctx context.Context, publicDashboardUid string, panelId int64) (dtos.MetricRequest, error) {
publicDashboardConfig, dashboard, err := dr.dashboardStore.GetPublicDashboard(publicDashboardUid)
if err != nil {
return dtos.MetricRequest{}, err
}
if !dashboard.IsPublic {
return dtos.MetricRequest{}, models.ErrPublicDashboardNotFound
}
var timeSettings struct {
From string `json:"from"`
To string `json:"to"`
}
err = json.Unmarshal([]byte(publicDashboardConfig.TimeSettings), &timeSettings)
if err != nil {
return dtos.MetricRequest{}, err
}
queriesByPanel := models.GetQueriesFromDashboard(dashboard.Data)
if _, ok := queriesByPanel[panelId]; !ok {
return dtos.MetricRequest{}, models.ErrPublicDashboardPanelNotFound
}
return dtos.MetricRequest{
From: timeSettings.From,
To: timeSettings.To,
Queries: queriesByPanel[panelId],
}, nil
}