<commit_before>'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var co = require('co');
var bcrypt = require('co-bcrypt');

var UserSchema = new Schema({
    username: {type: String, unique: true},
    password: String,
    locationLog: [new Schema({
        lat: Number,
        lon: Number,
        timestamp: Date
    }, {_id: false})],
    wifiLog: [new Schema({
        name: String,
        timestamp: Date
    }, {_id: false})],
    lastWifiTS: {type: Date, default: 0},
    lastLocationTS: {type: Date, default: 0}
},
{
    toJSON: {
    transform: function (doc, ret, options){
        delete ret.password;
        delete ret.__v;
    }
  }
});

UserSchema.pre('save', function(done){
    if (!this.isModified('password'))
        return done();
    var user = this;
    co(function* (){
        try{
            var salt = yield bcrypt.genSalt();
            var hash = yield bcrypt.hash(user.password, salt);
            user.password = <PASSWORD>;
            done();
        }
        catch(err){
            done(err);
        }
    }).then(function(){
        done();
    },
    function(err){
        done(err);
    });
});

UserSchema.statics.getLastPositionTS = function* (id){
    var query = this.findById(id);
    query.select('lastLocationTS');
    return yield query.exec().lastLocationTS;
};

UserSchema.statics.getLastWifiTS = function* (id){
    var query = this.findById(id);
    query.select('lastWifiTS');
    return yield query.exec().lastWifiTS;
};

UserSchema.statics.addLocationLogs = function* (id, logs){
    var query = this.findById(id);
    query.select('locationLog lastLocationTS');
    var user = yield query.exec();
    var sortedLogs;
    if (logs.length <= 1)
        sortedLogs = logs;
    else
        sortedLogs = logs.sort(function(a, b){
            return a.timestamp - b.timestamp;
        });

    for (var i = 0; i < sortedLogs.length; ++i)
    {
        if (user.lastLocationTS === 0 ||
                sortedLogs[i].timestamp > user.lastLocationTS)
        {
            user.locationLog = user.locationLog.concat(sortedLogs.slice(i));
            user.save(function(err){
                if (err)
                    console.error(err);
            });
            return 'Done';
        }
    }
    return 'Unacceptable';
};

UserSchema.statics.addWifiLogs = function* (id, logs){
    var query = this.findById(id);
    query.select('wifiLog lastWifiTS');
    var user = yield query.exec();
    var sortedLogs;

    if (logs.length <= 1)
        sortedLogs = logs;
    else
        sortedLogs = logs.sort(function(a, b){
            return a.timestamp - b.timestamp;
        });

    for (var i = 0; i < sortedLogs.length; ++i)
    {
        if (user.lastWifiTS === 0 || 
                sortedLogs[i].timestamp > user.lastWifiTS)
        {
            user.wifiLog = user.wifiLog.concat(sortedLogs.slice(i));<commit_msg>updating lastlogs<commit_after>'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var co = require('co');
var bcrypt = require('co-bcrypt');

var UserSchema = new Schema({
    username: {type: String, unique: true},
    password: String,
    locationLog: [new Schema({
        lat: Number,
        lon: Number,
        timestamp: Date
    }, {_id: false})],
    wifiLog: [new Schema({
        name: String,
        timestamp: Date
    }, {_id: false})],
    lastWifiTS: {type: Date, default: 0},
    lastLocationTS: {type: Date, default: 0}
},
{
    toJSON: {
    transform: function (doc, ret, options){
        delete ret.password;
        delete ret.__v;
    }
  }
});

UserSchema.pre('save', function(done){
    if (!this.isModified('password'))
        return done();
    var user = this;
    co(function* (){
        try{
            var salt = yield bcrypt.genSalt();
            var hash = yield bcrypt.hash(user.password, salt);
            user.password = <PASSWORD>;
            done();
        }
        catch(err){
            done(err);
        }
    }).then(function(){
        done();
    },
    function(err){
        done(err);
    });
});

UserSchema.statics.getLastPositionTS = function* (id){
    var query = this.findById(id);
    query.select('lastLocationTS');
    return yield query.exec().lastLocationTS;
};

UserSchema.statics.getLastWifiTS = function* (id){
    var query = this.findById(id);
    query.select('lastWifiTS');
    return yield query.exec().lastWifiTS;
};

UserSchema.statics.addLocationLogs = function* (id, logs){
    var query = this.findById(id);
    query.select('locationLog lastLocationTS');
    var user = yield query.exec();
    var sortedLogs;
    if (logs.length <= 1)
        sortedLogs = logs;
    else
        sortedLogs = logs.sort(function(a, b){
            return a.timestamp - b.timestamp;
        });

    for (var i = 0; i < sortedLogs.length; ++i)
    {
        if (user.lastLocationTS === 0 ||
                sortedLogs[i].timestamp > user.lastLocationTS)
        {
            user.locationLog = user.locationLog.concat(sortedLogs.slice(i));
            user.lastLocationTS = sortedLogs[sortedLogs.length-1].timestamp;
            user.save(function(err){
                if (err)
                    console.error(err);
            });
            return 'Done';
        }
    }
    return 'Unacceptable';
};

UserSchema.statics.addWifiLogs = function* (id, logs){
    var query = this.findById(id);
    query.select('wifiLog lastWifiTS');
    var user = yield query.exec();
    var sortedLogs;

    if (logs.length <= 1)
        sortedLogs = logs;
    else
        sortedLogs = logs.sort(function(a, b){
            return a.timestamp - b.timestamp;
        });

    for (var i = 0; i < sortedLogs.length; ++i)
    {
        if (user.lastWifiTS === 0 || 
                sortedLogs[i].timestamp > user.lastWifiTS)
        {
            user.wifiLog = user.wifiLog.concat(sortedLogs.slice(i));
            user.lastWifiTS = sortedLogs[sortedLogs.length-1].timestamp;
{'fraction_non_alphanumeric': 0.14266577361018085, 'fraction_numerical': 0.003014065639651708, 'mean_word_length': 2.17375132837407, 'pattern_counts': {'":': 0, '<': 13, '<?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': '3340513', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1876, 'n_words': 412}