Permalink
Browse files

Timestamps are converted to start of the days and filtered to b uniqu…

…e before calculating streaks
  • Loading branch information...
1 parent 2d33525 commit 631c7ea319202c9dd323ebc03e99462db0b19d16 @LenaBarinova LenaBarinova committed Feb 9, 2016
View
@@ -148,6 +148,7 @@
"loopback-component-explorer": "^2.1.1",
"loopback-testing": "^1.1.0",
"mocha": "^2.3.3",
+ "sinon": "^1.17.3",
"tap-spec": "^4.1.1",
"tape": "^4.2.2"
}
View
@@ -14,7 +14,11 @@ import certTypes from '../utils/certTypes.json';
import { ifNoUser401, ifNoUserRedirectTo } from '../utils/middleware';
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 sendNonUserToMap = ifNoUserRedirectTo('/map');
@@ -192,17 +196,18 @@ module.exports = function(app) {
const timezone = req.user &&
req.user.timezone ? req.user.timezone : 'UTC';
- var cals = profileUser
+ const timestamps = profileUser
.progressTimestamps
.map(objOrNum => {
return typeof objOrNum === 'number' ?
objOrNum :
objOrNum.timestamp;
- })
- .sort();
+ });
+
+ const uniqueDays = prepUniqueDays(timestamps, timezone);
- profileUser.currentStreak = calcCurrentStreak(cals, timezone);
- profileUser.longestStreak = calcLongestStreak(cals, timezone);
+ profileUser.currentStreak = calcCurrentStreak(uniqueDays, timezone);
+ profileUser.longestStreak = calcLongestStreak(uniqueDays, timezone);
const data = profileUser
.progressTimestamps
@@ -3,7 +3,7 @@ import moment from 'moment-timezone';
// day count between two epochs (inclusive)
export function dayCount([head, tail], timezone = 'UTC') {
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'),
'days',
true)
@@ -1,46 +1,54 @@
+import _ from 'lodash';
import moment from 'moment-timezone';
import { dayCount } from '../utils/date-utils';
const daysBetween = 1.5;
-export function calcCurrentStreak(cals, timezone = 'UTC') {
- const revCals = cals.slice().reverse();
+export function prepUniqueDays(cals, tz = 'UTC') {
- 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;
}
+ 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;
- 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);
+ return currentStreak;
}
-export function calcLongestStreak(cals, timezone = 'UTC') {
+export function calcLongestStreak(cals, tz = 'UTC') {
+
let tail = cals[0];
const longest = cals.reduce((longest, head, index) => {
const last = cals[index === 0 ? 0 : index - 1];
// 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;
}
- if (dayCount(longest, timezone) < dayCount([head, tail], timezone)) {
+ if (dayCount(longest, tz) < dayCount([head, tail], tz)) {
return [head, tail];
}
return longest;
}, [cals[0], cals[0]]);
- return dayCount(longest, timezone);
+ return dayCount(longest, tz);
}
@@ -3,6 +3,7 @@ import moment from 'moment-timezone';
import { dayCount } from '../../../server/utils/date-utils';
let test = require('tape');
+const PST = 'America/Los_Angeles';
test('Day count between two epochs (inclusive) calculation', function (t) {
t.plan(7);
@@ -25,18 +26,18 @@ test('Day count between two epochs (inclusive) calculation', function (t) {
t.equal(dayCount([
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()
- ]), 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([
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()
- ], '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([
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()
]), 12222, "should return correct count when there is very big period");
-
+
t.equal(dayCount([
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()
Oops, something went wrong.

0 comments on commit 631c7ea

Please sign in to comment.