From d3c79c9b4997d6020ca097f86f0b03bfcce28286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sun, 30 Aug 2015 10:09:45 +0200 Subject: [PATCH] fix(datasource query editors): fixed issue with duplicate query and the query letter (refId) --- CHANGELOG.md | 1 + public/app/features/dashboard/dashboardSrv.js | 40 ++++++++++++++++++- public/app/features/panel/panelSrv.js | 25 +++++------- .../graphite/partials/query.editor.html | 6 +-- .../plugins/datasource/graphite/queryCtrl.js | 9 ----- .../influxdb/partials/query.editor.html | 6 +-- .../plugins/datasource/influxdb/queryCtrl.js | 9 ----- .../influxdb_08/partials/query.editor.html | 6 +-- .../datasource/influxdb_08/queryCtrl.js | 12 +----- .../kairosdb/partials/query.editor.html | 6 +-- .../plugins/datasource/kairosdb/queryCtrl.js | 9 ----- .../opentsdb/partials/query.editor.html | 9 ++--- .../plugins/datasource/opentsdb/queryCtrl.js | 5 --- public/test/specs/dashboardSrv-specs.js | 33 +++++++++++++++ public/test/specs/panelSrv-specs.js | 18 --------- 15 files changed, 99 insertions(+), 95 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 176531fbf05..db526bf7d62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ it allows you to add queries of differnet data source types & instances to the s - Notice to makers/users of custom data sources, there is a minor breaking change in 2.2 that require an update to custom data sources for them to work in 2.2. [Read this doc](https://github.com/grafana/grafana/tree/master/docs/sources/datasources/plugin_api.md) for more on the data source api change. +- The duplicate query function used in data source editors is changed, and moveMetricQuery function was renamed 2.1.3 (2015-08-24) diff --git a/public/app/features/dashboard/dashboardSrv.js b/public/app/features/dashboard/dashboardSrv.js index 4b2071701bc..0ee2fde8a4b 100644 --- a/public/app/features/dashboard/dashboardSrv.js +++ b/public/app/features/dashboard/dashboardSrv.js @@ -184,12 +184,48 @@ function (angular, $, kbn, _, moment) { return newPanel; }; + p.getNextQueryLetter = function(panel) { + var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + + return _.find(letters, function(refId) { + return _.every(panel.targets, function(other) { + return other.refId !== refId; + }); + }); + }; + + p.addDataQueryTo = function(panel, datasource) { + var target = { + refId: this.getNextQueryLetter(panel) + }; + + if (datasource) { + target.datasource = datasource.name; + } + + panel.targets.push(target); + }; + + p.removeDataQuery = function (panel, query) { + panel.targets = _.without(panel.targets, query); + }; + + p.duplicateDataQuery = function(panel, query) { + var clone = angular.copy(query); + clone.refId = this.getNextQueryLetter(panel); + panel.targets.push(clone); + }; + + p.moveDataQuery = function(panel, fromIndex, toIndex) { + _.move(panel.targets, fromIndex, toIndex); + }; + p.formatDate = function(date, format) { format = format || 'YYYY-MM-DD HH:mm:ss'; return this.timezone === 'browser' ? - moment(date).format(format) : - moment.utc(date).format(format); + moment(date).format(format) : + moment.utc(date).format(format); }; p._updateSchema = function(old) { diff --git a/public/app/features/panel/panelSrv.js b/public/app/features/panel/panelSrv.js index bf8c983c018..d93a4575615 100644 --- a/public/app/features/panel/panelSrv.js +++ b/public/app/features/panel/panelSrv.js @@ -44,27 +44,22 @@ function (angular, _, config) { }; $scope.addDataQuery = function(datasource) { - var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - var target = {}; - - if (datasource) { - target.datasource = datasource.name; - } - - target.refId = _.find(letters, function(refId) { - return _.every($scope.panel.targets, function(other) { - return other.refId !== refId; - }); - }); - - $scope.panel.targets.push(target); + $scope.dashboard.addDataQueryTo($scope.panel, datasource); }; $scope.removeDataQuery = function (query) { - $scope.panel.targets = _.without($scope.panel.targets, query); + $scope.dashboard.removeDataQuery($scope.panel, query); $scope.get_data(); }; + $scope.duplicateDataQuery = function(query) { + $scope.dashboard.duplicateDataQuery($scope.panel, query); + }; + + $scope.moveDataQuery = function(fromIndex, toIndex) { + $scope.dashboard.moveDataQuery($scope.panel, fromIndex, toIndex); + }; + $scope.setDatasource = function(datasource) { // switching to mixed if (datasource.meta.mixed) { diff --git a/public/app/plugins/datasource/graphite/partials/query.editor.html b/public/app/plugins/datasource/graphite/partials/query.editor.html index 3608c2573cb..2de4b67dc6c 100755 --- a/public/app/plugins/datasource/graphite/partials/query.editor.html +++ b/public/app/plugins/datasource/graphite/partials/query.editor.html @@ -20,13 +20,13 @@
  • - Duplicate + Duplicate
  • - Move up + Move up
  • - Move down + Move down
  • diff --git a/public/app/plugins/datasource/graphite/queryCtrl.js b/public/app/plugins/datasource/graphite/queryCtrl.js index 21b1534fbe1..9a3179c5c4b 100644 --- a/public/app/plugins/datasource/graphite/queryCtrl.js +++ b/public/app/plugins/datasource/graphite/queryCtrl.js @@ -284,15 +284,6 @@ function (angular, _, config, gfunc, Parser) { } }; - $scope.moveMetricQuery = function(fromIndex, toIndex) { - _.move($scope.panel.targets, fromIndex, toIndex); - }; - - $scope.duplicate = function() { - var clone = angular.copy($scope.target); - $scope.panel.targets.push(clone); - }; - function MetricSegment(options) { if (options === '*' || options.value === '*') { this.value = '*'; diff --git a/public/app/plugins/datasource/influxdb/partials/query.editor.html b/public/app/plugins/datasource/influxdb/partials/query.editor.html index 379bbf76a27..f5cb4fdda2c 100644 --- a/public/app/plugins/datasource/influxdb/partials/query.editor.html +++ b/public/app/plugins/datasource/influxdb/partials/query.editor.html @@ -16,9 +16,9 @@ diff --git a/public/app/plugins/datasource/influxdb/queryCtrl.js b/public/app/plugins/datasource/influxdb/queryCtrl.js index be87edcad21..0d1a9354b65 100644 --- a/public/app/plugins/datasource/influxdb/queryCtrl.js +++ b/public/app/plugins/datasource/influxdb/queryCtrl.js @@ -116,15 +116,6 @@ function (angular, _, InfluxQueryBuilder) { $scope.target.rawQuery = !$scope.target.rawQuery; }; - $scope.moveMetricQuery = function(fromIndex, toIndex) { - _.move($scope.panel.targets, fromIndex, toIndex); - }; - - $scope.duplicate = function() { - var clone = angular.copy($scope.target); - $scope.panel.targets.push(clone); - }; - $scope.getMeasurements = function () { var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS'); return $scope.datasource.metricFindQuery(query) diff --git a/public/app/plugins/datasource/influxdb_08/partials/query.editor.html b/public/app/plugins/datasource/influxdb_08/partials/query.editor.html index 9c162741896..2498a16eb41 100644 --- a/public/app/plugins/datasource/influxdb_08/partials/query.editor.html +++ b/public/app/plugins/datasource/influxdb_08/partials/query.editor.html @@ -10,9 +10,9 @@ diff --git a/public/app/plugins/datasource/influxdb_08/queryCtrl.js b/public/app/plugins/datasource/influxdb_08/queryCtrl.js index 0cb6b49e993..c6304bc50ae 100644 --- a/public/app/plugins/datasource/influxdb_08/queryCtrl.js +++ b/public/app/plugins/datasource/influxdb_08/queryCtrl.js @@ -1,8 +1,7 @@ define([ 'angular', - 'lodash' ], -function (angular, _) { +function (angular) { 'use strict'; var module = angular.module('grafana.controllers'); @@ -90,15 +89,6 @@ function (angular, _) { } }; - $scope.moveMetricQuery = function(fromIndex, toIndex) { - _.move($scope.panel.targets, fromIndex, toIndex); - }; - - $scope.duplicate = function() { - var clone = angular.copy($scope.target); - $scope.panel.targets.push(clone); - }; - }); }); diff --git a/public/app/plugins/datasource/kairosdb/partials/query.editor.html b/public/app/plugins/datasource/kairosdb/partials/query.editor.html index 7fd1d6527fe..2fa6880abd9 100644 --- a/public/app/plugins/datasource/kairosdb/partials/query.editor.html +++ b/public/app/plugins/datasource/kairosdb/partials/query.editor.html @@ -9,9 +9,9 @@ diff --git a/public/app/plugins/datasource/kairosdb/queryCtrl.js b/public/app/plugins/datasource/kairosdb/queryCtrl.js index 4cf1b8c0f21..86178c934b8 100644 --- a/public/app/plugins/datasource/kairosdb/queryCtrl.js +++ b/public/app/plugins/datasource/kairosdb/queryCtrl.js @@ -37,15 +37,6 @@ function (angular, _) { $scope.get_data(); }; - $scope.duplicate = function() { - var clone = angular.copy($scope.target); - $scope.panel.targets.push(clone); - }; - - $scope.moveMetricQuery = function(fromIndex, toIndex) { - _.move($scope.panel.targets, fromIndex, toIndex); - }; - $scope.getTextValues = function(metricFindResult) { return _.map(metricFindResult, function(value) { return value.text; }); }; diff --git a/public/app/plugins/datasource/opentsdb/partials/query.editor.html b/public/app/plugins/datasource/opentsdb/partials/query.editor.html index 23781365eb0..f2fc4afb181 100644 --- a/public/app/plugins/datasource/opentsdb/partials/query.editor.html +++ b/public/app/plugins/datasource/opentsdb/partials/query.editor.html @@ -9,11 +9,10 @@ diff --git a/public/app/plugins/datasource/opentsdb/queryCtrl.js b/public/app/plugins/datasource/opentsdb/queryCtrl.js index 186f2d2016c..dd2e00aca6f 100644 --- a/public/app/plugins/datasource/opentsdb/queryCtrl.js +++ b/public/app/plugins/datasource/opentsdb/queryCtrl.js @@ -33,11 +33,6 @@ function (angular, _, kbn) { } }; - $scope.duplicate = function() { - var clone = angular.copy($scope.target); - $scope.panel.targets.push(clone); - }; - $scope.getTextValues = function(metricFindResult) { return _.map(metricFindResult, function(value) { return value.text; }); }; diff --git a/public/test/specs/dashboardSrv-specs.js b/public/test/specs/dashboardSrv-specs.js index 27be14c8ca4..4fdb48f4ed1 100644 --- a/public/test/specs/dashboardSrv-specs.js +++ b/public/test/specs/dashboardSrv-specs.js @@ -49,6 +49,39 @@ define([ }); }); + describe('addDataQueryTo', function() { + var dashboard, panel; + + beforeEach(function() { + panel = {targets:[]}; + dashboard = _dashboardSrv.create({}); + dashboard.rows.push({panels: [panel]}); + }); + + it('should add target', function() { + dashboard.addDataQueryTo(panel); + expect(panel.targets.length).to.be(1); + }); + + it('should set refId', function() { + dashboard.addDataQueryTo(panel); + expect(panel.targets[0].refId).to.be('A'); + }); + + it('should set refId to first available letter', function() { + panel.targets = [{refId: 'A'}]; + dashboard.addDataQueryTo(panel); + expect(panel.targets[1].refId).to.be('B'); + }); + + it('duplicate should get unique refId', function() { + panel.targets = [{refId: 'A'}]; + dashboard.duplicateDataQuery(panel, panel.targets[0]); + expect(panel.targets[1].refId).to.be('B'); + }); + + }); + describe('row and panel manipulation', function() { var dashboard; diff --git a/public/test/specs/panelSrv-specs.js b/public/test/specs/panelSrv-specs.js index 52e82379db3..1ea24d14707 100644 --- a/public/test/specs/panelSrv-specs.js +++ b/public/test/specs/panelSrv-specs.js @@ -33,24 +33,6 @@ define([ _panelSrv.init(_panelScope); }); - describe('addDataQuery', function() { - it('should add target', function() { - _panelScope.addDataQuery(); - expect(_panelScope.panel.targets.length).to.be(1); - }); - - it('should set refId', function() { - _panelScope.addDataQuery(); - expect(_panelScope.panel.targets[0].refId).to.be('A'); - }); - - it('should set refId to first available letter', function() { - _panelScope.panel.targets = [{refId: 'A'}]; - _panelScope.addDataQuery(); - expect(_panelScope.panel.targets[1].refId).to.be('B'); - }); - }); - }); });