<commit_before>/* jshint strict:false */

var http = require('http');
var url = require('url');
var qs = require('querystring');
var Parameter = require('./parameter');

var defaultConfig = {
  segments: [],
  port: 8000
};

function Metron(config){
  this.config = config || {};
  this.set(config);
}

Metron.prototype.get = function(key){
  return this.config[key];
}

Metron.prototype.set = function(obj){
  for(var key in defaultConfig){
    this.config[key] =
      [obj[key], this.config[key], defaultConfig[key]].filter(function(v){
        return v !== undefined
      })[0];
  }

  return this.config;
}

Metron.prototype.start = function(){
  var server  = http.createServer(this.processRequest.bind(this));
  this.server = server;

  server.listen(this.get('port'));
}

Metron.prototype.stop = function(){
  this.server.close();
}

Metron.prototype.processRequest = function(req, res){
  var params = { };
  var parsedUrl = url.parse(req.url, true);

  if(req.method === 'GET'){
    try{
      req.params = JSON.parse(parsedUrl.query.data);
      this.processParameters(req, res);
    }catch(e){
      return this.endRequest(req, res, 400);
    }
  }else{
    var body = [];

    req.on('data', function (data) {
      body.push(data.toString());
    });

    req.on('end', function () {
      body = body.join('');

      if(req.headers['Content-Type'].toLowerCase().indexOf('json') > -1){
        try{
          params = JSON.parse(body);
        }catch(e){
          return this.endRequest(req, res, 400);
        }
      }else{
        req.params = qs.parse(body);
        this.processParameters(req, res);
      }
    });
  }
}

Metron.prototype.endRequest = function(req, res, statusCode){
  res.writeHead(statusCode || 204);
  res.end();
};

Metron.prototype.processParameters = function(req, res){
  var params = req.params;

  for(var segmentName in params){
    var segmentConfig = this.config.segments[segmentName];
    var segment = params[segmentName];

    if(!segmentConfig)
      return this.endRequest(req, res, 422);

    for(var statName in segment){
      var statConfig = segmentConfig.stats[statName];
      var statValue = segment[statName];
      statValue = new Parameter(statValue, statConfig).value();

      if(statValue !== undefined){
        var config = { };
        var store = statConfig.dataStore ||
                    segmentConfig.dataStore ||
                    console.log;

        for(var key in segmentConfig){
          if(key == 'stats') continue;
          config[key] = statConfig[key];
        }

        for(key in statConfig){
          config[key] = statConfig[key];<commit_msg>Allow middleware<commit_after>/* jshint strict:false */

var http = require('http');
var url = require('url');
var qs = require('querystring');
var Parameter = require('./parameter');

var defaultConfig = {
  segments: [],
  port: 8000
};

function Metron(config, middleware){
  this.config = config || {};
  this.middleware = middleware || [];
  this.set(config);
}

Metron.prototype.get = function(key){
  return this.config[key];
}

Metron.prototype.set = function(obj){
  for(var key in defaultConfig){
    this.config[key] =
      [obj[key], this.config[key], defaultConfig[key]].filter(function(v){
        return v !== undefined
      })[0];
  }

  return this.config;
}

Metron.prototype.start = function(){
  var server  = http.createServer(this.processRequest.bind(this));
  this.server = server;

  server.listen(this.get('port'));
}

Metron.prototype.stop = function(){
  this.server.close();
}

Metron.prototype.processRequest = function(req, res){
  var params = { };
  var parsedUrl = url.parse(req.url, true);

  if(req.method === 'GET'){
    try{
      req.params = JSON.parse(parsedUrl.query.data);
      this.processParameters(req, res);
    }catch(e){
      return this.endRequest(req, res, 400);
    }
  }else{
    var body = [];

    req.on('data', function (data) {
      body.push(data.toString());
    });

    req.on('end', function () {
      body = body.join('');

      if(req.headers['Content-Type'].toLowerCase().indexOf('json') > -1){
        try{
          params = JSON.parse(body);
        }catch(e){
          return this.endRequest(req, res, 400);
        }
      }else{
        req.params = qs.parse(body);
        this.processParameters(req, res);
      }
    });
  }
}

Metron.prototype.endRequest = function(req, res, statusCode){
  res.writeHead(statusCode || 204);
  res.end();
};

Metron.prototype.processParameters = function(req, res){
  var params = req.params;

  for(var i = 0; i < this.middleware.length; i++){
    this.middleware[i](req, res);
  }

  for(var segmentName in params){
    var segmentConfig = this.config.segments[segmentName];
    var segment = params[segmentName];

    if(!segmentConfig)
      return this.endRequest(req, res, 422);

    for(var statName in segment){
      var statConfig = segmentConfig.stats[statName];
      var statValue = segment[statName];
      statValue = new Parameter(statValue, statConfig).value();

      if(statValue !== undefined){
        var config = { };
        var store = statConfig.dataStore ||
                    segmentConfig.dataStore ||
                    console.log;

        for(var key in segmentConfig){
          if(key == 'stats') continue;
          config[key] = statConfig[key];
        }

        for(key in statConfig){
          config[key] = statConfig[key];
{'fraction_non_alphanumeric': 0.15723502304147466, 'fraction_numerical': 0.0068202764976958525, 'mean_word_length': 2.8373408769448374, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '3163952', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1680, 'n_words': 347}