From 697d0867faaad772aa1fdd284c508565f36177c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 23 Jan 2017 10:15:02 +0100 Subject: [PATCH] fix(Elasticsearch): fix for alias patterns that refers to term that is numeric zero, fixes #7323 --- CHANGELOG.md | 1 + .../elasticsearch/elastic_response.js | 4 ++-- .../specs/elastic_response_specs.ts | 19 +++++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ba532356cf2..0a42a07b64d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ * **Dashboard**: Avoid duplicate data in dashboard json for panels with alerts [#7256](https://github.com/grafana/grafana/pull/7256) * **Alertlist**: Only show scrollbar when required [#7269](https://github.com/grafana/grafana/issues/7269) * **SMTP**: Set LocalName to hostname [#7223](https://github.com/grafana/grafana/issues/7223) +* **Elasticsearch**: Fix for alias patterns for terms that had numeric zero, fixes [#7323](https://github.com/grafana/grafana/issues/7323) # 4.1.2 (unreleased) diff --git a/public/app/plugins/datasource/elasticsearch/elastic_response.js b/public/app/plugins/datasource/elasticsearch/elastic_response.js index 4e82a280024..27285852e33 100644 --- a/public/app/plugins/datasource/elasticsearch/elastic_response.js +++ b/public/app/plugins/datasource/elasticsearch/elastic_response.js @@ -166,7 +166,7 @@ function (_, queryDef) { for (var nameIndex in esAgg.buckets) { bucket = esAgg.buckets[nameIndex]; props = _.clone(props); - if (bucket.key) { + if (bucket.key !== void 0) { props[aggDef.field] = bucket.key; } else { props["filter"] = nameIndex; @@ -199,7 +199,7 @@ function (_, queryDef) { var group = g1 || g2; if (group.indexOf('term ') === 0) { return series.props[group.substring(5)]; } - if (series.props[group]) { return series.props[group]; } + if (series.props[group] !== void 0) { return series.props[group]; } if (group === 'metric') { return metricName; } if (group === 'field') { return series.field; } diff --git a/public/app/plugins/datasource/elasticsearch/specs/elastic_response_specs.ts b/public/app/plugins/datasource/elasticsearch/specs/elastic_response_specs.ts index c80fcb7169f..4b87cc52c04 100644 --- a/public/app/plugins/datasource/elasticsearch/specs/elastic_response_specs.ts +++ b/public/app/plugins/datasource/elasticsearch/specs/elastic_response_specs.ts @@ -302,7 +302,7 @@ describe('ElasticResponse', function() { targets = [{ refId: 'A', metrics: [{type: 'count', id: '1'}], - alias: '{{term @host}} {{metric}} and!', + alias: '{{term @host}} {{metric}} and {{not_exist}} {{@host}}', bucketAggs: [ {type: 'terms', field: '@host', id: '2'}, {type: 'date_histogram', field: '@timestamp', id: '3'} @@ -333,6 +333,16 @@ describe('ElasticResponse', function() { doc_count: 10, key: 'server2', }, + { + "3": { + buckets: [ + {doc_count: 2, key: 1000}, + {doc_count: 8, key: 2000} + ] + }, + doc_count: 10, + key: 0, + }, ] } } @@ -343,10 +353,11 @@ describe('ElasticResponse', function() { }); it('should return 2 series', function() { - expect(result.data.length).to.be(2); + expect(result.data.length).to.be(3); expect(result.data[0].datapoints.length).to.be(2); - expect(result.data[0].target).to.be('server1 Count and!'); - expect(result.data[1].target).to.be('server2 Count and!'); + expect(result.data[0].target).to.be('server1 Count and {{not_exist}} server1'); + expect(result.data[1].target).to.be('server2 Count and {{not_exist}} server2'); + expect(result.data[2].target).to.be('0 Count and {{not_exist}} 0'); }); });