From 4a678c288461ec1264d001b4e35ae453a023bbc4 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Sat, 22 Jul 2017 01:31:26 +0200 Subject: [PATCH] influxdb: fix for adding math query part Fixes #8870. Fixes out of bound exception on adding a math query part when there is no group by interval (or only one query part). --- public/app/plugins/datasource/influxdb/query_part.ts | 2 +- .../datasource/influxdb/specs/influx_query_specs.ts | 11 +++++++++++ .../datasource/influxdb/specs/query_part_specs.ts | 4 ++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/query_part.ts b/public/app/plugins/datasource/influxdb/query_part.ts index 683b3bddd55..0e63e3c7e24 100644 --- a/public/app/plugins/datasource/influxdb/query_part.ts +++ b/public/app/plugins/datasource/influxdb/query_part.ts @@ -87,7 +87,7 @@ function addMathStrategy(selectParts, partModel) { return; } // if next to last is math, replace it - if (selectParts[partCount-2].def.type === 'math') { + if (partCount > 1 && selectParts[partCount-2].def.type === 'math') { selectParts[partCount-2] = partModel; return; } else if (selectParts[partCount-1].def.type === 'alias') { // if last is alias add it before diff --git a/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts b/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts index 96d37f34323..595861064fd 100644 --- a/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts +++ b/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts @@ -236,6 +236,17 @@ describe('InfluxQuery', function() { expect(query.target.select[0][2].type).to.be('math'); }); + it('should add math when one only query part', function() { + var query = new InfluxQuery({ + measurement: 'cpu', + select: [[{type: 'field', params: ['value']}]] + }, templateSrv, {}); + + query.addSelectPart(query.selectModels[0], 'math'); + expect(query.target.select[0].length).to.be(2); + expect(query.target.select[0][1].type).to.be('math'); + }); + describe('when render adhoc filters', function() { it('should generate correct query segment', function() { var query = new InfluxQuery({measurement: 'cpu', }, templateSrv, {}); diff --git a/public/app/plugins/datasource/influxdb/specs/query_part_specs.ts b/public/app/plugins/datasource/influxdb/specs/query_part_specs.ts index 299ec9b47fd..ba32c961909 100644 --- a/public/app/plugins/datasource/influxdb/specs/query_part_specs.ts +++ b/public/app/plugins/datasource/influxdb/specs/query_part_specs.ts @@ -5,7 +5,7 @@ import queryPart from '../query_part'; describe('InfluxQueryPart', () => { - describe('series with mesurement only', () => { + describe('series with measurement only', () => { it('should handle nested function parts', () => { var part = queryPart.create({ type: 'derivative', @@ -25,7 +25,7 @@ describe('InfluxQueryPart', () => { expect(part.render('value')).to.be('spread(value)'); }); - it('should handle suffirx parts', () => { + it('should handle suffix parts', () => { var part = queryPart.create({ type: 'math', params: ['/ 100'],