<commit_before>define([
  'angular',
  'lodash',
  'app/core/utils/kbn',
],
function (angular, _, kbn) {
  'use strict';

  var module = angular.module('grafana.services');

  module.service('templateValuesSrv', function($q, $rootScope, datasourceSrv, $location, templateSrv, timeSrv) {
    var self = this;

    function getNoneOption() { return { text: 'None', value: '', isNone: true }; }

    // update time variant variables
    $rootScope.onAppEvent('refresh', function() {

      // look for interval variables
      var intervalVariable = _.findWhere(self.variables, { type: 'interval' });
      if (intervalVariable) {
        self.updateAutoInterval(intervalVariable);
      }

      // update variables with refresh === 2
      var promises = self.variables
        .filter(function(variable) {
          return variable.refresh === 2;
        }).map(function(variable) {
          return self.updateOptions(variable);
        });

      return $q.all(promises);

    }, $rootScope);

    this.init = function(dashboard) {
      this.variables = dashboard.templating.list;
      templateSrv.init(this.variables);

      var queryParams = $location.search();
      var promises = [];

      // use promises to delay processing variables that
      // depend on other variables.
      this.variableLock = {};
      _.forEach(this.variables, function(variable) {
        self.variableLock[variable.name] = $q.defer();
      });

      for (var i = 0; i < this.variables.length; i++) {
        var variable = this.variables[i];
        promises.push(this.processVariable(variable, queryParams));
      }

      return $q.all(promises);
    };

    this.processVariable = function(variable, queryParams) {
      var dependencies = [];
      var lock = self.variableLock[variable.name];

      // determine our dependencies.
      if (variable.type === "query") {
        _.forEach(this.variables, function(v) {
          // both query and datasource can contain variable
          if (templateSrv.containsVariable(variable.query, v.name) ||
              templateSrv.containsVariable(variable.datasource, v.name)) {
            dependencies.push(self.variableLock[v.name].promise);
          }
        });
      }

      return $q.all(dependencies).then(function() {
        var urlValue = queryParams['var-' + variable.name];
        if (urlValue !== void 0) {
          return self.setVariableFromUrl(variable, urlValue).then(lock.resolve);
        }
        else if (variable.refresh === 1 || variable.refresh === 2) {
          return self.updateOptions(variable).then(function() {
            if (_.isEmpty(variable.current) && variable.options.length) {
              console.log("setting current for %s", variable.name);
              self.setVariableValue(variable, variable.options[0]);
            }
            lock.resolve();
          });
        }
        else if (variable.type === 'interval') {
          self.updateAutoInterval(variable);
          lock.resolve();
        } else {
          lock.resolve();
        }
      });
    };

    this.setVariableFromUrl = function(variable, urlValue) {
      var promise = $q.when(true);

      if (variable.refresh) {
        promise = this.updateOptions(variable);
      }

      return promise.then(function() {
        var option = _.findWhere(variable.options, { text: urlValue });
        option = option || { text: urlValue, value: urlValue };

        self.updateAutoInterval(variable);
        return self.setVariableValue(variable, option, true);
      });
    };

    this.updateAutoInterval = function(variable) {
      if (!variable.auto) { return; }

      // add auto option if missing
      if (variable.options.length && variable.options[0].text !== 'auto') {
        variable.options.unshift({ text: 'auto', value: '$__auto_interval' });
      }

      var interval = kbn.calculateInterval(timeSrv.timeRange(), variable.auto_count, (variable.auto_min ? ">"+variable.auto_min : null));
      templateSrv.setGrafanaVariable('$__auto_interval', interval);
    };

    this.setVariableValue = function(variable, option, initPhase) {
      variable.current = angular.copy(option);

      if (_.isArray(variable.current.value)) {
        variable.current.text = variable.current.value.join(' + ');
      }

      self.selectOptionsForCurrentValue(variable);
      templateSrv.updateTemplateData();

      // on first load, variable loading is ordered to ensure
      // that parents are updated before children.
      if (initPhase) {
        return $q.when();
      }

      return self.updateOptionsInChildVariables(variable);
    };

    this.variableUpdated = function(variable) {
      templateSrv.updateTemplateData();
      return this.updateOptionsInChildVariables(variable);
    };

    this.updateOptionsInChildVariables = function(updatedVariable) {
      var promises = _.map(self.variables, function(otherVariable) {
        if (otherVariable === updatedVariable) {
          return;
        }
        if (templateSrv.containsVariable(otherVariable.query, updatedVariable.name) ||
            templateSrv.containsVariable(otherVariable.datasource, updatedVariable.name)) {
          return self.updateOptions(otherVariable);<commit_msg>reflect panel repeat status when variable updated<commit_after>define([
  'angular',
  'lodash',
  'app/core/utils/kbn',
],
function (angular, _, kbn) {
  'use strict';

  var module = angular.module('grafana.services');

  module.service('templateValuesSrv', function($q, $rootScope, datasourceSrv, $location, templateSrv, timeSrv, dynamicDashboardSrv) {
    var self = this;

    function getNoneOption() { return { text: 'None', value: '', isNone: true }; }

    // update time variant variables
    $rootScope.onAppEvent('refresh', function() {

      // look for interval variables
      var intervalVariable = _.findWhere(self.variables, { type: 'interval' });
      if (intervalVariable) {
        self.updateAutoInterval(intervalVariable);
      }

      // update variables with refresh === 2
      var promises = self.variables
        .filter(function(variable) {
          return variable.refresh === 2;
        }).map(function(variable) {
          return self.updateOptions(variable).then(function () {
            return self.variableUpdated(variable).then(function () {
              dynamicDashboardSrv.update(self.dashboard);
              $rootScope.$emit('template-variable-value-updated');
            });
          });
        });

      return $q.all(promises);

    }, $rootScope);

    this.init = function(dashboard) {
      this.dashboard = dashboard;
      this.variables = dashboard.templating.list;
      templateSrv.init(this.variables);

      var queryParams = $location.search();
      var promises = [];

      // use promises to delay processing variables that
      // depend on other variables.
      this.variableLock = {};
      _.forEach(this.variables, function(variable) {
        self.variableLock[variable.name] = $q.defer();
      });

      for (var i = 0; i < this.variables.length; i++) {
        var variable = this.variables[i];
        promises.push(this.processVariable(variable, queryParams));
      }

      return $q.all(promises);
    };

    this.processVariable = function(variable, queryParams) {
      var dependencies = [];
      var lock = self.variableLock[variable.name];

      // determine our dependencies.
      if (variable.type === "query") {
        _.forEach(this.variables, function(v) {
          // both query and datasource can contain variable
          if (templateSrv.containsVariable(variable.query, v.name) ||
              templateSrv.containsVariable(variable.datasource, v.name)) {
            dependencies.push(self.variableLock[v.name].promise);
          }
        });
      }

      return $q.all(dependencies).then(function() {
        var urlValue = queryParams['var-' + variable.name];
        if (urlValue !== void 0) {
          return self.setVariableFromUrl(variable, urlValue).then(lock.resolve);
        }
        else if (variable.refresh === 1 || variable.refresh === 2) {
          return self.updateOptions(variable).then(function() {
            if (_.isEmpty(variable.current) && variable.options.length) {
              console.log("setting current for %s", variable.name);
              self.setVariableValue(variable, variable.options[0]);
            }
            lock.resolve();
          });
        }
        else if (variable.type === 'interval') {
          self.updateAutoInterval(variable);
          lock.resolve();
        } else {
          lock.resolve();
        }
      });
    };

    this.setVariableFromUrl = function(variable, urlValue) {
      var promise = $q.when(true);

      if (variable.refresh) {
        promise = this.updateOptions(variable);
      }

      return promise.then(function() {
        var option = _.findWhere(variable.options, { text: urlValue });
        option = option || { text: urlValue, value: urlValue };

        self.updateAutoInterval(variable);
        return self.setVariableValue(variable, option, true);
      });
    };

    this.updateAutoInterval = function(variable) {
      if (!variable.auto) { return; }

      // add auto option if missing
      if (variable.options.length && variable.options[0].text !== 'auto') {
        variable.options.unshift({ text: 'auto', value: '$__auto_interval' });
      }

      var interval = kbn.calculateInterval(timeSrv.timeRange(), variable.auto_count, (variable.auto_min ? ">"+variable.auto_min : null));
      templateSrv.setGrafanaVariable('$__auto_interval', interval);
    };

    this.setVariableValue = function(variable, option, initPhase) {
      variable.current = angular.copy(option);

      if (_.isArray(variable.current.value)) {
        variable.current.text = variable.current.value.join(' + ');
      }

      self.selectOptionsForCurrentValue(variable);
      templateSrv.updateTemplateData();

      // on first load, variable loading is ordered to ensure
      // that parents are updated before children.
      if (initPhase) {
        return $q.when();
      }

      return self.updateOptionsInChildVariables(variable);
    };

    this.variableUpdated = function(variable) {
      templateSrv.updateTemplateData();
      return self.updateOptionsInChildVariables(variable);
    };

    this.updateOptionsInChildVariables = function(updatedVariable) {
      var promises = _.map(self.variables, function(otherVariable) {
        if (otherVariable === updatedVariable) {
          return;
        }
        if (templateSrv.containsVariable(otherVariable.query, updatedVariable.name) ||
            templateSrv.containsVariable(otherVariable.datasource, updatedVariable.name)) {
          return self.updateOptions(otherVariable);
{'fraction_non_alphanumeric': 0.12902029845212717, 'fraction_numerical': 0.0014829919362313467, 'mean_word_length': 2.6147403685092128, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1883245', 'n_tokens_mistral': 2936, 'n_tokens_neox': 2858, 'n_words': 584}