port cron and preening

This commit is contained in:
Matteo Pagliazzi
2015-12-10 19:07:09 +01:00
parent 6a0f9564e0
commit 9ffa0d5893
4 changed files with 385 additions and 14 deletions
+64
View File
@@ -0,0 +1,64 @@
import moment from 'moment';
import _ from 'lodash';
function _preen (newHistory, history, amount, groupBy) {
let groups = _.chain(history)
.groupBy(h => moment(h.date).format(groupBy))
.sortBy((h, k) => k)
.value();
groups = groups.slice(-amount);
groups.pop();
_.each(groups, (group) => {
newHistory.push({
date: moment(group[0].date).toDate(),
value: _.reduce(group, (m, obj) => m + obj.value, 0) / group.length,
});
});
}
// Free users:
// Preen history for users with > 7 history entries
// This takes an infinite array of single day entries [day day day day day...], and turns it into a condensed array
// of averages, condensing more the further back in time we go. Eg, 7 entries each for last 7 days; 1 entry each week
// of this month; 1 entry for each month of this year; 1 entry per previous year: [day*7 week*4 month*12 year*infinite]
//
// Subscribers:
// TODO implement
export function preenHistory (history) {
// TODO remember to add this to migration
/* history = _.filter(history, function(h) {
return !!h;
}); */
let newHistory = [];
_preen(newHistory, history, 50, 'YYYY');
_preen(newHistory, history, moment().format('MM'), 'YYYYMM');
let thisMonth = moment().format('YYYYMM');
newHistory = newHistory.concat(history.filter(h => {
return moment(h.date).format('YYYYMM') === thisMonth;
}));
return newHistory;
}
export function preenUserHistory (user, tasksByType, minHistLen = 7) {
tasksByType.habits.concat(user.dailys).forEach((task) => {
if (task.history.length > minHistLen) {
task.history = preenHistory(user, task.history);
task.markModified('history');
}
});
if (user.history.exp.length > minHistLen) {
user.history.exp = preenHistory(user, user.history.exp);
user.markModified('history.exp');
}
if (user.history.todos.length > minHistLen) {
user.history.todos = preenHistory(user, user.history.todos);
user.markModified('history.todos');
}
}
+5 -13
View File
@@ -1,5 +1,4 @@
import _ from 'lodash';
import moment from 'moment';
import {
NotAuthorized,
} from '../../../website/src/libs/api-v3/errors';
@@ -195,18 +194,11 @@ export default function scoreTask (options = {}, req = {}) {
}
_gainMP(user, _.max([0.25, 0.0025 * user._statsComputed.maxMP]) * (direction === 'down' ? -1 : 1));
// history
let th = task.history;
let thl = task.history.length;
if (th[thl - 1] && moment(th[thl - 1].date).isSame(new Date(), 'day')) {
th[thl - 1].value = task.value; // TODO mark modified?
} else {
th.push({
date: Number(new Date()), // TODO are we going to cast history entries?
value: task.value,
});
}
// Add history entry, even more than 1 per day
task.history.push({
date: Number(new Date()), // TODO are we going to cast history entries?
value: task.value,
});
} else if (task.type === 'daily') {
if (cron) {
delta += _changeTaskValue(user, task, direction, times, cron);