<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 winston = require('winston');
var path = require('path');
// find the first module to be loaded
var topModule = module;
while(topModule.parent)
    topModule = topModule.parent;
var appDir = path.dirname(topModule.filename);

var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: appDir +'/constraints.log' })
    ]
});


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
        logger.log('info', 'Query execution started', {
            expression: expression,
            language: language
        });
        var start = process.hrtime();
        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);
            } else {
                var elapsed_time = function(note){
                    var precision = 3; // 3 decimal places
                    var elapsed = process.hrtime(start)[1] / 1000000; // divide by a million to get nano to milli
                    console.log(process.hrtime(start)[0] + " s, " + elapsed.toFixed(precision) + " ms - " + note); // print message + time
                    start = process.hrtime(); // reset the timer
                };
                logger.log('info', 'Query execution finished', {
                    expression: expression,
                    language: language,
                    time: elapsed_time()
                });
                responseData.computeResponse = results;
                res.send(responseData);
            }
        });
    } else {
        res.status(400);
        res.send(responseData);
    }
});

module.exports = router;<commit_msg>Adding logs for constraint executions<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 winston = require('winston');
var path = require('path');
// find the first module to be loaded
var topModule = module;
while(topModule.parent)
    topModule = topModule.parent;
var appDir = path.dirname(topModule.filename);

var logger = new (winston.Logger)({
    transports: [
        new (winston.transports.Console)(),
        new (winston.transports.File)({ filename: appDir +'/constraints.log' })
    ]
});


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
        logger.log('info', 'Query execution started', {
            expression: expression,
            language: language
        });
        var start = process.hrtime();
        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);
            } else {
                var elapsed_time = function(note){
                    var precision = 3; // 3 decimal places
                    var elapsed = process.hrtime(start)[1] / 1000000; // divide by a million to get nano to milli
                    //console.log(process.hrtime(start)[0] + " s, " + elapsed.toFixed(precision) + " ms - " + note); // print message + time
                    start = process.hrtime(); // reset the timer
                    return elapsed.toFixed(precision);
                };
                logger.log('info', 'Query execution finished', {
                    expression: "\" " + expression + " \"",
                    language: language,
                    time: elapsed_time()
                });
                responseData.computeResponse = results;
                res.send(responseData);
            }
        });
    } else {
        res.status(400);
        res.send(responseData);
    }
});

module.exports = router;
{'fraction_non_alphanumeric': 0.13714581446500193, 'fraction_numerical': 0.011126924569802044, 'mean_word_length': 2.204809286898839, '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': '5134786', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2050, 'n_words': 455}