<commit_before>module.exports = function() {

    var MongoClient = require('mongodb').MongoClient;

    var EVENT_TYPES = ['view', 'call', 'email'];

    function connectToMongo(onSuccess) {
        MongoClient.connect(
            process.env.MONGOHQ_URL,
            function onConnect(err, db) {
                if (err) {
                    throw err;
                }
                console.log("Connected to MongoDB...");

                process.nextTick(
                    function() {
                        onSuccess(db);
                    }
                );
            }
        );

    }

    function sendToMongo(event_type, db) {
        var new_doc = {"event_type": event_type};

        db.collection("log_totals").insert(
                new_doc,
                function(err, result) {
                    if (err) {
                        throw err;
                    }

                    console.log("Added event to MongoDB");
                }
        );
    }

    function success(res) {
        res.send({
            message: 'Event logged'
        });
    }

    function fail(res) {
        res.send({
            message: 'Failed to log'
        });
    }

    function main(req, res) {

        var event_type = req.query.event;

        if (EVENT_TYPES.indexOf(event_type) > -1) {
            console.log("LOG A "+event_type+" EVENT");

            connectToMongo(
                    function(db) {
                        sendToMongo(event_type, db);
                        success(res);
                    }
            );
            return;
        }

        fail(res);
        return;

    };

    return main;

}();
<commit_msg>Updated /log route to increment event totals<commit_after>module.exports = function() {

    var MongoClient = require('mongodb').MongoClient;

    var EVENT_TYPES = ['view', 'call', 'email'];
    var COLLECTION_NAME = 'log_totals';
    var OVERALL_TOTALS_ID = 'overall_totals';

    function connectToMongo(onSuccess) {
        MongoClient.connect(
            process.env.MONGOHQ_URL,
            function onConnect(err, db) {
                if (err) {
                    throw err;
                }
                console.log("Connected to MongoDB...");

                process.nextTick(
                    function() {
                        onSuccess(db);
                    }
                );
            }
        );

    }

    function sendToMongo(event_type, db) {

        query = {_id: OVERALL_TOTALS_ID};

        inc_update = {$inc: {} };
        inc_update['$inc'][event_type] = 1;

        db.collection(COLLECTION_NAME).update(
            query,
            inc_update,
            function(err, result) {
                if (err) {
                    throw err;
                }

                console.log("Added event to MongoDB");
            }
        );
    }

    function success(res) {
        res.send({
            message: 'Event logged'
        });
    }

    function fail(res) {
        res.send({
            message: 'Failed to log'
        });
    }

    function main(req, res) {

        var event_type = req.query.event;

        if (EVENT_TYPES.indexOf(event_type) > -1) {
            console.log("LOG A "+event_type+" EVENT");

            connectToMongo(
                    function(db) {
                        sendToMongo(event_type, db);
                        success(res);
                    }
            );
            return;
        }

        fail(res);
        return;

    };

    return main;

}();

{'fraction_non_alphanumeric': 0.11051743532058493, 'fraction_numerical': 0.000843644544431946, 'mean_word_length': 1.1401925391095067, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 5, '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': '3963696', 'n_tokens_mistral': 1028, 'n_tokens_neox': 944, 'n_words': 180}