Commit a053552f by Torkel Ödegaard

fixes to data source query editors

parent 6886faa9
...@@ -9,7 +9,7 @@ function metricsQueryEditor(dynamicDirectiveSrv, datasourceSrv) { ...@@ -9,7 +9,7 @@ function metricsQueryEditor(dynamicDirectiveSrv, datasourceSrv) {
directive: scope => { directive: scope => {
let datasource = scope.target.datasource || scope.ctrl.panel.datasource; let datasource = scope.target.datasource || scope.ctrl.panel.datasource;
return datasourceSrv.get(datasource).then(ds => { return datasourceSrv.get(datasource).then(ds => {
scope.ctrl.datasource = ds; scope.datasource = ds;
if (!scope.target.refId) { if (!scope.target.refId) {
scope.target.refId = 'A'; scope.target.refId = 'A';
......
...@@ -12,6 +12,7 @@ function (angular, _, config, gfunc, Parser) { ...@@ -12,6 +12,7 @@ function (angular, _, config, gfunc, Parser) {
module.controller('GraphiteQueryCtrl', function($scope, uiSegmentSrv, templateSrv) { module.controller('GraphiteQueryCtrl', function($scope, uiSegmentSrv, templateSrv) {
var panelCtrl = $scope.panelCtrl = $scope.ctrl; var panelCtrl = $scope.panelCtrl = $scope.ctrl;
var datasource = $scope.datasource;
$scope.init = function() { $scope.init = function() {
if ($scope.target) { if ($scope.target) {
...@@ -126,7 +127,7 @@ function (angular, _, config, gfunc, Parser) { ...@@ -126,7 +127,7 @@ function (angular, _, config, gfunc, Parser) {
} }
var path = getSegmentPathUpTo(fromIndex + 1); var path = getSegmentPathUpTo(fromIndex + 1);
return panelCtrl.datasource.metricFindQuery(path) return datasource.metricFindQuery(path)
.then(function(segments) { .then(function(segments) {
if (segments.length === 0) { if (segments.length === 0) {
if (path !== '') { if (path !== '') {
...@@ -160,7 +161,7 @@ function (angular, _, config, gfunc, Parser) { ...@@ -160,7 +161,7 @@ function (angular, _, config, gfunc, Parser) {
$scope.getAltSegments = function (index) { $scope.getAltSegments = function (index) {
var query = index === 0 ? '*' : getSegmentPathUpTo(index) + '.*'; var query = index === 0 ? '*' : getSegmentPathUpTo(index) + '.*';
return panelCtrl.datasource.metricFindQuery(query).then(function(segments) { return datasource.metricFindQuery(query).then(function(segments) {
var altSegments = _.map(segments, function(segment) { var altSegments = _.map(segments, function(segment) {
return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable }); return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
}); });
......
...@@ -19,14 +19,13 @@ describe('GraphiteQueryCtrl', function() { ...@@ -19,14 +19,13 @@ describe('GraphiteQueryCtrl', function() {
ctx.scope = $rootScope.$new(); ctx.scope = $rootScope.$new();
ctx.scope.ctrl = {panel: ctx.panel}; ctx.scope.ctrl = {panel: ctx.panel};
ctx.panelCtrl = ctx.scope.ctrl; ctx.panelCtrl = ctx.scope.ctrl;
ctx.scope.datasource = ctx.datasource;
ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
ctx.controller = $controller('GraphiteQueryCtrl', {$scope: ctx.scope}); ctx.controller = $controller('GraphiteQueryCtrl', {$scope: ctx.scope});
})); }));
beforeEach(function() { beforeEach(function() {
ctx.scope.target = {target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)'}; ctx.scope.target = {target: 'aliasByNode(scaleToSeconds(test.prod.*,1),2)'};
ctx.panelCtrl.datasource = ctx.datasource;
ctx.panelCtrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
}); });
describe('init', function() { describe('init', function() {
...@@ -36,7 +35,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -36,7 +35,7 @@ describe('GraphiteQueryCtrl', function() {
}); });
it('should validate metric key exists', function() { it('should validate metric key exists', function() {
expect(ctx.panelCtrl.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*'); expect(ctx.scope.datasource.metricFindQuery.getCall(0).args[0]).to.be('test.prod.*');
}); });
it('should delete last segment if no metrics are found', function() { it('should delete last segment if no metrics are found', function() {
...@@ -51,7 +50,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -51,7 +50,7 @@ describe('GraphiteQueryCtrl', function() {
describe('when adding function', function() { describe('when adding function', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target.target = 'test.prod.*.count'; ctx.scope.target.target = 'test.prod.*.count';
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.$digest(); ctx.scope.$digest();
...@@ -75,7 +74,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -75,7 +74,7 @@ describe('GraphiteQueryCtrl', function() {
describe('when adding function before any metric segment', function() { describe('when adding function before any metric segment', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target.target = ''; ctx.scope.target.target = '';
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: true}])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: true}]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.$digest(); ctx.scope.$digest();
ctx.scope.addFunction(gfunc.getFuncDef('asPercent')); ctx.scope.addFunction(gfunc.getFuncDef('asPercent'));
...@@ -89,7 +88,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -89,7 +88,7 @@ describe('GraphiteQueryCtrl', function() {
describe('when initalizing target without metric expression and only function', function() { describe('when initalizing target without metric expression and only function', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target.target = 'asPercent(#A, #B)'; ctx.scope.target.target = 'asPercent(#A, #B)';
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.$digest(); ctx.scope.$digest();
}); });
...@@ -107,7 +106,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -107,7 +106,7 @@ describe('GraphiteQueryCtrl', function() {
describe('when initializing a target with single param func using variable', function() { describe('when initializing a target with single param func using variable', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target.target = 'movingAverage(prod.count, $var)'; ctx.scope.target.target = 'movingAverage(prod.count, $var)';
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.$digest(); ctx.scope.$digest();
}); });
...@@ -125,7 +124,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -125,7 +124,7 @@ describe('GraphiteQueryCtrl', function() {
describe('when initalizing target without metric expression and function with series-ref', function() { describe('when initalizing target without metric expression and function with series-ref', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target.target = 'asPercent(metric.node.count, #A)'; ctx.scope.target.target = 'asPercent(metric.node.count, #A)';
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.$digest(); ctx.scope.$digest();
ctx.scope.$parent = { get_data: sinon.spy() }; ctx.scope.$parent = { get_data: sinon.spy() };
...@@ -143,7 +142,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -143,7 +142,7 @@ describe('GraphiteQueryCtrl', function() {
describe('when getting altSegments and metricFindQuery retuns empty array', function() { describe('when getting altSegments and metricFindQuery retuns empty array', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target.target = 'test.count'; ctx.scope.target.target = 'test.count';
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.getAltSegments(1).then(function(results) { ctx.scope.getAltSegments(1).then(function(results) {
ctx.altSegments = results; ctx.altSegments = results;
...@@ -159,7 +158,7 @@ describe('GraphiteQueryCtrl', function() { ...@@ -159,7 +158,7 @@ describe('GraphiteQueryCtrl', function() {
describe('targetChanged', function() { describe('targetChanged', function() {
beforeEach(function() { beforeEach(function() {
ctx.panelCtrl.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}])); ctx.scope.datasource.metricFindQuery.returns(ctx.$q.when([{expandable: false}]));
ctx.scope.init(); ctx.scope.init();
ctx.scope.$digest(); ctx.scope.$digest();
......
...@@ -16,15 +16,15 @@ ...@@ -16,15 +16,15 @@
</a> </a>
<ul class="dropdown-menu pull-right" role="menu"> <ul class="dropdown-menu pull-right" role="menu">
<li role="menuitem"><a tabindex="1" ng-click="toggleQueryMode()">Switch editor mode</a></li> <li role="menuitem"><a tabindex="1" ng-click="toggleQueryMode()">Switch editor mode</a></li>
<li role="menuitem"><a tabindex="1" ng-click="duplicateDataQuery(target)">Duplicate</a></li> <li role="menuitem"><a tabindex="1" ng-click="panelCtrl.duplicateDataQuery(target)">Duplicate</a></li>
<li role="menuitem"><a tabindex="1" ng-click="moveDataQuery($index, $index-1)">Move up</a></li> <li role="menuitem"><a tabindex="1" ng-click="panelCtrl.moveDataQuery($index, $index-1)">Move up</a></li>
<li role="menuitem"><a tabindex="1" ng-click="moveDataQuery($index, $index+1)">Move down</a></li> <li role="menuitem"><a tabindex="1" ng-click="panelCtrl.moveDataQuery($index, $index+1)">Move down</a></li>
</ul> </ul>
</div> </div>
</li> </li>
<li class="tight-form-item last"> <li class="tight-form-item last">
<a class="pointer" tabindex="1" ng-click="removeDataQuery(target)"> <a class="pointer" tabindex="1" ng-click="panelCtrl.removeDataQuery(target)">
<i class="fa fa-remove"></i> <i class="fa fa-remove"></i>
</a> </a>
</li> </li>
......
...@@ -16,6 +16,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -16,6 +16,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
module.controller('InfluxQueryCtrl', function($scope, templateSrv, $q, uiSegmentSrv) { module.controller('InfluxQueryCtrl', function($scope, templateSrv, $q, uiSegmentSrv) {
var panelCtrl = $scope.ctrl; var panelCtrl = $scope.ctrl;
var datasource = $scope.datasource;
$scope.panelCtrl = panelCtrl; $scope.panelCtrl = panelCtrl;
$scope.init = function() { $scope.init = function() {
...@@ -23,7 +24,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -23,7 +24,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
$scope.target = $scope.target; $scope.target = $scope.target;
$scope.queryModel = new InfluxQuery($scope.target); $scope.queryModel = new InfluxQuery($scope.target);
$scope.queryBuilder = new InfluxQueryBuilder($scope.target, panelCtrl.datasource.database); $scope.queryBuilder = new InfluxQueryBuilder($scope.target, datasource.database);
$scope.groupBySegment = uiSegmentSrv.newPlusButton(); $scope.groupBySegment = uiSegmentSrv.newPlusButton();
$scope.resultFormats = [ $scope.resultFormats = [
{text: 'Time series', value: 'time_series'}, {text: 'Time series', value: 'time_series'},
...@@ -77,7 +78,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -77,7 +78,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
$scope.getGroupByOptions = function() { $scope.getGroupByOptions = function() {
var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS'); var query = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
return panelCtrl.datasource.metricFindQuery(query) return datasource.metricFindQuery(query)
.then(function(tags) { .then(function(tags) {
var options = []; var options = [];
if (!$scope.queryModel.hasFill()) { if (!$scope.queryModel.hasFill()) {
...@@ -137,7 +138,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -137,7 +138,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
$scope.getPolicySegments = function() { $scope.getPolicySegments = function() {
var policiesQuery = $scope.queryBuilder.buildExploreQuery('RETENTION POLICIES'); var policiesQuery = $scope.queryBuilder.buildExploreQuery('RETENTION POLICIES');
return panelCtrl.datasource.metricFindQuery(policiesQuery) return datasource.metricFindQuery(policiesQuery)
.then($scope.transformToSegments(false)) .then($scope.transformToSegments(false))
.then(null, $scope.handleQueryError); .then(null, $scope.handleQueryError);
}; };
...@@ -153,19 +154,19 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -153,19 +154,19 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
$scope.getMeasurements = function () { $scope.getMeasurements = function () {
var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS'); var query = $scope.queryBuilder.buildExploreQuery('MEASUREMENTS');
return panelCtrl.datasource.metricFindQuery(query) return datasource.metricFindQuery(query)
.then($scope.transformToSegments(true), $scope.handleQueryError); .then($scope.transformToSegments(true), $scope.handleQueryError);
}; };
$scope.getPartOptions = function(part) { $scope.getPartOptions = function(part) {
if (part.def.type === 'field') { if (part.def.type === 'field') {
var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS'); var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
return panelCtrl.datasource.metricFindQuery(fieldsQuery) return datasource.metricFindQuery(fieldsQuery)
.then($scope.transformToSegments(true), $scope.handleQueryError); .then($scope.transformToSegments(true), $scope.handleQueryError);
} }
if (part.def.type === 'tag') { if (part.def.type === 'tag') {
var tagsQuery = $scope.queryBuilder.buildExploreQuery('TAG_KEYS'); var tagsQuery = $scope.queryBuilder.buildExploreQuery('TAG_KEYS');
return panelCtrl.datasource.metricFindQuery(tagsQuery) return datasource.metricFindQuery(tagsQuery)
.then($scope.transformToSegments(true), $scope.handleQueryError); .then($scope.transformToSegments(true), $scope.handleQueryError);
} }
}; };
...@@ -213,7 +214,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -213,7 +214,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
addTemplateVars = true; addTemplateVars = true;
} }
return panelCtrl.datasource.metricFindQuery(query) return datasource.metricFindQuery(query)
.then($scope.transformToSegments(addTemplateVars)) .then($scope.transformToSegments(addTemplateVars))
.then(function(results) { .then(function(results) {
if (segment.type === 'key') { if (segment.type === 'key') {
...@@ -226,7 +227,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) { ...@@ -226,7 +227,7 @@ function (angular, _, InfluxQueryBuilder, InfluxQuery, queryPart) {
$scope.getFieldSegments = function() { $scope.getFieldSegments = function() {
var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS'); var fieldsQuery = $scope.queryBuilder.buildExploreQuery('FIELDS');
return panelCtrl.datasource.metricFindQuery(fieldsQuery) return datasource.metricFindQuery(fieldsQuery)
.then($scope.transformToSegments(false)) .then($scope.transformToSegments(false))
.then(null, $scope.handleQueryError); .then(null, $scope.handleQueryError);
}; };
......
...@@ -15,6 +15,8 @@ describe('InfluxDBQueryCtrl', function() { ...@@ -15,6 +15,8 @@ describe('InfluxDBQueryCtrl', function() {
ctx.$q = $q; ctx.$q = $q;
ctx.scope = $rootScope.$new(); ctx.scope = $rootScope.$new();
ctx.scope.ctrl = {panel: ctx.panel}; ctx.scope.ctrl = {panel: ctx.panel};
ctx.scope.datasource = ctx.datasource;
ctx.scope.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
ctx.panelCtrl = ctx.scope.ctrl; ctx.panelCtrl = ctx.scope.ctrl;
ctx.controller = $controller('InfluxQueryCtrl', {$scope: ctx.scope}); ctx.controller = $controller('InfluxQueryCtrl', {$scope: ctx.scope});
})); }));
...@@ -22,8 +24,6 @@ describe('InfluxDBQueryCtrl', function() { ...@@ -22,8 +24,6 @@ describe('InfluxDBQueryCtrl', function() {
beforeEach(function() { beforeEach(function() {
ctx.scope.target = {}; ctx.scope.target = {};
ctx.panelCtrl.refresh = sinon.spy(); ctx.panelCtrl.refresh = sinon.spy();
ctx.panelCtrl.datasource = ctx.datasource;
ctx.panelCtrl.datasource.metricFindQuery = sinon.stub().returns(ctx.$q.when([]));
}); });
describe('init', function() { describe('init', function() {
......
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