From 930fd8b43a4a471b2590ad9bf2738daf9e9d3d5b Mon Sep 17 00:00:00 2001 From: Igor Ratsuk Date: Fri, 29 Mar 2019 07:22:04 +0100 Subject: [PATCH] Elasticsearch: Fix view percentiles metric in table without date histogram (#15686) Fix for properly display percentiles metrics in table panel when using a query without date histogram and for example grouping by terms. Fixes #3786 --- .../elasticsearch/elastic_response.ts | 8 +++ .../specs/elastic_response.test.ts | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/public/app/plugins/datasource/elasticsearch/elastic_response.ts b/public/app/plugins/datasource/elasticsearch/elastic_response.ts index 49f33e2963a..52fecd79e8b 100644 --- a/public/app/plugins/datasource/elasticsearch/elastic_response.ts +++ b/public/app/plugins/datasource/elasticsearch/elastic_response.ts @@ -156,6 +156,14 @@ export class ElasticResponse { } break; } + case 'percentiles': { + const percentiles = bucket[metric.id].values; + + for (const percentileName in percentiles) { + addMetricValue(values, `p${percentileName} ${metric.field}`, percentiles[percentileName]); + } + break; + } default: { let metricName = this.getMetricName(metric.type); const otherMetrics = _.filter(target.metrics, { type: metric.type }); diff --git a/public/app/plugins/datasource/elasticsearch/specs/elastic_response.test.ts b/public/app/plugins/datasource/elasticsearch/specs/elastic_response.test.ts index bedc71a0b58..1c6bcc86332 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/elastic_response.test.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/elastic_response.test.ts @@ -582,6 +582,59 @@ describe('ElasticResponse', () => { }); }); + describe('No group by time with percentiles ', () => { + let result; + + beforeEach(() => { + targets = [ + { + refId: 'A', + metrics: [{ type: 'percentiles', field: 'value', settings: { percents: [75, 90] }, id: '1' }], + bucketAggs: [{ type: 'term', field: 'id', id: '3' }], + }, + ]; + response = { + responses: [ + { + aggregations: { + '3': { + buckets: [ + { + '1': { values: { '75': 3.3, '90': 5.5 } }, + doc_count: 10, + key: 'id1', + }, + { + '1': { values: { '75': 2.3, '90': 4.5 } }, + doc_count: 15, + key: 'id2', + }, + ], + }, + }, + }, + ], + }; + + result = new ElasticResponse(targets, response).getTimeSeries(); + }); + + it('should return table', () => { + expect(result.data.length).toBe(1); + expect(result.data[0].type).toBe('table'); + expect(result.data[0].columns[0].text).toBe('id'); + expect(result.data[0].columns[1].text).toBe('p75 value'); + expect(result.data[0].columns[2].text).toBe('p90 value'); + expect(result.data[0].rows.length).toBe(2); + expect(result.data[0].rows[0][0]).toBe('id1'); + expect(result.data[0].rows[0][1]).toBe(3.3); + expect(result.data[0].rows[0][2]).toBe(5.5); + expect(result.data[0].rows[1][0]).toBe('id2'); + expect(result.data[0].rows[1][1]).toBe(2.3); + expect(result.data[0].rows[1][2]).toBe(4.5); + }); + }); + describe('Multiple metrics of same type', () => { beforeEach(() => { targets = [