<commit_before>define([
  'angular',
  'lodash'
],
  function (angular, _) {
    'use strict';

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

    var metricList = null;

    module.controller('KairosDBTargetCtrl', function($scope) {

      $scope.init = function() {
        $scope.panel.stack = false;
        if (!$scope.target.downsampling) {
          $scope.target.downsampling = 'avg';
        }
        $scope.target.errors = validateTarget($scope.target);
      };

      $scope.targetBlur = function() {
        $scope.target.errors = validateTarget($scope.target);
        if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
          $scope.oldTarget = angular.copy($scope.target);
          $scope.get_data();
        }
      };

      $scope.duplicate = function() {
        var clone = angular.copy($scope.target);
        $scope.panel.targets.push(clone);
      };

      //////////////////////////////
      // SUGGESTION QUERIES
      //////////////////////////////

      $scope.suggestMetrics = function(query, callback) {
        if (!metricList) {
          $scope.updateMetricList();
        }
        else {
          callback(metricList);
        }

      };

      $scope.updateMetricList = function() {
        $scope.metricListLoading = true;
        metricList = [];
        $scope.datasource.performMetricSuggestQuery().then(function(series) {
          metricList = series;
          $scope.metricListLoading = false;
          return metricList;
        });
      };

      $scope.suggestTagKeys = function(query, callback) {
        $scope.updateTimeRange();
        callback($scope.datasource
          .performTagSuggestQuery($scope.target.metric,$scope.rangeUnparsed, 'key',''));

      };

      $scope.suggestTagValues = function(query, callback) {
        callback($scope.datasource
          .performTagSuggestQuery($scope.target.metric,$scope.rangeUnparsed, 'value',$scope.target.currentTagKey));
      };

      //////////////////////////////
      // FILTER by TAG
      //////////////////////////////

      $scope.addFilterTag = function() {
        if (!$scope.addFilterTagMode) {
          $scope.addFilterTagMode = true;
          $scope.validateFilterTag();
          return;
        }

        if (!$scope.target.tags) {
          $scope.target.tags = {};
        }

        $scope.validateFilterTag();
        if (!$scope.target.errors.tags) {
          if(!_.has($scope.target.tags,$scope.target.currentTagKey)) {
            $scope.target.tags[$scope.target.currentTagKey] = [];
          }
          $scope.target.tags[$scope.target.currentTagKey].push($scope.target.currentTagValue);
          $scope.target.currentTagKey = '';
          $scope.target.currentTagValue = '';
          $scope.targetBlur();
        }

        $scope.addFilterTagMode = false;
      };

      $scope.removeFilterTag = function(key) {
        delete $scope.target.tags[key];
        if(_.size($scope.target.tags)===0) {
          $scope.target.tags = null;
        }
        $scope.targetBlur();
      };

      $scope.validateFilterTag = function() {<commit_msg>Tag filter list can not have duplicates<commit_after>define([
  'angular',
  'lodash'
],
  function (angular, _) {
    'use strict';

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

    var metricList = null;

    module.controller('KairosDBTargetCtrl', function($scope) {

      $scope.init = function() {
        $scope.panel.stack = false;
        if (!$scope.target.downsampling) {
          $scope.target.downsampling = 'avg';
        }
        $scope.target.errors = validateTarget($scope.target);
      };

      $scope.targetBlur = function() {
        $scope.target.errors = validateTarget($scope.target);
        if (!_.isEqual($scope.oldTarget, $scope.target) && _.isEmpty($scope.target.errors)) {
          $scope.oldTarget = angular.copy($scope.target);
          $scope.get_data();
        }
      };

      $scope.duplicate = function() {
        var clone = angular.copy($scope.target);
        $scope.panel.targets.push(clone);
      };

      //////////////////////////////
      // SUGGESTION QUERIES
      //////////////////////////////

      $scope.suggestMetrics = function(query, callback) {
        if (!metricList) {
          $scope.updateMetricList();
        }
        else {
          callback(metricList);
        }

      };

      $scope.updateMetricList = function() {
        $scope.metricListLoading = true;
        metricList = [];
        $scope.datasource.performMetricSuggestQuery().then(function(series) {
          metricList = series;
          $scope.metricListLoading = false;
          return metricList;
        });
      };

      $scope.suggestTagKeys = function(query, callback) {
        $scope.updateTimeRange();
        callback($scope.datasource
          .performTagSuggestQuery($scope.target.metric,$scope.rangeUnparsed, 'key',''));

      };

      $scope.suggestTagValues = function(query, callback) {
        callback($scope.datasource
          .performTagSuggestQuery($scope.target.metric,$scope.rangeUnparsed, 'value',$scope.target.currentTagKey));
      };

      //////////////////////////////
      // FILTER by TAG
      //////////////////////////////

      $scope.addFilterTag = function() {
        if (!$scope.addFilterTagMode) {
          $scope.addFilterTagMode = true;
          $scope.validateFilterTag();
          return;
        }

        if (!$scope.target.tags) {
          $scope.target.tags = {};
        }

        $scope.validateFilterTag();
        if (!$scope.target.errors.tags) {
          if(!_.has($scope.target.tags,$scope.target.currentTagKey)) {
            $scope.target.tags[$scope.target.currentTagKey] = [];
          }
          if (!_.contains($scope.target.tags[$scope.target.currentTagKey], $scope.target.currentTagValue)) {
            $scope.target.tags[$scope.target.currentTagKey].push($scope.target.currentTagValue);
            $scope.targetBlur();
          }
          $scope.target.currentTagKey = '';
          $scope.target.currentTagValue = '';
        }

        $scope.addFilterTagMode = false;
      };

      $scope.removeFilterTag = function(key) {
        delete $scope.target.tags[key];
        if(_.size($scope.target.tags)===0) {
          $scope.target.tags = null;
        }
        $scope.targetBlur();
      };

      $scope.validateFilterTag = function() {
{'fraction_non_alphanumeric': 0.1769003575314783, 'fraction_numerical': 0.000310896937665164, 'mean_word_length': 1.8968932913102206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4203168', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1764, 'n_words': 233}