From a6c8e079ff8b473b120970f186fb1823602b4fa6 Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Wed, 1 Sep 2021 08:30:32 -0400 Subject: [PATCH] Implemented better fix for Uptime type issues. --- .../lib/requests/get_ping_histogram.test.ts | 38 +++++++++++++++++++ .../server/lib/requests/get_ping_histogram.ts | 12 +++--- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.test.ts b/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.test.ts index 5d03110e5cf28..d946003d41990 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.test.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.test.ts @@ -217,4 +217,42 @@ describe('getPingHistogram', () => { expect(mockEsClient.search).toHaveBeenCalledTimes(1); expect(result).toMatchSnapshot(); }); + + it('returns an empty array if agg buckets are undefined', async () => { + const { esClient: mockEsClient, uptimeEsClient } = getUptimeESMockClient(); + + mockEsClient.search.mockResolvedValueOnce({ + body: { + aggregations: { + timeseries: { + buckets: undefined, + interval: '1m', + }, + }, + }, + } as any); + + const result = await getPingHistogram({ uptimeEsClient, dateStart: 'now-15m', dateEnd: 'now' }); + + expect(result.histogram).toEqual([]); + }); + + it('returns an empty array if agg buckets are empty', async () => { + const { esClient: mockEsClient, uptimeEsClient } = getUptimeESMockClient(); + + mockEsClient.search.mockResolvedValueOnce({ + body: { + aggregations: { + timeseries: { + buckets: [], + interval: '1m', + }, + }, + }, + } as any); + + const result = await getPingHistogram({ uptimeEsClient, dateStart: 'now-15m', dateEnd: 'now' }); + + expect(result.histogram).toEqual([]); + }); }); diff --git a/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts b/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts index 8f6fe5ee30285..3c7f557367953 100644 --- a/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts +++ b/x-pack/plugins/uptime/server/lib/requests/get_ping_histogram.ts @@ -90,13 +90,13 @@ export const getPingHistogram: UMElasticsearchQueryFn< }); const { body: result } = await uptimeEsClient.search(params); - const buckets: Array<{ - key: number; - down: { value: number | null }; - up: { value: number | null }; - }> = result?.aggregations?.timeseries?.buckets ?? []; - const histogram = buckets.map((bucket) => { + const buckets = result?.aggregations?.timeseries?.buckets ?? []; + if (typeof buckets === 'undefined' || buckets.length === 0) { + return { histogram: [], minInterval }; + } + + const histogram = buckets.map((bucket: Pick) => { const x: number = bucket.key; const downCount = bucket.down.value || 0; const upCount = bucket.up.value || 0;