Fix: Fix dashboards not showing in folders with search v2 enabled (#69638)

* Fix: Fix dashboards not showing in folders with search v2 enabled

* tests
This commit is contained in:
Josh Hunt 2023-06-06 17:26:06 +01:00 committed by GitHub
parent 7a132680ef
commit 52c4761218
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 2 deletions

View File

@ -0,0 +1,49 @@
import { DataFrame, DataFrameView, FieldType } from '@grafana/data';
import { DashboardQueryResult, getGrafanaSearcher, QueryResponse } from 'app/features/search/service';
import { listDashboards } from './services';
describe('browse-dashboards services', () => {
describe('listDashboards', () => {
const searchData: DataFrame = {
fields: [
{ name: 'kind', type: FieldType.string, config: {}, values: [] },
{ name: 'name', type: FieldType.string, config: {}, values: [] },
{ name: 'uid', type: FieldType.string, config: {}, values: [] },
{ name: 'url', type: FieldType.string, config: {}, values: [] },
{ name: 'tags', type: FieldType.other, config: {}, values: [] },
{ name: 'location', type: FieldType.string, config: {}, values: [] },
],
length: 0,
};
const mockSearchResult: QueryResponse = {
isItemLoaded: jest.fn(),
loadMoreItems: jest.fn(),
totalRows: searchData.length,
view: new DataFrameView<DashboardQueryResult>(searchData),
};
const searchMock = jest.spyOn(getGrafanaSearcher(), 'search');
searchMock.mockResolvedValue(mockSearchResult);
const PAGE_SIZE = 50;
it.each([
{ page: undefined, expectedFrom: 0 },
{ page: 1, expectedFrom: 0 },
{ page: 2, expectedFrom: 50 },
{ page: 4, expectedFrom: 150 },
])('skips first $expectedFrom when listing page $page', async ({ page, expectedFrom }) => {
await listDashboards('abc-123', page, PAGE_SIZE);
expect(searchMock).toHaveBeenCalledWith({
kind: ['dashboard'],
query: '*',
location: 'abc-123',
from: expectedFrom,
limit: PAGE_SIZE,
});
});
});
});

View File

@ -38,7 +38,7 @@ export async function listDashboards(parentUID?: string, page = 1, pageSize = PA
kind: ['dashboard'], kind: ['dashboard'],
query: '*', query: '*',
location: parentUID || 'general', location: parentUID || 'general',
from: page * pageSize, from: (page - 1) * pageSize, // our pages are 1-indexed, so we need to -1 to convert that to correct value to skip
limit: pageSize, limit: pageSize,
}); });

View File

@ -118,4 +118,31 @@ describe('SQLSearcher', () => {
starred: true, starred: true,
}); });
}); });
describe('pagination', () => {
it.each([
{ from: undefined, expectedPage: undefined },
{ from: 0, expectedPage: 1 },
{ from: 50, expectedPage: 2 },
{ from: 150, expectedPage: 4 },
])('should search page $expectedPage when skipping $from results', async ({ from, expectedPage }) => {
searchMock.mockResolvedValue([]);
const sqlSearcher = new SQLSearcher();
await sqlSearcher.search({
query: '*',
kind: ['dashboard'],
from,
limit: 50,
});
expect(searchMock).toHaveBeenLastCalledWith('/api/search', {
limit: 50,
page: expectedPage,
sort: undefined,
tag: undefined,
type: 'dash-db',
});
});
});
}); });

View File

@ -81,7 +81,11 @@ export class SQLSearcher implements GrafanaSearcher {
} }
const limit = query.limit ?? (query.from !== undefined ? 1 : DEFAULT_MAX_VALUES); const limit = query.limit ?? (query.from !== undefined ? 1 : DEFAULT_MAX_VALUES);
const page = query.from !== undefined ? query.from / limit : undefined; const page =
query.from !== undefined
? // prettier-ignore
(query.from / limit) + 1 // pages are 1-indexed, so need to +1 to get there
: undefined;
const q = await this.composeQuery( const q = await this.composeQuery(
{ {