<commit_before>/*
 * Copyright 2000-2013 Namics AG. All rights reserved.
 */
/**
 * @author: dnyffenegger, Namics AG
 * @since 07.12.13
 */

var MongoClient = require('mongodb').MongoClient
    , ObjectID = require('mongodb').ObjectID
    , _ = require('underscore')
    , moment = require('moment')
    , Q = require('q')
    ;

module.exports = function(settings) {

    var resolveOrReject = function(deferred, err, errMsg, value) {
        if (err) {
            deferred.reject(errMsg + ': ' + err.message);
        } else {
            if (value && typeof value === "function" )
                deferred.resolve(value());
            else
                deferred.resolve(value);
        }
    };

    var getDb = function() {
        var deferred = Q.defer();
        MongoClient.connect(settings.db, function(err, db) {
            resolveOrReject(deferred, err, 'Fehler beim Verbinden auf die Datenbank', db);
        });
        return deferred.promise;
    };

    var getCollection = function(collectionName) {
        return getDb().then(function(db){
            var deferred = Q.defer();
            db.collection(collectionName, function(err, collection){
                var ctx = {collection: collection, db: db};
                resolveOrReject(deferred, err, 'Fehler beim Zugriff auf ' + collectionName, ctx);
            });
            return deferred.promise;
        });
    };

    var findWithCursor = function(collectionName, query, options) {
        return getCollection(collectionName).then(function(ctx){
            var deferred = Q.defer();
            ctx.collection.find(query, options, function(err, cursor){
                ctx.cursor = cursor;
                resolveOrReject(deferred, err, 'Fehler bei der Abfrage von ' + collectionName, ctx);
            });
            return deferred.promise;
        });
    };

    var findAsArray = function(collectionName, query, options) {
        return getCollection(collectionName).then(function(ctx){
            var deferred = Q.defer();
            ctx.collection.find({}, options).toArray(function(err, docs){
                ctx.docs = docs;
                resolveOrReject(deferred, err, 'Fehler bei der Abfrage von ' + collectionName, ctx);
            });
            return deferred.promise;
        });
    };


    // ATTENTION: assumes the there are no orders on days in future
    var getNextOrderNo = function() {
        // select only orders with order numbers
        var query = { no: { $gt: 0} };
        var sort = [
            ['_id', -1], // first by id descending which means the most recent order
            ['no', -1] // then by order number
        ];
        return findWithCursor("orders", query, {sort: sort, limit: 1}).then(function(ctx){
            var deferred = Q.defer();
            ctx.cursor.nextObject(function(err, doc){
                resolveOrReject(deferred, err, 'Fehler beim Bestimmen der nächsten Bestellnummer', function(){
                    var orderNo = 1;
                    if (doc && moment().startOf('day').isBefore(moment(doc._id.getTimestamp()))) {
                        console.log('Found an order today with a order.no: ' + doc.no);
                        orderNo = doc.no + 1;
                    } else {
                        console.log('No orders yet with an no, use 1');
                    }
                    ctx.orderNo = orderNo;
                    return ctx;
                });
            });
            return deferred.promise;
        });
    };

    var getIdQuery = function(id) {
        return {_id: ObjectID(id)}
    };


    var getAllDocsAsArray = function(collectionName, options) {
        return findAsArray(collectionName, {}, options).then(function(ctx){
            var deferred = Q.defer();
            deferred.resolve(ctx.docs);
            deferred.promise.finally(function(){ ctx.db.close(); });<commit_msg>Removes log statements<commit_after>/*
 * Copyright 2000-2013 Namics AG. All rights reserved.
 */
/**
 * @author: dnyffenegger, Namics AG
 * @since 07.12.13
 */

var MongoClient = require('mongodb').MongoClient
    , ObjectID = require('mongodb').ObjectID
    , _ = require('underscore')
    , moment = require('moment')
    , Q = require('q')
    ;

module.exports = function(settings) {

    var resolveOrReject = function(deferred, err, errMsg, value) {
        if (err) {
            deferred.reject(errMsg + ': ' + err.message);
        } else {
            if (value && typeof value === "function" )
                deferred.resolve(value());
            else
                deferred.resolve(value);
        }
    };

    var getDb = function() {
        var deferred = Q.defer();
        MongoClient.connect(settings.db, function(err, db) {
            resolveOrReject(deferred, err, 'Fehler beim Verbinden auf die Datenbank', db);
        });
        return deferred.promise;
    };

    var getCollection = function(collectionName) {
        return getDb().then(function(db){
            var deferred = Q.defer();
            db.collection(collectionName, function(err, collection){
                var ctx = {collection: collection, db: db};
                resolveOrReject(deferred, err, 'Fehler beim Zugriff auf ' + collectionName, ctx);
            });
            return deferred.promise;
        });
    };

    var findWithCursor = function(collectionName, query, options) {
        return getCollection(collectionName).then(function(ctx){
            var deferred = Q.defer();
            ctx.collection.find(query, options, function(err, cursor){
                ctx.cursor = cursor;
                resolveOrReject(deferred, err, 'Fehler bei der Abfrage von ' + collectionName, ctx);
            });
            return deferred.promise;
        });
    };

    var findAsArray = function(collectionName, query, options) {
        return getCollection(collectionName).then(function(ctx){
            var deferred = Q.defer();
            ctx.collection.find({}, options).toArray(function(err, docs){
                ctx.docs = docs;
                resolveOrReject(deferred, err, 'Fehler bei der Abfrage von ' + collectionName, ctx);
            });
            return deferred.promise;
        });
    };


    // ATTENTION: assumes the there are no orders on days in future
    var getNextOrderNo = function() {
        // select only orders with order numbers
        var query = { no: { $gt: 0} };
        var sort = [
            ['_id', -1], // first by id descending which means the most recent order
            ['no', -1] // then by order number
        ];
        return findWithCursor("orders", query, {sort: sort, limit: 1}).then(function(ctx){
            var deferred = Q.defer();
            ctx.cursor.nextObject(function(err, doc){
                resolveOrReject(deferred, err, 'Fehler beim Bestimmen der nächsten Bestellnummer', function(){
                    var orderNo = 1;
                    if (doc && moment().startOf('day').isBefore(moment(doc._id.getTimestamp()))) {
                        //console.log('Found an order today with a order.no: ' + doc.no);
                        orderNo = doc.no + 1;
                    } else {
                        //console.log('No orders yet with an no, use 1');
                    }
                    ctx.orderNo = orderNo;
                    return ctx;
                });
            });
            return deferred.promise;
        });
    };

    var getIdQuery = function(id) {
        return {_id: ObjectID(id)}
    };


    var getAllDocsAsArray = function(collectionName, options) {
        return findAsArray(collectionName, {}, options).then(function(ctx){
            var deferred = Q.defer();
            deferred.resolve(ctx.docs);
            deferred.promise.finally(function(){ ctx.db.close(); });
{'fraction_non_alphanumeric': 0.12322274881516587, 'fraction_numerical': 0.005379787370308697, 'mean_word_length': 1.8737578211262422, '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': '5343427', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2065, 'n_words': 526}