preen-cron: fix up history cron, should be fully functional now

This commit is contained in:
Tyler Renelle
2013-06-15 12:01:03 -04:00
parent 42be939b12
commit 22d4bb27ac
+54 -43
View File
@@ -7,64 +7,74 @@
load('./node_modules/lodash/lodash.js');
load('./node_modules/moment/moment.js');
/*
Users are allowed to experiment with the site before registering. Every time a new browser visits habitrpg, a new
"staged" account is created - and if the user later registeres, that staged account is considered a "production" account.
This function removes all staged accounts that have been abandoned - either older than a month, or corrupted in some way (lastCron==undefined)
var today = +new Date;
/**
* Users are allowed to experiment with the site before registering. Every time a new browser visits habitrpg, a new
* "staged" account is created - and if the user later registeres, that staged account is considered a "production" account.
* This function removes all staged accounts that have been abandoned - either older than a month, or corrupted in some way (lastCron==undefined)
*/
db.users.find({
var
today = +new Date,
// Un-registered users
"auth.local": {$exists: false},
"auth.facebook": {$exists: false}
un_registered = {
"auth.local": {$exists: false},
"auth.facebook": {$exists: false}
},
emptyParties = {
$where: function(){
return this.type === 'party' && this.members.length === 0;
}).forEach(function(user) {
//if (!user) return;
var lastCron = user.lastCron;
if (lastCron && moment(lastCron).isValid()) {
if (Math.abs(moment(today).diff(lastCron, 'days')) > 5) {
return db.users.remove({_id: user._id});
}
};
} else {
return db.users.update({_id: user._id}, {$set: {'lastCron': today}});
}
});
//db.users.find(un_registered).forEach(function(user) {
// //if (!user) return;
// var lastCron = user.lastCron;
// if (lastCron && moment(lastCron).isValid()) {
// if (Math.abs(moment(today).diff(lastCron, 'days')) > 5) {
// return db.users.remove({_id: user._id});
// }
// } else {
// return db.users.update({_id: user._id}, {$set: {'lastCron': today}});
// }
//});
//db.users.groups.remove(emptyParties);
//FIXME make sure this doesn't conflict with preen un_registered above
/**
* Remove empty parties
*/
db.users.groups.remove({
// Empty Parties
$where: function(){ return this.type === 'party' && this.members.length === 0; }
});
/**
* Preen history for users with > 7 history entries
*/
function preenHistory(history) {
var newHistory = [];
function preen(amount, format) {
var group, sliced, avg;
group = _(history)
var groups, avg, start;
groups = _(history)
.groupBy(function(h){ return moment(h.date).format(format) })
.sortBy(function(h,k) {return k;}); // TODO make sure this is the right order
if (_.size(group) === 0) return;
sliced = _.toArray(group).slice(_.size(group) - 1, -amount);
avg = _.reduce(sliced, function(mem, obj){ return mem + obj.value }) / _.size(group);
newHistory.concat({date: sliced[0].date, value: avg});
.sortBy(function(h,k) {return k;}) // TODO make sure this is the right order
.value()
start = (groups.length - amount > 0) ? groups.length - amount : 0;
groups = groups.slice(start, groups.length - 1)
_.each(groups, function(group){
avg = _.reduce(group, function(mem, obj){ return mem + obj.value }, 0) / group.length;
newHistory.push({date: +moment(group[0].date), value: avg});
})
}
preen(50,'YYYY'); // last 50 years
preen(12,'MMYYYY'); // last 12 months
preen(4,'wYYYY'); // last 4 weeks
newHistory.concat(history.slice(-7)); // last 7 days
preen(50, 'YYYY', 'YYYY'); // last 50 years
preen(12, 'YYYYMM', 'MMM YYYY'); // last 12 months
preen(4, 'YYYYww', 'WW YYYY'); // last 4 weeks
newHistory = newHistory.concat(history.slice(-7)); // last 7 days
return newHistory;
}
db.users.find({
// Registered users with > 7 history entries
$or: [
{ 'auth.local': { $exists: true }},
{ 'auth.facebook': { $exists: true }}
],
$where: function(){ return this.history && this.history.exp && this.history.exp.length > 7; }
}).forEach(function(user) {
var update = {$set:{}};
@@ -73,9 +83,10 @@ db.users.find({
update['$set']['tasks.' + task.id + '.history'] = preenHistory(task.history);
})
// TODO user.history.exp, user.history.todos
update['$set']['history.exp'] = preenHistory(user.history.exp);
update['$set']['history.todos'] = preenHistory(user.history.todos);
if (!_.isEmpty(update['$set'])) db.users.update({_id:user.id}, update);
db.users.update({_id: user._id}, update);
});
/**