Merge pull request #16077 from kennethlumalicay/fix/user-streak

fix(user): new function to compute unique days within hours.
pull/15943/merge
Berkeley Martinez 2017-12-06 09:39:35 -08:00 committed by GitHub
commit 03f85af341
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 52 deletions

View File

@ -17,7 +17,7 @@ import {
} from '../utils/middleware';
import { observeQuery } from '../utils/rx';
import {
prepUniqueDays,
prepUniqueDaysByHours,
calcCurrentStreak,
calcLongestStreak
} from '../utils/user-stats';
@ -492,7 +492,7 @@ module.exports = function(app) {
// not of the profile she is viewing
const timezone = user && user.timezone ?
user.timezone :
'UTC';
'EST';
const query = {
where: { username },
@ -517,10 +517,10 @@ module.exports = function(app) {
objOrNum.timestamp;
});
const uniqueDays = prepUniqueDays(timestamps, timezone);
const uniqueHours = prepUniqueDaysByHours(timestamps, timezone);
userPortfolio.currentStreak = calcCurrentStreak(uniqueDays, timezone);
userPortfolio.longestStreak = calcLongestStreak(uniqueDays, timezone);
userPortfolio.currentStreak = calcCurrentStreak(uniqueHours, timezone);
userPortfolio.longestStreak = calcLongestStreak(uniqueHours, timezone);
const calender = userPortfolio
.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 { dayCount } from '../utils/date-utils';
const daysBetween = 1.5;
const transform = trans.convert({ cap: false });
export function prepUniqueDays(cals, tz = 'UTC') {
return _.uniq(
_.map(cals, ts => moment(ts).tz(tz).startOf('day').valueOf())
).sort();
const hoursBetween = 24;
const hoursDay = 24;
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') {
let prev = _.last(cals);
if (moment().tz(tz).startOf('day').diff(prev, 'days') > daysBetween) {
let prev = last(cals);
if (
moment()
.tz(tz)
.startOf('day')
.diff(moment(prev).tz(tz), 'hours')
> hoursBetween
) {
return 0;
}
let currentStreak = 0;
let streakContinues = true;
_.forEachRight(cals, cur => {
if (moment(prev).diff(cur, 'days') < daysBetween) {
forEachRight(cur => {
if (
moment(prev)
.tz(tz)
.startOf('day')
.diff(moment(cur).tz(tz), 'hours')
<= hoursBetween
) {
prev = cur;
currentStreak++;
} else {
@ -27,7 +66,7 @@ export function calcCurrentStreak(cals, tz = 'UTC') {
streakContinues = false;
}
return streakContinues;
});
})(cals);
return currentStreak;
}
@ -38,7 +77,8 @@ export function calcLongestStreak(cals, tz = 'UTC') {
const longest = cals.reduce((longest, head, index) => {
const last = cals[index === 0 ? 0 : index - 1];
// 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;
}
if (dayCount(longest, tz) < dayCount([head, tail], tz)) {

View File

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