[ResponseOps] Add pagination and sorting to the alerts search strategy (#126813)

* Add pagination and sorting

* PR feedback

* PR feedback

* Fix merge issues

* Fix types and PR feedback
This commit is contained in:
Chris Roberson 2022-03-09 17:50:14 -05:00 committed by GitHub
parent 1b30d982a0
commit 4b6f754c34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 113 additions and 2 deletions

View file

@ -12,8 +12,15 @@ import { IEsSearchRequest, IEsSearchResponse } from 'src/plugins/data/common';
export type RuleRegistrySearchRequest = IEsSearchRequest & {
featureIds: ValidFeatureId[];
query?: { bool: estypes.QueryDslBoolQuery };
sort?: estypes.SortCombinations[];
pagination?: RuleRegistrySearchRequestPagination;
};
export interface RuleRegistrySearchRequestPagination {
pageIndex: number;
pageSize: number;
}
type Prev = [
never,
0,

View file

@ -306,7 +306,75 @@ describe('ruleRegistrySearchStrategyProvider()', () => {
await strategy
.search(request, options, deps as unknown as SearchStrategyDependencies)
.toPromise();
expect(data.search.searchAsInternalUser.search).not.toHaveBeenCalled();
expect(data.search.searchAsInternalUser.search as jest.Mock).not.toHaveBeenCalled();
expect(searchStrategySearch).toHaveBeenCalled();
});
it('should support pagination', async () => {
const request: RuleRegistrySearchRequest = {
featureIds: [AlertConsumers.LOGS],
pagination: {
pageSize: 10,
pageIndex: 0,
},
};
const options = {};
const deps = {
request: {},
};
const strategy = ruleRegistrySearchStrategyProvider(
data,
ruleDataService,
alerting,
logger,
security,
spaces
);
await strategy
.search(request, options, deps as unknown as SearchStrategyDependencies)
.toPromise();
expect((data.search.searchAsInternalUser.search as jest.Mock).mock.calls.length).toBe(1);
expect(
(data.search.searchAsInternalUser.search as jest.Mock).mock.calls[0][0].params.body.size
).toBe(10);
expect(
(data.search.searchAsInternalUser.search as jest.Mock).mock.calls[0][0].params.body.from
).toBe(0);
});
it('should support sorting', async () => {
const request: RuleRegistrySearchRequest = {
featureIds: [AlertConsumers.LOGS],
sort: [
{
test: {
order: 'desc',
},
},
],
};
const options = {};
const deps = {
request: {},
};
const strategy = ruleRegistrySearchStrategyProvider(
data,
ruleDataService,
alerting,
logger,
security,
spaces
);
await strategy
.search(request, options, deps as unknown as SearchStrategyDependencies)
.toPromise();
expect((data.search.searchAsInternalUser.search as jest.Mock).mock.calls.length).toBe(1);
expect(
(data.search.searchAsInternalUser.search as jest.Mock).mock.calls[0][0].params.body.sort
).toStrictEqual([{ test: { order: 'desc' } }]);
});
});

View file

@ -107,17 +107,22 @@ export const ruleRegistrySearchStrategyProvider = (
filter.push(getSpacesFilter(space.id) as estypes.QueryDslQueryContainer);
}
const sort = request.sort ?? [];
const query = {
bool: {
filter,
},
};
const size = request.pagination ? request.pagination.pageSize : MAX_ALERT_SEARCH_SIZE;
const params = {
index: indices,
body: {
_source: false,
fields: ['*'],
size: MAX_ALERT_SEARCH_SIZE,
sort,
size,
from: request.pagination ? request.pagination.pageIndex * size : 0,
query,
},
};

View file

@ -45,6 +45,7 @@ export default ({ getService }: FtrProviderContext) => {
after(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/observability/alerts');
});
it('should return alerts from log rules', async () => {
const result = await secureBsearch.send<RuleRegistrySearchResponse>({
supertestWithoutAuth,
@ -63,6 +64,36 @@ export default ({ getService }: FtrProviderContext) => {
});
expect(consumers.every((consumer) => consumer === AlertConsumers.LOGS));
});
it('should support pagination and sorting', async () => {
const result = await secureBsearch.send<RuleRegistrySearchResponse>({
supertestWithoutAuth,
auth: {
username: logsOnlySpacesAll.username,
password: logsOnlySpacesAll.password,
},
options: {
featureIds: [AlertConsumers.LOGS],
pagination: {
pageSize: 2,
pageIndex: 1,
},
sort: [
{
'kibana.alert.evaluation.value': {
order: 'desc',
},
},
],
},
strategy: 'ruleRegistryAlertsSearchStrategy',
});
expect(result.rawResponse.hits.total).to.eql(5);
expect(result.rawResponse.hits.hits.length).to.eql(2);
const first = result.rawResponse.hits.hits[0].fields?.['kibana.alert.evaluation.value'];
const second = result.rawResponse.hits.hits[1].fields?.['kibana.alert.evaluation.value'];
expect(first > second).to.be(true);
});
});
// TODO: need xavier's help here