From 43539792a9582e49333f8e2cbb65aac0073cff5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Tue, 15 Sep 2020 19:01:29 +0100 Subject: [PATCH] [7.x] [Visualizations] Fix to usage collection when no results (#77478) (#77511) --- .../server/usage_collector/get_usage_collector.test.ts | 10 ++++++++++ .../server/usage_collector/get_usage_collector.ts | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/plugins/visualizations/server/usage_collector/get_usage_collector.test.ts b/src/plugins/visualizations/server/usage_collector/get_usage_collector.test.ts index 4a8e4b70ae07..108f270bc8ea 100644 --- a/src/plugins/visualizations/server/usage_collector/get_usage_collector.test.ts +++ b/src/plugins/visualizations/server/usage_collector/get_usage_collector.test.ts @@ -132,6 +132,16 @@ describe('Visualizations usage collector', () => { expect(usageCollector.fetch).toEqual(expect.any(Function)); }); + test('Returns undefined when no results found (undefined)', async () => { + const result = await usageCollector.fetch(getMockCallCluster(undefined as any)); + expect(result).toBe(undefined); + }); + + test('Returns undefined when no results found (0 results)', async () => { + const result = await usageCollector.fetch(getMockCallCluster([])); + expect(result).toBe(undefined); + }); + test('Summarizes visualizations response data', async () => { const result = await usageCollector.fetch(getMockCallCluster(defaultMockSavedObjects)); diff --git a/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts b/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts index 165c3ee64986..9ba09a3303b5 100644 --- a/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts +++ b/src/plugins/visualizations/server/usage_collector/get_usage_collector.ts @@ -55,7 +55,7 @@ async function getStats(callCluster: LegacyAPICaller, index: string) { }, }; const esResponse: ESResponse = await callCluster('search', searchParams); - const size = get(esResponse, 'hits.hits.length'); + const size = get(esResponse, 'hits.hits.length', 0); if (size < 1) { return; }