@cronjobs.register
def weekly_downloads():
 if (not waffle.switch_is_active('local-statistics-processing')):
  return False
 raise_if_reindex_in_progress('amo')
 cursor = connection.cursor()
 cursor.execute('\n        SELECT addon_id, SUM(count) AS weekly_count\n        FROM download_counts\n        WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)\n        GROUP BY addon_id\n        ORDER BY addon_id')
 counts = cursor.fetchall()
 addon_ids = [r[0] for r in counts]
 if (not addon_ids):
  return
 cursor.execute('\n        SELECT id, 0\n        FROM addons\n        WHERE id NOT IN %s', (addon_ids,))
 counts += cursor.fetchall()
 cursor.execute('\n        CREATE TEMPORARY TABLE tmp_wd\n        (addon_id INT PRIMARY KEY, count INT)')
 cursor.execute(('INSERT INTO tmp_wd VALUES %s' % ','.join((['(%s,%s)'] * len(counts)))), list(itertools.chain(*counts)))
 cursor.execute('\n        UPDATE addons INNER JOIN tmp_wd\n            ON addons.id = tmp_wd.addon_id\n        SET weeklydownloads = tmp_wd.count')
 cursor.execute('DROP TABLE IF EXISTS tmp_wd')

@ cronjobs . register def weekly_downloads ( ) : if ( not waffle . switch_is_active ( 'local-statistics-processing' ) ) : return False raise_if_reindex_in_progress ( 'amo' ) cursor = connection . cursor ( ) cursor . execute ( '\n        SELECT addon_id, SUM(count) AS weekly_count\n        FROM download_counts\n        WHERE `date` >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)\n        GROUP BY addon_id\n        ORDER BY addon_id' ) counts = cursor . fetchall ( ) addon_ids = [ r [ 0 ] for r in counts ] if ( not addon_ids ) : return cursor . execute ( '\n        SELECT id, 0\n        FROM addons\n        WHERE id NOT IN %s' , ( addon_ids , ) ) counts += cursor . fetchall ( ) cursor . execute ( '\n        CREATE TEMPORARY TABLE tmp_wd\n        (addon_id INT PRIMARY KEY, count INT)' ) cursor . execute ( ( 'INSERT INTO tmp_wd VALUES %s' % ',' . join ( ( [ '(%s,%s)' ] * len ( counts ) ) ) ) , list ( itertools . chain ( * counts ) ) ) cursor . execute ( '\n        UPDATE addons INNER JOIN tmp_wd\n            ON addons.id = tmp_wd.addon_id\n        SET weeklydownloads = tmp_wd.count' ) cursor . execute ( 'DROP TABLE IF EXISTS tmp_wd' )
update 7-day add-on download counts .