From cfad77d3571aec69b43bef6c73be47e101aee3c5 Mon Sep 17 00:00:00 2001 From: Wylie Conlon Date: Thu, 11 Feb 2021 13:54:58 -0500 Subject: [PATCH] Add test for edge case --- .../plugins/lens/server/routes/field_stats.ts | 7 +++- .../api_integration/apis/lens/field_stats.ts | 34 ++++++++++++++++--- .../apps/visualize/precalculated_histogram.ts | 4 +-- .../pre_calculated_histogram/data.json | 21 +++++++++++- 4 files changed, 57 insertions(+), 9 deletions(-) diff --git a/x-pack/plugins/lens/server/routes/field_stats.ts b/x-pack/plugins/lens/server/routes/field_stats.ts index b7e4f8223757a1..7fd884755d86df 100644 --- a/x-pack/plugins/lens/server/routes/field_stats.ts +++ b/x-pack/plugins/lens/server/routes/field_stats.ts @@ -187,7 +187,12 @@ export async function getNumberHistogram( sampledValues: minMaxResult.aggregations!.sample.sample_count.value!, sampledDocuments: minMaxResult.aggregations!.sample.doc_count, topValues: topValuesBuckets, - histogram: { buckets: [] }, + histogram: useTopHits + ? { buckets: [] } + : { + // Insert a fake bucket for a single-value histogram + buckets: [{ count: minMaxResult.aggregations!.sample.doc_count, key: minValue }], + }, }; } 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 e715d765d93496..ac4ebb4e5b02c2 100644 --- a/x-pack/test/api_integration/apis/lens/field_stats.ts +++ b/x-pack/test/api_integration/apis/lens/field_stats.ts @@ -376,7 +376,7 @@ export default ({ getService }: FtrProviderContext) => { key: 0.47000000000000003, }, { - count: 556, + count: 454, key: 0.9400000000000001, }, { @@ -396,7 +396,7 @@ export default ({ getService }: FtrProviderContext) => { key: 2.8200000000000003, }, { - count: 596, + count: 391, key: 3.29, }, { @@ -408,15 +408,39 @@ export default ({ getService }: FtrProviderContext) => { key: 4.23, }, { - count: 639, + count: 628, key: 4.7, }, ], }, sampledDocuments: 7, - sampledValues: 4151, + sampledValues: 3833, totalDocuments: 7, - topValues: [], + topValues: { buckets: [] }, + }); + }); + + it('should return a single-value histogram when filtering a precalculated histogram', async () => { + const { body } = await supertest + .post('/api/lens/index_stats/histogram-test/field') + .set(COMMON_HEADERS) + .send({ + dslQuery: { match: { 'histogram-title': 'single value' } }, + fromDate: TEST_START_TIME, + toDate: TEST_END_TIME, + field: { + name: 'histogram-content', + type: 'histogram', + }, + }) + .expect(200); + + expect(body).to.eql({ + histogram: { buckets: [{ count: 1, key: 1 }] }, + sampledDocuments: 1, + sampledValues: 1, + totalDocuments: 1, + topValues: { buckets: [] }, }); }); diff --git a/x-pack/test/functional/apps/visualize/precalculated_histogram.ts b/x-pack/test/functional/apps/visualize/precalculated_histogram.ts index a0c8fa3b21ffd2..151c9e981250f9 100644 --- a/x-pack/test/functional/apps/visualize/precalculated_histogram.ts +++ b/x-pack/test/functional/apps/visualize/precalculated_histogram.ts @@ -64,7 +64,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { it('with average aggregation', async () => { const data = await renderTableForAggregation('Average'); - expect(data).to.eql([['2.8510720308359434']]); + expect(data).to.eql([['2.8653795982259327']]); }); it('with median aggregation', async () => { @@ -79,7 +79,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { it('with sum aggregation', async () => { const data = await renderTableForAggregation('Sum'); - expect(data).to.eql([['11834.800000000001']]); + expect(data).to.eql([['10983']]); }); }); }); diff --git a/x-pack/test/functional/es_archives/pre_calculated_histogram/data.json b/x-pack/test/functional/es_archives/pre_calculated_histogram/data.json index 9efbf02681eda8..121a4036aaacdd 100644 --- a/x-pack/test/functional/es_archives/pre_calculated_histogram/data.json +++ b/x-pack/test/functional/es_archives/pre_calculated_histogram/data.json @@ -193,4 +193,23 @@ } } } -} \ No newline at end of file +} + +{ + "type": "doc", + "value": { + "id": "5e694159d909d9d99b5e12d1", + "index": "histogram-test", + "source": { + "histogram-title": "single value", + "histogram-content": { + "values": [ + 1 + ], + "counts": [ + 1 + ] + } + } + } +}