PublicDashboards: Query collapsed panels inside rows (#64779)

This commit is contained in:
Ezequiel Victorero 2023-03-15 12:44:17 -03:00 committed by GitHub
parent 4f13e78d11
commit 3f5acf346d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 105 additions and 4 deletions

View File

@ -239,9 +239,22 @@ func getUniqueDashboardDatasourceUids(dashboard *simplejson.Json) []string {
func groupQueriesByPanelId(dashboard *simplejson.Json) map[int64][]*simplejson.Json {
result := make(map[int64][]*simplejson.Json)
for _, panelObj := range dashboard.Get("panels").MustArray() {
extractQueriesFromPanels(dashboard.Get("panels").MustArray(), result)
return result
}
func extractQueriesFromPanels(panels []interface{}, result map[int64][]*simplejson.Json) {
for _, panelObj := range panels {
panel := simplejson.NewFromAny(panelObj)
// if the panel is a row and it is collapsed, get the queries from the panels inside the row
if panel.Get("type").MustString() == "row" && panel.Get("collapsed").MustBool() {
// recursive call to get queries from panels inside a row
extractQueriesFromPanels(panel.Get("panels").MustArray(), result)
continue
}
var panelQueries []*simplejson.Json
for _, queryObj := range panel.Get("targets").MustArray() {
@ -257,15 +270,12 @@ func groupQueriesByPanelId(dashboard *simplejson.Json) map[int64][]*simplejson.J
datasource := map[string]interface{}{"type": "public-ds", "uid": uid}
query.Set("datasource", datasource)
}
panelQueries = append(panelQueries, query)
}
}
result[panel.Get("id").MustInt64()] = panelQueries
}
return result
}
func getDataSourceUidFromJson(query *simplejson.Json) string {

View File

@ -352,6 +352,85 @@ const (
],
"schemaVersion": 35
}`
dashboardWithRows = `
{
"panels": [
{
"id": 2,
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "_yxMP8Ynk"
},
"exemplar": true,
"expr": "go_goroutines{job=\"$job\"}",
"interval": "",
"legendFormat": "",
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "promds2"
},
"exemplar": true,
"expr": "query2",
"interval": "",
"legendFormat": "",
"refId": "B"
}
],
"title": "Panel Title",
"type": "timeseries"
},
{
"id": 3,
"collapsed": true,
"gridPos": {
"h": 1,
"w": 24,
"x": 0,
"y": 9
},
"title": "This panel is a Row",
"type": "row",
"panels": [
{
"id": 4,
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "_yxMP8Ynk"
},
"exemplar": true,
"expr": "go_goroutines{job=\"$job\"}",
"interval": "",
"legendFormat": "",
"refId": "A"
},
{
"datasource": {
"type": "prometheus",
"uid": "promds2"
},
"exemplar": true,
"expr": "query2",
"interval": "",
"legendFormat": "",
"refId": "B"
}
],
"title": "Panel inside a row",
"type": "timeseries"
}
]
}
],
"schemaVersion": 35
}`
)
func TestGetQueryDataResponse(t *testing.T) {
@ -1180,6 +1259,18 @@ func TestGroupQueriesByPanelId(t *testing.T) {
require.Len(t, queries, 0)
})
t.Run("queries inside panels inside rows are returned", func(t *testing.T) {
json, err := simplejson.NewJson([]byte(dashboardWithRows))
require.NoError(t, err)
queries := groupQueriesByPanelId(json)
for idx := range queries {
assert.NotNil(t, queries[idx])
}
assert.Len(t, queries, 2)
})
}
func TestGroupQueriesByDataSource(t *testing.T) {