From 9ee4fcb36cb646a0f973d3a1046a739c8efa1941 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Wed, 27 Aug 2014 17:58:49 +0200 Subject: [PATCH] continued large refactoring of filterSrv, timeSrv and templating --- src/app/controllers/dashboardCtrl.js | 7 - src/app/controllers/submenuCtrl.js | 63 +-------- src/app/panels/text/module.js | 4 +- src/app/panels/timepicker/module.html | 6 +- src/app/panels/timepicker/module.js | 4 +- src/app/partials/submenu.html | 2 +- src/app/services/all.js | 1 + src/app/services/dashboard/dashboardSrv.js | 24 +--- .../services/graphite/graphiteDatasource.js | 12 +- .../services/influxdb/influxdbDatasource.js | 10 +- src/app/services/panelSrv.js | 7 - src/app/services/templateSrv.js | 36 ++++- src/app/services/templateValuesSrv.js | 72 ++++++++++ src/app/services/timeSrv.js | 128 +++++++++--------- src/test/mocks/dashboard-mock.js | 7 +- src/test/specs/helpers.js | 2 +- src/test/specs/templateSrv-specs.js | 55 ++++++++ src/test/specs/timeSrv-specs.js | 38 +----- src/test/test-main.js | 1 + 19 files changed, 261 insertions(+), 218 deletions(-) create mode 100644 src/app/services/templateValuesSrv.js create mode 100644 src/test/specs/templateSrv-specs.js diff --git a/src/app/controllers/dashboardCtrl.js b/src/app/controllers/dashboardCtrl.js index 3f113ab3a8c..03cdddfbf7b 100644 --- a/src/app/controllers/dashboardCtrl.js +++ b/src/app/controllers/dashboardCtrl.js @@ -41,8 +41,6 @@ function (angular, $, config, _) { }; $scope.setupDashboard = function(event, dashboardData) { - timer.cancel_all(); - $rootScope.performance.dashboardLoadStart = new Date().getTime(); $rootScope.performance.panelsInitialized = 0; $rootScope.performance.panelsRendered= 0; @@ -67,11 +65,6 @@ function (angular, $, config, _) { window.document.title = config.window_title_prefix + $scope.dashboard.title; - // start auto refresh - if($scope.dashboard.refresh) { - $scope.dashboard.set_interval($scope.dashboard.refresh); - } - dashboardKeybindings.shortcuts($scope); $scope.emitAppEvent("dashboard-loaded", $scope.dashboard); diff --git a/src/app/controllers/submenuCtrl.js b/src/app/controllers/submenuCtrl.js index edaab142eb3..dcc147d49d9 100644 --- a/src/app/controllers/submenuCtrl.js +++ b/src/app/controllers/submenuCtrl.js @@ -8,7 +8,7 @@ function (angular, app, _) { var module = angular.module('grafana.controllers'); - module.controller('SubmenuCtrl', function($scope, $q, $rootScope, datasourceSrv) { + module.controller('SubmenuCtrl', function($scope, $q, $rootScope, templateValuesSrv) { var _d = { enable: true }; @@ -18,62 +18,7 @@ function (angular, app, _) { $scope.init = function() { $scope.panel = $scope.pulldown; $scope.row = $scope.pulldown; - }; - - $scope.filterOptionSelected = function(templateParameter, option, recursive) { - templateParameter.current = option; - - $scope.filter.updateTemplateData(); - - return $scope.applyFilterToOtherFilters(templateParameter) - .then(function() { - // only refresh in the outermost call - if (!recursive) { - $scope.dashboard.emit_refresh(); - } - }); - }; - - $scope.applyFilterToOtherFilters = function(updatedTemplatedParam) { - var promises = _.map($scope.filter.templateParameters, function(templateParam) { - if (templateParam === updatedTemplatedParam) { - return; - } - if (templateParam.query.indexOf('[[' + updatedTemplatedParam.name + ']]') !== -1) { - return $scope.applyFilter(templateParam); - } - }); - - return $q.all(promises); - }; - - $scope.applyFilter = function(templateParam) { - return datasourceSrv.default.metricFindQuery($scope.filter, templateParam.query) - .then(function (results) { - templateParam.options = _.map(results, function(node) { - return { text: node.text, value: node.text }; - }); - - if (templateParam.includeAll) { - var allExpr = '{'; - _.each(templateParam.options, function(option) { - allExpr += option.text + ','; - }); - allExpr = allExpr.substring(0, allExpr.length - 1) + '}'; - templateParam.options.unshift({text: 'All', value: allExpr}); - } - - // if parameter has current value - // if it exists in options array keep value - if (templateParam.current) { - var currentExists = _.findWhere(templateParam.options, { value: templateParam.current.value }); - if (currentExists) { - return $scope.filterOptionSelected(templateParam, templateParam.current, true); - } - } - - return $scope.filterOptionSelected(templateParam, templateParam.options[0], true); - }); + $scope.templateParameters = $scope.dashboard.templating.list; }; $scope.disableAnnotation = function (annotation) { @@ -81,6 +26,10 @@ function (angular, app, _) { $rootScope.$broadcast('refresh'); }; + $scope.filterOptionSelected = function(param, option) { + templateValuesSrv.filterOptionSelected(param, option); + }; + $scope.init(); }); diff --git a/src/app/panels/text/module.js b/src/app/panels/text/module.js index c3aabba2804..e652b40a56b 100644 --- a/src/app/panels/text/module.js +++ b/src/app/panels/text/module.js @@ -12,7 +12,7 @@ function (angular, app, _, require) { var converter; - module.controller('text', function($scope, filterSrv, $sce, panelSrv) { + module.controller('text', function($scope, templateSrv, $sce, panelSrv) { $scope.panelMeta = { description : "A static text panel that can use plain text, markdown, or (sanitized) HTML" @@ -75,7 +75,7 @@ function (angular, app, _, require) { $scope.updateContent = function(html) { try { - $scope.content = $sce.trustAsHtml(filterSrv.applyTemplateToTarget(html)); + $scope.content = $sce.trustAsHtml(templateSrv.replace(html)); } catch(e) { console.log('Text panel error: ', e); $scope.content = $sce.trustAsHtml(html); diff --git a/src/app/panels/timepicker/module.html b/src/app/panels/timepicker/module.html index 0eacee58d4b..8357f66a7a3 100644 --- a/src/app/panels/timepicker/module.html +++ b/src/app/panels/timepicker/module.html @@ -32,10 +32,10 @@ Auto-Refresh @@ -44,7 +44,7 @@
  • - +
  • diff --git a/src/app/panels/timepicker/module.js b/src/app/panels/timepicker/module.js index 7dcd222e4cd..656af0898a0 100644 --- a/src/app/panels/timepicker/module.js +++ b/src/app/panels/timepicker/module.js @@ -25,7 +25,7 @@ function (angular, app, _, moment, kbn) { var module = angular.module('grafana.panels.timepicker', []); app.useModule(module); - module.controller('timepicker', function($scope, timeSrv) { + module.controller('timepicker', function($scope, $rootScope, timeSrv) { $scope.panelMeta = { status : "Stable", @@ -50,6 +50,8 @@ function (angular, app, _, moment, kbn) { millisecond: /^[0-9]*$/ }; + $scope.timeSrv = timeSrv; + $scope.$on('refresh', function() { $scope.init(); }); diff --git a/src/app/partials/submenu.html b/src/app/partials/submenu.html index 4d9d6b10125..2d5c3e1831d 100644 --- a/src/app/partials/submenu.html +++ b/src/app/partials/submenu.html @@ -18,7 +18,7 @@