From 3f5acf346d27fb9468f850d1e7b91baaf7af36ca Mon Sep 17 00:00:00 2001 From: Ezequiel Victorero Date: Wed, 15 Mar 2023 12:44:17 -0300 Subject: [PATCH] PublicDashboards: Query collapsed panels inside rows (#64779) --- .../publicdashboards/service/query.go | 18 +++- .../publicdashboards/service/query_test.go | 91 +++++++++++++++++++ 2 files changed, 105 insertions(+), 4 deletions(-) diff --git a/pkg/services/publicdashboards/service/query.go b/pkg/services/publicdashboards/service/query.go index 2831090b93f..3445061cd39 100644 --- a/pkg/services/publicdashboards/service/query.go +++ b/pkg/services/publicdashboards/service/query.go @@ -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 { diff --git a/pkg/services/publicdashboards/service/query_test.go b/pkg/services/publicdashboards/service/query_test.go index c7195039d9c..d0d68a520c7 100644 --- a/pkg/services/publicdashboards/service/query_test.go +++ b/pkg/services/publicdashboards/service/query_test.go @@ -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) {