Skip to content

Commit

Permalink
Chunkify dates while processing summaries.
Browse files Browse the repository at this point in the history
  • Loading branch information
grokcode committed Dec 19, 2014
1 parent 17ace59 commit aa262a5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
7 changes: 5 additions & 2 deletions misfitapp/extras.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import re
import datetime

# Maximum number of days Misfit will allow in a date range api request
DAYS_IN_CHUNK = 30

def cc_to_underscore(name):
""" Convert camelCase name to under_score """
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
Expand All @@ -12,15 +15,15 @@ def cc_to_underscore_keys(dictionary):
return dict((cc_to_underscore(key), val) for key, val in dictionary.items())


def chunkify_dates(start, end, days_in_chunk):
def chunkify_dates(start, end, days_in_chunk=DAYS_IN_CHUNK):
"""
Return a list of tuples that chunks the date range into ranges
of length days_in_chunk.
"""
chunks = []
s = start
e = start + datetime.timedelta(days=days_in_chunk)
while e - datetime.timedelta(days=30) < end:
while e - datetime.timedelta(days=days_in_chunk) < end:
e = min(e, end)
chunks.append((s, e))
s = e + datetime.timedelta(days=1)
Expand Down
24 changes: 13 additions & 11 deletions misfitapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
Summary,
Goal
)
from .extras import cc_to_underscore_keys
from .extras import cc_to_underscore_keys, chunkify_dates

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -231,13 +231,15 @@ def update_summaries(date_ranges):
continue

misfit = utils.create_misfit(access_token=mfuser.access_token)
summaries = misfit.summary(detail=True, **date_range)
for summary in summaries:
data = cc_to_underscore_keys(summary.data)
s, created = Summary.objects.get_or_create(user_id=mfuser.user_id,
date=data['date'],
defaults=data)
if not created:
for attr, val in data.items():
setattr(s, attr, val)
s.save()

for start, end in chunkify_dates(date_range['start_date'], date_range['end_date']):
summaries = misfit.summary(detail=True, start_date=start, end_date=end)
for summary in summaries:
data = cc_to_underscore_keys(summary.data)
s, created = Summary.objects.get_or_create(user_id=mfuser.user_id,
date=data['date'],
defaults=data)
if not created:
for attr, val in data.items():
setattr(s, attr, val)
s.save()

0 comments on commit aa262a5

Please sign in to comment.