var Metrics = require('metrics');
var InfluxReporter = require('metrics-influxdb');
var os = require('os');
var logger = require('./logger');

var CATEGORIES = {
  all: 'global.all',
  static: 'global.static', // i.e. '/favicon.ico'
  status: 'status',
  method: 'method'
};

function Server(port, statsd, statsdRoutes, influxOptions) {
  this.metrics = new Metrics.Server(port);
  this.statsd = statsd;
  this.statsdRoutes = statsdRoutes;

  if(influxOptions){
    this.influxReporter = new InfluxReporter({ protocol: influxOptions.portocol || 'http',
        host:  influxOptions.host,
        database: influxOptions.database,
        consistency: influxOptions.consistency || 'any',
        tags: influxOptions.tags || {'host': os.hostname()},
        precision: influxOptions.precision || 'ms'
      });

    this.influxReporterInterval = setInterval(function () {
        this.influxReporter.report();
        logger.info('reported metrics to influx');
      }, 60000);
  }
}

Server.prototype.getMetricName = function getMetricName(route, methodName) {
  var routeName = CATEGORIES.static;

  if (route && route.path) {
    routeName = route.path;

    if (Object.prototype.toString.call(routeName) === '[object RegExp]') {
      routeName = routeName.source;
    }

    routeName = routeName + '.' + methodName.toLowerCase();
  }

  return routeName;
};

Server.prototype.update = function update(message) {
  var metricName = this.getMetricName(message.route, message.method);
  var path = message.route ? message.route.path : undefined;

  this.updateMetric(CATEGORIES.all, message.time);
  this.updateMetric(CATEGORIES.status + '.' + message.status, message.time);
  this.updateMetric(CATEGORIES.method + '.' + message.method, message.time);
  this.updateMetric(metricName, message.time);

  if (this.statsd && this.statsdRoutes[path]) {
    var route = this.statsdRoutes[path];
    if (route.methods.indexOf(message.method) !== -1) {
      this.sendToStatsD(route.name, message.time);
    }
  }
};

Server.prototype.updateMetric = function updateMetric(name, time) {
  if (!this.metrics.report.getMetric(name)) {
    this.metrics.addMetric(name, new Metrics.Timer());
    
    if(this.influxReporter){
      this.influxReporter.addMetric("express." + name, this.metrics.report.getMetric(name));
    }
  }

  this.metrics.report.getMetric(name).update(time);
};


Server.prototype.sendToStatsD = function sendToStatsD(name, time) {
  this.statsd.timing('.' + name, time, null, function (error) {
    if (error) {
      logger.error('Error sending response time to StatsD: ', error);
    }
  });
};


Server.prototype.stop = function stop(callback) {
  this.metrics.server.close(callback);
};

module.exports = Server;


module.exports = Server;

@@ -455,24 +455,26 @@
 tions)%7B%0A    
+  
 this.influxR
@@ -556,16 +556,18 @@
         
+  
 host:  i
@@ -585,16 +585,18 @@
 s.host,%0A
+  
         
@@ -637,16 +637,18 @@
         
+  
 consiste
@@ -688,16 +688,18 @@
  'any',%0A
+  
         
@@ -759,16 +759,18 @@
         
+ 
+ 
 precisio
@@ -815,16 +815,37 @@
    %7D);%0A%0A
+      that = this;%0A  
     this
@@ -900,28 +900,30 @@
 ) %7B%0A        
-this
+  that
 .influxRepor
@@ -936,16 +936,18 @@
 port();%0A
+  
         

this = that