Skip to content

Commit

Permalink
Rule find post (elastic#148836)
Browse files Browse the repository at this point in the history
## Summary

FIX -> elastic#148287


### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
XavierM authored and jennypavlova committed Jan 13, 2023
1 parent 58fcae7 commit dfee7e9
Show file tree
Hide file tree
Showing 10 changed files with 906 additions and 79 deletions.
25 changes: 25 additions & 0 deletions x-pack/plugins/alerting/server/routes/aggregate_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,29 @@ export const aggregateRulesRoute = (
})
)
);
router.post(
{
path: `${INTERNAL_BASE_ALERTING_API_PATH}/rules/_aggregate`,
validate: {
body: querySchema,
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = (await context.alerting).getRulesClient();
const options = rewriteQueryReq({
...req.body,
has_reference: req.body.has_reference || undefined,
});
trackLegacyTerminology(
[req.body.search, req.body.search_fields].filter(Boolean) as string[],
usageCounter
);
const aggregateResult = await rulesClient.aggregate({ options });
return res.ok({
body: rewriteBodyRes(aggregateResult),
});
})
)
);
};
45 changes: 45 additions & 0 deletions x-pack/plugins/alerting/server/routes/find_rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,51 @@ const buildFindRulesRoute = ({
})
)
);
if (path === `${INTERNAL_BASE_ALERTING_API_PATH}/rules/_find`) {
router.post(
{
path,
validate: {
body: querySchema,
},
},
router.handleLegacyErrors(
verifyAccessAndContext(licenseState, async function (context, req, res) {
const rulesClient = (await context.alerting).getRulesClient();

trackLegacyTerminology(
[req.body.search, req.body.search_fields, req.body.sort_field].filter(
Boolean
) as string[],
usageCounter
);

const options = rewriteQueryReq({
...req.body,
has_reference: req.body.has_reference || undefined,
search_fields: searchFieldsAsArray(req.body.search_fields),
});

if (req.body.fields) {
usageCounter?.incrementCounter({
counterName: `alertingFieldsUsage`,
counterType: 'alertingFieldsUsage',
incrementBy: 1,
});
}

const findResult = await rulesClient.find({
options,
excludeFromPublicApi,
includeSnoozeData: true,
});
return res.ok({
body: rewriteBodyRes(findResult),
});
})
)
);
}
};

export const findRulesRoute = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);
http.post.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({ http });
expect(result).toEqual({
Expand All @@ -35,16 +35,11 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
});
expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": undefined,
"search": undefined,
"search_fields": undefined,
},
"body": "{\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -60,7 +55,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);
http.post.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({ http, searchText: 'apples' });
expect(result).toEqual({
Expand All @@ -72,16 +67,11 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
});
expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": undefined,
"search": "apples",
"search_fields": "[\\"name\\",\\"tags\\"]",
},
"body": "{\\"search_fields\\":\\"[\\\\\\"name\\\\\\",\\\\\\"tags\\\\\\"]\\",\\"search\\":\\"apples\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -97,7 +87,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);
http.post.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({
http,
Expand All @@ -113,16 +103,11 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
});
expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "(alert.attributes.actions:{ actionTypeId:action } OR alert.attributes.actions:{ actionTypeId:type })",
"search": "foo",
"search_fields": "[\\"name\\",\\"tags\\"]",
},
"body": "{\\"search_fields\\":\\"[\\\\\\"name\\\\\\",\\\\\\"tags\\\\\\"]\\",\\"search\\":\\"foo\\",\\"filter\\":\\"(alert.attributes.actions:{ actionTypeId:action } OR alert.attributes.actions:{ actionTypeId:type })\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -138,7 +123,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);
http.post.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({
http,
Expand All @@ -153,16 +138,11 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
});
expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.alertTypeId:(foo or bar)",
"search": undefined,
"search_fields": undefined,
},
"body": "{\\"filter\\":\\"alert.attributes.alertTypeId:(foo or bar)\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -178,7 +158,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);
http.post.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({
http,
Expand All @@ -195,16 +175,11 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
});
expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.alertTypeId:(foo or bar) and (alert.attributes.actions:{ actionTypeId:action } OR alert.attributes.actions:{ actionTypeId:type })",
"search": "baz",
"search_fields": "[\\"name\\",\\"tags\\"]",
},
"body": "{\\"search_fields\\":\\"[\\\\\\"name\\\\\\",\\\\\\"tags\\\\\\"]\\",\\"search\\":\\"baz\\",\\"filter\\":\\"alert.attributes.alertTypeId:(foo or bar) and (alert.attributes.actions:{ actionTypeId:action } OR alert.attributes.actions:{ actionTypeId:type })\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -220,7 +195,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValue(resolvedValue);
http.post.mockResolvedValue(resolvedValue);

let result = await loadRuleAggregations({
http,
Expand All @@ -237,16 +212,11 @@ describe('loadRuleAggregations', () => {
},
});

expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.enabled: true",
"search": undefined,
"search_fields": undefined,
},
"body": "{\\"filter\\":\\"alert.attributes.enabled: true\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -256,16 +226,11 @@ describe('loadRuleAggregations', () => {
ruleStatusesFilter: ['enabled', 'snoozed'],
});

expect(http.get.mock.calls[1]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[1]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.enabled: true and (alert.attributes.muteAll:true OR alert.attributes.snoozeSchedule: { duration > 0 })",
"search": undefined,
"search_fields": undefined,
},
"body": "{\\"filter\\":\\"alert.attributes.enabled: true and (alert.attributes.muteAll:true OR alert.attributes.snoozeSchedule: { duration > 0 })\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -275,16 +240,11 @@ describe('loadRuleAggregations', () => {
ruleStatusesFilter: ['enabled', 'disabled', 'snoozed'],
});

expect(http.get.mock.calls[1]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[1]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.enabled: true and (alert.attributes.muteAll:true OR alert.attributes.snoozeSchedule: { duration > 0 })",
"search": undefined,
"search_fields": undefined,
},
"body": "{\\"filter\\":\\"alert.attributes.enabled: true and (alert.attributes.muteAll:true OR alert.attributes.snoozeSchedule: { duration > 0 })\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand All @@ -300,7 +260,7 @@ describe('loadRuleAggregations', () => {
unknown: 0,
},
};
http.get.mockResolvedValueOnce(resolvedValue);
http.post.mockResolvedValueOnce(resolvedValue);

const result = await loadRuleAggregations({
http,
Expand All @@ -318,16 +278,11 @@ describe('loadRuleAggregations', () => {
},
});

expect(http.get.mock.calls[0]).toMatchInlineSnapshot(`
expect(http.post.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"/internal/alerting/rules/_aggregate",
Object {
"query": Object {
"default_search_operator": "AND",
"filter": "alert.attributes.tags:(a or b or c)",
"search": "baz",
"search_fields": "[\\"name\\",\\"tags\\"]",
},
"body": "{\\"search_fields\\":\\"[\\\\\\"name\\\\\\",\\\\\\"tags\\\\\\"]\\",\\"search\\":\\"baz\\",\\"filter\\":\\"alert.attributes.tags:(a or b or c)\\",\\"default_search_operator\\":\\"AND\\"}",
},
]
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ export async function loadRuleAggregations({
ruleStatusesFilter,
tagsFilter,
});
const res = await http.get<AsApiContract<RuleAggregations>>(
const res = await http.post<AsApiContract<RuleAggregations>>(
`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_aggregate`,
{
query: {
body: JSON.stringify({
search_fields: searchText ? JSON.stringify(['name', 'tags']) : undefined,
search: searchText,
filter: filters.length ? filters.join(' and ') : undefined,
default_search_operator: 'AND',
},
}),
}
);
return rewriteBodyRes(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export async function loadRuleAggregationsWithKueryFilter({
searchText,
});

const res = await http.get<AsApiContract<RuleAggregations>>(
const res = await http.post<AsApiContract<RuleAggregations>>(
`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_aggregate`,
{
query: {
body: JSON.stringify({
...(filtersKueryNode ? { filter: JSON.stringify(filtersKueryNode) } : {}),
default_search_operator: 'AND',
},
}),
}
);
return rewriteBodyRes(res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,23 @@ export async function loadRulesWithKueryFilter({
searchText,
});

const res = await http.get<
const res = await http.post<
AsApiContract<{
page: number;
perPage: number;
total: number;
data: Array<AsApiContract<Rule>>;
}>
>(`${INTERNAL_BASE_ALERTING_API_PATH}/rules/_find`, {
query: {
body: JSON.stringify({
page: page.index + 1,
per_page: page.size,
...(filtersKueryNode ? { filter: JSON.stringify(filtersKueryNode) } : {}),
sort_field: sort.field,
sort_order: sort.direction,
},
}),
});

return {
page: res.page,
perPage: res.per_page,
Expand Down
Loading

0 comments on commit dfee7e9

Please sign in to comment.