Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Migrate addons_adu -> update_addon_average_daily_users
Browse files Browse the repository at this point in the history
  • Loading branch information
clouserw committed May 6, 2010
1 parent 230bbc7 commit 909d97e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
37 changes: 34 additions & 3 deletions apps/addons/cron.py
@@ -1,10 +1,10 @@
import logging

from django.db import connection, transaction
from django.db.models import Max, Q, F

from celery.decorators import task
from celery.messaging import establish_connection
from django.db import connection, connections, transaction
from django.db.models import Max, Q, F
import multidb

import amo
from amo.utils import chunked
Expand All @@ -16,6 +16,37 @@
task_log = logging.getLogger('z.task')


@cronjobs.register
def update_addon_average_daily_users():
"""Update add-ons ADU totals."""
cursor = connections[multidb.get_slave()].cursor()
# We need to use SQL for this until
# http://code.djangoproject.com/ticket/11003 is resolved
q = """SELECT
addon_id, AVG(`count`)
FROM update_counts
USE KEY (`addon_and_count`)
GROUP BY addon_id
ORDER BY addon_id"""
cursor.execute(q)
d = cursor.fetchall()
cursor.close()

with establish_connection() as conn:
for chunk in chunked(d, 1000):
_update_addon_average_daily_users.apply_async(args=[chunk],
connection=conn)


@task(rate_limit='15/m')
def _update_addon_average_daily_users(data, **kw):
task_log.debug("[%s@%s] Updating add-ons ADU totals." %
(len(data), _update_addon_average_daily_users.rate_limit))

for pk, count in data:
Addon.objects.filter(pk=pk).update(average_daily_users=count)


def _change_last_updated(next):
# We jump through some hoops here to make sure we only change the add-ons
# that really need it, and to invalidate properly.
Expand Down
5 changes: 3 additions & 2 deletions apps/users/cron.py
Expand Up @@ -2,7 +2,8 @@

from celery.decorators import task
from celery.messaging import establish_connection
from django.db import connection
from django.db import connections
import multidb

from .models import UserProfile
from amo import VALID_STATUSES
Expand All @@ -16,7 +17,7 @@
def update_user_ratings():
"""Update add-on author's ratings."""

cursor = connection.cursor()
cursor = connections[multidb.get_slave()].cursor()
# We build this query ahead of time because the cursor complains about data
# truncation if it does the parameters. Also, this query is surprisingly
# quick, <1sec for 6100 rows returned
Expand Down

0 comments on commit 909d97e

Please sign in to comment.