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 _ from 'lodash';
import moment from 'moment'; import moment from 'moment';
/* /*
Preen history for users with > 7 history entries Preen history for users and tasks. This code runs only on the server.
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 Free users:
of this month; 1 entry for each month of this year; 1 entry per previous year: [day*7 week*4 month*12 year*infinite] - 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 = []; 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) { function _preen (amount, groupBy) {
let groups = _.chain(history).groupBy(h => { let groups = _.chain(history).groupBy(h => {
return moment(h.date).format(groupBy); return moment(h.date).format(groupBy);
@@ -42,10 +55,13 @@ function _preenHistory (history) {
return newHistory; 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) => { _.each(user.habits.concat(user.dailys), (task, index) => {
if (task.history && task.history.length > minHistLen) { if (task.history && task.history.length > minHistoryLength) {
task.history = _preenHistory(task.history); task.history = _preenHistory(task.history, isSubscribed);
user.markModified(`user.${task.type}s.${index}.history`); user.markModified(`user.${task.type}s.${index}.history`);
} }
}); });
@@ -55,12 +71,13 @@ export default function (user, minHistLen = 7) {
exp: [], exp: [],
}); });
if (user.history.exp.length > minHistLen) { if (user.history.exp.length > minHistoryLength) {
user.history.exp = _preenHistory(user.history.exp); user.history.exp = _preenHistory(user.history.exp, isSubscribed);
user.markModified('user.history.exp'); 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'); user.markModified('user.history.todos');
} }
} }

View File

@@ -990,124 +990,6 @@ describe('Cron', () => {
expect(beforeTasks).to.eql(afterTasks); expect(beforeTasks).to.eql(afterTasks);
}); });
describe('preening', () => {
beforeEach(function () {
this.clock = sinon.useFakeTimers(Date.parse('2013-11-20'), 'Date');
});
afterEach(function () {
return this.clock.restore();
});
it('should preen user history', function () {
let ref = beforeAfter({
daysAgo: 1,
});
let after = ref.after;
let history = [
{
date: '09/01/2012',
value: 0,
}, {
date: '10/01/2012',
value: 0,
}, {
date: '11/01/2012',
value: 2,
}, {
date: '12/01/2012',
value: 2,
}, {
date: '01/01/2013',
value: 1,
}, {
date: '01/15/2013',
value: 3,
}, {
date: '02/01/2013',
value: 2,
}, {
date: '02/15/2013',
value: 4,
}, {
date: '03/01/2013',
value: 3,
}, {
date: '03/15/2013',
value: 5,
}, {
date: '04/01/2013',
value: 4,
}, {
date: '04/15/2013',
value: 6,
}, {
date: '05/01/2013',
value: 5,
}, {
date: '05/15/2013',
value: 7,
}, {
date: '06/01/2013',
value: 6,
}, {
date: '06/15/2013',
value: 8,
}, {
date: '07/01/2013',
value: 7,
}, {
date: '07/15/2013',
value: 9,
}, {
date: '08/01/2013',
value: 8,
}, {
date: '08/15/2013',
value: 10,
}, {
date: '09/01/2013',
value: 9,
}, {
date: '09/15/2013',
value: 11,
}, {
date: '010/01/2013',
value: 10,
}, {
date: '010/15/2013',
value: 12,
}, {
date: '011/01/2013',
value: 12,
}, {
date: '011/02/2013',
value: 13,
}, {
date: '011/03/2013',
value: 14,
}, {
date: '011/04/2013',
value: 15,
},
];
after.history = {
exp: _.cloneDeep(history),
todos: _.cloneDeep(history),
};
after.habits[0].history = _.cloneDeep(history);
after.fns.cron();
after.history.exp.pop();
after.history.todos.pop();
_.each([after.history.exp, after.history.todos, after.habits[0].history], function (arr) {
expect(_.map(arr, (x) => {
return x.value;
})).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
});
});
});
describe('Todos', () => { describe('Todos', () => {
it('1 day missed', () => { it('1 day missed', () => {
let ref = beforeAfter({ let ref = beforeAfter({