<commit_before>var _ = require('underscore');
var loopback = require('loopback');
var async = require('async');
var request = require('request');

var return_response = function(request, responses, cb) {
    responses = _.reject(responses, function(response) { return response.error });
    responses = _.map(responses, function(response) { return response.body['suggestions'] });
    var documentIds = _.uniq(_.without(_.flatten(_.zip.apply(_, responses)), undefined));
    if(documentIds.length === 0) {
        documentIds = [7, 1, 4, 14, 66, 9];
    }

    var Document = loopback.getModel('document');
    Document.find({'where': {'id': {'inq': documentIds}}}, function(err, documents) {
        documentsById = _.indexBy(documents, 'id');
        documents = _.map(documentIds, function(documentId) { return documentsById[documentId] });
        cb(null, {documents: documents, requestId: request.id});
    });
}

var post_with_timeout = function(url, options, cb) {
    var timeout_error = 'Timed_out!';
    async.parallel([
        function(cb) {
            request.post(url, options, function(err, response, body) {
                cb(err, {response: response, body: body})
            })
        },
        function(cb) {
            setTimeout(function() {
                cb(timeout_error, null);
            }, 1000);
        }
    ], function(err, results) {
        if(results[0] && err == timeout_error) {
            err = null;
        }
        cb(err, results[0] && results[0].response, results[0] && results[0].body)
    });
}<commit_msg>Fixed suggestion timeout error code.<commit_after>var _ = require('underscore');
var loopback = require('loopback');
var async = require('async');
var request = require('request');

var return_response = function(request, responses, cb) {
    responses = _.reject(responses, function(response) { return response.error });
    responses = _.map(responses, function(response) { return response.body['suggestions'] });
    var documentIds = _.uniq(_.without(_.flatten(_.zip.apply(_, responses)), undefined));
    if(documentIds.length === 0) {
        documentIds = [7, 1, 4, 14, 66, 9];
    }

    var Document = loopback.getModel('document');
    Document.find({'where': {'id': {'inq': documentIds}}}, function(err, documents) {
        documentsById = _.indexBy(documents, 'id');
        documents = _.map(documentIds, function(documentId) { return documentsById[documentId] });
        cb(null, {documents: documents, requestId: request.id});
    });
}

var post_with_timeout = function(url, options, cb) {
    async.parallel([
        function(cb) {
            request.post(url, options, function(err, response, body) {
                err = err || "No error";
                cb(err, {response: response, body: body})
            })
        },
        function(cb) {
            setTimeout(function() {
                cb("Timed out.", null);
            }, 3000);
        }
    ], function(err, results) {
        if(err == "No error") {
            err = null;
        }
        cb(err, results[0] && results[0].response, results[0] && results[0].body)
    });
}
{'fraction_non_alphanumeric': 0.16080562659846548, 'fraction_numerical': 0.01118925831202046, 'mean_word_length': 2.848708487084871, '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': '3478764', 'n_tokens_mistral': 931, 'n_tokens_neox': 894, 'n_words': 204}