<commit_before>'use strict';

/*!
 * Module dependancies
 */
var fs = require('fs');
var restify = require('restify');
var cp = require('child_process')
var es = require('event-stream');
var _ = require('underscore');
var async = require('async');


module.exports = function(ummon){
  var api = {};


  api.doesCollectionExist = function(req, res, next) {
    var collection = req.params.collection

    if (ummon.config.collections[collection]) {
      next();
    } else {
      return next(new restify.ResourceNotFoundError('No collection of name '+collection+' found'));
    }
  };


  api.doesTaskExist = function(req, res, next) {
    if (!(req.params.taskid in ummon.tasks)) {
      return next(new restify.ResourceNotFoundError('Task not found! Consider broadening your search to a collection'));
    } else {
      next();
    }
  };

  /**
   * Return the configuration object
   */
  api.getConfig = function(req, res, next) {
    res.json(200, ummon.config);
    next();
  };


  /**
   * Update the configuration
   *
   * Currently limited to only top level of config. So
   * changing log.path won't work just yet
   */
  api.setConfig = function(req, res, next) {
    _.each(req.query, function(value, key) {
      // Convert strings for true and false to boolean
      if (value == "true" || value == "false") {
        value = (value == "true") ? true : false;

      } else if (!isNaN(value)) {
        value = +value;
      }

      ummon.config[key] = value;
    })

    res.json(200, ummon.config);
    next();
  };


  /**
   * What tasks are running
   */
  api.ps = function(req, res, next){
    var pids = Object.keys(ummon.workers);

    res.json(200, {
      "count":pids.length,
      "pids": pids,
      "runs": _.pluck(ummon.workers, 'run')
    });
    next();
  };


  /**
   * Return a snapshot of what is going on
   *
   * returns an object like:
   *
   *   {
   *     "workers": [...],
   *     "queue": [...],
   *     "activeTimers": 1,
   *     "isPaused": falase,
   *     "maxWorkers": 10,
   *     "collections": 2,
   *     "totalTasks":
   *   }
   */
  api.getStatus = function(req, res, next){
    var pids = Object.keys(ummon.workers);

    var workers = (_.size(ummon.workers))
      ? _.map(ummon.workers, function(worker) {return worker.run.task.id})
      : [];

    var queuedItems = (_.size(ummon.queue.items))
      ? _.map(ummon.queue.items, function(item) {return item.task.id})
      : [];

    res.json(200, {
      "workers": workers,
      "queue": queuedItems,
      "activeTimers": Object.keys(ummon.timers),
      "isPaused": ummon.config.pause,
      "maxWorkers": ummon.MAX_WORKERS,
      "collections": ummon.getCollections(),
      "totalTasks": _.size(ummon.tasks)
    });
    next();
  };


  /**
   * Get a number of tasks. Could be for a specific colleciton
   * or all configured tasks
   *
   * Returns:
   *   {
   *     'collectionName': {
   *       'defaults': { ...defaults... },
   *       'tasks': { ...tasks... }
   *     }
   *   }
   *
   * @param  {[type]}   req
   * @param  {[type]}   res
   * @param  {Function} next The callback
   * @return {[type]}        Heavily structured object. See above
   */

  api.getTasks = function(req, res, next) {
    var filter = req.params.collection || req.params.taskid || false;

    ummon.getTasks(filter, function(err, collections){
      if (err) {
        return next(err);
      }
      res.json(200, { 'collections': collections } );
      next();
    });
  };


  api.getTask = function(req, res, next){
    var p = req.params;

    ummon.getTask(p.taskid, function(err, task){
      if (err) {
        return next(err);
      }
      res.json(200, { 'task': task } );
      next();
    });
  };


  /**
   * PUT a collection
   *
   * @param {[type]}   req  [description]
   * @param {[type]}   res  [description]
   * @param {Function} next [description]
   */<commit_msg>Proper 404 if collection doesn't exist<commit_after>'use strict';

/*!
 * Module dependancies
 */
var fs = require('fs');
var restify = require('restify');
var cp = require('child_process')
var es = require('event-stream');
var _ = require('underscore');
var async = require('async');


module.exports = function(ummon){
  var api = {};


  api.doesCollectionExist = function(req, res, next) {
    var collection = req.params.collection

    if (ummon.config.collections[collection]) {
      next();
    } else {
      return next(new restify.ResourceNotFoundError('No collection of name '+collection+' found'));
    }
  };


  api.doesTaskExist = function(req, res, next) {
    if (!(req.params.taskid in ummon.tasks)) {
      return next(new restify.ResourceNotFoundError('Task not found! Consider broadening your search to a collection'));
    } else {
      next();
    }
  };

  /**
   * Return the configuration object
   */
  api.getConfig = function(req, res, next) {
    res.json(200, ummon.config);
    next();
  };


  /**
   * Update the configuration
   *
   * Currently limited to only top level of config. So
   * changing log.path won't work just yet
   */
  api.setConfig = function(req, res, next) {
    _.each(req.query, function(value, key) {
      // Convert strings for true and false to boolean
      if (value == "true" || value == "false") {
        value = (value == "true") ? true : false;

      } else if (!isNaN(value)) {
        value = +value;
      }

      ummon.config[key] = value;
    })

    res.json(200, ummon.config);
    next();
  };


  /**
   * What tasks are running
   */
  api.ps = function(req, res, next){
    var pids = Object.keys(ummon.workers);

    res.json(200, {
      "count":pids.length,
      "pids": pids,
      "runs": _.pluck(ummon.workers, 'run')
    });
    next();
  };


  /**
   * Return a snapshot of what is going on
   *
   * returns an object like:
   *
   *   {
   *     "workers": [...],
   *     "queue": [...],
   *     "activeTimers": 1,
   *     "isPaused": falase,
   *     "maxWorkers": 10,
   *     "collections": 2,
   *     "totalTasks":
   *   }
   */
  api.getStatus = function(req, res, next){
    var pids = Object.keys(ummon.workers);

    var workers = (_.size(ummon.workers))
      ? _.map(ummon.workers, function(worker) {return worker.run.task.id})
      : [];

    var queuedItems = (_.size(ummon.queue.items))
      ? _.map(ummon.queue.items, function(item) {return item.task.id})
      : [];

    res.json(200, {
      "workers": workers,
      "queue": queuedItems,
      "activeTimers": Object.keys(ummon.timers),
      "isPaused": ummon.config.pause,
      "maxWorkers": ummon.MAX_WORKERS,
      "collections": ummon.getCollections(),
      "totalTasks": _.size(ummon.tasks)
    });
    next();
  };


  /**
   * Get a number of tasks. Could be for a specific colleciton
   * or all configured tasks
   *
   * Returns:
   *   {
   *     'collectionName': {
   *       'defaults': { ...defaults... },
   *       'tasks': { ...tasks... }
   *     }
   *   }
   *
   * @param  {[type]}   req
   * @param  {[type]}   res
   * @param  {Function} next The callback
   * @return {[type]}        Heavily structured object. See above
   */

  api.getTasks = function(req, res, next) {
    var filter = req.params.collection || req.params.taskid || false;

    ummon.getTasks(filter, function(err, collections){
      if (err) {
        if (err.message === "There is no tasks or collections that match the provided filter") {
          return next(new restify.ResourceNotFoundError(err.message));
        } else {
          return next(err);
        }
      }
      res.json(200, { 'collections': collections } );
      next();
    });
  };


  api.getTask = function(req, res, next){
    var p = req.params;

    ummon.getTask(p.taskid, function(err, task){
      if (err) {
        return next(err);
      }
      res.json(200, { 'task': task } );
      next();
    });
  };


  /**
   * PUT a collection
   *
   * @param {[type]}   req  [description]
   * @param {[type]}   res  [description]
   * @param {Function} next [description]
   */
{'fraction_non_alphanumeric': 0.1634735008708634, 'fraction_numerical': 0.00584722567802936, 'mean_word_length': 2.3068696009872482, 'pattern_counts': {'":': 34, '<': 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': '3640421', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2630, 'n_words': 624}