From 05d725d0b85ae9f8a0d1d1b34814c7ce3c11a894 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sat, 16 May 2015 16:46:24 +0200 Subject: [PATCH] Slight refactoring, #1525 --- .../datasource/influxdb/queryBuilder.js | 12 +++++++- .../plugins/datasource/influxdb/queryCtrl.js | 19 ++++--------- .../test/specs/influx09-querybuilder-specs.js | 28 +++++++++++++++++++ public/test/specs/influxdbQueryCtrl-specs.js | 17 ----------- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/queryBuilder.js b/public/app/plugins/datasource/influxdb/queryBuilder.js index 461408307d3..fa2069ea35d 100644 --- a/public/app/plugins/datasource/influxdb/queryBuilder.js +++ b/public/app/plugins/datasource/influxdb/queryBuilder.js @@ -14,6 +14,16 @@ function (_) { return this.target.rawQuery ? this._modifyRawQuery() : this._buildQuery(); }; + p.showTagsQuery = function() { + var query = 'SHOW TAG KEYS'; + + if (this.target.measurement) { + query += ' FROM "' + this.target.measurement + '"'; + } + + return query; + }; + p._buildQuery = function() { var target = this.target; @@ -27,7 +37,7 @@ function (_) { var measurement = target.measurement; var aggregationFunc = target.function || 'mean'; - if(!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) { + if (!measurement.match('^/.*/') && !measurement.match(/^merge\(.*\)/)) { measurement = '"' + measurement+ '"'; } diff --git a/public/app/plugins/datasource/influxdb/queryCtrl.js b/public/app/plugins/datasource/influxdb/queryCtrl.js index 00729391b59..826ef26565d 100644 --- a/public/app/plugins/datasource/influxdb/queryCtrl.js +++ b/public/app/plugins/datasource/influxdb/queryCtrl.js @@ -1,8 +1,9 @@ define([ 'angular', - 'lodash' + 'lodash', + './queryBuilder', ], -function (angular, _) { +function (angular, _, InfluxQueryBuilder) { 'use strict'; var module = angular.module('grafana.controllers'); @@ -24,6 +25,8 @@ function (angular, _) { target.tags = target.tags || []; target.groupByTags = target.groupByTags || []; + $scope.queryBuilder = new InfluxQueryBuilder(target); + if (!target.measurement) { $scope.measurementSegment = MetricSegment.newSelectMeasurement(); } else { @@ -129,21 +132,11 @@ function (angular, _) { return segments; }; - $scope.buildTagKeysQuery = function(target) { - var query = 'SHOW TAG KEYS'; - - if (target.measurement) { - query += ' FROM "' + target.measurement + '"'; - } - - return query; - }; - $scope.getTagsOrValues = function(segment, index) { var query, queryType; if (segment.type === 'key' || segment.type === 'plus-button') { queryType = 'TAG_KEYS'; - query = $scope.buildTagKeysQuery($scope.target, segment); + query = $scope.queryBuilder.showTagsQuery(); } else if (segment.type === 'value') { queryType = 'TAG_VALUES'; query = 'SHOW TAG VALUES FROM "' + $scope.target.measurement + '" WITH KEY = ' + $scope.tagSegments[index-2].value; diff --git a/public/test/specs/influx09-querybuilder-specs.js b/public/test/specs/influx09-querybuilder-specs.js index 1b126b0bdf9..81602da51f7 100644 --- a/public/test/specs/influx09-querybuilder-specs.js +++ b/public/test/specs/influx09-querybuilder-specs.js @@ -62,6 +62,34 @@ define([ }); }); + describe('when building tag keys query', function() { + + describe('given picked measurement', function() { + it('build query with measurement filter', function() { + var builder = new InfluxQueryBuilder({ measurement: 'cpu', tags: [] }); + var query = builder.showTagsQuery(); + expect(query).to.be('SHOW TAG KEYS FROM "cpu"'); + }); + }); + + describe('given no picked measurement', function() { + it('build query without filter', function() { + var builder = new InfluxQueryBuilder({ measurement: '', tags: [] }); + var query = builder.showTagsQuery(); + expect(query).to.be('SHOW TAG KEYS'); + }); + }); + + describe('given an existing tag', function() { + it('build query with filter', function() { + var builder = new InfluxQueryBuilder({ measurement: '', tags: [{key: 'host', value: 'se1'}] }); + var query = builder.showTagsQuery(); + expect(query).to.be('SHOW TAG KEYS'); + }); + }); + + }); + }); }); diff --git a/public/test/specs/influxdbQueryCtrl-specs.js b/public/test/specs/influxdbQueryCtrl-specs.js index 2c9915c6760..4ce9b0e6696 100644 --- a/public/test/specs/influxdbQueryCtrl-specs.js +++ b/public/test/specs/influxdbQueryCtrl-specs.js @@ -198,23 +198,6 @@ define([ }); }); - describe('when building tag keys query', function() { - - describe('given picked measurement', function() { - it('build query with measurement filter', function() { - var query = ctx.scope.buildTagKeysQuery({ measurement: 'cpu', tags: [] }, {type: 'key'}); - expect(query).to.be('SHOW TAG KEYS FROM "cpu"'); - }); - }); - - describe('given no picked measurement', function() { - it('build query without filter', function() { - var query = ctx.scope.buildTagKeysQuery({ measurement: '', tags: [] }, {type: 'key'}); - expect(query).to.be('SHOW TAG KEYS'); - }); - }); - - }); }); });