Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make alert params sortable and filterable #87835

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/core/server/saved_objects/service/lib/filter_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,16 @@ export const hasFilterKeyError = (
) {
return `This key '${key}' does NOT match the filter proposition SavedObjectType.attributes.key`;
}
if (
(keySplit.length === 2 && !fieldDefined(indexMapping, keySplit[1])) ||
(keySplit.length > 2 &&
!fieldDefined(
indexMapping,
`${keySplit[0]}.${keySplit.slice(2, keySplit.length).join('.')}`
))
) {
return `This key '${key}' does NOT exist in ${types.join()} saved object index patterns`;
}
// if (
// (keySplit.length === 2 && !fieldDefined(indexMapping, keySplit[1])) ||
// (keySplit.length > 2 &&
// !fieldDefined(
// indexMapping,
// `${keySplit[0]}.${keySplit.slice(2, keySplit.length).join('.')}`
// ))
// ) {
// return `This key '${key}' does NOT exist in ${types.join()} saved object index patterns`;
// }
}
return null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ export function getSortingParams(
}

const [typeField] = types;
let key = `${typeField}.${sortField}`;
let field = getProperty(mappings, key);
if (!field) {
// type field does not exist, try checking the root properties
key = sortField;
field = getProperty(mappings, sortField);
if (!field) {
throw Boom.badRequest(`Unknown sort field ${sortField}`);
}
}
const key = `${typeField}.${sortField}`;
// let field = getProperty(mappings, key);
// if (!field) {
// // type field does not exist, try checking the root properties
// key = sortField;
// field = getProperty(mappings, sortField);
// if (!field) {
// throw Boom.badRequest(`Unknown sort field ${sortField}`);
// }
// }

return {
sort: [
{
[key]: {
order: sortOrder,
unmapped_type: field.type,
// unmapped_type: field.type,
},
},
],
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/alerts/server/saved_objects/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
}
},
"params": {
"enabled": false,
"type": "object"
"type": "flattened"
},
"scheduledTaskId": {
"type": "keyword"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,51 @@ export default function createFindTests({ getService }: FtrProviderContext) {
data: [],
});
});

it('should be able to sort on alert param values', async () => {
await Promise.all([
createAlert({ params: { strValue: 'b' } }),
createAlert({ params: { strValue: 'c' } }),
createAlert({ params: { strValue: 'a' } }),
]);

const response = await supertest.get(
`${getUrlPrefix(Spaces.space1.id)}/api/alerts/_find?sort_field=params.strValue`
);

expect(response.status).to.eql(200);
expect(response.body.total).to.equal(3);
expect(response.body.data[0].params.strValue).to.eql('a');
expect(response.body.data[1].params.strValue).to.eql('b');
expect(response.body.data[2].params.strValue).to.eql('c');
});

it('should be able to filter on alert param values', async () => {
await Promise.all([
createAlert({ params: { strValue: 'my a' } }),
createAlert({ params: { strValue: 'my b' } }),
createAlert({ params: { strValue: 'my c' } }),
]);

const response = await supertest.get(
`${getUrlPrefix(
Spaces.space1.id
)}/api/alerts/_find?filter=alert.attributes.params.strValue:"my b"`
);

expect(response.status).to.eql(200);
expect(response.body.total).to.equal(1);
expect(response.body.data[0].params.strValue).to.eql('my b');
});

async function createAlert(overwrites = {}) {
const { body: createdAlert } = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerts/alert`)
.set('kbn-xsrf', 'foo')
.send(getTestAlertData(overwrites))
.expect(200);
objectRemover.add(Spaces.space1.id, createdAlert.id, 'alert', 'alerts');
return createdAlert;
}
});
}