From 243b839392394d7e0c08cbe26e4ebb6b0eb98439 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Thu, 7 Jan 2016 20:07:13 +0100 Subject: [PATCH] wip: new history preening, delineate process and differentiate between subscribers and non --- common/script/preenUserHistory.js | 47 ++++++++---- test/common/algos.mocha.js | 118 ------------------------------ 2 files changed, 32 insertions(+), 133 deletions(-) diff --git a/common/script/preenUserHistory.js b/common/script/preenUserHistory.js index dfeb36cc1b..5979720e12 100644 --- a/common/script/preenUserHistory.js +++ b/common/script/preenUserHistory.js @@ -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'); } } diff --git a/test/common/algos.mocha.js b/test/common/algos.mocha.js index ba96498284..18914f2303 100644 --- a/test/common/algos.mocha.js +++ b/test/common/algos.mocha.js @@ -990,124 +990,6 @@ describe('Cron', () => { 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', () => { it('1 day missed', () => { let ref = beforeAfter({