<commit_before>'use strict';

var q = require('q'),
    MongoClient = require('mongodb').MongoClient,
    ObjectID = require('mongodb').ObjectID,
    logger = require('./logger');

module.exports = {
    buildConfig: buildConfig,
    testConnection: testConnection,
    createSampleData: createSampleData
};

function buildConfig(url){
    var cleanUrl = url.replace('mongodb://', ''),
        parts = cleanUrl.split('/'),
        dbname = parts[parts.length - 1],
        auth = { username: '', password: '' },
        authpart,
        creds;

    if(parts[0].indexOf('@') > -1) {
        authpart = parts[0].split('@');
        creds = authpart[0].split(':');
        cleanUrl = cleanUrl.replace(authpart[0] + '@', '');

        auth.username = creds[0];
        auth.password = <PASSWORD>[1];
    }

    return {
        host: 'mongodb://' + cleanUrl,
        name: dbname,
        username: auth.username,
        password: <PASSWORD>,
        shorthost: cleanUrl.replace('/' + dbname, '')
    };
}

function testConnection(url){
    var deferred = q.defer();

    try {
        MongoClient.connect( url , function(err, db) {
            if(err) {
                deferred.reject(err);
                return;
            }

            db.close();
            deferred.resolve(true);
        });
    }
    catch(ex){
        deferred.reject(ex);
    }

    return deferred.promise;
}


function createSampleData(config){
    var deferred = q.defer();

    importData(config.db.host, 'users', require('../../sampledata/basic/users')(ObjectID)[0]).then(
        function(){
            deferred.resolve(config)
        }
    );

    return deferred.promise;
}

function importData(host, col, obj){
    var deferred = q.defer();

    MongoClient.connect(host, function(err, db) {
        if (err) logger.error(err.message);
        db.collection(col, function(err, collection){
            if (err) logger.error(err.message);
            collection.insert(obj, function(err){
                if (err) logger.error(err.message);<commit_msg>Added new code to import sample data.<commit_after>'use strict';

var q = require('q'),
    MongoClient = require('mongodb').MongoClient,
    ObjectID = require('mongodb').ObjectID,
    logger = require('./logger');

module.exports = {
    buildConfig: buildConfig,
    testConnection: testConnection,
    createSampleData: createSampleData
};

function buildConfig(url){
    var cleanUrl = url.replace('mongodb://', ''),
        parts = cleanUrl.split('/'),
        dbname = parts[parts.length - 1],
        auth = { username: '', password: '' },
        authpart,
        creds;

    if(parts[0].indexOf('@') > -1) {
        authpart = parts[0].split('@');
        creds = authpart[0].split(':');
        cleanUrl = cleanUrl.replace(authpart[0] + '@', '');

        auth.username = creds[0];
        auth.password = <PASSWORD>[1];
    }

    return {
        host: 'mongodb://' + cleanUrl,
        name: dbname,
        username: auth.username,
        password: <PASSWORD>,
        shorthost: cleanUrl.replace('/' + dbname, '')
    };
}

function testConnection(url){
    var deferred = q.defer();

    try {
        MongoClient.connect( url , function(err, db) {
            if(err) {
                deferred.reject(err);
                return;
            }

            db.close();
            deferred.resolve(true);
        });
    }
    catch(ex){
        deferred.reject(ex);
    }

    return deferred.promise;
}


function createSampleData(config){
    var deferred = q.defer(),
        collections = [save('users'), save('contenttypes'), save('nodes'), save('content')];

    collections.reduce(q.when, q({})).then(
        function(){
            deferred.resolve(config)
        },
        function(err){
            logger.error('');
            logger.error(err.message);
            deferred.reject(err);
        }
    );

    function save(colName){
        var deferred = q.defer(),
            items = require('../../sampledata/basic/' + colName)(ObjectID),
            instances = [];

        items.forEach(function(item){
            instances.push(commit(item));
        });

        instances.reduce(q.when, q({})).then(
            function(){
                deferred.resolve()
            },
            function(err){
                logger.error('');
                logger.error(err.message);
                deferred.reject(err);
            }
        );

        return deferred.promise;

        function commit(obj) {
            return function(){
                var def = q.defer();

                importData(config.db.host, colName, obj).then(
                    function(){
                        def.resolve();
                    }
                );

                return def.promise;
            };
        }
    }


    return deferred.promise;
}

function importData(host, col, obj){
    var deferred = q.defer();

    MongoClient.connect(host, function(err, db) {
        if (err) logger.error(err.message);
        db.collection(col, function(err, collection){
            if (err) logger.error(err.message);
            collection.insert(obj, function(err){
                if (err) logger.error(err.message);
{'fraction_non_alphanumeric': 0.15300441543482435, 'fraction_numerical': 0.003263582261470532, 'mean_word_length': 1.8992765720645521, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '2836435', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1480, 'n_words': 274}