Просмотр исходного кода

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).
Daniel Lee 8 лет назад
Родитель
Сommit
4a678c2884

+ 1 - 1
public/app/plugins/datasource/influxdb/query_part.ts

@@ -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

+ 11 - 0
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');
       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, {});

+ 2 - 2
public/app/plugins/datasource/influxdb/specs/query_part_specs.ts

@@ -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', () => {
       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'],