Library Panels: Modify connection api endpoint to be compatible with unified storage (#107088)
This commit is contained in:
@@ -229,6 +229,12 @@ func (c *DashboardSearchClient) Search(ctx context.Context, req *resourcepb.Reso
|
||||
return nil, fmt.Errorf("only one repo name is supported")
|
||||
}
|
||||
query.ManagerIdentity = vals[0]
|
||||
case unisearch.DASHBOARD_LIBRARY_PANEL_REFERENCE:
|
||||
if len(vals) != 1 {
|
||||
return nil, fmt.Errorf("only one library panel uid is supported")
|
||||
}
|
||||
|
||||
return c.getLibraryPanelConnections(ctx, user, vals[0], req.Options.Key.Namespace)
|
||||
case resource.SEARCH_FIELD_TITLE_PHRASE:
|
||||
if len(vals) != 1 {
|
||||
return nil, fmt.Errorf("only one title supported")
|
||||
@@ -356,6 +362,34 @@ func getResourceKey(item *dashboards.DashboardSearchProjection, namespace string
|
||||
}
|
||||
}
|
||||
|
||||
// retrieves all the dashboards that are connected to the given library panel
|
||||
func (c *DashboardSearchClient) getLibraryPanelConnections(ctx context.Context, user identity.Requester, libraryElementUID, namespace string) (*resourcepb.ResourceSearchResponse, error) {
|
||||
connections, err := c.dashboardStore.GetDashboardsByLibraryPanelUID(ctx, libraryElementUID, user.GetOrgID())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
columns := c.getColumns("", &dashboards.FindPersistedDashboardsQuery{})
|
||||
list := &resourcepb.ResourceSearchResponse{
|
||||
Results: &resourcepb.ResourceTable{
|
||||
Columns: columns,
|
||||
},
|
||||
}
|
||||
|
||||
for _, dashboard := range connections {
|
||||
cells := c.createCommonCells("", dashboard.FolderUID, dashboard.ID, nil) // nolint:staticcheck
|
||||
list.Results.Rows = append(list.Results.Rows, &resourcepb.ResourceTableRow{
|
||||
Key: getResourceKey(&dashboards.DashboardSearchProjection{
|
||||
UID: dashboard.UID,
|
||||
}, namespace),
|
||||
Cells: cells,
|
||||
})
|
||||
}
|
||||
|
||||
list.TotalHits = int64(len(list.Results.Rows))
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (c *DashboardSearchClient) GetStats(ctx context.Context, req *resourcepb.ResourceStatsRequest, _ ...grpc.CallOption) (*resourcepb.ResourceStatsResponse, error) {
|
||||
info, err := claims.ParseNamespace(req.Namespace)
|
||||
if err != nil {
|
||||
|
||||
@@ -572,6 +572,72 @@ func TestDashboardSearchClient_Search(t *testing.T) {
|
||||
require.Equal(t, []byte(strconv.FormatInt(100, 10)), resp.Results.Rows[0].Cells[i]) // views should be set to 100
|
||||
mockStore.AssertExpectations(t)
|
||||
})
|
||||
|
||||
t.Run("Should search dashboards based on what is connected to a library panel", func(t *testing.T) {
|
||||
mockStore.On("GetDashboardsByLibraryPanelUID", mock.Anything, "test-library-panel", int64(2)).Return([]*dashboards.DashboardRef{
|
||||
{UID: "dashboard1", FolderUID: "folder1", ID: 1},
|
||||
{UID: "dashboard2", FolderUID: "folder2", ID: 2},
|
||||
}, nil).Once()
|
||||
|
||||
req := &resourcepb.ResourceSearchRequest{
|
||||
Options: &resourcepb.ListOptions{
|
||||
Key: dashboardKey,
|
||||
Fields: []*resourcepb.Requirement{
|
||||
{
|
||||
Key: unisearch.DASHBOARD_LIBRARY_PANEL_REFERENCE,
|
||||
Operator: "=",
|
||||
Values: []string{"test-library-panel"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := client.Search(ctx, req)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, resp)
|
||||
require.Equal(t, int64(2), resp.TotalHits)
|
||||
require.Len(t, resp.Results.Rows, 2)
|
||||
|
||||
dashboardNames := make([]string, len(resp.Results.Rows))
|
||||
dashboardFolders := make([]string, len(resp.Results.Rows))
|
||||
dashboardIDs := make([]string, len(resp.Results.Rows))
|
||||
|
||||
for i, row := range resp.Results.Rows {
|
||||
dashboardNames[i] = row.Key.Name
|
||||
dashboardFolders[i] = string(row.Cells[1])
|
||||
dashboardIDs[i] = string(row.Cells[3])
|
||||
}
|
||||
|
||||
require.Contains(t, dashboardNames, "dashboard1")
|
||||
require.Contains(t, dashboardNames, "dashboard2")
|
||||
require.Contains(t, dashboardFolders, "folder1")
|
||||
require.Contains(t, dashboardFolders, "folder2")
|
||||
require.Contains(t, dashboardIDs, "1")
|
||||
require.Contains(t, dashboardIDs, "2")
|
||||
|
||||
mockStore.AssertExpectations(t)
|
||||
for _, row := range resp.Results.Rows {
|
||||
require.Equal(t, len(row.Cells), len(resp.Results.Columns))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Only one library panel uid is supported", func(t *testing.T) {
|
||||
req := &resourcepb.ResourceSearchRequest{
|
||||
Options: &resourcepb.ListOptions{
|
||||
Key: dashboardKey,
|
||||
Fields: []*resourcepb.Requirement{
|
||||
{
|
||||
Key: unisearch.DASHBOARD_LIBRARY_PANEL_REFERENCE,
|
||||
Operator: "=",
|
||||
Values: []string{"panel1", "panel2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
resp, err := client.Search(ctx, req)
|
||||
require.Error(t, err)
|
||||
require.Contains(t, err.Error(), "only one library panel uid is supported")
|
||||
require.Nil(t, resp)
|
||||
})
|
||||
}
|
||||
|
||||
func TestParseSortName(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user