fix(user): new function to compute unique days within hours.

This commit is contained in:
kennethlumalicay
2017-11-03 06:17:13 -04:00
parent a92ff79b5e
commit 46383ddaa6
3 changed files with 92 additions and 52 deletions

View File

@ -17,7 +17,7 @@ import {
} from '../utils/middleware'; } from '../utils/middleware';
import { observeQuery } from '../utils/rx'; import { observeQuery } from '../utils/rx';
import { import {
prepUniqueDays, prepUniqueDaysByHours,
calcCurrentStreak, calcCurrentStreak,
calcLongestStreak calcLongestStreak
} from '../utils/user-stats'; } from '../utils/user-stats';
@ -492,7 +492,7 @@ module.exports = function(app) {
// not of the profile she is viewing // not of the profile she is viewing
const timezone = user && user.timezone ? const timezone = user && user.timezone ?
user.timezone : user.timezone :
'UTC'; 'EST';
const query = { const query = {
where: { username }, where: { username },
@ -517,10 +517,10 @@ module.exports = function(app) {
objOrNum.timestamp; objOrNum.timestamp;
}); });
const uniqueDays = prepUniqueDays(timestamps, timezone); const uniqueHours = prepUniqueDaysByHours(timestamps, timezone);
userPortfolio.currentStreak = calcCurrentStreak(uniqueDays, timezone); userPortfolio.currentStreak = calcCurrentStreak(uniqueHours, timezone);
userPortfolio.longestStreak = calcLongestStreak(uniqueDays, timezone); userPortfolio.longestStreak = calcLongestStreak(uniqueHours, timezone);
const calender = userPortfolio const calender = userPortfolio
.progressTimestamps .progressTimestamps

View File

@ -1,25 +1,64 @@
import _ from 'lodash'; import compose from 'lodash/fp/compose';
import map from 'lodash/fp/map';
import sortBy from 'lodash/fp/sortBy';
import trans from 'lodash/fp/transform';
import last from 'lodash/fp/last';
import forEachRight from 'lodash/fp/forEachRight';
import moment from 'moment-timezone'; import moment from 'moment-timezone';
import { dayCount } from '../utils/date-utils'; import { dayCount } from '../utils/date-utils';
const daysBetween = 1.5; const transform = trans.convert({ cap: false });
export function prepUniqueDays(cals, tz = 'UTC') { const hoursBetween = 24;
return _.uniq( const hoursDay = 24;
_.map(cals, ts => moment(ts).tz(tz).startOf('day').valueOf())
).sort(); export function prepUniqueDaysByHours(cals, tz = 'UTC') {
let prev = null;
// compose goes bottom to top (map > sortBy > transform)
return compose(
transform((data, cur, i) => {
if (i < 1) {
data.push(cur);
prev = cur;
} else if (
moment(cur)
.tz(tz)
.diff(moment(prev).tz(tz).startOf('day'), 'hours')
>= hoursDay
) {
data.push(cur);
prev = cur;
}
}, []),
sortBy(e => e),
map(ts => moment(ts).tz(tz).startOf('hours').valueOf())
)(cals);
} }
export function calcCurrentStreak(cals, tz = 'UTC') { export function calcCurrentStreak(cals, tz = 'UTC') {
let prev = _.last(cals); let prev = last(cals);
if (moment().tz(tz).startOf('day').diff(prev, 'days') > daysBetween) { if (
moment()
.tz(tz)
.startOf('day')
.diff(moment(prev).tz(tz), 'hours')
> hoursBetween
) {
return 0; return 0;
} }
let currentStreak = 0; let currentStreak = 0;
let streakContinues = true; let streakContinues = true;
_.forEachRight(cals, cur => { forEachRight(cur => {
if (moment(prev).diff(cur, 'days') < daysBetween) { if (
moment(prev)
.tz(tz)
.startOf('day')
.diff(moment(cur).tz(tz), 'hours')
<= hoursBetween
) {
prev = cur; prev = cur;
currentStreak++; currentStreak++;
} else { } else {
@ -27,7 +66,7 @@ export function calcCurrentStreak(cals, tz = 'UTC') {
streakContinues = false; streakContinues = false;
} }
return streakContinues; return streakContinues;
}); })(cals);
return currentStreak; return currentStreak;
} }
@ -38,7 +77,8 @@ export function calcLongestStreak(cals, tz = 'UTC') {
const longest = cals.reduce((longest, head, index) => { const longest = cals.reduce((longest, head, index) => {
const last = cals[index === 0 ? 0 : index - 1]; const last = cals[index === 0 ? 0 : index - 1];
// is streak broken // is streak broken
if (moment(head).tz(tz).diff(moment(last).tz(tz), 'days') > daysBetween) { if (moment(head).tz(tz).startOf('day').diff(moment(last).tz(tz), 'hours')
> hoursBetween) {
tail = head; tail = head;
} }
if (dayCount(longest, tz) < dayCount([head, tail], tz)) { if (dayCount(longest, tz) < dayCount([head, tail], tz)) {

View File

@ -3,7 +3,7 @@ import moment from 'moment-timezone';
import sinon from 'sinon'; import sinon from 'sinon';
import { import {
prepUniqueDays, prepUniqueDaysByHours,
calcCurrentStreak, calcCurrentStreak,
calcLongestStreak calcLongestStreak
} from './user-stats'; } from './user-stats';
@ -17,48 +17,48 @@ test('Prepare calendar items', function(t) {
t.plan(5); t.plan(5);
t.deepEqual( t.deepEqual(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf() moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf()
]), ]),
[1438560000000], [1438567200000],
'should return correct epoch when all entries fall into one day in UTC' 'should return correct epoch when all entries fall into one day in UTC'
); );
t.deepEqual( t.deepEqual(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf() moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf()
]), ]),
[1438560000000], [1438567200000],
'should return correct epoch when given two identical dates' 'should return correct epoch when given two identical dates'
); );
t.deepEqual( t.deepEqual(
prepUniqueDays([ prepUniqueDaysByHours([
// 8/2/2015 in America/Los_Angeles // 8/2/2015 in America/Los_Angeles
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf() moment.utc('8/3/2015 20:00', 'M/D/YYYY H:mm').valueOf()
], PST), ], PST),
[1438498800000, 1438585200000], [1438567200000, 1438610400000],
'should return 2 epochs when dates fall into two days in PST' 'should return 2 epochs when dates fall into two days in PST'
); );
t.deepEqual( t.deepEqual(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/1/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/1/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('3/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('3/3/2015 14:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/30/2014 20:00', 'M/D/YYYY H:mm').valueOf() moment.utc('9/30/2014 20:00', 'M/D/YYYY H:mm').valueOf()
]), ]),
[1412035200000, 1425340800000, 1438387200000], [1412107200000, 1425391200000, 1438394400000],
'should return 3 epochs when dates fall into three days' 'should return 3 epochs when dates fall into three days'
); );
t.deepEqual( t.deepEqual(
prepUniqueDays([ prepUniqueDaysByHours([
1438387200000, 1425340800000, 1412035200000 1438387200000, 1425340800000, 1412035200000
]), ]),
[1412035200000, 1425340800000, 1438387200000], [1412035200000, 1425340800000, 1438387200000],
@ -73,7 +73,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), ),
@ -83,7 +83,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(), moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
@ -94,7 +94,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf() moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf()
]) ])
), ),
@ -104,7 +104,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
@ -115,7 +115,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 1:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 1:00', 'M/D/YYYY H:mm').valueOf(),
@ -135,7 +135,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(47, 'hours')).valueOf(), moment.utc(moment.utc().subtract(47, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(11, 'hours')).valueOf() moment.utc(moment.utc().subtract(11, 'hours')).valueOf()
]) ])
@ -147,7 +147,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(40, 'hours')).valueOf(), moment.utc(moment.utc().subtract(40, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
@ -159,7 +159,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf(), moment.utc(moment.utc().subtract(1, 'hours')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
@ -171,7 +171,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
], PST), ], PST),
@ -184,7 +184,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453174506164, 1453175436725, 1453252466853, 1453294968225,
1453383782844, 1453431903117, 1453471373080, 1453594733026, 1453383782844, 1453431903117, 1453471373080, 1453594733026,
1453645014058, 1453746762747, 1453747659197, 1453748029416, 1453645014058, 1453746762747, 1453747659197, 1453748029416,
@ -201,7 +201,7 @@ test('Current streak calculation', function(t) {
t.equal( t.equal(
calcCurrentStreak( calcCurrentStreak(
prepUniqueDays([ prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453174506164, 1453175436725, 1453252466853, 1453294968225,
1453383782844, 1453431903117, 1453471373080, 1453594733026, 1453383782844, 1453431903117, 1453471373080, 1453594733026,
1453645014058, 1453746762747, 1453747659197, 1453748029416, 1453645014058, 1453746762747, 1453747659197, 1453748029416,
@ -221,7 +221,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('9/12/2015 4:00', 'M/D/YYYY H:mm').valueOf() moment.utc('9/12/2015 4:00', 'M/D/YYYY H:mm').valueOf()
]) ])
), ),
@ -231,7 +231,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/13/2015 3:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/13/2015 3:00', 'M/D/YYYY H:mm').valueOf(),
@ -245,7 +245,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
), ),
@ -255,7 +255,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
@ -266,7 +266,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
@ -283,7 +283,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 15:30', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 15:30', 'M/D/YYYY H:mm').valueOf(),
@ -302,7 +302,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 1:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 1:00', 'M/D/YYYY H:mm').valueOf(),
@ -323,7 +323,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('8/3/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 1:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 1:00', 'M/D/YYYY H:mm').valueOf(),
@ -346,14 +346,14 @@ test('Longest streak calculation', function(t) {
} }
t.equal( t.equal(
calcLongestStreak(prepUniqueDays(cals)), calcLongestStreak(prepUniqueDaysByHours(cals)),
n, n,
'should return correct longest streak when there is a very long period' 'should return correct longest streak when there is a very long period'
); );
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc(moment.utc().subtract(1, 'days')).valueOf(), moment.utc(moment.utc().subtract(1, 'days')).valueOf(),
moment.utc(moment.utc().subtract(1, 'hours')).valueOf() moment.utc(moment.utc().subtract(1, 'hours')).valueOf()
]) ])
@ -365,7 +365,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 4:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/13/2015 3:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/13/2015 3:00', 'M/D/YYYY H:mm').valueOf(),
@ -379,7 +379,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
moment.utc('9/11/2015 23:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/11/2015 23:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/12/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
moment.utc('9/13/2015 2:00', 'M/D/YYYY H:mm').valueOf(), moment.utc('9/13/2015 2:00', 'M/D/YYYY H:mm').valueOf(),
@ -393,7 +393,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453174506164, 1453175436725, 1453252466853, 1453294968225,
1453383782844, 1453431903117, 1453471373080, 1453594733026, 1453383782844, 1453431903117, 1453471373080, 1453594733026,
1453645014058, 1453746762747, 1453747659197, 1453748029416, 1453645014058, 1453746762747, 1453747659197, 1453748029416,
@ -410,7 +410,7 @@ test('Longest streak calculation', function(t) {
t.equal( t.equal(
calcLongestStreak( calcLongestStreak(
prepUniqueDays([ prepUniqueDaysByHours([
1453174506164, 1453175436725, 1453252466853, 1453294968225, 1453174506164, 1453175436725, 1453252466853, 1453294968225,
1453383782844, 1453431903117, 1453471373080, 1453594733026, 1453383782844, 1453431903117, 1453471373080, 1453594733026,
1453645014058, 1453746762747, 1453747659197, 1453748029416, 1453645014058, 1453746762747, 1453747659197, 1453748029416,