fix up lte in global stats (bug 801071)
This commit is contained in:
Родитель
067a2b3e1a
Коммит
efccaa7417
|
@ -45,6 +45,8 @@ def update_collections_total():
|
|||
def update_global_totals(date=None):
|
||||
"""Update global statistics totals."""
|
||||
|
||||
if date:
|
||||
date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
|
||||
today = date or datetime.date.today()
|
||||
today_jobs = [dict(job=job, date=today) for job in
|
||||
tasks._get_daily_jobs(date)]
|
||||
|
|
|
@ -89,20 +89,30 @@ def _get_daily_jobs(date=None):
|
|||
If a date is specified and applies to the job it will be used. Otherwise
|
||||
the date will default to today().
|
||||
"""
|
||||
|
||||
if not date:
|
||||
date = datetime.date.today()
|
||||
|
||||
extra = dict(where=['DATE(created)=%s'], params=[date])
|
||||
# Passing through a datetime would not generate an error,
|
||||
# but would pass and give incorrect values.
|
||||
if isinstance(date, datetime.datetime):
|
||||
raise ValueError('This requires a valid date, not a datetime')
|
||||
|
||||
# Testing on lte created date doesn't get you todays date, you need to do
|
||||
# less than next date. That's because 2012-1-1 becomes 2012-1-1 00:00
|
||||
next_date = date + datetime.timedelta(days=1)
|
||||
|
||||
date_str = date.strftime('%Y-%m-%d')
|
||||
extra = dict(where=['DATE(created)=%s'], params=[date_str])
|
||||
# Where we need to specify the extra on some of the joins.
|
||||
addon_extra = dict(where=['DATE(addons.created)=%s'], params=[date])
|
||||
addon_extra = dict(where=['DATE(addons.created)=%s'], params=[date_str])
|
||||
|
||||
# If you're editing these, note that you are returning a function! This
|
||||
# cheesy hackery was done so that we could pass the queries to celery
|
||||
# lazily and not hammer the db with a ton of these all at once.
|
||||
stats = {
|
||||
# Add-on Downloads
|
||||
'addon_total_downloads': lambda: DownloadCount.objects.filter(
|
||||
date__lte=date).aggregate(sum=Sum('count'))['sum'],
|
||||
date__lt=next_date).aggregate(sum=Sum('count'))['sum'],
|
||||
'addon_downloads_new': lambda: DownloadCount.objects.filter(
|
||||
date=date).aggregate(sum=Sum('count'))['sum'],
|
||||
|
||||
|
@ -114,7 +124,7 @@ def _get_daily_jobs(date=None):
|
|||
|
||||
# User counts
|
||||
'user_count_total': UserProfile.objects.filter(
|
||||
created__lte=date).count,
|
||||
created__lt=next_date).count,
|
||||
'user_count_new': UserProfile.objects.extra(**extra).count,
|
||||
|
||||
# Review counts
|
||||
|
@ -125,10 +135,10 @@ def _get_daily_jobs(date=None):
|
|||
|
||||
# Collection counts
|
||||
'collection_count_total': Collection.objects.filter(
|
||||
created__lte=date).count,
|
||||
created__lt=next_date).count,
|
||||
'collection_count_new': Collection.objects.extra(**extra).count,
|
||||
'collection_count_autopublishers': Collection.objects.filter(
|
||||
created__lte=date, type=amo.COLLECTION_SYNCHRONIZED).count,
|
||||
created__lt=next_date, type=amo.COLLECTION_SYNCHRONIZED).count,
|
||||
|
||||
'collection_addon_downloads': (lambda:
|
||||
AddonCollectionCount.objects.filter(date__lte=date).aggregate(
|
||||
|
@ -146,7 +156,7 @@ def _get_daily_jobs(date=None):
|
|||
|
||||
# New users
|
||||
'mmo_user_count_total': UserProfile.objects.filter(
|
||||
created__lte=date,
|
||||
created__lt=next_date,
|
||||
source=amo.LOGIN_SOURCE_MMO_BROWSERID).count,
|
||||
'mmo_user_count_new': UserProfile.objects.filter(
|
||||
source=amo.LOGIN_SOURCE_MMO_BROWSERID).extra(**extra).count,
|
||||
|
|
|
@ -20,7 +20,7 @@ class TestGlobalStats(amo.tests.TestCase):
|
|||
|
||||
def test_stats_for_date(self):
|
||||
|
||||
date = '2009-06-01'
|
||||
date = datetime.date(2009, 6, 1)
|
||||
job = 'addon_total_downloads'
|
||||
|
||||
eq_(GlobalStat.objects.no_cache().filter(date=date,
|
||||
|
@ -51,10 +51,20 @@ class TestGlobalStats(amo.tests.TestCase):
|
|||
Review.objects.create(addon=addon, user=user)
|
||||
eq_(tasks._get_daily_jobs()['apps_review_count_new'](), 1)
|
||||
|
||||
|
||||
def test_input(self):
|
||||
for x in ['2009-1-1',
|
||||
datetime.datetime(2009, 1, 1),
|
||||
datetime.datetime(2009, 1, 1, 11, 0)]:
|
||||
with self.assertRaises((TypeError, ValueError)):
|
||||
tasks._get_daily_jobs(x)
|
||||
|
||||
def test_user_total(self):
|
||||
day = datetime.date(2009, 1, 1)
|
||||
p = UserProfile.objects.create(username='foo',
|
||||
source=amo.LOGIN_SOURCE_MMO_BROWSERID)
|
||||
p.update(created=datetime.date(2009, 1, 1))
|
||||
p.update(created=day)
|
||||
eq_(tasks._get_daily_jobs(day)['mmo_user_count_total'](), 1)
|
||||
eq_(tasks._get_daily_jobs()['mmo_user_count_total'](), 1)
|
||||
eq_(tasks._get_daily_jobs()['mmo_user_count_new'](), 0)
|
||||
|
||||
|
|
|
@ -482,7 +482,9 @@ def _site_query(period, start, end):
|
|||
cursor = connection.cursor()
|
||||
# Let MySQL make this fast. Make sure we prevent SQL injection with the
|
||||
# assert.
|
||||
assert period in SERIES_GROUPS_DATE, '%s period is not valid.'
|
||||
if period not in SERIES_GROUPS_DATE:
|
||||
raise AssertionError('%s period is not valid.' % period)
|
||||
|
||||
sql = ("SELECT name, MIN(date), SUM(count) "
|
||||
"FROM global_stats "
|
||||
"WHERE date > %%s AND date <= %%s "
|
||||
|
|
Загрузка…
Ссылка в новой задаче