Torkel Ödegaard 10 лет назад
Родитель
Сommit
5de499c7f6

+ 6 - 0
src/app/features/dashboard/dynamicDashboardSrv.js

@@ -33,9 +33,15 @@ function (angular, _) {
         return;
       }
 
+      dashboard.scopedVars = {
+        panel: {}
+      };
+
       _.each(variable.options, function(option) {
         var copy = dashboard.duplicatePanel(panel, row);
         copy.repeat = null;
+        dashboard.scopedVars.panel[panel.id] = {};
+        dashboard.scopedVars.panel[panel.id][variable.name] = option.value;
         console.log('duplicatePanel');
       });
     };

+ 1 - 0
src/app/features/panel/panelHelper.js

@@ -8,6 +8,7 @@ function (angular, _, kbn, $) {
   'use strict';
 
   var module = angular.module('grafana.services');
+
   module.service('panelHelper', function(timeSrv) {
 
     this.updateTimeRange = function(scope) {

+ 6 - 1
src/app/features/templating/templateSrv.js

@@ -63,13 +63,18 @@ function (angular, _) {
       });
     };
 
-    this.replace = function(target) {
+    this.replace = function(target, scopedVars) {
       if (!target) { return; }
 
       var value;
       this._regex.lastIndex = 0;
 
       return target.replace(this._regex, function(match, g1, g2) {
+        if (scopedVars) {
+          value = scopedVars[g1 || g2];
+          if (value) { return value; }
+        }
+
         value = self._values[g1 || g2];
         if (!value) { return match; }
 

+ 3 - 3
src/app/plugins/datasource/graphite/datasource.js

@@ -36,7 +36,7 @@ function (angular, _, $, config, kbn, moment) {
           maxDataPoints: options.maxDataPoints,
         };
 
-        var params = this.buildGraphiteParams(graphOptions);
+        var params = this.buildGraphiteParams(graphOptions, options.panelId);
 
         if (options.format === 'png') {
           return $q.when(this.url + '/render' + '?' + params.join('&'));
@@ -231,7 +231,7 @@ function (angular, _, $, config, kbn, moment) {
       '#Y', '#Z'
     ];
 
-    GraphiteDatasource.prototype.buildGraphiteParams = function(options) {
+    GraphiteDatasource.prototype.buildGraphiteParams = function(options, panelId) {
       var graphite_options = ['from', 'until', 'rawData', 'format', 'maxDataPoints', 'cacheTimeout'];
       var clean_options = [], targets = {};
       var target, targetValue, i;
@@ -252,7 +252,7 @@ function (angular, _, $, config, kbn, moment) {
           continue;
         }
 
-        targetValue = templateSrv.replace(target.target);
+        targetValue = templateSrv.replace(target.target, panelId);
         targetValue = targetValue.replace(intervalFormatFixRegex, fixIntervalFormat);
         targets[this._seriesRefLetters[i]] = targetValue;
       }

+ 12 - 0
src/test/specs/templateSrv-specs.js

@@ -29,6 +29,18 @@ define([
       });
     });
 
+    describe('replace can pass scoped vars', function() {
+      beforeEach(function() {
+        _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);
+      });
+
+      it('should replace $test with scoped value', function() {
+        var target = _templateSrv.replace('this.$test.filters', {'test': 'mupp'});
+        expect(target).to.be('this.mupp.filters');
+      });
+    });
+
+
     describe('can check if variable exists', function() {
       beforeEach(function() {
         _templateSrv.init([{ name: 'test', current: { value: 'oogle' } }]);