preen-script: start implementing history-preening

This commit is contained in:
Tyler Renelle 2013-06-14 19:05:26 -04:00
parent 9361b015f6
commit 42be939b12

View file

@ -4,6 +4,7 @@
* mongo habitrpg migrations/preen_cron.js * mongo habitrpg migrations/preen_cron.js
*/ */
load('./node_modules/lodash/lodash.js');
load('./node_modules/moment/moment.js'); load('./node_modules/moment/moment.js');
/* /*
@ -26,21 +27,57 @@ var
} }
}; };
db.users.find(un_registered).forEach(function(user) { //db.users.find(un_registered).forEach(function(user) {
//if (!user) return; // //if (!user) return;
var lastCron = user.lastCron; // var lastCron = user.lastCron;
if (lastCron && moment(lastCron).isValid()) { // if (lastCron && moment(lastCron).isValid()) {
if (Math.abs(moment(today).diff(lastCron, 'days')) > 5) { // if (Math.abs(moment(today).diff(lastCron, 'days')) > 5) {
return db.users.remove({_id: user._id}); // return db.users.remove({_id: user._id});
} // }
} else { // } else {
return db.users.update({_id: user._id}, {$set: {'lastCron': today}}); // 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
function preenHistory(history) {
var newHistory = [];
function preen(amount, format) {
var group, sliced, avg;
group = _(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});
} }
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
return newHistory;
}
db.users.find({
$where: function(){ return this.history && this.history.exp && this.history.exp.length > 7; }
}).forEach(function(user) {
var update = {$set:{}};
_.each(user.tasks, function(task) {
if (task.type === 'habit' || task.type === 'daily')
update['$set']['tasks.' + task.id + '.history'] = preenHistory(task.history);
})
// TODO user.history.exp, user.history.todos
if (!_.isEmpty(update['$set'])) db.users.update({_id:user.id}, update);
}); });
db.users.groups.remove(emptyParties);
/** /**
* Don't remove missing user auths anymore. This was previously necessary due to data corruption, * Don't remove missing user auths anymore. This was previously necessary due to data corruption,
* revisit if needs be * revisit if needs be