mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Owensmallwood/pubdash panel blank when target has no datasource (#52115)
Fixes public dashboards bug. When panel targets have no datasource, the datasource on the panel can be a json object or a string and will get added to the targets for pubdash.
This commit is contained in:
parent
554ebd647b
commit
894e519406
@ -10,7 +10,7 @@ func GetUniqueDashboardDatasourceUids(dashboard *simplejson.Json) []string {
|
|||||||
|
|
||||||
for _, panelObj := range dashboard.Get("panels").MustArray() {
|
for _, panelObj := range dashboard.Get("panels").MustArray() {
|
||||||
panel := simplejson.NewFromAny(panelObj)
|
panel := simplejson.NewFromAny(panelObj)
|
||||||
uid := panel.Get("datasource").Get("uid").MustString()
|
uid := GetDataSourceUidFromJson(panel)
|
||||||
|
|
||||||
// if uid is for a mixed datasource, get the datasource uids from the targets
|
// if uid is for a mixed datasource, get the datasource uids from the targets
|
||||||
if uid == "-- Mixed --" {
|
if uid == "-- Mixed --" {
|
||||||
@ -44,8 +44,11 @@ func GroupQueriesByPanelId(dashboard *simplejson.Json) map[int64][]*simplejson.J
|
|||||||
for _, queryObj := range panel.Get("targets").MustArray() {
|
for _, queryObj := range panel.Get("targets").MustArray() {
|
||||||
query := simplejson.NewFromAny(queryObj)
|
query := simplejson.NewFromAny(queryObj)
|
||||||
|
|
||||||
|
// if query target has no datasource, set it to have the datasource on the panel
|
||||||
if _, ok := query.CheckGet("datasource"); !ok {
|
if _, ok := query.CheckGet("datasource"); !ok {
|
||||||
query.Set("datasource", panel.Get("datasource"))
|
uid := GetDataSourceUidFromJson(panel)
|
||||||
|
datasource := map[string]interface{}{"type": "public-ds", "uid": uid}
|
||||||
|
query.Set("datasource", datasource)
|
||||||
}
|
}
|
||||||
|
|
||||||
panelQueries = append(panelQueries, query)
|
panelQueries = append(panelQueries, query)
|
||||||
@ -61,13 +64,8 @@ func GroupQueriesByDataSource(queries []*simplejson.Json) (result [][]*simplejso
|
|||||||
byDataSource := make(map[string][]*simplejson.Json)
|
byDataSource := make(map[string][]*simplejson.Json)
|
||||||
|
|
||||||
for _, query := range queries {
|
for _, query := range queries {
|
||||||
dataSourceUid, err := query.GetPath("datasource", "uid").String()
|
uid := GetDataSourceUidFromJson(query)
|
||||||
|
byDataSource[uid] = append(byDataSource[uid], query)
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
byDataSource[dataSourceUid] = append(byDataSource[dataSourceUid], query)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, queries := range byDataSource {
|
for _, queries := range byDataSource {
|
||||||
@ -76,3 +74,14 @@ func GroupQueriesByDataSource(queries []*simplejson.Json) (result [][]*simplejso
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetDataSourceUidFromJson(query *simplejson.Json) string {
|
||||||
|
uid := query.Get("datasource").Get("uid").MustString()
|
||||||
|
|
||||||
|
// before 8.3 special types could be sent as datasource (expr)
|
||||||
|
if uid == "" {
|
||||||
|
uid = query.Get("datasource").MustString()
|
||||||
|
}
|
||||||
|
|
||||||
|
return uid
|
||||||
|
}
|
||||||
|
@ -20,6 +20,37 @@ const (
|
|||||||
"schemaVersion": 35
|
"schemaVersion": 35
|
||||||
}`
|
}`
|
||||||
|
|
||||||
|
dashboardWithTargetsWithNoDatasources = `
|
||||||
|
{
|
||||||
|
"panels": [
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"datasource": {
|
||||||
|
"type": "postgres",
|
||||||
|
"uid": "abc123"
|
||||||
|
},
|
||||||
|
"targets": [
|
||||||
|
{
|
||||||
|
"expr": "go_goroutines{job=\"$job\"}",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "",
|
||||||
|
"refId": "A"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"exemplar": true,
|
||||||
|
"expr": "query2",
|
||||||
|
"interval": "",
|
||||||
|
"legendFormat": "",
|
||||||
|
"refId": "B"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"title": "Panel Title",
|
||||||
|
"type": "timeseries"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"schemaVersion": 35
|
||||||
|
}`
|
||||||
|
|
||||||
dashboardWithQueries = `
|
dashboardWithQueries = `
|
||||||
{
|
{
|
||||||
"panels": [
|
"panels": [
|
||||||
@ -256,6 +287,24 @@ func TestGetUniqueDashboardDatasourceUids(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestGroupQueriesByPanelId(t *testing.T) {
|
func TestGroupQueriesByPanelId(t *testing.T) {
|
||||||
|
t.Run("can extract queries from dashboard with panel datasource string that has no datasource on panel targets", func(t *testing.T) {
|
||||||
|
json, err := simplejson.NewJson([]byte(oldStyleDashboard))
|
||||||
|
require.NoError(t, err)
|
||||||
|
queries := GroupQueriesByPanelId(json)
|
||||||
|
|
||||||
|
panelId := int64(2)
|
||||||
|
queriesByDatasource := GroupQueriesByDataSource(queries[panelId])
|
||||||
|
require.Len(t, queriesByDatasource[0], 1)
|
||||||
|
})
|
||||||
|
t.Run("can extract queries from dashboard with panel json datasource that has no datasource on panel targets", func(t *testing.T) {
|
||||||
|
json, err := simplejson.NewJson([]byte(dashboardWithTargetsWithNoDatasources))
|
||||||
|
require.NoError(t, err)
|
||||||
|
queries := GroupQueriesByPanelId(json)
|
||||||
|
|
||||||
|
panelId := int64(2)
|
||||||
|
queriesByDatasource := GroupQueriesByDataSource(queries[panelId])
|
||||||
|
require.Len(t, queriesByDatasource[0], 2)
|
||||||
|
})
|
||||||
t.Run("can extract no queries from empty dashboard", func(t *testing.T) {
|
t.Run("can extract no queries from empty dashboard", func(t *testing.T) {
|
||||||
json, err := simplejson.NewJson([]byte(`{"panels": {}}`))
|
json, err := simplejson.NewJson([]byte(`{"panels": {}}`))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -321,7 +370,10 @@ func TestGroupQueriesByPanelId(t *testing.T) {
|
|||||||
query, err := queries[2][0].MarshalJSON()
|
query, err := queries[2][0].MarshalJSON()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.JSONEq(t, `{
|
require.JSONEq(t, `{
|
||||||
"datasource": "_yxMP8Ynk",
|
"datasource": {
|
||||||
|
"uid": "_yxMP8Ynk",
|
||||||
|
"type": "public-ds"
|
||||||
|
},
|
||||||
"exemplar": true,
|
"exemplar": true,
|
||||||
"expr": "go_goroutines{job=\"$job\"}",
|
"expr": "go_goroutines{job=\"$job\"}",
|
||||||
"interval": "",
|
"interval": "",
|
||||||
|
Loading…
Reference in New Issue
Block a user