<commit_before>/*
 * Flush stats to InfluxDB (http://influxdb.org/)
 *
 * To enable this backend, include 'statsd-influxdb-backend' in the backends
 * configuration array:
 *
 *   backends: ['statsd-influxdb-backend']
 *
 * The backend will read the configuration options from the following
 * 'influxdb' hash defined in the main statsd config file:
 *
 * influxdb: {
 *   host: '127.0.0.1',   // InfluxDB host. (default 127.0.0.1)
 *   port: 8086,          // InfluxDB port. (default 8086)
 *   database: 'dbname',  // InfluxDB database instance. (required)
 *   username: 'user',    // InfluxDB database username. (required)
 *   password: '<PASSWORD>',    // InfluxDB database password. (required)
 *   flush: {
 *     enable: true       // Enable regular flush strategy. (default true)
 *   },
 *   proxy: {
 *     enable: false,       // Enable the proxy strategy. (default false)
 *     suffix: 'raw',       // Metric name suffix. (default 'raw')
 *     flushInterval: 1000  // Flush interval for the internal buffer.
 *                          // (default 1000)
 *   }
 * }
 *
 */
var util = require('util'),
    querystring = require('querystring'),
    http = require('http');

function InfluxdbBackend(startupTime, config, events) {
  var self = this;

  self.debug = config.debug;
  self.registry = {};

  self.defaultHost = '127.0.0.1';
  self.defaultPort = 8086;
  self.defaultFlushEnable = true;
  self.defaultProxyEnable = false;
  self.defaultProxySuffix = 'raw';
  self.defaultProxyFlushInterval = 1000;

  self.host = self.defaultHost;
  self.port = self.defaultPort;
  self.flushEnable = self.defaultFlushEnable;
  self.proxyEnable = self.defaultProxyEnable;
  self.proxySuffix = self.defaultProxySuffix;
  self.proxyFlushInterval = self.defaultProxyFlushInterval;

  if (config.influxdb) {
    self.host = config.influxdb.host || self.defaultHost;
    self.port = config.influxdb.port || self.defaultPort;
    self.user = config.influxdb.username;
    self.pass = config.influxdb.password;
    self.database = config.influxdb.database;

    if (config.influxdb.flush) {
      self.flushEnable = config.influxdb.flush.enable;
    }

    if (config.influxdb.proxy) {
      self.proxyEnable = config.influxdb.proxy.enable || self.defaultProxyEnable;
      self.proxySuffix = config.influxdb.proxy.suffix || self.defaultProxySuffix;
      self.proxyFlushInterval = config.influxdb.proxy.flushInterval || self.defaultProxyFlushInterval;
    }
  }

  if (self.proxyEnable) {
    self.log('Starting the buffer flush interval. (every ' + self.proxyFlushInterval + 'ms)');
    setInterval(function () {
      self.flushQueue();
    }, self.proxyFlushInterval);

    events.on('packet', function (packet, rinfo) {
      try {
        self.processPacket(packet, rinfo);
      } catch (e) {
        self.log(e);
      }
    });
  }

  if (self.flushEnable) {
    events.on('flush', function (timestamp, metrics) {
      try {
        self.processFlush(timestamp, metrics);
      } catch (e) {
        self.log(e);
      }
    });
  }

  return true;
}

InfluxdbBackend.prototype.log = function (msg) {
  util.log('[influxdb] ' + msg);
}

InfluxdbBackend.prototype.logDebug = function (msg) {
  if (this.debug) {
    var string;

    if (msg instanceof Function) {
      string = msg();
    } else {
      string = msg;
    }

    util.log('[influxdb] (DEBUG) ' + string);
  }
}

InfluxdbBackend.prototype.processFlush = function (timestamp, metrics) {
  var self = this,
      counters = metrics.counters,
      gauges = metrics.gauges,
      timerData = metrics.timer_data,
      points = [],
      key, timerKey;

  /* Convert timestamp from seconds to milliseconds. */
  timestamp = (timestamp * 1000);

  for (key in counters) {
    /* Do not include statsd counters. */
    if (key.match(/^statsd\./)) { continue; }

    var value = counters[key],
        k = key + '.counter';

    if (value) {
      points.push(self.assembleEvent(k, [{value: value, time: timestamp}]));
    }
  }

  for (key in gauges) {
    var value = gauges[key],
        k = key + '.gauge';

    if (value) {
      points.push(self.assembleEvent(k, [{value: value, time: timestamp}]));
    }
  }

  for (key in timerData) {
    var timerMetrics = timerData[key];

    for (timerKey in timerMetrics) {
      var value = timerMetrics[timerKey],
          k = key + '.timer' + '.' + timerKey;

      points.push(self.assembleEvent(k, [{value: value, time: timestamp}]));
    }
  }

  self.httpPOST(points);
}

InfluxdbBackend.prototype.processPacket = function (packet, rinfo) {
  var self = this,
      ts = (new Date()).valueOf();<commit_msg>Gauges with zero value are not sent<commit_after>/*
 * Flush stats to InfluxDB (http://influxdb.org/)
 *
 * To enable this backend, include 'statsd-influxdb-backend' in the backends
 * configuration array:
 *
 *   backends: ['statsd-influxdb-backend']
 *
 * The backend will read the configuration options from the following
 * 'influxdb' hash defined in the main statsd config file:
 *
 * influxdb: {
 *   host: '127.0.0.1',   // InfluxDB host. (default 127.0.0.1)
 *   port: 8086,          // InfluxDB port. (default 8086)
 *   database: 'dbname',  // InfluxDB database instance. (required)
 *   username: 'user',    // InfluxDB database username. (required)
 *   password: '<PASSWORD>',    // InfluxDB database password. (required)
 *   flush: {
 *     enable: true       // Enable regular flush strategy. (default true)
 *   },
 *   proxy: {
 *     enable: false,       // Enable the proxy strategy. (default false)
 *     suffix: 'raw',       // Metric name suffix. (default 'raw')
 *     flushInterval: 1000  // Flush interval for the internal buffer.
 *                          // (default 1000)
 *   }
 * }
 *
 */
var util = require('util'),
    querystring = require('querystring'),
    http = require('http');

function InfluxdbBackend(startupTime, config, events) {
  var self = this;

  self.debug = config.debug;
  self.registry = {};

  self.defaultHost = '127.0.0.1';
  self.defaultPort = 8086;
  self.defaultFlushEnable = true;
  self.defaultProxyEnable = false;
  self.defaultProxySuffix = 'raw';
  self.defaultProxyFlushInterval = 1000;

  self.host = self.defaultHost;
  self.port = self.defaultPort;
  self.flushEnable = self.defaultFlushEnable;
  self.proxyEnable = self.defaultProxyEnable;
  self.proxySuffix = self.defaultProxySuffix;
  self.proxyFlushInterval = self.defaultProxyFlushInterval;

  if (config.influxdb) {
    self.host = config.influxdb.host || self.defaultHost;
    self.port = config.influxdb.port || self.defaultPort;
    self.user = config.influxdb.username;
    self.pass = <PASSWORD>.influxdb.password;
    self.database = config.influxdb.database;

    if (config.influxdb.flush) {
      self.flushEnable = config.influxdb.flush.enable;
    }

    if (config.influxdb.proxy) {
      self.proxyEnable = config.influxdb.proxy.enable || self.defaultProxyEnable;
      self.proxySuffix = config.influxdb.proxy.suffix || self.defaultProxySuffix;
      self.proxyFlushInterval = config.influxdb.proxy.flushInterval || self.defaultProxyFlushInterval;
    }
  }

  if (self.proxyEnable) {
    self.log('Starting the buffer flush interval. (every ' + self.proxyFlushInterval + 'ms)');
    setInterval(function () {
      self.flushQueue();
    }, self.proxyFlushInterval);

    events.on('packet', function (packet, rinfo) {
      try {
        self.processPacket(packet, rinfo);
      } catch (e) {
        self.log(e);
      }
    });
  }

  if (self.flushEnable) {
    events.on('flush', function (timestamp, metrics) {
      try {
        self.processFlush(timestamp, metrics);
      } catch (e) {
        self.log(e);
      }
    });
  }

  return true;
}

InfluxdbBackend.prototype.log = function (msg) {
  util.log('[influxdb] ' + msg);
}

InfluxdbBackend.prototype.logDebug = function (msg) {
  if (this.debug) {
    var string;

    if (msg instanceof Function) {
      string = msg();
    } else {
      string = msg;
    }

    util.log('[influxdb] (DEBUG) ' + string);
  }
}

InfluxdbBackend.prototype.processFlush = function (timestamp, metrics) {
  var self = this,
      counters = metrics.counters,
      gauges = metrics.gauges,
      timerData = metrics.timer_data,
      points = [],
      key, timerKey;

  /* Convert timestamp from seconds to milliseconds. */
  timestamp = (timestamp * 1000);

  for (key in counters) {
    /* Do not include statsd counters. */
    if (key.match(/^statsd\./)) { continue; }

    var value = counters[key],
        k = key + '.counter';

    if (value) {
      points.push(self.assembleEvent(k, [{value: value, time: timestamp}]));
    }
  }

  for (key in gauges) {
    var value = gauges[key],
        k = key + '.gauge';

    if (!isNaN(parseFloat(value)) && isFinite(value)) {
      points.push(self.assembleEvent(k, [{value: value, time: timestamp}]));
    }
  }

  for (key in timerData) {
    var timerMetrics = timerData[key];

    for (timerKey in timerMetrics) {
      var value = timerMetrics[timerKey],
          k = key + '.timer' + '.' + timerKey;

      points.push(self.assembleEvent(k, [{value: value, time: timestamp}]));
    }
  }

  self.httpPOST(points);
}

InfluxdbBackend.prototype.processPacket = function (packet, rinfo) {
  var self = this,
      ts = (new Date()).valueOf();
{'fraction_non_alphanumeric': 0.13890378633487074, 'fraction_numerical': 0.009868068218384641, 'mean_word_length': 2.7979633401221995, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '5582477', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2965, 'n_words': 678}