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

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

    module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) {

      var zabbixCache = $scope.datasource.zabbixCache;

      $scope.init = function () {
        $scope.targetLetters = targetLetters;
        $scope.metric = {};

        // Load default values
        var targetDefaults = {
          mode: 0,
          group: { filter: "" },
          host: { filter: "" },
          application: { filter: "" },
          item: { filter: "" }
        };
        _.defaults($scope.target, targetDefaults);

        if ($scope.target.mode ===0 ||
            $scope.target.mode === 2) {

          $scope.downsampleFunctionList = [
            {name: "avg", value: "avg"},
            {name: "min", value: "min"},
            {name: "max", value: "max"}
          ];

          // Set avg by default
          if (!$scope.target.downsampleFunction) {
            $scope.target.downsampleFunction = $scope.downsampleFunctionList[0];
          }

          // Load metrics from cache
          if (zabbixCache._initialized) {
            $scope.getMetricsFromCache();
            $scope.initFilters();
            console.log("Cached", $scope.metric);
          } else {
            zabbixCache.refresh().then(function () {
              $scope.getMetricsFromCache();
              $scope.initFilters();
              console.log("From server", $scope.metric);
            });
          }

          setItemAlias();
        }
        else if ($scope.target.mode === 1) {
          $scope.slaPropertyList = [
            {name: "Status", property: "status"},
            {name: "SLA", property: "sla"},
            {name: "OK time", property: "okTime"},
            {name: "Problem time", property: "problemTime"},
            {name: "Down time", property: "downtimeTime"}
          ];
          $scope.itserviceList = [{name: "test"}];
          $scope.updateITServiceList();
        }
      };

      $scope.initFilters = function () {
        $scope.onGroupBlur();
        $scope.onHostBlur();
        $scope.onApplicationBlur();
      };

      $scope.getMetricsFromCache = function () {
        $scope.metric = {
          groupList: zabbixCache.getGroups(),
          hostList: zabbixCache.getHosts(),
          applicationList: zabbixCache.getApplications(),
          itemList: zabbixCache.getItems()
        };
      };

      // Get list of metric names for bs-typeahead directive
      function getMetricNames(scope, metricList) {
        return _.uniq(_.map(scope.metric[metricList], 'name'));
      }

      // Map functions for bs-typeahead
      $scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList');
      $scope.getHostNames = _.partial(getMetricNames, $scope, 'filteredHosts');
      $scope.getApplicationNames = _.partial(getMetricNames, $scope, 'filteredApplications');
      $scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');

      $scope.filterHosts = function () {
        var group = $scope.target.group;
        var groups = [];
        var hosts = [];

        // Filter groups by regex
        if (group.isRegex) {
          var filterPattern = Utils.buildRegex(group.filter);
          groups = _.filter($scope.metric.groupList, function (groupObj) {
            return filterPattern.test(groupObj.name);
          });
        }
        // Find hosts in selected group
        else {
          var finded = _.find($scope.metric.groupList, {'name': group.filter});
          if (finded) {
            groups.push(finded);
          } else {
            groups = undefined;
          }
        }

        if (groups) {
          var groupids = _.map(groups, 'groupid');
          hosts = _.filter($scope.metric.hostList, function (hostObj) {
            return _.intersection(groupids, hostObj.groups).length;
          });
        }
        return hosts;
      };

      $scope.filterApplications = function () {
        var host = $scope.target.host;
        var hosts = [];
        var apps = [];

        // Filter hosts by regex
        if (host.isRegex) {
          var filterPattern = Utils.buildRegex(host.filter);
          hosts = _.filter($scope.metric.hostList, function (hostObj) {
            return filterPattern.test(hostObj.name);
          });
        }
        // Find applications in selected host
        else {
          var finded = _.find($scope.metric.hostList, {'name': host.filter});
          if (finded) {
            hosts.push(finded);
          } else {
            hosts = undefined;
          }
        }

        if (hosts) {
          var hostsids = _.map(hosts, 'hostid');
          apps = _.filter($scope.metric.applicationList, function (appObj) {
            return _.intersection(hostsids, appObj.hosts).length;
          });
        }

        return apps;
      };

      $scope.filterItems = function () {
        var app = $scope.target.application;
        var apps = [];
        var items = [];

        // Filter applications by regex
        if (app.isRegex) {
          var filterPattern = Utils.buildRegex(app.filter);
          apps = _.filter($scope.metric.applicationList, function (appObj) {
            return filterPattern.test(appObj.name);
          });
        }
        // Find items in selected application
        else {
          var finded = _.find($scope.metric.applicationList, {'name': app.filter});
          if (finded) {
            apps.push(finded);
          } else {
            apps = undefined;
          }
        }

        if (apps) {
          var appids = _.flatten(_.map(apps, 'applicationids'));
          items = _.filter($scope.metric.itemList, function (itemObj) {
            return _.intersection(appids, itemObj.applications).length;
          });
        }

        return items;
      };

      $scope.onTargetPartChange = function (targetPart) {
        var regexStyle = {'color': '#CCA300'};
        targetPart.isRegex = Utils.isRegex(targetPart.filter);
        targetPart.style = targetPart.isRegex ? regexStyle : {};
      };

      // Handle group blur and filter hosts
      $scope.onGroupBlur = function() {
        $scope.metric.filteredHosts = $scope.filterHosts();
        $scope.parseTarget();
        $scope.get_data();
      };

      // Handle host blur and filter applications
      $scope.onHostBlur = function() {<commit_msg>Fixed items filtering in query editor.<commit_after>define([
    'angular',
    'lodash',
    './utils'
  ],
  function (angular, _, Utils) {
    'use strict';

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

    module.controller('ZabbixAPIQueryCtrl', function ($scope, $sce, templateSrv) {

      var zabbixCache = $scope.datasource.zabbixCache;

      $scope.init = function () {
        $scope.targetLetters = targetLetters;
        $scope.metric = {};

        // Load default values
        var targetDefaults = {
          mode: 0,
          group: { filter: "" },
          host: { filter: "" },
          application: { filter: "" },
          item: { filter: "" }
        };
        _.defaults($scope.target, targetDefaults);

        if ($scope.target.mode ===0 ||
            $scope.target.mode === 2) {

          $scope.downsampleFunctionList = [
            {name: "avg", value: "avg"},
            {name: "min", value: "min"},
            {name: "max", value: "max"}
          ];

          // Set avg by default
          if (!$scope.target.downsampleFunction) {
            $scope.target.downsampleFunction = $scope.downsampleFunctionList[0];
          }

          // Load metrics from cache
          if (zabbixCache._initialized) {
            $scope.getMetricsFromCache();
            $scope.initFilters();
            console.log("Cached", $scope.metric);
          } else {
            zabbixCache.refresh().then(function () {
              $scope.getMetricsFromCache();
              $scope.initFilters();
              console.log("From server", $scope.metric);
            });
          }

          setItemAlias();
        }
        else if ($scope.target.mode === 1) {
          $scope.slaPropertyList = [
            {name: "Status", property: "status"},
            {name: "SLA", property: "sla"},
            {name: "OK time", property: "okTime"},
            {name: "Problem time", property: "problemTime"},
            {name: "Down time", property: "downtimeTime"}
          ];
          $scope.itserviceList = [{name: "test"}];
          $scope.updateITServiceList();
        }
      };

      $scope.initFilters = function () {
        $scope.onGroupBlur();
        $scope.onHostBlur();
        $scope.onApplicationBlur();
      };

      $scope.getMetricsFromCache = function () {
        $scope.metric = {
          groupList: zabbixCache.getGroups(),
          hostList: zabbixCache.getHosts(),
          applicationList: zabbixCache.getApplications(),
          itemList: zabbixCache.getItems()
        };
      };

      // Get list of metric names for bs-typeahead directive
      function getMetricNames(scope, metricList) {
        return _.uniq(_.map(scope.metric[metricList], 'name'));
      }

      // Map functions for bs-typeahead
      $scope.getGroupNames = _.partial(getMetricNames, $scope, 'groupList');
      $scope.getHostNames = _.partial(getMetricNames, $scope, 'filteredHosts');
      $scope.getApplicationNames = _.partial(getMetricNames, $scope, 'filteredApplications');
      $scope.getItemNames = _.partial(getMetricNames, $scope, 'filteredItems');

      $scope.filterHosts = function () {
        var group = $scope.target.group;
        var groups = [];
        var hosts = [];

        // Filter groups by regex
        if (group.isRegex) {
          var filterPattern = Utils.buildRegex(group.filter);
          groups = _.filter($scope.metric.groupList, function (groupObj) {
            return filterPattern.test(groupObj.name);
          });
        }
        // Find hosts in selected group
        else {
          var finded = _.find($scope.metric.groupList, {'name': group.filter});
          if (finded) {
            groups.push(finded);
          } else {
            groups = undefined;
          }
        }

        if (groups) {
          var groupids = _.map(groups, 'groupid');
          hosts = _.filter($scope.metric.hostList, function (hostObj) {
            return _.intersection(groupids, hostObj.groups).length;
          });
        }
        return hosts;
      };

      $scope.filterApplications = function () {
        var host = $scope.target.host;
        var hosts = [];
        var apps = [];

        // Filter hosts by regex
        if (host.isRegex) {
          var filterPattern = Utils.buildRegex(host.filter);
          hosts = _.filter($scope.metric.hostList, function (hostObj) {
            return filterPattern.test(hostObj.name);
          });
        }
        // Find applications in selected host
        else {
          var finded = _.find($scope.metric.hostList, {'name': host.filter});
          if (finded) {
            hosts.push(finded);
          } else {
            hosts = undefined;
          }
        }

        if (hosts) {
          var hostsids = _.map(hosts, 'hostid');
          apps = _.filter($scope.metric.applicationList, function (appObj) {
            return _.intersection(hostsids, appObj.hosts).length;
          });
        }

        return apps;
      };

      $scope.filterItems = function () {
        var app = $scope.target.application;
        var host = $scope.target.host;
        var hosts = [];
        var apps = [];
        var items = [];

        // Filter hosts by regex
        if (host.isRegex) {
          var hostFilterPattern = Utils.buildRegex(host.filter);
          hosts = _.filter($scope.metric.hostList, function (hostObj) {
            return hostFilterPattern.test(hostObj.name);
          });
        }
        else {
          var findedHosts = _.find($scope.metric.hostList, {'name': host.filter});
          if (findedHosts) {
            hosts.push(findedHosts);
          } else {
            hosts = undefined;
          }
        }

        // Filter applications by regex
        if (app.isRegex) {
          var filterPattern = Utils.buildRegex(app.filter);
          apps = _.filter($scope.metric.applicationList, function (appObj) {
            return filterPattern.test(appObj.name);
          });
        }
        // Find items in selected application
        else if (app.filter) {
          var finded = _.find($scope.metric.applicationList, {'name': app.filter});
          if (finded) {
            apps.push(finded);
          } else {
            apps = undefined;
          }
        } else {
          apps = undefined;
          if (hosts) {
            items = _.filter($scope.metric.itemList, function (itemObj) {
              return _.find(hosts, {'hostid': itemObj.hostid });
            });
          }
        }

        if (apps) {
          var appids = _.flatten(_.map(apps, 'applicationids'));
          items = _.filter($scope.metric.itemList, function (itemObj) {
            return _.intersection(appids, itemObj.applications).length;
          });
          items = _.filter(items, function (itemObj) {
            return _.find(hosts, {'hostid': itemObj.hostid });
          });
        }

        if (true) {
          items = _.filter(items, {'status': '0'});
        }

        return items;
      };

      $scope.onTargetPartChange = function (targetPart) {
        var regexStyle = {'color': '#CCA300'};
        targetPart.isRegex = Utils.isRegex(targetPart.filter);
        targetPart.style = targetPart.isRegex ? regexStyle : {};
      };

      // Handle group blur and filter hosts
      $scope.onGroupBlur = function() {
        $scope.metric.filteredHosts = $scope.filterHosts();
        $scope.parseTarget();
        $scope.get_data();
      };

      // Handle host blur and filter applications
      $scope.onHostBlur = function() {
{'fraction_non_alphanumeric': 0.13381772009029344, 'fraction_numerical': 0.0011992099322799096, 'mean_word_length': 1.8056600039580448, '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': '4137528', 'n_tokens_mistral': 3987, 'n_tokens_neox': 3876, 'n_words': 808}