<commit_before>var cache = require('memory-cache');
var Stat = require('./Stat');
var stats = require('./lib/stats');



module.exports = function(app){
  app.get('/stats/:mountain',handleStats);
};

function hash(mountain){
  return 'mt-' + mountain;
}

function handleStats(req,res){
  if(Stat.Mountains.indexOf(req.params.mountain) == -1){
    res.statusCode = 404;
    return res.send();
  }

  var k = hash(req.params.mountain);
  var v = cache.get(k);
  if(v !== null)
    return respond(v);

  function respond(obj){
    res.set('Cache-Control', 'public, s-maxage=5');
    res.set('Expires', '5');
    res.send(obj);
  }

  stats.currentBusyIndex(req.params.mountain,function(err,obj){
    if(err){
      res.statusCode = 500;
      return res.send();
    }

    cache.put(k,obj,5*60*1000);
    respond(obj);<commit_msg>Returns all stats now<commit_after>var cache = require('memory-cache');
var async = require('async');
var Stat = require('./Stat');
var stats = require('./lib/stats');

module.exports = function(app){
  app.get('/stats/all',handleStats);
};


function getStat(mountain,callback){
  stats.currentBusyIndex(mountain,function(err,obj){
    if(err)
      return callback(err);

    obj.mountain = mountain;
    callback(null,obj);
  });
}


function handleStats(req,res){
  var k = 'all-stats';
  var v = cache.get(k);
  if(v !== null)
    return respond(v);

  function respond(obj){
    res.set('Cache-Control', 'public, s-maxage=5');
    res.set('Expires', '5');
    res.send(obj);
  }

  async.map(Stat.Mountains,getStat,function(err,results){
    if(err){
      res.statusCode = 500;
      return res.send({});
    }
    
    cache.put(k,results,5*60*1000);
    respond(results);
{'fraction_non_alphanumeric': 0.18629173989455183, 'fraction_numerical': 0.016403046280023433, 'mean_word_length': 3.6666666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5129170', 'n_tokens_mistral': 621, 'n_tokens_neox': 592, 'n_words': 105}