Previews: return an empty array, rather than a null, if a dashboard has no datasources (#52961)

This commit is contained in:
Artur Wierzbicki 2022-07-28 22:49:06 +04:00 committed by GitHub
parent d0aa688e36
commit 273c37c27d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 148 additions and 1 deletions

View File

@ -60,7 +60,7 @@ func getDatasourceUIDs(resp *backend.DataResponse, uid string) ([]string, error)
}
func filterOutGrafanaDs(uids []string) []string {
var filtered []string
filtered := make([]string, 0)
for _, uid := range uids {
if uid != grafanads.DatasourceUID {
filtered = append(filtered, uid)

View File

@ -18,6 +18,11 @@ var (
exampleListFrameJSON string
exampleListFrame = &data.Frame{}
_ = exampleListFrame.UnmarshalJSON([]byte(exampleListFrameJSON))
//go:embed testdata/empty_search_response_frame.json
listFrameJSONWithNoDatasources string
listFrameWithNoDatasources = &data.Frame{}
_ = listFrameWithNoDatasources.UnmarshalJSON([]byte(listFrameJSONWithNoDatasources))
)
func TestShouldParseUidFromSearchResponseFrame(t *testing.T) {
@ -41,6 +46,28 @@ func TestShouldParseUidFromSearchResponseFrame(t *testing.T) {
require.Equal(t, []string{"datasource-2", "datasource-3", "datasource-4"}, uids)
}
func TestShouldReturnEmptyArrayIfThereAreNoDatasources(t *testing.T) {
searchService := &searchV2.MockSearchService{}
dsLookup := &dsUidsLookup{
searchService: searchService,
crawlerAuth: &crawlerAuth{},
features: featuremgmt.WithFeatures(featuremgmt.FlagPanelTitleSearch),
}
dashboardUid := "abc"
searchService.On("IsDisabled").Return(false)
searchService.On("DoDashboardQuery", mock.Anything, mock.Anything, mock.Anything, searchV2.DashboardQuery{
UIDs: []string{dashboardUid},
}).Return(&backend.DataResponse{
Frames: []*data.Frame{listFrameWithNoDatasources},
})
uids, err := dsLookup.getDatasourceUidsForDashboard(context.Background(), dashboardUid, 1)
require.NoError(t, err)
require.Equal(t, []string{}, uids)
require.NotNil(t, uids)
}
func TestShouldReturnNullIfSearchServiceIsDisabled(t *testing.T) {
searchService := &searchV2.MockSearchService{}
dsLookup := &dsUidsLookup{

View File

@ -0,0 +1,120 @@
{
"schema": {
"name": "Query results",
"refId": "Search",
"meta": {
"type": "search-results",
"custom": {
"count": 106,
"locationInfo": {
"yboVMzb7z": {
"name": "gdev dashboards",
"kind": "folder",
"url": "/dashboards/f/yboVMzb7z/gdev-dashboards"
}
},
"sortBy": "name_sort"
}
},
"fields": [
{
"name": "kind",
"type": "string",
"typeInfo": {
"frame": "string"
}
},
{
"name": "uid",
"type": "string",
"typeInfo": {
"frame": "string"
}
},
{
"name": "name",
"type": "string",
"typeInfo": {
"frame": "string"
}
},
{
"name": "panel_type",
"type": "string",
"typeInfo": {
"frame": "string"
}
},
{
"name": "url",
"type": "string",
"typeInfo": {
"frame": "string"
},
"config": {
"links": [
{
"title": "link",
"url": "${__value.text}"
}
]
}
},
{
"name": "tags",
"type": "other",
"typeInfo": {
"frame": "json.RawMessage",
"nullable": true
}
},
{
"name": "ds_uid",
"type": "other",
"typeInfo": {
"frame": "json.RawMessage",
"nullable": false
}
},
{
"name": "location",
"type": "string",
"typeInfo": {
"frame": "string"
}
}
]
},
"data": {
"values": [
[
"dashboard"
],
[
"ujaM1h6nz"
],
[
"abc2"
],
[
""
],
[
"/dashboards/f/ujaM1h6nz/abc2"
],
[
[
"gdev"
]
],
[
[
"grafana"
]
],
[
""
]
]
}
}