wip: new history preening, delineate process and differentiate between subscribers and non

This commit is contained in:
Matteo Pagliazzi
2016-01-07 20:07:13 +01:00
parent fdd5ab724a
commit 243b839392
2 changed files with 32 additions and 133 deletions

View File

@@ -1,17 +1,30 @@
import _ from 'lodash';
import moment from 'moment';
/*
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]
Preen history for users and tasks. This code runs only on the server.
Free users:
- 1 value for each day of the past 60 days (no compression)
- 1 value each month for the previous 10 months
- 1 value each year for the previous years
Subscribers:
- 1 value for each day of the past 365 days (no compression)
- 1 value each month for the previous 12 months
- 1 value each year for the previous years
*/
function _preenHistory (history) {
history = _.filter(history, h => {
return Boolean(h); // filter missing entries
});
function _preenHistory (history, isSubscribed) {
history = _.filter(history, historyEntry => return Boolean(historyEntry)); // Filter missing entries
let newHistory = [];
// Steps
// Take first 365 or 60 entries and keep them unmodified - only if of consecutive days? In the long term we want history to be continuous not with big jumps
// Group the rest by month and keep 10 or 12
// Group the rest by year
function _preen (amount, groupBy) {
let groups = _.chain(history).groupBy(h => {
return moment(h.date).format(groupBy);
@@ -42,10 +55,13 @@ function _preenHistory (history) {
return newHistory;
}
export default function (user, minHistLen = 7) {
export default function (user) {
let isSubscribed = user.purchased && user.purchased.plan && user.purchased.plan.customerId;
let minHistoryLength = isSubscribed ? 365 : 60;
_.each(user.habits.concat(user.dailys), (task, index) => {
if (task.history && task.history.length > minHistLen) {
task.history = _preenHistory(task.history);
if (task.history && task.history.length > minHistoryLength) {
task.history = _preenHistory(task.history, isSubscribed);
user.markModified(`user.${task.type}s.${index}.history`);
}
});
@@ -55,12 +71,13 @@ export default function (user, minHistLen = 7) {
exp: [],
});
if (user.history.exp.length > minHistLen) {
user.history.exp = _preenHistory(user.history.exp);
if (user.history.exp.length > minHistoryLength) {
user.history.exp = _preenHistory(user.history.exp, isSubscribed);
user.markModified('user.history.exp');
}
if (user.history.todos.length > minHistLen) {
user.history.todos = _preenHistory(user.history.todos);
if (user.history.todos.length > minHistoryLength) {
user.history.todos = _preenHistory(user.history.todos, isSubscribed);
user.markModified('user.history.todos');
}
}