Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Jan 11, 2021
1 parent 1c30525 commit aa9e7a9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 23 deletions.
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;
}
});
}

0 comments on commit aa9e7a9

Please sign in to comment.