<commit_before>
/*!
 * Engine - stats
 * Copyright (c) 2010 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var os = require('os')
  , net = require('net')
  , repl = require('repl');

/**
 * Enable REPL with all arguments passed to `net.Server#listen()`.
 *
 * Examples:
 *
 *    engine(server)
 *      .use(engine.stats())
 *      .use(engine.repl('/var/run/engine'))
 *      .listen();
 *
 * In the terminal:
 *
 *    $ sudo telnet /var/run/engine 
 *
 * @return {Function}
 * @api public
 */

exports = module.exports = function(){
  var args = arguments;
  return function(master){
    master.on('start', function(){
      // track REPL connections
      var connections = 0;

      // TCP or unix-domain socket repl
      var server = net.createServer(function(sock){
        ++connections;
        var ctx = repl.start('engine> ', sock).context;
        ctx.connections = connections;

        // merge commands into context
        // executing in context of master
        Object.keys(exports).forEach(function(cmd){
          ctx[cmd] = function(){
            var args = Array.prototype.slice.call(arguments);
            args.unshift(master, sock);
            return exports[cmd].apply(master, args);
          };
        });
      });

      // Apply all arguments given
      server.listen.apply(server, args);
    });
  }
};

/**
 * Display statistics.
 */

exports.stats = function(master, sock){
  function head(str){ sock.write('\n\033[36m  ' + str + '\033[0m\n'); }
  function row(key, val){ sock.write('  \033[90m' + key + ':\033[0m ' + val + '\n'); }

  // master stats
  if (master.stats) {
    head('Master');
    row('started', master.stats.start.toUTCString());
    row('uptime', diff(new Date, master.stats.start))
    sock.write('\n');
  }
};
<commit_msg>Added help() command<commit_after>
/*!
 * Engine - stats
 * Copyright (c) 2010 LearnBoost <<EMAIL>>
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var os = require('os')
  , net = require('net')
  , repl = require('repl');

/**
 * Enable REPL with all arguments passed to `net.Server#listen()`.
 *
 * Examples:
 *
 *    engine(server)
 *      .use(engine.stats())
 *      .use(engine.repl('/var/run/engine'))
 *      .listen();
 *
 * In the terminal:
 *
 *    $ sudo telnet /var/run/engine 
 *
 * @return {Function}
 * @api public
 */

exports = module.exports = function(){
  var args = arguments;
  return function(master){
    master.on('start', function(){
      // track REPL connections
      var connections = 0;

      // TCP or unix-domain socket repl
      var server = net.createServer(function(sock){
        ++connections;
        var ctx = repl.start('engine> ', sock).context;
        ctx.connections = connections;

        // augment socket to provide some formatting methods
        sock.head = function(str){ this.write('\n  \033[36m' + str + '\033[0m\n'); }
        sock.row = function(key, val){ this.write('  \033[90m' + key + ':\033[0m ' + val + '\n'); }
        

        // merge commands into context
        // executing in context of master
        Object.keys(exports).forEach(function(cmd){
          ctx[cmd] = function(){
            var args = Array.prototype.slice.call(arguments);
            args.unshift(master, sock);
            return exports[cmd].apply(master, args);
          };
        });
      });

      // Apply all arguments given
      server.listen.apply(server, args);
    });
  }
};

/**
 * Display commmand help.
 */

exports.help = function(master, sock){
  sock.head('Commands');
  Object.keys(exports).forEach(function(cmd){
    var fn = exports[cmd]
      , params = fn.toString().match(/^function +\((.*?)\)/)[1]
      , params = params.split(/ *, */).slice(2);

    sock.row(
      cmd + '(' + params.join(', ') + ')'
      , fn.description);
  });
  sock.write('\n');
};

exports.help.description = 'Display help information';

/**
 * Display statistics.
 */

exports.stats = function(master, sock){
  // master stats
  if (master.stats) {
    sock.head('Master');
    sock.row('started', master.stats.start.toUTCString());
    sock.row('uptime', diff(new Date, master.stats.start))
    sock.write('\n');
  }
};

exports.stats.description = 'Display server statistics';

{'fraction_non_alphanumeric': 0.17490583804143126, 'fraction_numerical': 0.011299435028248588, 'mean_word_length': 2.4600977198697067, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '7178250', 'n_tokens_mistral': 1349, 'n_tokens_neox': 1302, 'n_words': 300}