Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
refactor sms graph to hit db less
Browse files Browse the repository at this point in the history
  • Loading branch information
monty5811 committed Aug 11, 2016
1 parent c647ae3 commit e7db7f5
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions graphs/sms_freq.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,37 @@ def sms_graph_data(direction='in'):
smsdata = cache.get(cache_id)
if smsdata is None:
td = timezone.now()
sms_list = model_class.objects.all()
# grab all the message timestamps from last 31 days as a list
if cache_id == 'ogd':
sms_list = list(
model_class.objects.filter(
time_sent__gt=td - timezone.timedelta(days=31)
).values_list(
'time_sent', flat=True
)
)
elif cache_id == 'igd':
sms_list = list(
model_class.objects.filter(
time_received__gt=td - timezone.timedelta(days=31)
).values_list(
'time_received', flat=True
)
)
# count sms per day
smsdata = []
for x in range(-30, 1):
delta = timezone.timedelta(days=x)
today = td + delta
if cache_id == 'ogd':
num_of_sms = sms_list.filter(
time_sent__year=today.year,
time_sent__month=today.month,
time_sent__day=today.day
).count()
elif cache_id == 'igd':
num_of_sms = sms_list.filter(
time_received__year=today.year,
time_received__month=today.month,
time_received__day=today.day
).count()
num_of_sms = len(
[
x for x in sms_list
if x.year == today.year and x.month == today.month and
x.day == today.day
]
)
smsdata.append(num_of_sms)

cache.set(cache_id, smsdata, 5 * 60)

return smsdata

0 comments on commit e7db7f5

Please sign in to comment.