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

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

  module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv) {

    /**
     * Datasource initialization. Calls when you refresh page, add
     * or modify datasource.
     *
     * @param {Object} datasource Grafana datasource object.
     */
    function ZabbixAPIDatasource(datasource) {
      this.name             = datasource.name;
      this.url              = datasource.url;

      // TODO: fix passing username and password from config.html
      this.username         = datasource.meta.username;
      this.password         = <PASSWORD>;

      // Limit metrics per panel for templated request
      this.limitmetrics = datasource.meta.limitmetrics || 50;
    }


    /**
     * Calls for each panel in dashboard.
     *
     * @param  {Object} options   Query options. Contains time range, targets
     *                            and other info.
     *
     * @return {Object}           Grafana metrics object with timeseries data
     *                            for each target.
     */
    ZabbixAPIDatasource.prototype.query = function(options) {

      // get from & to in seconds
      var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
      var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);

      // Create request for each target
      var promises = _.map(options.targets, function(target) {

        // Don't show undefined and hidden targets
        if (target.hide || !target.group || !target.host
                        || !target.application || !target.item) {
          return [];
        }

        // Replace templated variables
        var groupname = templateSrv.replace(target.group.name);
        var hostname  = templateSrv.replace(target.host.name);
        var appname   = templateSrv.replace(target.application.name);
        var itemname  = templateSrv.replace(target.item.name);

        // Extract zabbix groups, hosts and apps from string:
        // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
        var groups = splitMetrics(groupname);
        var hosts  = splitMetrics(hostname);
        var apps   = splitMetrics(appname);

        // Remove hostnames from item names and then
        // extract item names
        // "hostname: itemname" --> "itemname"
        var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
        var itemnames = splitMetrics(itemname.replace(delete_hostname_pattern, ''));

        // Find items by item names and perform queries
        var self = this;
        return this.itemFindQuery(groups, hosts, apps)
          .then(function (items) {
            if (itemnames == 'All') {
              return items;
            } else {

              // Filtering items
              return _.filter(items, function (item) {
                return _.contains(itemnames, expandItemName(item));
              });
            }
          }).then(function (items) {

            // Don't perform query for high number of items
            // to prevent Grafana slowdown
            if (items.length > self.limitmetrics) {
              return [];
            } else {
              items = _.flatten(items);
              return self.getTrends(items, from, to)
                .then(_.partial(self.handleTrendResponse, items));
            }
          });
      }, this);
<commit_msg>iss #16 Add auto selection between history and trends.<commit_after>define([
  'angular',
  'lodash',
  'kbn',
  './queryCtrl'
],
function (angular, _, kbn) {
  'use strict';

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

  module.factory('ZabbixAPIDatasource', function($q, backendSrv, templateSrv) {

    /**
     * Datasource initialization. Calls when you refresh page, add
     * or modify datasource.
     *
     * @param {Object} datasource Grafana datasource object.
     */
    function ZabbixAPIDatasource(datasource) {
      this.name             = datasource.name;
      this.url              = datasource.url;

      // TODO: fix passing username and password from config.html
      this.username         = datasource.meta.username;
      this.password         = datasource.meta.password;

      // Limit metrics per panel for templated request
      this.limitmetrics = datasource.meta.limitmetrics || 50;
    }


    /**
     * Calls for each panel in dashboard.
     *
     * @param  {Object} options   Query options. Contains time range, targets
     *                            and other info.
     *
     * @return {Object}           Grafana metrics object with timeseries data
     *                            for each target.
     */
    ZabbixAPIDatasource.prototype.query = function(options) {

      // get from & to in seconds
      var from = Math.ceil(kbn.parseDate(options.range.from).getTime() / 1000);
      var to = Math.ceil(kbn.parseDate(options.range.to).getTime() / 1000);
      var getTrendsFrom = Math.ceil(kbn.parseDate('now-7d').getTime() / 1000);

      // Create request for each target
      var promises = _.map(options.targets, function(target) {

        // Don't show undefined and hidden targets
        if (target.hide || !target.group || !target.host
                        || !target.application || !target.item) {
          return [];
        }

        // Replace templated variables
        var groupname = templateSrv.replace(target.group.name);
        var hostname  = templateSrv.replace(target.host.name);
        var appname   = templateSrv.replace(target.application.name);
        var itemname  = templateSrv.replace(target.item.name);

        // Extract zabbix groups, hosts and apps from string:
        // "{host1,host2,...,hostN}" --> [host1, host2, ..., hostN]
        var groups = splitMetrics(groupname);
        var hosts  = splitMetrics(hostname);
        var apps   = splitMetrics(appname);

        // Remove hostnames from item names and then
        // extract item names
        // "hostname: itemname" --> "itemname"
        var delete_hostname_pattern = /(?:\[[\w\.]+\]\:\s)/g;
        var itemnames = splitMetrics(itemname.replace(delete_hostname_pattern, ''));

        // Find items by item names and perform queries
        var self = this;
        return this.itemFindQuery(groups, hosts, apps)
          .then(function (items) {
            if (itemnames == 'All') {
              return items;
            } else {

              // Filtering items
              return _.filter(items, function (item) {
                return _.contains(itemnames, expandItemName(item));
              });
            }
          }).then(function (items) {

            // Don't perform query for high number of items
            // to prevent Grafana slowdown
            if (items.length > self.limitmetrics) {
              return [];
            } else {
              items = _.flatten(items);

              if (from > getTrendsFrom) {
                return self.performTimeSeriesQuery(items, from, to)
                    .then(_.partial(self.handleHistoryResponse, items));
              } else {
                return self.getTrends(items, from, to)
                  .then(_.partial(self.handleTrendResponse, items));
              }
            }
          });
      }, this);

{'fraction_non_alphanumeric': 0.1142004611420046, 'fraction_numerical': 0.004747050047470501, 'mean_word_length': 1.8614668218859138, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1317240', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1971, 'n_words': 508}