From 6ac46c1ec2a4b5762ab1f1b67813fcae7f2caf52 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Tue, 4 Feb 2020 13:35:13 +0100 Subject: [PATCH 1/4] Fix field stats not loaded without time field --- .../plugins/lens/server/routes/field_stats.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/x-pack/legacy/plugins/lens/server/routes/field_stats.ts b/x-pack/legacy/plugins/lens/server/routes/field_stats.ts index ceefb492cdab..786aba5efe3f 100644 --- a/x-pack/legacy/plugins/lens/server/routes/field_stats.ts +++ b/x-pack/legacy/plugins/lens/server/routes/field_stats.ts @@ -27,7 +27,7 @@ export async function initFieldsRoute(setup: CoreSetup) { dslQuery: schema.object({}, { allowUnknowns: true }), fromDate: schema.string(), toDate: schema.string(), - timeFieldName: schema.string(), + timeFieldName: schema.maybe(schema.string()), field: schema.object( { name: schema.string(), @@ -46,9 +46,8 @@ export async function initFieldsRoute(setup: CoreSetup) { const { fromDate, toDate, timeFieldName, field, dslQuery } = req.body; try { - const query = { - bool: { - filter: [ + const filter = timeFieldName + ? [ { range: { [timeFieldName]: { @@ -58,7 +57,12 @@ export async function initFieldsRoute(setup: CoreSetup) { }, }, dslQuery, - ], + ] + : [dslQuery]; + + const query = { + bool: { + filter, }, }; From a6c5aa8e7e2b8fd8fbeab855860f975653d05539 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Tue, 4 Feb 2020 16:43:42 +0100 Subject: [PATCH 2/4] Add integration test for API --- .../api_integration/apis/lens/field_stats.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/x-pack/test/api_integration/apis/lens/field_stats.ts b/x-pack/test/api_integration/apis/lens/field_stats.ts index b2bb791e2da7..51d81668d275 100644 --- a/x-pack/test/api_integration/apis/lens/field_stats.ts +++ b/x-pack/test/api_integration/apis/lens/field_stats.ts @@ -47,6 +47,24 @@ export default ({ getService }: FtrProviderContext) => { .expect(404); }); + it('should also work without specifying a time field', async () => { + const { body } = await supertest + .post('/api/lens/index_stats/logstash-2015.09.22/field') + .set(COMMON_HEADERS) + .send({ + dslQuery: { match_all: {} }, + fromDate: TEST_START_TIME, + toDate: TEST_END_TIME, + field: { + name: 'bytes', + type: 'number', + }, + }) + .expect(200); + + expect(body).to.have.property('totalDocuments', 4633); + }); + it('should return an auto histogram for numbers and top values', async () => { const { body } = await supertest .post('/api/lens/index_stats/logstash-2015.09.22/field') From fd6f1085f5219dc6e6219a74c6c887c275abb803 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Wed, 5 Feb 2020 15:52:04 +0100 Subject: [PATCH 3/4] Add unit test for API call --- .../indexpattern_plugin/field_item.test.tsx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx index 1b49eb6bca7f..c6ee2201c465 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx @@ -90,6 +90,24 @@ describe('IndexPattern Field Item', () => { } as unknown) as FieldFormatsStart; }); + it('should request field stats without a time field, if the index pattern has none', async () => { + indexPattern.timeFieldName = undefined; + core.http.post.mockImplementationOnce(() => { + return Promise.resolve({}); + }); + const wrapper = mountWithIntl(); + wrapper.find('[data-test-subj="lnsFieldListPanelField-bytes"]').simulate('click'); + + expect(core.http.post).toHaveBeenCalledWith( + '/api/lens/index_stats/my-fake-index-pattern/field', + expect.anything() + ); + // Argment functions not detected correctly (https://github.com/microsoft/TypeScript/issues/26591) + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const { body } = (core.http.post.mock.calls[0] as any)[1]; + expect(JSON.parse(body)).not.toHaveProperty('timeFieldName'); + }); + it('should request field stats every time the button is clicked', async () => { let resolveFunction: (arg: unknown) => void; From d5ee4fe6b6a1f7cdd9a47fa83b404edfe3a4f7e7 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Wed, 5 Feb 2020 16:37:24 +0100 Subject: [PATCH 4/4] Correct typo in comment --- .../plugins/lens/public/indexpattern_plugin/field_item.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx index c6ee2201c465..62e2e628c254 100644 --- a/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx +++ b/x-pack/legacy/plugins/lens/public/indexpattern_plugin/field_item.test.tsx @@ -102,7 +102,7 @@ describe('IndexPattern Field Item', () => { '/api/lens/index_stats/my-fake-index-pattern/field', expect.anything() ); - // Argment functions not detected correctly (https://github.com/microsoft/TypeScript/issues/26591) + // Function argument types not detected correctly (https://github.com/microsoft/TypeScript/issues/26591) // eslint-disable-next-line @typescript-eslint/no-explicit-any const { body } = (core.http.post.mock.calls[0] as any)[1]; expect(JSON.parse(body)).not.toHaveProperty('timeFieldName');