Fix time filter in alerts table (#142377) (#142459)

(cherry picked from commit bc65bc5b56)

Co-authored-by: Maryam Saeidi <maryam.saeidi@elastic.co>
This commit is contained in:
Kibana Machine 2022-10-03 06:38:00 -06:00 committed by GitHub
parent 8a3afc220b
commit eac5a6a1af
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 3 deletions

View file

@ -166,3 +166,36 @@ Object {
},
}
`;
exports[`buildEsQuery should generate correct es query for {"timeRange":{"from":"now-15min","to":"now"},"kuery":"kibana.alert.status: \\"recovered\\""} 1`] = `
Object {
"bool": Object {
"filter": Array [
Object {
"bool": Object {
"minimum_should_match": 1,
"should": Array [
Object {
"match_phrase": Object {
"kibana.alert.status": "recovered",
},
},
],
},
},
Object {
"range": Object {
"@timestamp": Object {
"format": "strict_date_optional_time",
"gte": "now-15min",
"lte": "now",
},
},
},
],
"must": Array [],
"must_not": Array [],
"should": Array [],
},
}
`;

View file

@ -34,6 +34,13 @@ describe('buildEsQuery', () => {
timeRange: defaultTimeRange,
kuery: 'kibana.alert.status: "recovered" and kibana.alert.duration.us >= 120',
},
{
timeRange: {
from: 'now-15min',
to: 'now',
},
kuery: 'kibana.alert.status: "recovered"',
},
];
test.each(testData)('should generate correct es query for %j', ({ kuery, timeRange }) => {

View file

@ -7,15 +7,15 @@
import { buildEsQuery as kbnBuildEsQuery, TimeRange } from '@kbn/es-query';
import { TIMESTAMP } from '@kbn/rule-data-utils';
import { getTime } from '@kbn/data-plugin/common';
import { getRelativeTime } from '@kbn/data-plugin/common';
export function buildEsQuery(timeRange: TimeRange, kuery?: string) {
const timeFilter =
timeRange &&
getTime(undefined, timeRange, {
getRelativeTime(undefined, timeRange, {
fieldName: TIMESTAMP,
});
const filtersToUse = [...(timeFilter ? [timeFilter] : [])];
const filtersToUse = timeFilter ? [timeFilter] : [];
const queryToUse = kuery ? { query: kuery, language: 'kuery' } : [];
return kbnBuildEsQuery(undefined, queryToUse, filtersToUse);