From d7d985be77962fc5e821c82b0966f21fbb0e21f8 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 20 Aug 2020 12:24:36 -0400 Subject: [PATCH 01/11] Add test and modify code to account for complex filter input. --- .../__tests__/get_monitor_status.test.ts | 290 ++++++++++++++++++ .../server/lib/requests/get_monitor_status.ts | 8 + 2 files changed, 298 insertions(+) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts index 7dba71a8126e2..a8a9ad8e5dcba 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts @@ -286,6 +286,296 @@ describe('getMonitorStatus', () => { `); }); + it('properly assigns filters for complex kuery filters', async () => { + const [callES, esMock] = setupMockEsCompositeQuery( + [{ bucketCriteria: [] }], + genBucketItem + ); + const clientParameters = { + dynamicSettings: { + heartbeatIndices: 'heartbeat-8*', + certAgeThreshold: 730, + certExpirationThreshold: 30, + }, + timerange: { + from: 'now-15m', + to: 'now', + }, + numTimes: 5, + locations: [], + filters: { + bool: { + filter: [ + { + bool: { + should: [ + { + match_phrase: { + tags: 'org:google', + }, + }, + ], + minimum_should_match: 1, + }, + }, + { + bool: { + should: [ + { + bool: { + should: [ + { + match_phrase: { + 'monitor.type': 'http', + }, + }, + ], + minimum_should_match: 1, + }, + }, + { + bool: { + should: [ + { + match_phrase: { + 'monitor.type': 'tcp', + }, + }, + ], + minimum_should_match: 1, + }, + }, + ], + minimum_should_match: 1, + }, + }, + ], + }, + }, + }; + await getMonitorStatus({ + callES, + dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS, + ...clientParameters, + }); + expect(esMock.callAsCurrentUser).toHaveBeenCalledTimes(1); + const [, params] = esMock.callAsCurrentUser.mock.calls[0]; + expect(params).toMatchInlineSnapshot(` + Object { + "body": Object { + "aggs": Object { + "monitors": Object { + "aggs": Object { + "fields": Object { + "top_hits": Object { + "size": 1, + }, + }, + }, + "composite": Object { + "size": 2000, + "sources": Array [ + Object { + "monitorId": Object { + "terms": Object { + "field": "monitor.id", + }, + }, + }, + Object { + "status": Object { + "terms": Object { + "field": "monitor.status", + }, + }, + }, + Object { + "location": Object { + "terms": Object { + "field": "observer.geo.name", + "missing_bucket": true, + }, + }, + }, + ], + }, + }, + }, + "query": Object { + "bool": Object { + "filter": Array [ + Object { + "term": Object { + "monitor.status": "down", + }, + }, + Object { + "range": Object { + "@timestamp": Object { + "gte": "now-15m", + "lte": "now", + }, + }, + }, + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "tags": "org:google", + }, + }, + ], + }, + }, + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.type": "http", + }, + }, + ], + }, + }, + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.type": "tcp", + }, + }, + ], + }, + }, + ], + }, + }, + ], + }, + }, + "size": 0, + }, + "index": "heartbeat-8*", + } + `); + }); + + it('properly assigns filters for complex kuery filters object', async () => { + const [callES, esMock] = setupMockEsCompositeQuery( + [{ bucketCriteria: [] }], + genBucketItem + ); + const clientParameters = { + dynamicSettings: { + heartbeatIndices: 'heartbeat-8*', + certAgeThreshold: 730, + certExpirationThreshold: 30, + }, + timerange: { + from: 'now-15m', + to: 'now', + }, + numTimes: 5, + locations: [], + filters: { + bool: { + filter: { + exists: { + field: 'monitor.status', + }, + }, + }, + }, + }; + await getMonitorStatus({ + callES, + dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS, + ...clientParameters, + }); + expect(esMock.callAsCurrentUser).toHaveBeenCalledTimes(1); + const [, params] = esMock.callAsCurrentUser.mock.calls[0]; + expect(params).toMatchInlineSnapshot(` + Object { + "body": Object { + "aggs": Object { + "monitors": Object { + "aggs": Object { + "fields": Object { + "top_hits": Object { + "size": 1, + }, + }, + }, + "composite": Object { + "size": 2000, + "sources": Array [ + Object { + "monitorId": Object { + "terms": Object { + "field": "monitor.id", + }, + }, + }, + Object { + "status": Object { + "terms": Object { + "field": "monitor.status", + }, + }, + }, + Object { + "location": Object { + "terms": Object { + "field": "observer.geo.name", + "missing_bucket": true, + }, + }, + }, + ], + }, + }, + }, + "query": Object { + "bool": Object { + "filter": Array [ + Object { + "term": Object { + "monitor.status": "down", + }, + }, + Object { + "range": Object { + "@timestamp": Object { + "gte": "now-15m", + "lte": "now", + }, + }, + }, + Object { + "exists": Object { + "field": "monitor.status", + }, + }, + ], + }, + }, + "size": 0, + }, + "index": "heartbeat-8*", + } + `); + }); + it('fetches single page of results', async () => { const [callES, esMock] = setupMockEsCompositeQuery( [ diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index 0788880994200..8e4d734de5247 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -117,6 +117,14 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }; if (filters?.bool) { + if (filters.bool.filter) { + if (Array.isArray(filters.bool.filter)) { + esParams.body.query.bool.filter.push(...filters.bool.filter); + } else { + esParams.body.query.bool.filter.push(filters.bool.filter); + } + delete filters.bool.filter; + } esParams.body.query.bool = Object.assign({}, esParams.body.query.bool, filters.bool); } From f1062c297e417375e964b3f8d61e0ddd1b9a8228 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 20 Aug 2020 12:37:02 -0400 Subject: [PATCH 02/11] Avoid unnecessary nesting. --- .../server/lib/requests/get_monitor_status.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index 8e4d734de5247..3fc391c33c58d 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -116,15 +116,16 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }, }; - if (filters?.bool) { - if (filters.bool.filter) { - if (Array.isArray(filters.bool.filter)) { - esParams.body.query.bool.filter.push(...filters.bool.filter); - } else { - esParams.body.query.bool.filter.push(filters.bool.filter); - } - delete filters.bool.filter; + if (filters?.bool?.filter) { + if (Array.isArray(filters.bool.filter)) { + esParams.body.query.bool.filter.push(...filters.bool.filter); + } else { + esParams.body.query.bool.filter.push(filters.bool.filter); } + delete filters.bool.filter; + } + + if (filters?.bool) { esParams.body.query.bool = Object.assign({}, esParams.body.query.bool, filters.bool); } From 6172d35ae3112094d0fb2cde99fddde088742015 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 20 Aug 2020 15:05:14 -0400 Subject: [PATCH 03/11] Simplify filtering code. --- .../__tests__/get_monitor_status.test.ts | 148 ++++++++++-------- .../server/lib/requests/get_monitor_status.ts | 11 +- 2 files changed, 82 insertions(+), 77 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts index a8a9ad8e5dcba..594e2b9cd0d83 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts @@ -149,33 +149,37 @@ describe('getMonitorStatus', () => { }, }, ], - "minimum_should_match": 1, - "should": Array [ - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.id": "apm-dev", - }, + "should": Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.id": "apm-dev", + }, + }, + ], }, - ], - }, - }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.id": "auto-http-0X8D6082B94BBE3B8A", - }, + }, + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.id": "auto-http-0X8D6082B94BBE3B8A", + }, + }, + ], }, - ], - }, + }, + ], }, - ], + }, }, }, "size": 0, @@ -417,50 +421,56 @@ describe('getMonitorStatus', () => { }, }, }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "tags": "org:google", - }, - }, - ], - }, - }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.type": "http", - }, + ], + "should": Object { + "bool": Object { + "filter": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "tags": "org:google", }, - ], - }, + }, + ], }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.type": "tcp", - }, + }, + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.type": "http", + }, + }, + ], }, - ], - }, + }, + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.type": "tcp", + }, + }, + ], + }, + }, + ], }, - ], - }, + }, + ], }, - ], + }, }, }, "size": 0, @@ -561,12 +571,16 @@ describe('getMonitorStatus', () => { }, }, }, - Object { - "exists": Object { - "field": "monitor.status", + ], + "should": Object { + "bool": Object { + "filter": Object { + "exists": Object { + "field": "monitor.status", + }, }, }, - ], + }, }, }, "size": 0, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index 3fc391c33c58d..4508aa231d6f3 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -116,17 +116,8 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }, }; - if (filters?.bool?.filter) { - if (Array.isArray(filters.bool.filter)) { - esParams.body.query.bool.filter.push(...filters.bool.filter); - } else { - esParams.body.query.bool.filter.push(filters.bool.filter); - } - delete filters.bool.filter; - } - if (filters?.bool) { - esParams.body.query.bool = Object.assign({}, esParams.body.query.bool, filters.bool); + esParams.body.query.bool.should = filters; } /** From e0098088529029045c3e8721f041adfe7a3f51ff Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 20 Aug 2020 15:52:27 -0400 Subject: [PATCH 04/11] Use `must` instead of `should`. --- x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index 4508aa231d6f3..6fea71774b62e 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -117,7 +117,7 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }; if (filters?.bool) { - esParams.body.query.bool.should = filters; + esParams.body.query.bool.must = filters; } /** From 383e136122086dc6e84361c056a64b298dda6dba Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 20 Aug 2020 16:02:19 -0400 Subject: [PATCH 05/11] Fix types. --- .../lib/requests/__tests__/get_monitor_status.test.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts index 594e2b9cd0d83..c226776ddbb52 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts @@ -296,11 +296,6 @@ describe('getMonitorStatus', () => { genBucketItem ); const clientParameters = { - dynamicSettings: { - heartbeatIndices: 'heartbeat-8*', - certAgeThreshold: 730, - certExpirationThreshold: 30, - }, timerange: { from: 'now-15m', to: 'now', @@ -486,11 +481,6 @@ describe('getMonitorStatus', () => { genBucketItem ); const clientParameters = { - dynamicSettings: { - heartbeatIndices: 'heartbeat-8*', - certAgeThreshold: 730, - certExpirationThreshold: 30, - }, timerange: { from: 'now-15m', to: 'now', From 58e627fb0981444d536bf84972f099c1d93fdb92 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Thu, 20 Aug 2020 16:47:06 -0400 Subject: [PATCH 06/11] Refresh snapshots. --- .../lib/requests/__tests__/get_monitor_status.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts index c226776ddbb52..dc0bdeee0ca4a 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts @@ -149,7 +149,7 @@ describe('getMonitorStatus', () => { }, }, ], - "should": Object { + "must": Object { "bool": Object { "minimum_should_match": 1, "should": Array [ @@ -417,7 +417,7 @@ describe('getMonitorStatus', () => { }, }, ], - "should": Object { + "must": Object { "bool": Object { "filter": Array [ Object { @@ -562,7 +562,7 @@ describe('getMonitorStatus', () => { }, }, ], - "should": Object { + "must": Object { "bool": Object { "filter": Object { "exists": Object { From 7e588ed6f6d16f44780b8be46689669806b06922 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Fri, 21 Aug 2020 09:35:00 -0400 Subject: [PATCH 07/11] Use `filter` instead of `must`. --- .../__tests__/get_monitor_status.test.ts | 152 +++++++++--------- .../server/lib/requests/get_monitor_status.ts | 2 +- 2 files changed, 77 insertions(+), 77 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts index dc0bdeee0ca4a..e61d736e37106 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_status.test.ts @@ -148,38 +148,38 @@ describe('getMonitorStatus', () => { }, }, }, - ], - "must": Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.id": "apm-dev", + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.id": "apm-dev", + }, }, - }, - ], + ], + }, }, - }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.id": "auto-http-0X8D6082B94BBE3B8A", + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.id": "auto-http-0X8D6082B94BBE3B8A", + }, }, - }, - ], + ], + }, }, - }, - ], + ], + }, }, - }, + ], }, }, "size": 0, @@ -416,56 +416,56 @@ describe('getMonitorStatus', () => { }, }, }, - ], - "must": Object { - "bool": Object { - "filter": Array [ - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "tags": "org:google", + Object { + "bool": Object { + "filter": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "tags": "org:google", + }, }, - }, - ], + ], + }, }, - }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.type": "http", + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.type": "http", + }, }, - }, - ], + ], + }, }, - }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ - Object { - "match_phrase": Object { - "monitor.type": "tcp", + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.type": "tcp", + }, }, - }, - ], + ], + }, }, - }, - ], + ], + }, }, - }, - ], + ], + }, }, - }, + ], }, }, "size": 0, @@ -561,16 +561,16 @@ describe('getMonitorStatus', () => { }, }, }, - ], - "must": Object { - "bool": Object { - "filter": Object { - "exists": Object { - "field": "monitor.status", + Object { + "bool": Object { + "filter": Object { + "exists": Object { + "field": "monitor.status", + }, }, }, }, - }, + ], }, }, "size": 0, diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index 6fea71774b62e..ad84f233d3f33 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -117,7 +117,7 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }; if (filters?.bool) { - esParams.body.query.bool.must = filters; + esParams.body.query.bool.filter.push(filters); } /** From 61e9c945822f9a213b1ed57c052a47ca6bc5af1d Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Tue, 25 Aug 2020 13:57:51 -0400 Subject: [PATCH 08/11] Refactor to improve readability. --- .../uptime/server/lib/requests/get_monitor_status.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts index ad84f233d3f33..7cb35c6a5ea23 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_status.ts @@ -71,6 +71,8 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }, }, }, + // append user filters, if defined + ...(filters?.bool ? [filters] : []), ], }, }, @@ -116,10 +118,6 @@ export const getMonitorStatus: UMElasticsearchQueryFn< }, }; - if (filters?.bool) { - esParams.body.query.bool.filter.push(filters); - } - /** * Perform a logical `and` against the selected location filters. */ From 78b031e6a0b48bcbe7499f24edd5f41df1df1a2b Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Wed, 26 Aug 2020 14:42:42 -0400 Subject: [PATCH 09/11] Resolve filtering issue for availability query. --- .../get_monitor_availability.test.ts | 32 +++++++++++-------- .../lib/requests/get_monitor_availability.ts | 12 ++++--- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts index f0222de02697d..51e4104305aec 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts @@ -203,28 +203,32 @@ describe('monitor availability', () => { }, }, }, - ], - "minimum_should_match": 1, - "should": Array [ Object { "bool": Object { "minimum_should_match": 1, "should": Array [ Object { - "match_phrase": Object { - "monitor.id": "apm-dev", + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.id": "apm-dev", + }, + }, + ], }, }, - ], - }, - }, - Object { - "bool": Object { - "minimum_should_match": 1, - "should": Array [ Object { - "match_phrase": Object { - "monitor.id": "auto-http-0X8D6082B94BBE3B8A", + "bool": Object { + "minimum_should_match": 1, + "should": Array [ + Object { + "match_phrase": Object { + "monitor.id": "auto-http-0X8D6082B94BBE3B8A", + }, + }, + ], }, }, ], diff --git a/x-pack/plugins/uptime/server/lib/requests/get_monitor_availability.ts b/x-pack/plugins/uptime/server/lib/requests/get_monitor_availability.ts index 0801fc5769363..f78048100b998 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_monitor_availability.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_monitor_availability.ts @@ -47,6 +47,11 @@ export const getMonitorAvailability: UMElasticsearchQueryFn< const gte = `now-${range}${rangeUnit}`; + let parsedFilters: any; + if (filters) { + parsedFilters = JSON.parse(filters); + } + do { const esParams: any = { index: dynamicSettings.heartbeatIndices, @@ -62,6 +67,8 @@ export const getMonitorAvailability: UMElasticsearchQueryFn< }, }, }, + // append user filters, if defined + ...(parsedFilters?.bool ? [parsedFilters] : []), ], }, }, @@ -139,11 +146,6 @@ export const getMonitorAvailability: UMElasticsearchQueryFn< }, }; - if (filters) { - const parsedFilter = JSON.parse(filters); - esParams.body.query.bool = { ...esParams.body.query.bool, ...parsedFilter.bool }; - } - if (afterKey) { esParams.body.aggs.monitors.composite.after = afterKey; } From 3f9028730fe46e65adaafef0e6cb51719dd96238 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Wed, 26 Aug 2020 16:44:48 -0400 Subject: [PATCH 10/11] Add test covering filter fix. --- .../get_monitor_availability.test.ts | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts index 51e4104305aec..6cd29036c9c2a 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts @@ -801,6 +801,133 @@ describe('monitor availability', () => { ] `); }); + + it('does not overwrite filters', async () => { + const [callES, esMock] = setupMockEsCompositeQuery< + AvailabilityKey, + GetMonitorAvailabilityResult, + AvailabilityDoc + >( + [ + { + bucketCriteria: [], + }, + ], + genBucketItem + ); + const result = await getMonitorAvailability({ + callES, + dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS, + range: 3, + rangeUnit: 's', + threshold: '99', + filters: JSON.stringify({ bool: { filter: [{ term: { 'monitor.id': 'foo' } }] } }), + }); + const [, params] = esMock.callAsCurrentUser.mock.calls[0]; + expect(params).toMatchInlineSnapshot(` + Object { + "body": Object { + "aggs": Object { + "monitors": Object { + "aggs": Object { + "down_sum": Object { + "sum": Object { + "field": "summary.down", + "missing": 0, + }, + }, + "fields": Object { + "top_hits": Object { + "size": 1, + "sort": Array [ + Object { + "@timestamp": Object { + "order": "desc", + }, + }, + ], + }, + }, + "filtered": Object { + "bucket_selector": Object { + "buckets_path": Object { + "threshold": "ratio.value", + }, + "script": "params.threshold < 0.99", + }, + }, + "ratio": Object { + "bucket_script": Object { + "buckets_path": Object { + "downTotal": "down_sum", + "upTotal": "up_sum", + }, + "script": " + if (params.upTotal + params.downTotal > 0) { + return params.upTotal / (params.upTotal + params.downTotal); + } return null;", + }, + }, + "up_sum": Object { + "sum": Object { + "field": "summary.up", + "missing": 0, + }, + }, + }, + "composite": Object { + "size": 2000, + "sources": Array [ + Object { + "monitorId": Object { + "terms": Object { + "field": "monitor.id", + }, + }, + }, + Object { + "location": Object { + "terms": Object { + "field": "observer.geo.name", + "missing_bucket": true, + }, + }, + }, + ], + }, + }, + }, + "query": Object { + "bool": Object { + "filter": Array [ + Object { + "range": Object { + "@timestamp": Object { + "gte": "now-3s", + "lte": "now", + }, + }, + }, + Object { + "bool": Object { + "filter": Array [ + Object { + "term": Object { + "monitor.id": "foo", + }, + }, + ], + }, + }, + ], + }, + }, + "size": 0, + }, + "index": "heartbeat-8*", + } + `); + }); }); describe('formatBuckets', () => { From 07e69158397d3b42aa5e44a1e1ae9149bce2cbcf Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Mon, 31 Aug 2020 08:57:27 -0400 Subject: [PATCH 11/11] Removed unused var. --- .../lib/requests/__tests__/get_monitor_availability.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts index 6cd29036c9c2a..015d9a4925f3e 100644 --- a/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/__tests__/get_monitor_availability.test.ts @@ -815,7 +815,7 @@ describe('monitor availability', () => { ], genBucketItem ); - const result = await getMonitorAvailability({ + await getMonitorAvailability({ callES, dynamicSettings: DYNAMIC_SETTINGS_DEFAULTS, range: 3,