diff --git a/public/app/plugins/datasource/influxdb/influx_query.ts b/public/app/plugins/datasource/influxdb/influx_query.ts index a18562f90ed..e4b798feb95 100644 --- a/public/app/plugins/datasource/influxdb/influx_query.ts +++ b/public/app/plugins/datasource/influxdb/influx_query.ts @@ -44,20 +44,34 @@ class InfluxQuery { } hasGroupByTime() { - return false; + return _.find(this.target.groupBy, (g: any) => g.type === 'time'); } hasFill() { - return false; + return _.find(this.target.groupBy, (g: any) => g.type === 'fill'); } addGroupBy(value) { var stringParts = value.match(/^(\w+)\((.*)\)$/); var typePart = stringParts[1]; var arg = stringParts[2]; - console.log(value, stringParts); var partModel = queryPart.create({type: typePart, params: [arg]}); - this.target.groupBy.push(partModel.part); + var partCount = this.target.groupBy.length; + + if (partCount === 0) { + this.target.groupBy.push(partModel.part); + } else if (typePart === 'time') { + this.target.groupBy.splice(0, 0, partModel.part); + } else if (typePart === 'tag') { + if (this.target.groupBy[partCount-1].type === 'fill') { + this.target.groupBy.splice(partCount-1, 0, partModel.part); + } else { + this.target.groupBy.push(partModel.part); + } + } else { + this.target.groupBy.push(partModel.part); + } + this.updateProjection(); } diff --git a/public/app/plugins/datasource/influxdb/query_part.ts b/public/app/plugins/datasource/influxdb/query_part.ts index 09aae007750..1dd7c76808e 100644 --- a/public/app/plugins/datasource/influxdb/query_part.ts +++ b/public/app/plugins/datasource/influxdb/query_part.ts @@ -186,7 +186,7 @@ QueryPartDef.register({ QueryPartDef.register({ type: 'time', category: groupByTimeFunctions, - params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5min', '10m', '15m', '1h'] }], + params: [{ name: "rate", type: "interval", options: ['$interval', '1s', '10s', '1m', '5m', '10m', '15m', '1h'] }], defaultParams: ['$interval'], renderer: functionRenderer, }); 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 7b7ce8e4ae8..e6745d116a4 100644 --- a/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts +++ b/public/app/plugins/datasource/influxdb/specs/influx_query_specs.ts @@ -58,6 +58,34 @@ describe.only('InfluxQuery', function() { }); }); + describe('when adding group by part', function() { + + it('should add tag before fill', function() { + var query = new InfluxQuery({ + measurement: 'cpu', + groupBy: [{type: 'time'}, {type: 'fill'}] + }); + + query.addGroupBy('tag(host)'); + expect(query.target.groupBy.length).to.be(3); + expect(query.target.groupBy[1].type).to.be('tag'); + expect(query.target.groupBy[1].params[0]).to.be('host'); + expect(query.target.groupBy[2].type).to.be('fill'); + }); + + it('should add tag last if no fill', function() { + var query = new InfluxQuery({ + measurement: 'cpu', + groupBy: [] + }); + + query.addGroupBy('tag(host)'); + expect(query.target.groupBy.length).to.be(1); + expect(query.target.groupBy[0].type).to.be('tag'); + }); + + }); + describe('when adding select part', function() { it('should add mean after after field', function() {