Skip to content

Commit

Permalink
Timestamps are converted to start of the days and filtered to b uniqu…
Browse files Browse the repository at this point in the history
…e before calculating streaks
  • Loading branch information
LenaBarinova committed Feb 9, 2016
1 parent 2d33525 commit 631c7ea
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 85 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"loopback-component-explorer": "^2.1.1", "loopback-component-explorer": "^2.1.1",
"loopback-testing": "^1.1.0", "loopback-testing": "^1.1.0",
"mocha": "^2.3.3", "mocha": "^2.3.3",
"sinon": "^1.17.3",
"tap-spec": "^4.1.1", "tap-spec": "^4.1.1",
"tape": "^4.2.2" "tape": "^4.2.2"
} }
Expand Down
17 changes: 11 additions & 6 deletions server/boot/user.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import certTypes from '../utils/certTypes.json';


import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware'; import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware';
import { observeQuery } from '../utils/rx'; import { observeQuery } from '../utils/rx';
import { calcCurrentStreak, calcLongestStreak } from '../utils/user-stats'; import {
prepUniqueDays,
calcCurrentStreak,
calcLongestStreak
} from '../utils/user-stats';


const debug = debugFactory('freecc:boot:user'); const debug = debugFactory('freecc:boot:user');
const sendNonUserToMap = ifNoUserRedirectTo('/map'); const sendNonUserToMap = ifNoUserRedirectTo('/map');
Expand Down Expand Up @@ -192,17 +196,18 @@ module.exports = function(app) {
const timezone = req.user && const timezone = req.user &&
req.user.timezone ? req.user.timezone : 'UTC'; req.user.timezone ? req.user.timezone : 'UTC';


var cals = profileUser const timestamps = profileUser
.progressTimestamps .progressTimestamps
.map(objOrNum => { .map(objOrNum => {
return typeof objOrNum === 'number' ? return typeof objOrNum === 'number' ?
objOrNum : objOrNum :
objOrNum.timestamp; objOrNum.timestamp;
}) });
.sort();
const uniqueDays = prepUniqueDays(timestamps, timezone);


profileUser.currentStreak = calcCurrentStreak(cals, timezone); profileUser.currentStreak = calcCurrentStreak(uniqueDays, timezone);
profileUser.longestStreak = calcLongestStreak(cals, timezone); profileUser.longestStreak = calcLongestStreak(uniqueDays, timezone);


const data = profileUser const data = profileUser
.progressTimestamps .progressTimestamps
Expand Down
2 changes: 1 addition & 1 deletion server/utils/date-utils.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import moment from 'moment-timezone';
// day count between two epochs (inclusive) // day count between two epochs (inclusive)
export function dayCount([head, tail], timezone = 'UTC') { export function dayCount([head, tail], timezone = 'UTC') {
return Math.ceil( return Math.ceil(
moment(moment(head).tz(timezone).endOf('day')).tz(timezone).diff( moment(moment(head).tz(timezone).endOf('day')).diff(
moment(tail).tz(timezone).startOf('day'), moment(tail).tz(timezone).startOf('day'),
'days', 'days',
true) true)
Expand Down
54 changes: 31 additions & 23 deletions server/utils/user-stats.js
Original file line number Original file line Diff line number Diff line change
@@ -1,46 +1,54 @@
import _ from 'lodash';
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 daysBetween = 1.5;


export function calcCurrentStreak(cals, timezone = 'UTC') { export function prepUniqueDays(cals, tz = 'UTC') {
const revCals = cals.slice().reverse();


if (dayCount([moment().tz(timezone), revCals[0]], timezone) > daysBetween) { return _(cals)
.map(ts => moment(ts).tz(tz).startOf('day').valueOf())
.uniq()
.sort()
.value();
}

export function calcCurrentStreak(cals, tz = 'UTC') {

let prev = _.last(cals);
if (moment().tz(tz).startOf('day').diff(prev, 'days') > daysBetween) {
return 0; return 0;
} }
let currentStreak = 0;
let streakContinues = true;
_.forEachRight(cals, cur => {
if (moment(prev).diff(cur, 'days') < daysBetween) {
prev = cur;
currentStreak++;
} else {
// current streak found
streakContinues = false;
}
return streakContinues;
});


let streakBroken = false; return currentStreak;
const lastDayInStreak = revCals
.reduce((current, cal, index) => {
const before = revCals[index === 0 ? 0 : index - 1];
if (
!streakBroken &&
moment(before).tz(timezone).diff(cal, 'days', true) < daysBetween
) {
return index;
}
streakBroken = true;
return current;
}, 0);

const lastTimestamp = revCals[lastDayInStreak];
return dayCount([moment().tz(timezone), lastTimestamp], timezone);
} }


export function calcLongestStreak(cals, timezone = 'UTC') { export function calcLongestStreak(cals, tz = 'UTC') {

let tail = cals[0]; let tail = cals[0];
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(timezone).diff(last, 'days', true) > daysBetween) { if (moment(head).tz(tz).diff(moment(last).tz(tz), 'days') > daysBetween) {
tail = head; tail = head;
} }
if (dayCount(longest, timezone) < dayCount([head, tail], timezone)) { if (dayCount(longest, tz) < dayCount([head, tail], tz)) {
return [head, tail]; return [head, tail];
} }
return longest; return longest;
}, [cals[0], cals[0]]); }, [cals[0], cals[0]]);


return dayCount(longest, timezone); return dayCount(longest, tz);
} }
7 changes: 4 additions & 3 deletions test/server/utils/date-utils-test.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import moment from 'moment-timezone';
import { dayCount } from '../../../server/utils/date-utils'; import { dayCount } from '../../../server/utils/date-utils';


let test = require('tape'); let test = require('tape');
const PST = 'America/Los_Angeles';


test('Day count between two epochs (inclusive) calculation', function (t) { test('Day count between two epochs (inclusive) calculation', function (t) {
t.plan(7); t.plan(7);
Expand All @@ -25,18 +26,18 @@ test('Day count between two epochs (inclusive) calculation', function (t) {
t.equal(dayCount([ t.equal(dayCount([
moment.utc("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(), moment.utc("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf() moment.utc("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf()
]), 2, "should return 2 days when the diff is less than 24h but days are different in default timezone UTC"); ]), 2, "should return 2 days when the diff is less than 24h but days are different in UTC");


t.equal(dayCount([ t.equal(dayCount([
moment.utc("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(), moment.utc("8/4/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf() moment.utc("8/3/2015 23:00", "M/D/YYYY H:mm").valueOf()
], 'America/Los_Angeles'), 1, "should return 1 day when the diff is less than 24h and days are different in UTC, but given 'America/Los_Angeles' timezone"); ], PST), 1, "should return 1 day when the diff is less than 24h and days are different in UTC, but given PST");


t.equal(dayCount([ t.equal(dayCount([
moment.utc("10/27/2015 1:00", "M/D/YYYY H:mm").valueOf(), moment.utc("10/27/2015 1:00", "M/D/YYYY H:mm").valueOf(),
moment.utc("5/12/1982 1:00", "M/D/YYYY H:mm").valueOf() moment.utc("5/12/1982 1:00", "M/D/YYYY H:mm").valueOf()
]), 12222, "should return correct count when there is very big period"); ]), 12222, "should return correct count when there is very big period");

t.equal(dayCount([ t.equal(dayCount([
moment.utc("8/4/2015 2:00", "M/D/YYYY H:mm").valueOf(), moment.utc("8/4/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()
Expand Down
Loading

0 comments on commit 631c7ea

Please sign in to comment.