<commit_before>var mongoose = require("mongoose"),
    Period = require('../helpers/period'),
    async = require('async');

/**
 * ErrorLog schema
 * @type {mongoose}
 */
var ErrorSchema = new mongoose.Schema({
    message: { // text message
        type: String,
        required: true,
        index: true
    },

    line: Number, // line number
    symbol: Number, // symbol

    file: String, // file - url
    url: String, // file-url
    referrer: String, // who asked for it

    trace: String, // error trace
    browser: String, // browser

    beforeLoad: Boolean, // before page was loaded
    fake: Boolean, // is debug error

    createdAt: {
        type: Date,
        required: true,
        index: true,
        default: (+new Date()) // cached
    }

    // datetime
    // ago
    // timesOccured
    // isChild
});

/**
 * Get how many time error was occured
 * @param  {String}   message Error message
 * @param  {Function} cb      Callback
 */
ErrorSchema.statics.getTimesCount = function(message, cb) {
    this.count({ message: message }, cb);
};

ErrorSchema.statics.getCountForThePeriod = function(period, cb) {
    this.count({
        createdAt: { $gt: Period.getStartOf(period) }
    }, cb);
};

ErrorSchema.statics.findRichForThePeriod = function(period, filters, cb) {
    var that = this;
    this.find({
        createdAt: { $gt: Period.getStartOf(period) }
    }, null, filters, function(err, logs) {
        if (err) {
            cb(err);
        }
        async.each(logs, function(log, logCb) {
            that.addRichFields(log, logCb);
        }, function(err) {
            cb(err, logs);
        });
    });
};

ErrorSchema.statics.addRichFields = function(log, cb) {
    // dates
    log.datetime = Period.daytime(log.createdAt);
    log.ago = Period.ago(log.createdAt);
    if (log.isChild) {
        return cb(null, log);
    }
    // times
    this.getTimesCount(log.message, function(err, count) {
        if (count) {
            log.occuredTimes = count;
        }
        cb(err, log);
    });
};

ErrorSchema.statics.findRichErrorById = function(id, cb) {
    var that = this;
    this.findById(id, function(err, log) {
        if (err) {
            return cb(err);
        }
        that.addRichFields(log, cb);
    });
};

ErrorSchema.statics.findRichSimilarErrors = function(baseLog, filters, cb) {
    var that = this;
    this.find({ message: baseLog.message }, null, filters || {}, function(err, logs) {
        if (err) {
            cb(err);
        }
        async.each(logs, function(log, logCb) {
            log.isChild = true;
            that.addRichFields(log, logCb);
        }, function(err) {
            cb(err, logs);
        });
    });
};

module.exports = {
    ErrorLog: mongoose.model("ErrorLog", ErrorSchema)
};<commit_msg>doccomments close to yui style<commit_after>/**
 * ErrorLog entity
 *
 * @module model
 */

var mongoose = require("mongoose"),
    Period = require('../helpers/period'),
    async = require('async');

/**
 * ErrorLog fields and methods
 *
 * @class ErrorSchema
 * @static
 */
var ErrorSchema = new mongoose.Schema({
    message: { // text message
        type: String,
        required: true,
        index: true
    },

    line: Number, // line number
    symbol: Number, // symbol

    file: String, // file - url
    url: String, // file-url
    referrer: String, // who asked for it

    trace: String, // error trace
    browser: String, // browser

    beforeLoad: Boolean, // before page was loaded
    fake: Boolean, // is debug error

    createdAt: {
        type: Date,
        required: true,
        index: true,
        default: (+new Date()) // cached
    }

    // datetime
    // ago
    // timesOccured
    // isChild
});

/**
 * Get how many time error was occured
 *
 * @method getTimesCount
 * @param  {String}   message Error message
 * @param  {Function} cb      Callback
 */
ErrorSchema.statics.getTimesCount = function(message, cb) {
    this.count({ message: message }, cb);
};

/**
 * Get errors count for the period
 *
 * @method getCountForThePeriod
 * @param  {String}   period Period name (day, week, etc.)
 * @param  {Function} cb     Callback
 */
ErrorSchema.statics.getCountForThePeriod = function(period, cb) {
    this.count({
        createdAt: { $gt: Period.getStartOf(period) }
    }, cb);
};

/**
 * Find errors for the period with extended fields list
 *
 * @method findRichForThePeriod
 * @param  {String}   period  Period name
 * @param  {Object}   filters Mongoose filters
 * @param  {Function} cb      Callback
 */
ErrorSchema.statics.findRichForThePeriod = function(period, filters, cb) {
    var that = this;
    this.find({
        createdAt: { $gt: Period.getStartOf(period) }
    }, null, filters, function(err, logs) {
        if (err) {
            cb(err);
        }
        async.each(logs, function(log, logCb) {
            that.addRichFields(log, logCb);
        }, function(err) {
            cb(err, logs);
        });
    });
};

/**
 * Add computed fields to the model
 *
 * @method addRichFields
 * @param {Object}   log Error item
 * @param {Function} cb  Callback
 */
ErrorSchema.statics.addRichFields = function(log, cb) {
    // dates
    log.datetime = Period.daytime(log.createdAt);
    log.ago = Period.ago(log.createdAt);
    if (log.isChild) {
        return cb(null, log);
    }
    // times
    this.getTimesCount(log.message, function(err, count) {
        if (count) {
            log.occuredTimes = count;
        }
        cb(err, log);
    });
};

/**
 * Find the Error by it's ID
 *
 * @method findRichErrorById
 * @param  {String}   id ID hash
 * @param  {Function} cb Callback
 */
ErrorSchema.statics.findRichErrorById = function(id, cb) {
    var that = this;
    this.findById(id, function(err, log) {
        if (err) {
            return cb(err);
        }
        that.addRichFields(log, cb);
    });
};

/**
 * Find errors similar to the base one
 *
 * @method findRichSimilarErrors
 * @param  {Object}   baseLog Base error log
 * @param  {Object}   filters Mongoose filters
 * @param  {Function} cb      Callback
 */
ErrorSchema.statics.findRichSimilarErrors = function(baseLog, filters, cb) {
    var that = this;
    this.find({ message: baseLog.message }, null, filters || {}, function(err, logs) {
        if (err) {
            cb(err);
        }
        async.each(logs, function(log, logCb) {
            log.isChild = true;
            that.addRichFields(log, logCb);
        }, function(err) {
            cb(err, logs);
        });
    });
};

module.exports = {
    ErrorLog: mongoose.model("ErrorLog", ErrorSchema)
};
{'fraction_non_alphanumeric': 0.1392118375358599, 'fraction_numerical': 0.0, 'mean_word_length': 2.235955056179775, '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': '4069264', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2054, 'n_words': 523}