<commit_before>/**
 * Module dependencies.
 */

var Check           = require('../../../models/check');
var CheckHourlyStat = require('../../../models/checkHourlyStat');

/**
 * Check Routes
 */
module.exports = function(app) {
  
  app.get('/check', function(req, res) {
    Check.byUptime().find({}).asc('isUp').desc('lastChanged').exclude('qosPerHour').run(function(err, checks) {
      res.json(checks);
    });
  });
  
  app.get('/check/:name', function(req, res, next) {
    Check.find({ name: req.params.name }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.name));
      res.json(check);
    });
  });

  app.get('/check/:id/stat/:page?', function(req, res) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      CheckHourlyStat.find({ check: check }).desc('timestamp').limit(50).skip(req.params.page * 50).run(function(err, stats) {
        if (err) return next(err);
        if (!stats) return next(new Error('failed to load stats for check ' + req.params.id));
        res.json(stats);
      })
    });
  });

  app.get('/check/:id/hourlyUptime', function(req, res) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      var uptimes = [];
      CheckHourlyStat.find({ check: check, timestamp: { $gte: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000) } }).asc('timestamp').each(function(err, stat) {
        if (err) return next(err);
        if (stat) {
          uptimes.push([Date.parse(stat.timestamp), (stat.ups / stat.count).toFixed(5) * 100]);
        } else {
          res.json(uptimes);
        }
      })
    });
  });

  app.get('/check/:id/hourlyResponseTime', function(req, res) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      var responseTimes = [];
      CheckHourlyStat.find({ check: check, timestamp: { $gte: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000) } }).asc('timestamp').each(function(err, stat) {
        if (err) return next(err);
        if (stat) {
          responseTimes.push([Date.parse(stat.timestamp), Math.round(stat.time / stat.count)]);
        } else {
          res.json(responseTimes);
        }
      })
    });
  });

};<commit_msg>Update check API for better consistence<commit_after>/**
 * Module dependencies.
 */

var Check           = require('../../../models/check');
var CheckHourlyStat = require('../../../models/checkHourlyStat');

/**
 * Check Routes
 */
module.exports = function(app) {
  
  app.get('/check', function(req, res) {
    Check.byUptime().find({}).asc('isUp').desc('lastChanged').exclude('qosPerHour').run(function(err, checks) {
      res.json(checks);
    });
  });
  
  app.get('/check/:id', function(req, res, next) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      res.json(check);
    });
  });

  app.get('/check/:id/stat/:page?', function(req, res) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      CheckHourlyStat.find({ check: check }).desc('timestamp').limit(50).skip(req.params.page * 50).run(function(err, stats) {
        if (err) return next(err);
        if (!stats) return next(new Error('failed to load stats for check ' + req.params.id));
        res.json(stats);
      })
    });
  });

  app.get('/check/:id/hourlyUptime', function(req, res) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      var uptimes = [];
      CheckHourlyStat.find({ check: check, timestamp: { $gte: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000) } }).asc('timestamp').each(function(err, stat) {
        if (err) return next(err);
        if (stat) {
          uptimes.push([Date.parse(stat.timestamp), (stat.ups / stat.count).toFixed(5) * 100]);
        } else {
          res.json(uptimes);
        }
      })
    });
  });

  app.get('/check/:id/hourlyResponseTime', function(req, res) {
    Check.find({ _id: req.params.id }).exclude('qosPerHour').findOne(function(err, check) {
      if (err) return next(err);
      if (!check) return next(new Error('failed to load check ' + req.params.id));
      var responseTimes = [];
      CheckHourlyStat.find({ check: check, timestamp: { $gte: new Date(Date.now() - 14 * 24 * 60 * 60 * 1000) } }).asc('timestamp').each(function(err, stat) {
        if (err) return next(err);
        if (stat) {
          responseTimes.push([Date.parse(stat.timestamp), Math.round(stat.time / stat.count)]);
        } else {
          res.json(responseTimes);
        }
      })
    });
  });

};
{'fraction_non_alphanumeric': 0.19618730334999074, 'fraction_numerical': 0.011845271145659819, 'mean_word_length': 2.871060171919771, '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': '3706712', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1779, 'n_words': 369}