Commit 4a678c28 by Daniel Lee

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).
parent 1a25b78e
...@@ -87,7 +87,7 @@ function addMathStrategy(selectParts, partModel) { ...@@ -87,7 +87,7 @@ function addMathStrategy(selectParts, partModel) {
return; return;
} }
// if next to last is math, replace it // 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; selectParts[partCount-2] = partModel;
return; return;
} else if (selectParts[partCount-1].def.type === 'alias') { // if last is alias add it before } else if (selectParts[partCount-1].def.type === 'alias') { // if last is alias add it before
......
...@@ -236,6 +236,17 @@ describe('InfluxQuery', function() { ...@@ -236,6 +236,17 @@ describe('InfluxQuery', function() {
expect(query.target.select[0][2].type).to.be('math'); 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() { describe('when render adhoc filters', function() {
it('should generate correct query segment', function() { it('should generate correct query segment', function() {
var query = new InfluxQuery({measurement: 'cpu', }, templateSrv, {}); var query = new InfluxQuery({measurement: 'cpu', }, templateSrv, {});
......
...@@ -5,7 +5,7 @@ import queryPart from '../query_part'; ...@@ -5,7 +5,7 @@ import queryPart from '../query_part';
describe('InfluxQueryPart', () => { describe('InfluxQueryPart', () => {
describe('series with mesurement only', () => { describe('series with measurement only', () => {
it('should handle nested function parts', () => { it('should handle nested function parts', () => {
var part = queryPart.create({ var part = queryPart.create({
type: 'derivative', type: 'derivative',
...@@ -25,7 +25,7 @@ describe('InfluxQueryPart', () => { ...@@ -25,7 +25,7 @@ describe('InfluxQueryPart', () => {
expect(part.render('value')).to.be('spread(value)'); expect(part.render('value')).to.be('spread(value)');
}); });
it('should handle suffirx parts', () => { it('should handle suffix parts', () => {
var part = queryPart.create({ var part = queryPart.create({
type: 'math', type: 'math',
params: ['/ 100'], params: ['/ 100'],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment