<commit_before>/**
 * Created by alo on 7/9/15.
 */
var express = require('express');
var router = express.Router();
var expressionsParser = require('../grammars/apg/expressionParser');
var MongoClient = require('mongodb').MongoClient;
var connectTimeout = require('connect-timeout');

var databases = {};

var performMongoDbRequest = function(databaseName, callback) {
    if (databases[databaseName]) {
        //console.log("Using cache");
        callback(databases[databaseName]);
    } else {
        //console.log("Connecting");
        MongoClient.connect("mongodb://localhost:27017/"+databaseName, function(err, db) {
            if (err) {
                res.status(500);
                res.send(err.message);
                return;
            }
            //console.log("Connection OK")
            databases[databaseName] = db;
            callback(db);
        });
    }
};

/**
 * Parses the expression in the body
 */
router.post('/parse/:language', function (req, res) {
    var expression = req.body.expression.replace(/[^\x00-\x7F]/g, "");
    var language = req.param("language");
    var results = expressionsParser.parse(expression, language);
    res.send(results);
});

router.post('/:db/:collection/execute/:language', connectTimeout('120s'), function (req, res) {
    var request = req.body;
    var expression = request.expression.replace(/[^\x00-\x7F]/g, "");
    var language = req.param("language");
    var collectionName = req.params.collection;
    var results = expressionsParser.parse(expression, language);
    var responseData = {
        paserResponse: results,
        computeResponse: {}
    };
    if (results.validation) {
        // Execute query
        computeGrammarQuery3(results, request.form, req.params.db, collectionName, request.skip, request.limit, function(err, results) {
            if (err) {
                responseData.computeResponse = err;
                res.status(500);
                res.send(responseData);<commit_msg>Adding expressions support<commit_after>/**
 * Created by alo on 7/9/15.
 */
var express = require('express');
var router = express.Router();
var expressionsParser = require('../grammars/apg/expressionParser');
var MongoClient = require('mongodb').MongoClient;
var connectTimeout = require('connect-timeout');

var databases = {};

var performMongoDbRequest = function(databaseName, callback) {
    if (databases[databaseName]) {
        //console.log("Using cache");
        callback(databases[databaseName]);
    } else {
        //console.log("Connecting");
        MongoClient.connect("mongodb://localhost:27017/"+databaseName, function(err, db) {
            if (err) {
                res.status(500);
                res.send(err.message);
                return;
            }
            //console.log("Connection OK")
            databases[databaseName] = db;
            callback(db);
        });
    }
};

/**
 * Parses the expression in the body
 */
router.post('/parse/:language', function (req, res) {
    var expression = req.body.expression.replace(/[^\x00-\x7F]/g, "");
    var language = req.param("language");
    var results = expressionsParser.parse(expression, language);
    res.send(results);
});

router.post('/:db/:collection/execute/:language', connectTimeout('120s'), function (req, res) {
    var request = req.body;
    var expression = request.expression.replace(/[^\x00-\x7F]/g, "");
    var language = req.param("language");
    var collectionName = req.params.collection;
    var results = expressionsParser.parse(expression, language);
    var responseData = {
        paserResponse: results,
        computeResponse: {}
    };
    if (!results.validation) {
        if (expression.charAt(0) == "(" && expression.charAt(expression.length-1) == ")") {
            expression = expression.substr(1, expression.length - 2);
            results = expressionsParser.parse(expression, language);
            responseData.paserResponse = results;
        }
    }
    if (results.validation) {
        // Execute query
        computeGrammarQuery3(results, request.form, req.params.db, collectionName, request.skip, request.limit, function(err, results) {
            if (err) {
                responseData.computeResponse = err;
                res.status(500);
                res.send(responseData);
{'fraction_non_alphanumeric': 0.14649386716037954, 'fraction_numerical': 0.012497107151122426, 'mean_word_length': 2.675170068027211, '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': '2933214', 'n_tokens_mistral': 1226, 'n_tokens_neox': 1187, 'n_words': 239}