<commit_before>var ce = require('cloneextend');
var seq = require('seq');
var squel = require('squel');

function UserPersistence (database) {
    this._db = database;
}

UserPersistence.prototype.loadUsers = function (limitStatement, orderStatement, callback) {
    var query = this._createUserQuery();

    if (limitStatement !== null) {
        query.offset(limitStatement.offset());
        query.limit(limitStatement.limit());
    }

    this._db.selectMany(query.toString(), [], callback);
};

UserPersistence.prototype.loadUserById = function (userId, callback) {
    var query = this._createUserQuery()
        .where('users.id = ?', userId);

    var parametrized = query.toParam();

    this._db.selectOne(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.loadUserByName = function (name, callback) {
    var query = this._createUserQuery()
        .where('lower(users.name) = ?', name.toLowerCase());

    var parametrized = query.toParam();

    this._db.selectOne(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.createUser = function (name, callback) {
    var query = squel.insert()
        .into('users')
        .set('name', name);

    var parametrized = query.toParam();

    this._db.query(parametrized.text, parametrized.values, function (error, result) {
        if (error) return callback(error, null);

        callback(null, result.lastID);
    });
};

UserPersistence.prototype.createTransaction = function (userId, amount, callback) {
    var query = squel.insert()
        .into('transactions')
        .set('userId', userId)
        .set('value', amount);

    var parametrized = query.toParam();

    this._db.query(parametrized.text, parametrized.values, function (error, result) {
        if (error) return callback(error, null);

        callback(null, result.lastID);
    });
};

UserPersistence.prototype.loadTransaction = function (transactionId, callback) {
    var query = this._createTransactionQuery()
        .where('id = ?', transactionId);

    var parametrized = query.toParam();

    this._db.selectOne(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.loadTransactionsByUserId = function (userId, limitStatement, orderStatement, callback) {
    var query = this._createTransactionQuery()
        .where('userId = ?', userId);

    if (limitStatement !== null) {
        query.offset(limitStatement.offset());
        query.limit(limitStatement.limit());
    }

    var parametrized = query.toParam();

    this._db.selectMany(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.loadMetrics = function(callback) {
    var that = this;
    var result = {};

    seq()
        .par(this._createMetricSubStep('select count(*) as countTransactions from transactions', result))
        .par(this._createMetricSubStep('select sum(value) as overallBalance from transactions', result))
        .par(this._createMetricSubStep('select count(*) as countUsers from users', result))
        .par(this._createMetricSubStep('select avg(userBalance) as avgBalance from (select sum(value) as userBalance from transactions group by userId) as ghoti', result))
        .par(function() {
            var done = this;
            that._db.selectMany('select date(createDate) as date, count(*) as overallNumber,count(distinct userid) as distinctUsers,sum(value) as dayBalance,sum(max(value, 0)) as dayBalancePositive,sum(min(value, 0)) as dayBalanceNegative from transactions group by date(createDate);', [], function(error, transactions) {
                if (error) return done(error);<commit_msg>metrics return only the 30 newest dates<commit_after>var ce = require('cloneextend');
var seq = require('seq');
var squel = require('squel');

function UserPersistence (database) {
    this._db = database;
}

UserPersistence.prototype.loadUsers = function (limitStatement, orderStatement, callback) {
    var query = this._createUserQuery();

    if (limitStatement !== null) {
        query.offset(limitStatement.offset());
        query.limit(limitStatement.limit());
    }

    this._db.selectMany(query.toString(), [], callback);
};

UserPersistence.prototype.loadUserById = function (userId, callback) {
    var query = this._createUserQuery()
        .where('users.id = ?', userId);

    var parametrized = query.toParam();

    this._db.selectOne(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.loadUserByName = function (name, callback) {
    var query = this._createUserQuery()
        .where('lower(users.name) = ?', name.toLowerCase());

    var parametrized = query.toParam();

    this._db.selectOne(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.createUser = function (name, callback) {
    var query = squel.insert()
        .into('users')
        .set('name', name);

    var parametrized = query.toParam();

    this._db.query(parametrized.text, parametrized.values, function (error, result) {
        if (error) return callback(error, null);

        callback(null, result.lastID);
    });
};

UserPersistence.prototype.createTransaction = function (userId, amount, callback) {
    var query = squel.insert()
        .into('transactions')
        .set('userId', userId)
        .set('value', amount);

    var parametrized = query.toParam();

    this._db.query(parametrized.text, parametrized.values, function (error, result) {
        if (error) return callback(error, null);

        callback(null, result.lastID);
    });
};

UserPersistence.prototype.loadTransaction = function (transactionId, callback) {
    var query = this._createTransactionQuery()
        .where('id = ?', transactionId);

    var parametrized = query.toParam();

    this._db.selectOne(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.loadTransactionsByUserId = function (userId, limitStatement, orderStatement, callback) {
    var query = this._createTransactionQuery()
        .where('userId = ?', userId);

    if (limitStatement !== null) {
        query.offset(limitStatement.offset());
        query.limit(limitStatement.limit());
    }

    var parametrized = query.toParam();

    this._db.selectMany(parametrized.text, parametrized.values, callback);
};

UserPersistence.prototype.loadMetrics = function(callback) {
    var that = this;
    var result = {};

    seq()
        .par(this._createMetricSubStep('select count(*) as countTransactions from transactions', result))
        .par(this._createMetricSubStep('select sum(value) as overallBalance from transactions', result))
        .par(this._createMetricSubStep('select count(*) as countUsers from users', result))
        .par(this._createMetricSubStep('select avg(userBalance) as avgBalance from (select sum(value) as userBalance from transactions group by userId) as ghoti', result))
        .par(function() {
            var done = this;
            that._db.selectMany('select date(createDate) as date, count(*) as overallNumber,count(distinct userid) as distinctUsers,sum(value) as dayBalance,sum(max(value, 0)) as dayBalancePositive,sum(min(value, 0)) as dayBalanceNegative from transactions where createDate >=  date("now", "-30 day") group by date(createDate);', [], function(error, transactions) {
                if (error) return done(error);
{'fraction_non_alphanumeric': 0.13260869565217392, 'fraction_numerical': 0.0010869565217391304, 'mean_word_length': 3.871608206485771, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2934890', 'n_tokens_mistral': 2119, 'n_tokens_neox': 2049, 'n_words': 466}