From 62e0b45067b7f42cc6186bd6ee60564d8daa7802 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Wed, 5 Apr 2023 08:54:28 -0500 Subject: [PATCH] [v9.5.x] PublicDashboards: fix collapsed rows queries (#66030) PublicDashboards: fix collapsed rows queries (#66014) (cherry picked from commit 232834f455ea7390efb9c75a98f97cd0ba95788f) Co-authored-by: Ezequiel Victorero --- .../publicdashboards/service/query.go | 22 +- .../publicdashboards/service/query_test.go | 222 ++++++++++++++++++ 2 files changed, 243 insertions(+), 1 deletion(-) diff --git a/pkg/services/publicdashboards/service/query.go b/pkg/services/publicdashboards/service/query.go index 35180debcfb..22c6fd4f96c 100644 --- a/pkg/services/publicdashboards/service/query.go +++ b/pkg/services/publicdashboards/service/query.go @@ -213,7 +213,10 @@ func getUniqueDashboardDatasourceUids(dashboard *simplejson.Json) []string { var datasourceUids []string exists := map[string]bool{} - for _, panelObj := range dashboard.Get("panels").MustArray() { + // collapsed rows contain panels in a nested structure, so we need to flatten them before calculate unique uids + flattenedPanels := getFlattenedPanels(dashboard) + + for _, panelObj := range flattenedPanels { panel := simplejson.NewFromAny(panelObj) uid := getDataSourceUidFromJson(panel) @@ -238,6 +241,23 @@ func getUniqueDashboardDatasourceUids(dashboard *simplejson.Json) []string { return datasourceUids } +func getFlattenedPanels(dashboard *simplejson.Json) []interface{} { + var flatPanels []interface{} + for _, panelObj := range dashboard.Get("panels").MustArray() { + panel := simplejson.NewFromAny(panelObj) + // if the panel is a row and it is collapsed, get the queries from the panels inside the row + // if it is not collapsed, the row does not have any panels + if panel.Get("type").MustString() == "row" { + if panel.Get("collapsed").MustBool() { + flatPanels = append(flatPanels, panel.Get("panels").MustArray()...) + } + } else { + flatPanels = append(flatPanels, panelObj) + } + } + return flatPanels +} + func groupQueriesByPanelId(dashboard *simplejson.Json) map[int64][]*simplejson.Json { result := make(map[int64][]*simplejson.Json) diff --git a/pkg/services/publicdashboards/service/query_test.go b/pkg/services/publicdashboards/service/query_test.go index 1d8ccf2e8bd..2dbd20ec3a8 100644 --- a/pkg/services/publicdashboards/service/query_test.go +++ b/pkg/services/publicdashboards/service/query_test.go @@ -432,6 +432,218 @@ const ( ], "schemaVersion": 35 }` + + dashboardWithCollapsedRows = ` +{ +"panels": [ + { + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 0 + }, + "id": 12, + "title": "Row title", + "type": "row" + }, + { + "datasource": { + "type": "prometheus", + "uid": "qCbTUC37k" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 8, + "w": 12, + "x": 0, + "y": 1 + }, + "id": 11, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "single", + "sort": "none" + } + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "qCbTUC37k" + }, + "editorMode": "builder", + "expr": "access_evaluation_duration_bucket", + "legendFormat": "__auto", + "range": true, + "refId": "A" + } + ], + "title": "Panel Title", + "type": "timeseries" + }, + { + "collapsed": true, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 9 + }, + "id": 10, + "panels": [ + { + "datasource": { + "type": "influxdb", + "uid": "P49A45DF074423DFB" + }, + "fieldConfig": { + "defaults": { + "color": { + "mode": "palette-classic" + }, + "custom": { + "axisCenteredZero": false, + "axisColorMode": "text", + "axisLabel": "", + "axisPlacement": "auto", + "barAlignment": 0, + "drawStyle": "line", + "fillOpacity": 0, + "gradientMode": "none", + "hideFrom": { + "legend": false, + "tooltip": false, + "viz": false + }, + "lineInterpolation": "linear", + "lineWidth": 1, + "pointSize": 5, + "scaleDistribution": { + "type": "linear" + }, + "showPoints": "auto", + "spanNulls": false, + "stacking": { + "group": "A", + "mode": "none" + }, + "thresholdsStyle": { + "mode": "off" + } + }, + "mappings": [], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green" + }, + { + "color": "red", + "value": 80 + } + ] + } + }, + "overrides": [] + }, + "gridPos": { + "h": 9, + "w": 12, + "x": 0, + "y": 10 + }, + "id": 8, + "options": { + "legend": { + "calcs": [], + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "single", + "sort": "none" + } + }, + "pluginVersion": "9.4.0-pre", + "targets": [ + { + "datasource": { + "type": "influxdb", + "uid": "P49A45DF074423DFB" + }, + "query": "// v.bucket, v.timeRangeStart, and v.timeRange stop are all variables supported by the flux plugin and influxdb\nfrom(bucket: v.bucket)\n |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\n |> filter(fn: (r) => r[\"_value\"] >= 10 and r[\"_value\"] <= 20)", + "refId": "A" + } + ], + "title": "Panel Title", + "type": "timeseries" + } + ], + "title": "Row title 1", + "type": "row" + } + ] +}` ) func TestGetQueryDataResponse(t *testing.T) { @@ -951,6 +1163,16 @@ func TestGetUniqueDashboardDatasourceUids(t *testing.T) { uids := getUniqueDashboardDatasourceUids(json) require.Len(t, uids, 0) }) + + t.Run("can get unique datasource ids from dashboard with rows", func(t *testing.T) { + json, err := simplejson.NewJson([]byte(dashboardWithCollapsedRows)) + require.NoError(t, err) + + uids := getUniqueDashboardDatasourceUids(json) + require.Len(t, uids, 2) + require.Equal(t, "qCbTUC37k", uids[0]) + require.Equal(t, "P49A45DF074423DFB", uids[1]) + }) } func TestBuildMetricRequest(t *testing.T) {